-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
f0da1d7
commit 3882e4d
Showing
11 changed files
with
231 additions
and
3 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
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
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,102 @@ | ||
import { Parser, Grammar } from "nearley"; | ||
import grammar from "../grammar"; | ||
|
||
const oqlGrammar = Grammar.fromCompiled(grammar); | ||
|
||
const get = (input: string): unknown[] => { | ||
const oqlParser = new Parser(oqlGrammar); | ||
oqlParser.feed(input); | ||
return oqlParser.results; | ||
}; | ||
|
||
const tests: [string, { query: string; expected: unknown }][] = [ | ||
[ | ||
"pivot - with default arguments", | ||
{ | ||
query: `pivot count()`, | ||
expected: [ | ||
{ | ||
type: "pivot", | ||
value: { | ||
metric: { alias: undefined, args: [], operator: "count" }, | ||
fields: [], | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
[ | ||
"pivot - with just aggregation", | ||
{ | ||
query: `pivot sum("quantity")`, | ||
expected: [ | ||
{ | ||
type: "pivot", | ||
value: { | ||
metric: { alias: undefined, args: [{ type: "ref", value: "quantity" }], operator: "sum" }, | ||
fields: [], | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
[ | ||
"pivot - with aggregation and col", | ||
{ | ||
query: `pivot sum("quantity"), "fruit"`, | ||
expected: [ | ||
{ | ||
type: "pivot", | ||
value: { | ||
metric: { alias: undefined, args: [{ type: "ref", value: "quantity" }], operator: "sum" }, | ||
fields: [{ type: "ref", value: "fruit" }], | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
[ | ||
"pivot - with aggregation and col and row", | ||
{ | ||
query: `pivot sum("quantity"), "fruit", "size"`, | ||
expected: [ | ||
{ | ||
type: "pivot", | ||
value: { | ||
metric: { alias: undefined, args: [{ type: "ref", value: "quantity" }], operator: "sum" }, | ||
fields: [ | ||
{ type: "ref", value: "fruit" }, | ||
{ type: "ref", value: "size" }, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
[ | ||
"pivot - with aggregation and col and row with alias", | ||
{ | ||
query: `pivot "qty"=sum("quantity"), "fruit", "size"`, | ||
expected: [ | ||
{ | ||
type: "pivot", | ||
value: { | ||
metric: { alias: "qty", args: [{ type: "ref", value: "quantity" }], operator: "sum" }, | ||
fields: [ | ||
{ type: "ref", value: "fruit" }, | ||
{ type: "ref", value: "size" }, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
]; | ||
|
||
describe("grammar pivot", () => { | ||
it.each(tests)("%s", (_, test) => { | ||
const { query, expected } = test as { query: string; expected: unknown }; | ||
const results = get(query as string); | ||
expect(results[0]).toStrictEqual(expected); | ||
}); | ||
}); |
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
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,45 @@ | ||
import { uql } from "../index"; | ||
|
||
describe("pivot", () => { | ||
const data = { | ||
fruits: [ | ||
{ fruit: "apple", size: "sm", qty: 1 }, | ||
{ fruit: "apple", size: "md", qty: 2 }, | ||
{ fruit: "apple", size: "lg", qty: 3 }, | ||
{ fruit: "banana", size: "sm", qty: 1 }, | ||
{ fruit: "banana", size: "lg", qty: 6 }, | ||
{ fruit: "banana", size: "xl", qty: 5 }, | ||
], | ||
}; | ||
describe("basic", () => { | ||
it("with default arguments", async () => { | ||
expect(await uql(`pivot count()`, { data: data.fruits })).toStrictEqual(6); | ||
}); | ||
it("with custom operator", async () => { | ||
expect(await uql(`pivot sum("qty")`, { data: data.fruits })).toStrictEqual(18); | ||
expect(await uql(`pivot max("qty")`, { data: data.fruits })).toStrictEqual(6); | ||
}); | ||
it("with custom operator with row", async () => { | ||
expect(await uql(`pivot sum("qty"), "fruit"`, { data: data.fruits })).toStrictEqual([ | ||
{ fruit: "apple", value: 6 }, | ||
{ fruit: "banana", value: 12 }, | ||
]); | ||
expect(await uql(`pivot count("qty"), "size"`, { data: data.fruits })).toStrictEqual([ | ||
{ size: "sm", value: 2 }, | ||
{ size: "md", value: 1 }, | ||
{ size: "lg", value: 2 }, | ||
{ size: "xl", value: 1 }, | ||
]); | ||
}); | ||
it("with custom operator with row and col", async () => { | ||
expect(await uql(`pivot sum("qty"), "fruit", "size"`, { data: data.fruits })).toStrictEqual([ | ||
{ fruit: "apple", sm: 1, md: 2, lg: 3, xl: 0 }, | ||
{ fruit: "banana", sm: 1, md: 0, lg: 6, xl: 5 }, | ||
]); | ||
expect(await uql(`pivot max("qty"), "fruit", "size"`, { data: data.fruits })).toStrictEqual([ | ||
{ fruit: "apple", sm: 1, md: 2, lg: 3, xl: null }, | ||
{ fruit: "banana", sm: 1, md: null, lg: 6, xl: 5 }, | ||
]); | ||
}); | ||
}); | ||
}); |
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,55 @@ | ||
import { isArray, uniq } from "lodash"; | ||
import { UQLsummarize } from "./../summarize/summarize"; | ||
import { Command, CommandResult } from "../../types"; | ||
|
||
export const pivot = (pv: CommandResult, cv: Extract<Command, { type: "pivot" }>): CommandResult => { | ||
let input = pv.output; | ||
if (input == null || !isArray(input)) { | ||
return { ...pv, output: null }; | ||
} | ||
let item = cv.value; | ||
let rows: string[] = []; | ||
if (item && item.fields && item.fields.length > 0) { | ||
rows = uniq(input.map((u) => (item && item.fields ? u[item.fields[0].value] : ""))).filter((v) => v !== ""); | ||
} | ||
let cols: string[] = []; | ||
if (item && item.fields && item.fields.length > 1) { | ||
cols = uniq(input.map((u) => (item && item.fields ? u[item.fields[1].value] : ""))).filter((v) => v !== ""); | ||
} | ||
if (item.fields?.length === 2) { | ||
let out: any[] = []; | ||
rows.forEach((r) => { | ||
let rowName = item && item.fields ? item.fields[0].value : ""; | ||
let colName = item && item.fields ? item.fields[1].value : ""; | ||
let outValue: Record<string, any> = { [rowName]: r }; | ||
cols.forEach((c) => { | ||
let currentItems = (input as any[]).filter((ins) => ins[rowName] === r && ins[colName] === c) || []; | ||
if (currentItems.length === 0) { | ||
outValue[c] = item.metric.operator === "count" || item.metric.operator === "dcount" || item.metric.operator === "sum" ? 0 : null; | ||
} else { | ||
let v: any = UQLsummarize({}, [{ ...item.metric, alias: item.metric.operator }], currentItems); | ||
outValue[c] = v[item.metric.operator]; | ||
} | ||
}); | ||
out.push(outValue); | ||
}); | ||
return { ...pv, output: out }; | ||
} | ||
if (item.fields?.length === 1) { | ||
let out: any[] = []; | ||
rows.forEach((r) => { | ||
let rowName = item && item.fields ? item.fields[0].value : ""; | ||
let outValue = { [rowName]: r }; | ||
let v: any = UQLsummarize( | ||
{}, | ||
[{ ...item.metric, alias: item.metric.operator }], | ||
(input as any[]).filter((ins) => ins[rowName] === r) | ||
); | ||
outValue["value"] = v[item.metric.operator]; | ||
out.push(outValue); | ||
}); | ||
return { ...pv, output: out }; | ||
} | ||
let v: any = UQLsummarize({}, [{ ...item.metric, alias: item.metric.operator }], input); | ||
return { ...pv, output: v[item.metric.operator] }; | ||
}; |
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