Skip to content

Commit 48ab801

Browse files
Rename generalized round/floor/ceil functions.
1 parent 84d728e commit 48ab801

File tree

7 files changed

+80
-46
lines changed

7 files changed

+80
-46
lines changed

src/OpenDiffix.Core.Tests/Analyzer.Tests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,12 @@ type Tests(db: DBFixture) =
423423

424424
[<Fact>]
425425
let ``SQL seed from column generalization`` () =
426-
assertSqlSeed "SELECT substring(city, 1, 2) FROM customers_small" "substring,customers_small.city,1,2"
426+
assertSqlSeed "SELECT substring(city, 1, 2) FROM customers_small" "range,customers_small.city,1,2"
427427

428428
[<Fact>]
429429
let ``SQL seed from multiple groupings from multiple tables`` () =
430430
assertSqlSeed
431431
"SELECT count(*) FROM customers_small JOIN purchases ON id = cid GROUP BY city, round(amount)"
432-
"customers_small.city,round,purchases.amount"
432+
"customers_small.city,range,purchases.amount"
433433

434434
interface IClassFixture<DBFixture>

src/OpenDiffix.Core.Tests/Expression.Tests.fs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,18 @@ module DefaultFunctionsTests =
272272
Null, Null
273273
]
274274

275-
runsBinary
275+
fails
276276
Round
277+
[ //
278+
[ Integer 5L ]
279+
[ Boolean true ]
280+
[ String "a" ]
281+
]
282+
283+
[<Fact>]
284+
let RoundBy () =
285+
runsBinary
286+
RoundBy
277287
[
278288
Integer 3L, Integer 2L, Integer 4L
279289
Integer 3L, Real 2.5, Real 2.5
@@ -285,11 +295,8 @@ module DefaultFunctionsTests =
285295
]
286296

287297
fails
288-
Round
298+
RoundBy
289299
[ //
290-
[ Integer 5L ]
291-
[ Boolean true ]
292-
[ String "a" ]
293300
[ Integer 5L; String "a" ]
294301
[ String "a"; Real 1.0 ]
295302
]
@@ -306,8 +313,18 @@ module DefaultFunctionsTests =
306313
Null, Null
307314
]
308315

309-
runsBinary
316+
fails
310317
Ceil
318+
[ //
319+
[ Integer 5L ]
320+
[ Boolean true ]
321+
[ String "a" ]
322+
]
323+
324+
[<Fact>]
325+
let CeilBy () =
326+
runsBinary
327+
CeilBy
311328
[
312329
Integer 3L, Integer 2L, Integer 4L
313330
Integer 3L, Real 2.5, Real 5.0
@@ -319,11 +336,8 @@ module DefaultFunctionsTests =
319336
]
320337

321338
fails
322-
Ceil
339+
CeilBy
323340
[ //
324-
[ Integer 5L ]
325-
[ Boolean true ]
326-
[ String "a" ]
327341
[ Integer 5L; String "a" ]
328342
[ String "a"; Real 1.0 ]
329343
]
@@ -340,8 +354,18 @@ module DefaultFunctionsTests =
340354
Null, Null
341355
]
342356

343-
runsBinary
357+
fails
344358
Floor
359+
[ //
360+
[ Integer 5L ]
361+
[ Boolean true ]
362+
[ String "a" ]
363+
]
364+
365+
[<Fact>]
366+
let FloorbY () =
367+
runsBinary
368+
FloorBy
345369
[
346370
Integer 3L, Integer 2L, Integer 2L
347371
Integer 3L, Real 2.5, Real 2.5
@@ -353,11 +377,8 @@ module DefaultFunctionsTests =
353377
]
354378

355379
fails
356-
Floor
380+
FloorBy
357381
[ //
358-
[ Integer 5L ]
359-
[ Boolean true ]
360-
[ String "a" ]
361382
[ Integer 5L; String "a" ]
362383
[ String "a"; Real 1.0 ]
363384
]

src/OpenDiffix.Core.Tests/QueryEngine.Tests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ type Tests(db: DBFixture) =
168168

169169
[<Fact>]
170170
let ``query 12 - group with rounding`` () =
171-
let queryResult = runQuery "SELECT round(age, 5), count(*) FROM customers_small GROUP BY 1"
171+
let queryResult = runQuery "SELECT round_by(age, 5), count(*) FROM customers_small GROUP BY 1"
172172

173173
let expected =
174174
{
175-
Columns = [ { Name = "round"; Type = IntegerType }; { Name = "count"; Type = IntegerType } ]
175+
Columns = [ { Name = "round_by"; Type = IntegerType }; { Name = "count"; Type = IntegerType } ]
176176
Rows = [ [| Integer 25L; Integer 7L |]; [| Integer 30L; Integer 7L |]; [| Integer 35L; Integer 6L |] ]
177177
}
178178

src/OpenDiffix.Core/Analyzer.fs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,16 @@ let rec private basicSeedMaterial rangeColumns expression =
321321

322322
let private functionSeedMaterial =
323323
function
324-
| Substring -> "substring"
325-
| Ceil -> "ceil"
326-
| Floor -> "floor"
327-
| Round -> "round"
324+
| Substring
325+
| Ceil
326+
| Floor
327+
| Round
328+
| Ceil
329+
| Floor
330+
| Round
331+
| CeilBy
332+
| FloorBy
333+
| RoundBy -> "range"
328334
| WidthBucket -> "width_bucket"
329335
| Concat -> "concat"
330336
| _ -> failwith "Unsupported function used for defining buckets."

src/OpenDiffix.Core/CommonTypes.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ type ScalarFunction =
5959
| Round
6060
| Floor
6161
| Ceil
62+
| RoundBy
63+
| FloorBy
64+
| CeilBy
6265
| Abs
6366
| Length
6467
| Lower

src/OpenDiffix.Core/Expression.fs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ let typeOfScalarFunction fn args =
3333
| Length -> IntegerType
3434
| Round
3535
| Floor
36-
| Ceil ->
37-
match args with
38-
| [ _ ] -> IntegerType
39-
| [ _; amount ] -> typeOf amount
40-
| _ -> failwith $"Invalid arguments supplied to function `#{fn}`."
36+
| Ceil -> IntegerType
37+
| RoundBy
38+
| FloorBy
39+
| CeilBy -> args |> List.item 1 |> typeOf
4140
| Abs -> args |> List.exactlyOne |> typeOf
4241
| Lower
4342
| Upper
@@ -109,28 +108,30 @@ let rec evaluateScalarFunction fn args =
109108
| Or, [ Boolean b1; Boolean b2 ] -> Boolean(b1 || b2)
110109

111110
| Round, [ Real r ] -> r |> round |> int64 |> Integer
112-
| Round, [ _; Integer amount ] when amount <= 0L -> Null
113-
| Round, [ _; Real amount ] when amount <= 0.0 -> Null
114-
| Round, [ Integer value; Integer amount ] -> (float value / float amount) |> round |> int64 |> (*) amount |> Integer
115-
| Round, [ Integer value; Real amount ] -> (float value / amount) |> round |> (*) amount |> Real
116-
| Round, [ Real value; Integer amount ] -> (value / float amount) |> round |> int64 |> (*) amount |> Integer
117-
| Round, [ Real value; Real amount ] -> (value / amount) |> round |> (*) amount |> Real
111+
| RoundBy, [ _; Integer amount ] when amount <= 0L -> Null
112+
| RoundBy, [ _; Real amount ] when amount <= 0.0 -> Null
113+
| RoundBy, [ Integer value; Integer amount ] ->
114+
(float value / float amount) |> round |> int64 |> (*) amount |> Integer
115+
| RoundBy, [ Integer value; Real amount ] -> (float value / amount) |> round |> (*) amount |> Real
116+
| RoundBy, [ Real value; Integer amount ] -> (value / float amount) |> round |> int64 |> (*) amount |> Integer
117+
| RoundBy, [ Real value; Real amount ] -> (value / amount) |> round |> (*) amount |> Real
118118

119119
| Ceil, [ Real r ] -> r |> ceil |> int64 |> Integer
120-
| Ceil, [ _; Integer amount ] when amount <= 0L -> Null
121-
| Ceil, [ _; Real amount ] when amount <= 0.0 -> Null
122-
| Ceil, [ Integer value; Integer amount ] -> (float value / float amount) |> ceil |> int64 |> (*) amount |> Integer
123-
| Ceil, [ Integer value; Real amount ] -> (float value / amount) |> ceil |> (*) amount |> Real
124-
| Ceil, [ Real value; Integer amount ] -> (value / float amount) |> ceil |> int64 |> (*) amount |> Integer
125-
| Ceil, [ Real value; Real amount ] -> (value / amount) |> ceil |> (*) amount |> Real
120+
| CeilBy, [ _; Integer amount ] when amount <= 0L -> Null
121+
| CeilBy, [ _; Real amount ] when amount <= 0.0 -> Null
122+
| CeilBy, [ Integer value; Integer amount ] -> (float value / float amount) |> ceil |> int64 |> (*) amount |> Integer
123+
| CeilBy, [ Integer value; Real amount ] -> (float value / amount) |> ceil |> (*) amount |> Real
124+
| CeilBy, [ Real value; Integer amount ] -> (value / float amount) |> ceil |> int64 |> (*) amount |> Integer
125+
| CeilBy, [ Real value; Real amount ] -> (value / amount) |> ceil |> (*) amount |> Real
126126

127127
| Floor, [ Real r ] -> r |> floor |> int64 |> Integer
128-
| Floor, [ _; Integer amount ] when amount <= 0L -> Null
129-
| Floor, [ _; Real amount ] when amount <= 0.0 -> Null
130-
| Floor, [ Integer value; Integer amount ] -> (float value / float amount) |> floor |> int64 |> (*) amount |> Integer
131-
| Floor, [ Integer value; Real amount ] -> (float value / amount) |> floor |> (*) amount |> Real
132-
| Floor, [ Real value; Integer amount ] -> (value / float amount) |> floor |> int64 |> (*) amount |> Integer
133-
| Floor, [ Real value; Real amount ] -> (value / amount) |> floor |> (*) amount |> Real
128+
| FloorBy, [ _; Integer amount ] when amount <= 0L -> Null
129+
| FloorBy, [ _; Real amount ] when amount <= 0.0 -> Null
130+
| FloorBy, [ Integer value; Integer amount ] ->
131+
(float value / float amount) |> floor |> int64 |> (*) amount |> Integer
132+
| FloorBy, [ Integer value; Real amount ] -> (float value / amount) |> floor |> (*) amount |> Real
133+
| FloorBy, [ Real value; Integer amount ] -> (value / float amount) |> floor |> int64 |> (*) amount |> Integer
134+
| FloorBy, [ Real value; Real amount ] -> (value / amount) |> floor |> (*) amount |> Real
134135

135136
| Abs, [ Real r ] -> r |> abs |> Real
136137
| Abs, [ Integer i ] -> i |> abs |> Integer

src/OpenDiffix.Core/Utils.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ module Function =
5454
| "round" -> ScalarFunction Round
5555
| "ceil" -> ScalarFunction Ceil
5656
| "floor" -> ScalarFunction Floor
57+
| "round_by" -> ScalarFunction RoundBy
58+
| "ceil_by" -> ScalarFunction CeilBy
59+
| "floor_by" -> ScalarFunction FloorBy
5760
| "abs" -> ScalarFunction Abs
5861
| "length" -> ScalarFunction Length
5962
| "lower" -> ScalarFunction Lower

0 commit comments

Comments
 (0)