Skip to content

Commit

Permalink
Add percent_rank presto function (#1988)
Browse files Browse the repository at this point in the history
* Add percent_rank presto function

* Spell percent correctly
  • Loading branch information
christopherswenson authored Oct 31, 2024
1 parent 9948842 commit 0b95c39
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/malloy/src/dialect/trino/dialect_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ const to_unixtime: DefinitionBlueprint = {
impl: {function: 'TO_UNIXTIME'},
};

const percent_rank: DefinitionBlueprint = {
takes: {},
returns: {calculation: 'number'},
impl: {function: 'PERCENT_RANK', needsWindowOrderBy: true},
};

export const TRINO_DIALECT_FUNCTIONS: DefinitionBlueprintMap = {
// aggregate functions
approx_percentile,
Expand Down Expand Up @@ -254,4 +260,7 @@ export const TRINO_DIALECT_FUNCTIONS: DefinitionBlueprintMap = {
regexp_like,
regexp_replace,
to_unixtime,

// window functions
percent_rank,
};
40 changes: 40 additions & 0 deletions test/src/databases/presto-trino/presto-trino.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,46 @@ describe.each(runtimes.runtimeList)(
m2 is min_by(y, x)
}`).malloyResultMatches(runtime, {m1: 55, m2: 100});
});

it(`runs the percent_rank function - ${databaseName}`, async () => {
await expect(`# debug
run: ${databaseName}.sql(
"""
SELECT 55 as x
UNION ALL SELECT 22 as x
UNION ALL SELECT 1 as x
"""
) -> {
group_by: x
order_by: x desc
calculate: pctrnk is percent_rank()
}
`).malloyResultMatches(runtime, [
{x: 55, pctrnk: 0},
{x: 22, pctrnk: 0.5},
{x: 1, pctrnk: 1},
]);
});

it(`runs the percent_rank function with order_by - ${databaseName}`, async () => {
await expect(`# debug
run: ${databaseName}.sql(
"""
SELECT 55 as x
UNION ALL SELECT 22 as x
UNION ALL SELECT 1 as x
"""
) -> {
group_by: x
order_by: x desc
calculate: pctrnk is percent_rank() { order_by: x asc }
}
`).malloyResultMatches(runtime, [
{x: 55, pctrnk: 1},
{x: 22, pctrnk: 0.5},
{x: 1, pctrnk: 0},
]);
});
}
);

Expand Down

0 comments on commit 0b95c39

Please sign in to comment.