Skip to content

Add string_agg function, partition_by and order_by for analytic functions, and partition_by and limit for string_agg #1568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0209bd0
Add string_agg function
Jan 4, 2024
79698d0
WIP
Jan 5, 2024
b7bb290
WIP with syntax for order by and limit
Jan 9, 2024
945beec
WIP
Jan 10, 2024
6159639
Remove old stuff
Jan 29, 2024
b1774aa
Merge branch 'main' into string_agg
Jan 29, 2024
f0f2f57
Remove another old thing
Jan 29, 2024
2bea50e
Add/improve tests
Jan 29, 2024
e7b9bb8
Revert "Remove old stuff"
Jan 29, 2024
0a9fd14
Revert "Remove another old thing"
Jan 29, 2024
276c818
WIP -- actually go back to field props method of spcifying order by a…
Jan 29, 2024
0f15979
WIP Partition by
Jan 29, 2024
33b44cc
Allow order_by to be used for analytic functions, and add a real test…
Jan 30, 2024
9e99fb1
(experimental) allow dialects to have different values of supportsLim…
Jan 30, 2024
80ff15f
Partition by does not allow expressions actually, only output field n…
Jan 30, 2024
3077087
Remove test for filter shortcut (it is gone)
Jan 30, 2024
17a4009
Add string_agg_distinct
Jan 30, 2024
86e5efa
Fix issue with orderBy showing up as undefined: OVER ( undefined )
Jan 30, 2024
ab46357
Add errors for misusing field props, and add experiment ids
Jan 30, 2024
86af91c
Fix non-ordered string_agg test to allow either ordering
Jan 30, 2024
fac9810
Same thing for string_agg_distinct
Jan 30, 2024
634374a
Allow multuiple partition_by, order_by, and where statements in field…
Jan 30, 2024
9eb958e
Move some files around
Jan 30, 2024
5a66be0
Remove outdated methods
Jan 30, 2024
97300d2
Add partition by to grammar
Jan 30, 2024
84b2ed7
Remove old code
Jan 30, 2024
3c0206f
Lint fix
Jan 30, 2024
708ac4c
Fix analytic ordering to allow aggregate or output field, fix aggrega…
Jan 31, 2024
e292c31
Require comma for partition by
Jan 31, 2024
bf12634
Use `malloyResultMatches` for tests
Jan 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export const monarch: Monaco.IMonarchLanguage = {
[/\bmeasure\b/, 'keyword.control.measure'],
[/\bnest\b/, 'keyword.control.nest'],
[/\border_by\b/, 'keyword.control.order_by'],
[/\bpartition_by\b/, 'keyword.control.partition_by'],
[/\bprimary_key\b/, 'keyword.control.primary_key'],
[/\bproject\b/, 'keyword.control.project'],
[/\bquery\b/, 'keyword.control.query'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@
"match": "(?i)\\border_by\\b",
"name": "keyword.control.order_by"
},
{
"match": "(?i)\\bpartition_by\\b",
"name": "keyword.control.partition_by"
},
{
"match": "(?i)\\bprimary_key\\b",
"name": "keyword.control.primary_key"
Expand Down
14 changes: 12 additions & 2 deletions packages/malloy/src/dialect/dialect_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,24 @@ export function getDialectFunction(name: string): FunctionDef | undefined {
if (!returnEqual(overload, existingOverload)) {
throw new Error('params match but return types differ!');
}
existingOverload.dialect[dialect.name] = overload.e;
existingOverload.dialect[dialect.name] = {
e: overload.e,
supportsOrderBy: overload.supportsOrderBy,
supportsLimit: overload.supportsLimit,
};
handled = true;
}
if (!handled) {
func.overloads.push({
returnType: overload.returnType,
params: overload.params,
dialect: {[dialect.name]: overload.e},
dialect: {
[dialect.name]: {
e: overload.e,
supportsOrderBy: overload.supportsOrderBy,
supportsLimit: overload.supportsLimit,
},
},
needsWindowOrderBy: overload.needsWindowOrderBy,
between: overload.between,
isSymmetric: overload.isSymmetric,
Expand Down
3 changes: 3 additions & 0 deletions packages/malloy/src/dialect/functions/all_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import {
import {fnAvgRolling} from './avg_moving';
import {FunctionMap} from './function_map';
import {fnCoalesce} from './coalesce';
import {fnStringAgg, fnStringAggDistinct} from './string_agg';
import {
fnSqlBoolean,
fnSqlDate,
Expand Down Expand Up @@ -146,6 +147,8 @@ FUNCTIONS.add('exp', fnExp);

// Aggregate functions
FUNCTIONS.add('stddev', fnStddev);
FUNCTIONS.add('string_agg', fnStringAgg);
FUNCTIONS.add('string_agg_distinct', fnStringAggDistinct);

// Analytic functions
FUNCTIONS.add('row_number', fnRowNumber);
Expand Down
73 changes: 73 additions & 0 deletions packages/malloy/src/dialect/functions/string_agg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2023 Google LLC
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import {Fragment} from '../../model';
import {
overload,
minAggregate,
maxScalar,
sql,
DialectFunctionOverloadDef,
makeParam,
literal,
} from './util';

export function fnStringAgg(): DialectFunctionOverloadDef[] {
const value = makeParam('value', maxScalar('string'));
const separator = makeParam('separator', literal(maxScalar('string')));
const orderBy: Fragment = {type: 'aggregate_order_by'};
return [
overload(
minAggregate('string'),
[value.param],
sql`STRING_AGG(${value.arg}${orderBy})`,
{supportsOrderBy: true}
),
overload(
minAggregate('string'),
[value.param, separator.param],
sql`STRING_AGG(${value.arg}, ${separator.arg}${orderBy})`,
{supportsOrderBy: true}
),
];
}

export function fnStringAggDistinct(): DialectFunctionOverloadDef[] {
const value = makeParam('value', maxScalar('string'));
const separator = makeParam('separator', literal(maxScalar('string')));
const orderBy: Fragment = {type: 'aggregate_order_by'};
return [
overload(
minAggregate('string'),
[value.param],
sql`STRING_AGG(DISTINCT ${value.arg}${orderBy})`,
{isSymmetric: true, supportsOrderBy: true}
),
overload(
minAggregate('string'),
[value.param, separator.param],
sql`STRING_AGG(DISTINCT ${value.arg}, ${separator.arg}${orderBy})`,
{isSymmetric: true, supportsOrderBy: true}
),
];
}
8 changes: 8 additions & 0 deletions packages/malloy/src/dialect/functions/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface DialectFunctionOverloadDef {
e: Expr;
needsWindowOrderBy?: boolean;
isSymmetric?: boolean;
supportsOrderBy?: boolean;
supportsLimit?: boolean;
between: {preceding: number | string; following: number | string} | undefined;
}

Expand Down Expand Up @@ -213,6 +215,9 @@ export function overload(
options?: {
needsWindowOrderBy?: boolean;
between?: {preceding: number | string; following: number | string};
isSymmetric?: boolean;
supportsLimit?: boolean;
supportsOrderBy?: boolean;
}
): DialectFunctionOverloadDef {
return {
Expand All @@ -221,5 +226,8 @@ export function overload(
e,
needsWindowOrderBy: options?.needsWindowOrderBy,
between: options?.between,
isSymmetric: options?.isSymmetric,
supportsOrderBy: options?.supportsOrderBy,
supportsLimit: options?.supportsLimit,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

import {FUNCTIONS} from '../../functions';
import {fnStringAgg, fnStringAggDistinct} from './string_agg';
import {fnByteLength} from './byte_length';
import {fnEndsWith} from './ends_with';
import {fnGreatest, fnLeast} from './greatest_and_least';
Expand Down Expand Up @@ -54,5 +55,7 @@ POSTGRES_FUNCTIONS.add('trunc', fnTrunc);
POSTGRES_FUNCTIONS.add('substr', fnSubstr);
POSTGRES_FUNCTIONS.add('replace', fnReplace);
POSTGRES_FUNCTIONS.add('ends_with', fnEndsWith);
POSTGRES_FUNCTIONS.add('string_agg', fnStringAgg);
POSTGRES_FUNCTIONS.add('string_agg_distinct', fnStringAggDistinct);
POSTGRES_FUNCTIONS.add('log', fnLog);
POSTGRES_FUNCTIONS.seal();
73 changes: 73 additions & 0 deletions packages/malloy/src/dialect/postgres/functions/string_agg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2023 Google LLC
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import {Fragment} from '../../../model';
import {
overload,
minAggregate,
maxScalar,
sql,
DialectFunctionOverloadDef,
makeParam,
literal,
} from '../../functions/util';

export function fnStringAgg(): DialectFunctionOverloadDef[] {
const value = makeParam('value', maxScalar('string'));
const separator = makeParam('separator', literal(maxScalar('string')));
const orderBy: Fragment = {type: 'aggregate_order_by'};
return [
overload(
minAggregate('string'),
[value.param],
sql`STRING_AGG(${value.arg}, ','${orderBy})`,
{supportsOrderBy: true}
),
overload(
minAggregate('string'),
[value.param, separator.param],
sql`STRING_AGG(${value.arg}, ${separator.arg}${orderBy})`,
{supportsOrderBy: true}
),
];
}

export function fnStringAggDistinct(): DialectFunctionOverloadDef[] {
const value = makeParam('value', maxScalar('string'));
const separator = makeParam('separator', literal(maxScalar('string')));
const orderBy: Fragment = {type: 'aggregate_order_by'};
return [
overload(
minAggregate('string'),
[value.param],
sql`STRING_AGG(DISTINCT ${value.arg}, ','${orderBy})`,
{isSymmetric: true, supportsOrderBy: true}
),
overload(
minAggregate('string'),
[value.param, separator.param],
sql`STRING_AGG(DISTINCT ${value.arg}, ${separator.arg}${orderBy})`,
{isSymmetric: true, supportsOrderBy: true}
),
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
*/

import {FUNCTIONS} from '../../functions';
import {fnStringAgg, fnStringAggDistinct} from './string_agg';
import {fnChr} from './chr';
import {fnPi} from './pi';

export const STANDARDSQL_FUNCTIONS = FUNCTIONS.clone();
STANDARDSQL_FUNCTIONS.add('pi', fnPi);
STANDARDSQL_FUNCTIONS.add('chr', fnChr);
STANDARDSQL_FUNCTIONS.add('string_agg', fnStringAgg);
STANDARDSQL_FUNCTIONS.add('string_agg_distinct', fnStringAggDistinct);
STANDARDSQL_FUNCTIONS.seal();
75 changes: 75 additions & 0 deletions packages/malloy/src/dialect/standardsql/functions/string_agg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2023 Google LLC
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import {Fragment} from '../../../model';
import {
overload,
minAggregate,
maxScalar,
sql,
DialectFunctionOverloadDef,
makeParam,
literal,
} from '../../functions/util';

export function fnStringAgg(): DialectFunctionOverloadDef[] {
const value = makeParam('value', maxScalar('string'));
const separator = makeParam('separator', literal(maxScalar('string')));
const orderBy: Fragment = {type: 'aggregate_order_by'};
const limit: Fragment = {type: 'aggregate_limit'};
return [
overload(
minAggregate('string'),
[value.param],
sql`STRING_AGG(${value.arg}${orderBy}${limit})`,
{supportsOrderBy: true, supportsLimit: true}
),
overload(
minAggregate('string'),
[value.param, separator.param],
sql`STRING_AGG(${value.arg}, ${separator.arg}${orderBy}${limit})`,
{supportsOrderBy: true, supportsLimit: true}
),
];
}

export function fnStringAggDistinct(): DialectFunctionOverloadDef[] {
const value = makeParam('value', maxScalar('string'));
const separator = makeParam('separator', literal(maxScalar('string')));
const orderBy: Fragment = {type: 'aggregate_order_by'};
const limit: Fragment = {type: 'aggregate_limit'};
return [
overload(
minAggregate('string'),
[value.param],
sql`STRING_AGG(DISTINCT ${value.arg}${orderBy}${limit})`,
{isSymmetric: true, supportsOrderBy: true, supportsLimit: true}
),
overload(
minAggregate('string'),
[value.param, separator.param],
sql`STRING_AGG(DISTINCT ${value.arg}, ${separator.arg}${orderBy}${limit})`,
{isSymmetric: true, supportsOrderBy: true, supportsLimit: true}
),
];
}
Loading