Skip to content

Commit be6e04f

Browse files
authored
Merge pull request #261 from diffix/edon/number-parse
Casts to numbers allow invalid inputs
2 parents 464cf45 + 049f5e4 commit be6e04f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/OpenDiffix.Core/Expression.fs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module rec OpenDiffix.Core.Expression
22

3+
open System.Globalization
4+
35
// ----------------------------------------------------------------
46
// Type resolution
57
// ----------------------------------------------------------------
@@ -88,7 +90,7 @@ let widthBucket v b t c =
8890

8991
((v - b) / step) |> floor |> int64 |> max -1L |> min c |> (+) 1L
9092

91-
let private doubleStyle = System.Globalization.NumberFormatInfo.InvariantInfo
93+
let private doubleStyle = NumberFormatInfo.InvariantInfo
9294

9395
/// Evaluates the result of a scalar function invocation.
9496
let rec evaluateScalarFunction fn args =
@@ -174,8 +176,14 @@ let rec evaluateScalarFunction fn args =
174176
else s.Substring(start - 1, min (s.Length - start + 1) length) |> String
175177
| Concat, [ String s1; String s2 ] -> String(s1 + s2)
176178

177-
| Cast, [ String s; String "integer" ] -> if s = "" then Null else s |> System.Int64.Parse |> Integer
178-
| Cast, [ String s; String "real" ] -> if s = "" then Null else System.Double.Parse(s, doubleStyle) |> Real
179+
| Cast, [ String s; String "integer" ] ->
180+
match System.Int64.TryParse(s) with
181+
| true, i -> Integer i
182+
| false, _ -> Null
183+
| Cast, [ String s; String "real" ] ->
184+
match System.Double.TryParse(s, NumberStyles.Float ||| NumberStyles.AllowThousands, doubleStyle) with
185+
| true, r -> Real r
186+
| false, _ -> Null
179187
| Cast, [ String s; String "boolean" ] ->
180188
match s.ToLower() with
181189
| "true"

0 commit comments

Comments
 (0)