-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import stringToArray from "./stringToArray.ts"; | ||
|
||
export default function accumulateQuery( | ||
table: string, | ||
column: string, | ||
newColumn: string, | ||
options: { | ||
categories?: string | string[]; | ||
} = {}, | ||
) { | ||
const categories = options.categories | ||
? stringToArray(options.categories) | ||
: []; | ||
const partition = categories.length > 0 | ||
? `PARTITION BY ${categories.map((d) => `${d}`).join(", ")}` | ||
: ""; | ||
|
||
const query = | ||
`CREATE OR REPLACE TABLE ${table} AS SELECT *, SUM(${column}) OVER (${partition} ORDER BY idForAccumulate) AS ${newColumn} | ||
FROM ${table};`; | ||
|
||
return query; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { assertEquals } from "jsr:@std/assert"; | ||
import SimpleDB from "../../../src/class/SimpleDB.ts"; | ||
|
||
Deno.test("should add the cumulative sum in a new column", async () => { | ||
const sdb = new SimpleDB(); | ||
const table = sdb.newTable("data"); | ||
await table.loadArray([ | ||
{ key1: 1 }, | ||
{ key1: 2 }, | ||
{ key1: 3 }, | ||
]); | ||
await table.accumulate("key1", "cumulative"); | ||
const data = await table.getData(); | ||
assertEquals(data, [ | ||
{ key1: 1, cumulative: 1 }, | ||
{ key1: 2, cumulative: 3 }, | ||
{ key1: 3, cumulative: 6 }, | ||
]); | ||
await sdb.done(); | ||
}); | ||
Deno.test("should add the cumulative sum in a new column without reordering the rows", async () => { | ||
const sdb = new SimpleDB(); | ||
const table = sdb.newTable("data"); | ||
await table.loadArray([ | ||
{ key1: 3 }, | ||
{ key1: 1 }, | ||
{ key1: 2 }, | ||
]); | ||
await table.accumulate("key1", "cumulative"); | ||
const data = await table.getData(); | ||
assertEquals(data, [ | ||
{ key1: 3, cumulative: 3 }, | ||
{ key1: 1, cumulative: 4 }, | ||
{ key1: 2, cumulative: 6 }, | ||
]); | ||
await sdb.done(); | ||
}); | ||
Deno.test("should add the cumulative sum in a new column with categories", async () => { | ||
const sdb = new SimpleDB(); | ||
const table = sdb.newTable("data"); | ||
await table.loadArray([ | ||
{ key1: 6, key2: "b" }, | ||
{ key1: 1, key2: "a" }, | ||
{ key1: 4, key2: "b" }, | ||
{ key1: 2, key2: "a" }, | ||
{ key1: 3, key2: "a" }, | ||
{ key1: 5, key2: "b" }, | ||
]); | ||
await table.accumulate("key1", "cumulative", { categories: "key2" }); | ||
const data = await table.getData(); | ||
assertEquals(data, [ | ||
{ key1: 1, key2: "a", cumulative: 1 }, | ||
{ key1: 2, key2: "a", cumulative: 3 }, | ||
{ key1: 3, key2: "a", cumulative: 6 }, | ||
{ key1: 6, key2: "b", cumulative: 6 }, | ||
{ key1: 4, key2: "b", cumulative: 10 }, | ||
{ key1: 5, key2: "b", cumulative: 15 }, | ||
]); | ||
await sdb.done(); | ||
}); |