Skip to content

Commit 0e8f997

Browse files
committed
MB-36088 - implement NaN float comparisons
1 parent 8a68e74 commit 0e8f997

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

fastval.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,20 @@ func (val FastVal) compareFloat(other FastVal) int {
349349
floatVal := val.AsFloat()
350350
floatOval := other.AsFloat()
351351

352+
if math.IsNaN(floatVal) || math.IsNaN(floatOval) {
353+
// Comparing Not-A-Number
354+
// Documentation wise - they should be aware that NaN ops are undefined
355+
// In the meantime - because we have to return something, just let imaginary numbers be < real numbers
356+
if math.IsNaN(floatVal) && math.IsNaN(floatOval) {
357+
// In go, two NaN's are not the same - thus this should never return 0, so return -1 by default
358+
return -1
359+
} else if math.IsNaN(floatVal) && !math.IsNaN(floatOval) {
360+
return -1
361+
} else if !math.IsNaN(floatVal) && math.IsNaN(floatOval) {
362+
return 1
363+
}
364+
}
365+
352366
// Perform epsilon comparison first
353367
if math.Abs(floatVal-floatOval) < EPSILON {
354368
return 0

filterExprParser_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ OR
841841
userData = map[string]interface{}{
842842
"achievements": 4,
843843
"achievements2": 2,
844+
"int": 93,
845+
"int2": 0,
844846
}
845847
udMarsh, _ = json.Marshal(userData)
846848
match, err = m.Match(udMarsh)
@@ -853,6 +855,7 @@ OR
853855
assert.Nil(err)
854856
matchDef = trans.Transform([]Expression{expr})
855857
assert.NotNil(matchDef)
858+
m = NewFastMatcher(matchDef)
856859
match, err = m.Match(udMarsh)
857860
assert.True(match)
858861

@@ -863,6 +866,7 @@ OR
863866
assert.Nil(err)
864867
matchDef = trans.Transform([]Expression{expr})
865868
assert.NotNil(matchDef)
869+
m = NewFastMatcher(matchDef)
866870
match, err = m.Match(udMarsh)
867871
assert.True(match)
868872

@@ -873,6 +877,7 @@ OR
873877
assert.Nil(err)
874878
matchDef = trans.Transform([]Expression{expr})
875879
assert.NotNil(matchDef)
880+
m = NewFastMatcher(matchDef)
876881
match, err = m.Match(udMarsh)
877882
assert.True(match)
878883

@@ -883,6 +888,51 @@ OR
883888
assert.Nil(err)
884889
matchDef = trans.Transform([]Expression{expr})
885890
assert.NotNil(matchDef)
891+
m = NewFastMatcher(matchDef)
892+
match, err = m.Match(udMarsh)
893+
assert.True(match)
894+
895+
fe = &FilterExpression{}
896+
err = parser.ParseString("ASIN(int)<>3.05682983181e+307 AND ASIN(int) != ASIN(int) AND NOT ASIN(int) > ASIN(int) AND NOT ASIN(int) <= ASIN(int)", fe)
897+
assert.Nil(err)
898+
expr, err = fe.OutputExpression()
899+
assert.Nil(err)
900+
matchDef = trans.Transform([]Expression{expr})
901+
m = NewFastMatcher(matchDef)
902+
assert.NotNil(matchDef)
903+
match, err = m.Match(udMarsh)
904+
assert.True(match)
905+
906+
fe = &FilterExpression{}
907+
err = parser.ParseString("ATAN(int)<>3.05682983181e+307", fe)
908+
assert.Nil(err)
909+
expr, err = fe.OutputExpression()
910+
assert.Nil(err)
911+
matchDef = trans.Transform([]Expression{expr})
912+
m = NewFastMatcher(matchDef)
913+
assert.NotNil(matchDef)
914+
match, err = m.Match(udMarsh)
915+
assert.True(match)
916+
917+
fe = &FilterExpression{}
918+
err = parser.ParseString("ASIN(int2)<>3.05682983181e+307", fe)
919+
assert.Nil(err)
920+
expr, err = fe.OutputExpression()
921+
assert.Nil(err)
922+
matchDef = trans.Transform([]Expression{expr})
923+
m = NewFastMatcher(matchDef)
924+
assert.NotNil(matchDef)
925+
match, err = m.Match(udMarsh)
926+
assert.True(match)
927+
928+
fe = &FilterExpression{}
929+
err = parser.ParseString("ATAN(int2)<>3.05682983181e+307", fe)
930+
assert.Nil(err)
931+
expr, err = fe.OutputExpression()
932+
assert.Nil(err)
933+
matchDef = trans.Transform([]Expression{expr})
934+
m = NewFastMatcher(matchDef)
935+
assert.NotNil(matchDef)
886936
match, err = m.Match(udMarsh)
887937
assert.True(match)
888938

@@ -902,6 +952,7 @@ OR
902952
"achievements": [6]int{49, 58, 108, 141, 177, 229},
903953
}
904954
udMarsh, _ = json.Marshal(userData)
955+
m = NewFastMatcher(matchDef)
905956
match, err = m.Match(udMarsh)
906957
assert.True(match)
907958

@@ -929,6 +980,7 @@ OR
929980
assert.NotNil(matchDef)
930981
udMarsh, _ = json.Marshal(beer)
931982
match, err = m.Match(udMarsh)
983+
m = NewFastMatcher(matchDef)
932984
assert.True(match)
933985

934986
fe = &FilterExpression{}
@@ -944,6 +996,7 @@ OR
944996
assert.Nil(err)
945997
matchDef = trans.Transform([]Expression{expr})
946998
assert.NotNil(matchDef)
999+
m = NewFastMatcher(matchDef)
9471000
match, err = m.Match(marshalledData)
9481001
assert.True(match)
9491002

0 commit comments

Comments
 (0)