Skip to content

Commit

Permalink
Add numeric groupbys (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssanjay1 authored Feb 28, 2024
1 parent 8b0c295 commit d969be2
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 18 deletions.
44 changes: 43 additions & 1 deletion examples/basic/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,37 @@ async function runTests() {
const testAggregateCountWithGroups = await client.objects.BoundariesUsState
.aggregateOrThrow({
select: { $count: true, latitude: ["min", "max", "avg"] },
groupBy: { usState: "exact" },
groupBy: {
usState: "exact",
longitude: {
fixedWidth: 10,
},
},
});

const testAggregateCountWithFixedGroups = await client.objects
.BoundariesUsState
.aggregateOrThrow({
select: { $count: true, latitude: ["min", "max", "avg"] },
groupBy: {
longitude: {
exactWithLimit: 40,
},
},
});

const testAggregateCountWithRangeGroups = await client.objects
.BoundariesUsState
.aggregateOrThrow({
select: { $count: true },
groupBy: {
latitude: {
ranges: [[34, 39], [
39,
42,
], [43, 45]],
},
},
});

console.log(
Expand All @@ -211,6 +241,18 @@ async function runTests() {
testAggregateCountWithGroups[0].latitude.min,
);

console.log(testAggregateCountWithGroups[0].$group.longitude);
console.log(
"Limit worked:",
testAggregateCountWithFixedGroups.length === 40,
);

for (const group of testAggregateCountWithRangeGroups) {
console.log(
`start:${group.$group.latitude.startValue},end:${group.$group.latitude.endValue}:${group.$count}`,
);
}

if (runOld) await typeChecks(client);
} catch (e) {
console.error("Caught an error we did not expect", typeof e);
Expand Down
5 changes: 5 additions & 0 deletions packages/client/changelog/@unreleased/pr-99.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Add numeric groupbys
links:
- https://github.com/palantir/osdk-ts/pull/99
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
* limitations under the License.
*/

import type { AggregationGroupByV2 } from "@osdk/gateway/types";
import type {
AggregationGroupByV2,
AggregationRangeV2,
} from "@osdk/gateway/types";
import type { GroupByRange } from "../../query/aggregations/GroupByClause.js";
import type { AllGroupByValues, GroupByClause } from "../../query/index.js";

export function modernToLegacyGroupByClause(
Expand All @@ -27,16 +31,32 @@ export function modernToLegacyGroupByClause(
).flatMap<AggregationGroupByV2>(([field, type]) => {
if (type === "exact") {
return [{ type, field }];
} else if (type.exactWithLimit) {
return [
{
type: "exact",
field,
maxGroupCount: type.exactWithLimit,
},
];
} else {
return [];
}
} else if ("exactWithLimit" in type) {
{
return [
{
type: "exact",
field,
maxGroupCount: type.exactWithLimit,
},
];
}
} else if ("fixedWidth" in type) {
return [{
type: "fixedWidth",
field,
fixedWidth: type.fixedWidth,
}];
} else if ("ranges" in type) {
return [{
type: "ranges",
field,
ranges: type.ranges.map(range => convertRange(range)),
}];
} else return [];
});
}

function convertRange(range: GroupByRange): AggregationRangeV2 {
return { startValue: range[0], endValue: range[1] };
}
44 changes: 44 additions & 0 deletions packages/client/src/object/aggregateOrThrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ interface TodoDef extends ObjectTypeDefinition<"Todo"> {
id: {
type: "double";
};
intProp: {
type: "integer";
};
floatProp: {
type: "float";
};
shortProp: {
type: "short";
};
byteProp: {
type: "byte";
};
decimalProp: {
type: "decimal";
};
priority: {
type: "double";
};
Expand All @@ -61,6 +76,21 @@ const Todo: TodoDef = {
priority: {
type: "double",
},
intProp: {
type: "integer",
},
floatProp: {
type: "float",
},
shortProp: {
type: "short",
},
byteProp: {
type: "byte",
},
decimalProp: {
type: "decimal",
},
other: {
type: "string",
},
Expand Down Expand Up @@ -173,13 +203,27 @@ describe("aggregateOrThrow", () => {
},
groupBy: {
text: "exact",
priority: { exactWithLimit: 10 },
intProp: { ranges: [[1, 2]] },
shortProp: {
ranges: [[2, 3], [4, 5]],
},
floatProp: { fixedWidth: 10 },
},
},
);
expectType<Array<any>>(grouped);
expectType<string | undefined>(grouped[0].$group.text);
expectType<number>(grouped[0].id.approximateDistinct);
expectType<number>(grouped[0].$group.priority);
expectType<number>(grouped[0].$count);
expectType<{ startValue: number; endValue: number }>(
grouped[0].$group.intProp,
);
expectType<{ startValue: number; endValue: number }>(
grouped[0].$group.shortProp,
);
expectType<number>(grouped[0].$group.floatProp);
});

it("works with where: todo", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
AggregationResultsWithoutGroups,
} from "./AggregationResultsWithoutGroups.js";
import type { AggregationClause } from "./AggregationsClause.js";
import type { GroupByClause } from "./GroupByClause.js";
import type { GroupByClause, GroupByRange } from "./GroupByClause.js";

export type AggregationResultsWithGroups<
Q extends ObjectOrInterfaceDefinition<any, any>,
Expand All @@ -30,9 +30,11 @@ export type AggregationResultsWithGroups<
> = (
& {
$group: {
[P in keyof G & keyof Q["properties"]]: OsdkObjectPropertyType<
Q["properties"][P]
>;
[P in keyof G & keyof Q["properties"]]: G[P] extends
{ ranges: GroupByRange[] } ? { startValue: number; endValue: number }
: OsdkObjectPropertyType<
Q["properties"][P]
>;
};
}
& AggregationCountResult<Q, A>
Expand Down
6 changes: 6 additions & 0 deletions packages/client/src/query/aggregations/GroupByClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export type GroupByClause<
};
export type StringGroupByValue = "exact" | { exactWithLimit: number };

export type GroupByRange = [number, number];

export type NumericGroupByValue = "exact" | { exactWithLimit: number } | {
fixedWidth: number;
} | { ranges: GroupByRange[] };

type GroupByEntry<
Q extends ObjectOrInterfaceDefinition<any, any>,
P extends AggregatableKeys<Q>,
Expand Down
11 changes: 10 additions & 1 deletion packages/client/src/query/aggregations/GroupByMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@
* limitations under the License.
*/

import type { StringGroupByValue } from "./GroupByClause.js";
import type {
NumericGroupByValue,
StringGroupByValue,
} from "./GroupByClause.js";

export interface GroupByMapper {
string: StringGroupByValue;
short: NumericGroupByValue;
float: NumericGroupByValue;
decimal: NumericGroupByValue;
byte: NumericGroupByValue;
double: NumericGroupByValue;
integer: NumericGroupByValue;
}

0 comments on commit d969be2

Please sign in to comment.