Skip to content

Commit 230b125

Browse files
Allow functions to accept and return sql native types
1 parent e9c0bcd commit 230b125

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

packages/malloy/src/dialect/duckdb/dialect_functions.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ const dayname: DefinitionBlueprint = {
2424
impl: {function: 'DAYNAME'},
2525
};
2626

27+
const date_part: DefinitionBlueprint = {
28+
takes: {'part': 'string', 'interval': {sql_native: 'interval'}},
29+
returns: 'number',
30+
impl: {function: 'DATE_PART'},
31+
};
32+
33+
const to_seconds: DefinitionBlueprint = {
34+
takes: {'seconds': 'number'},
35+
returns: {sql_native: 'interval'},
36+
impl: {function: 'TO_SECONDS'},
37+
};
38+
2739
const to_timestamp: DefinitionBlueprint = {
2840
takes: {'epoch_seconds': 'number'},
2941
returns: 'timestamp',
@@ -87,4 +99,6 @@ export const DUCKDB_DIALECT_FUNCTIONS: DefinitionBlueprintMap = {
8799
to_timestamp,
88100
string_agg,
89101
string_agg_distinct,
102+
to_seconds,
103+
date_part,
90104
};

packages/malloy/src/dialect/functions/util.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,17 @@ export type TypeDescElementBlueprintOrNamedGeneric =
231231
export interface RecordBlueprint {
232232
record: Record<string, TypeDescElementBlueprintOrNamedGeneric>;
233233
}
234+
235+
export interface SQLNativeTypeBlueprint {
236+
sql_native: string;
237+
}
238+
234239
export type LeafPlusType = LeafExpressionType | 'any';
235240
export type TypeDescElementBlueprint =
236241
| LeafPlusType
237242
| ArrayBlueprint
238-
| RecordBlueprint;
243+
| RecordBlueprint
244+
| SQLNativeTypeBlueprint;
239245
export type NamedGeneric = {generic: string};
240246

241247
export type TypeDescBlueprint =
@@ -362,6 +368,8 @@ function expandTypeDescElementBlueprint(
362368
throw new Error('Cannot use generic');
363369
}
364370
return {type: 'generic', generic: blueprint.generic};
371+
} else if ('sql_native' in blueprint) {
372+
return {type: 'sql native', rawType: blueprint.sql_native};
365373
}
366374
throw new Error('Cannot figure out type');
367375
}
@@ -396,6 +404,8 @@ function expandReturnTypeBlueprint(
396404
return minAggregate(
397405
expandTypeDescElementBlueprint(blueprint.measure, false)
398406
);
407+
} else if ('sql_native' in blueprint) {
408+
return anyExprType({type: 'sql native', rawType: blueprint.sql_native});
399409
} else {
400410
return minAnalytic(
401411
expandTypeDescElementBlueprint(blueprint.calculation, false)
@@ -415,7 +425,8 @@ function isTypeDescBlueprint(
415425
'constant' in blueprint ||
416426
'dimension' in blueprint ||
417427
'measure' in blueprint ||
418-
'calculation' in blueprint
428+
'calculation' in blueprint ||
429+
'sql_native' in blueprint
419430
);
420431
}
421432

@@ -456,6 +467,8 @@ function expandParamTypeBlueprint(
456467
return anyExprType(expandTypeDescElementBlueprint(blueprint, false));
457468
} else if ('record' in blueprint) {
458469
return anyExprType(expandTypeDescElementBlueprint(blueprint, false));
470+
} else if ('sql_native' in blueprint) {
471+
return anyExprType({type: 'sql native', rawType: blueprint.sql_native});
459472
} else {
460473
return maxAnalytic(expandTypeDescElementBlueprint(blueprint.calculation));
461474
}

test/src/databases/all/functions.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,9 @@ expressionModels.forEach((x, databaseName) => {
13221322
it.when(isDuckdb)('list_extract', async () => {
13231323
await funcTest('list_extract(list_extract([[5]], 1), 1)', 5);
13241324
});
1325+
it.when(isDuckdb)('date_part,to_seconds', async () => {
1326+
await funcTest('date_part("seconds", to_seconds(5))', 5);
1327+
});
13251328
});
13261329

13271330
describe('trino', () => {

0 commit comments

Comments
 (0)