-
Notifications
You must be signed in to change notification settings - Fork 3.8k
add bitwise and, or, negate expressions #10084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |||||
| import org.junit.Test; | ||||||
| import org.junit.rules.ExpectedException; | ||||||
|
|
||||||
| import javax.annotation.Nullable; | ||||||
| import java.util.Collections; | ||||||
| import java.util.List; | ||||||
| import java.util.Set; | ||||||
|
|
@@ -172,6 +173,43 @@ public void testMixed() | |||||
| validateFlatten("min(1, max(3, 4))", "(min [1, (max [3, 4])])", "1"); | ||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
| public void testBitwiseOps() | ||||||
| { | ||||||
| validateFlatten("3 & 1", "(& 3 1)", "1"); | ||||||
| validateFlatten("3 & 1", "(& 3 1)", "1"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: duplicate test, did you intend to test the inverse of the previous line here?
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, it was just an accidental dupe I think |
||||||
| validateFlatten("2 & 1", "(& 2 1)", "0"); | ||||||
| validateFlatten("3 | 1", "(| 3 1)", "3"); | ||||||
| validateFlatten("2 | 1", "(| 2 1)", "3"); | ||||||
| validateFlatten("(~1) & 7", "(& ~1 7)", "6"); | ||||||
|
|
||||||
| validateFlatten("'2' & '1'", "(& 2 1)", "0"); | ||||||
| validateFlatten("'3' | '1'", "(| 3 1)", "3"); | ||||||
| validateFlatten("(~'1') & 7", "(& ~1 7)", "6"); | ||||||
|
|
||||||
| validateFlatten("'notanumber' & '1'", "(& notanumber 1)", null); | ||||||
| validateFlatten("'3' | 'notanumber'", "(| 3 notanumber)", null); | ||||||
| validateFlatten("~'notanumber'", "~notanumber", null); | ||||||
| validateFlatten("(~'notanumber') & '7'", "(& ~notanumber 7)", null); | ||||||
| validateFlatten("(~'notanumber') | '7'", "(| ~notanumber 7)", null); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add some tests with nulls for the bitwise operators
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this actually brings up something worth thinking about actually, and I'm not exactly certain what the most correct behavior is to have here. A test (at least here) would use a The "default" evaluator in This means tests using a which seems sort of funny, and not really correct. I'm not really certain that I think it really comes down to what should be the behavior in default mode of the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
| public void testBitwiseDoubleAndExplosion() | ||||||
| { | ||||||
| expectedException.expect(IllegalArgumentException.class); | ||||||
| expectedException.expectMessage("DOUBLE"); | ||||||
| validateFlatten("3.0 & 1", "(& 3.0 1)", "1"); | ||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
| public void testBitwiseDoubleOrExplosion() | ||||||
| { | ||||||
| expectedException.expect(IllegalArgumentException.class); | ||||||
| expectedException.expectMessage("DOUBLE"); | ||||||
| validateFlatten("3.0 | 1", "(| 3.0 1)", "1"); | ||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
| public void testIdentifiers() | ||||||
| { | ||||||
|
|
@@ -543,7 +581,7 @@ public void testUniquify() | |||||
| } | ||||||
|
|
||||||
|
|
||||||
| private void validateFlatten(String expression, String withoutFlatten, String withFlatten) | ||||||
| private void validateFlatten(String expression, String withoutFlatten, @Nullable String withFlatten) | ||||||
| { | ||||||
| Expr notFlat = Parser.parse(expression, ExprMacroTable.nil(), false); | ||||||
| Expr flat = Parser.parse(expression, ExprMacroTable.nil(), true); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -35,13 +35,15 @@ This expression language supports the following operators (listed in decreasing | |||||
|
|
||||||
| |Operators|Description| | ||||||
| |---------|-----------| | ||||||
| |!, -|Unary NOT and Minus| | ||||||
| |!, -, ^|Unary NOT, Minus, and bitwise Negate| | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, thanks 👍 |
||||||
| |^|Binary power op| | ||||||
| |&, ||Binary bitwise AND, OR| | ||||||
| |*, /, %|Binary multiplicative| | ||||||
| |+, -|Binary additive| | ||||||
| |<, <=, >, >=, ==, !=|Binary Comparison| | ||||||
| |&&, |||Binary Logical AND, OR| | ||||||
|
|
||||||
|
|
||||||
| Long, double, and string data types are supported. If a number contains a dot, it is interpreted as a double, otherwise it is interpreted as a long. That means, always add a '.' to your number if you want it interpreted as a double value. String literals should be quoted by single quotation marks. | ||||||
|
|
||||||
| Additionally, the expression language supports long, double, and string arrays. Array literals are created by wrapping square brackets around a list of scalar literals values delimited by a comma or space character. All values in an array literal must be the same type, however null values are accepted. Typed empty arrays may be defined by prefixing with their type in angle brackets: `<STRING>[]`, `<DOUBLE>[]`, or `<LONG>[]`. | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -196,6 +196,7 @@ backfills | |
| backpressure | ||
| base64 | ||
| big-endian | ||
| bitwise | ||
| blobstore | ||
| boolean | ||
| breakpoint | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👀 I'm surprised this didn't fail with a parse exception before
nice catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it just writes parse error stuff to stderr but still ends up as a valid expression which is how the test passes I guess