Skip to content

Commit

Permalink
Add GREATEST and LEAST functions (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
Igggr authored Jul 31, 2024
1 parent d7cb00c commit d08fc7d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { stringFunctions } from './string';
import { dateFunctions } from './date';
import { systemFunctions } from './system';
import { sequenceFunctions } from './sequence-fns';
import { numberFunctions } from './numbers';


export const allFunctions = [
...stringFunctions
, ... dateFunctions
, ... systemFunctions
, ... sequenceFunctions
, ... numberFunctions
]
18 changes: 18 additions & 0 deletions src/functions/numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DataType, FunctionDefinition } from "../interfaces";

export const numberFunctions: FunctionDefinition[] = [
{
name: 'greatest',
args: [DataType.integer],
argsVariadic: DataType.integer,
returns: DataType.integer,
implementation: (...args: number[]) => Math.max(...args),
},
{
name: 'least',
args: [DataType.integer],
argsVariadic: DataType.integer,
returns: DataType.integer,
implementation: (...args: number[]) => Math.min(...args),
},
]
20 changes: 20 additions & 0 deletions src/tests/function-calls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ describe('Functions', () => {
.to.deep.equal([{ concat: 'text-123-end' }]);
});

it('GREATEST 2 arguments', () => {
expect(many(`select GREATEST(0, -1);`))
.to.deep.equal([{ greatest: 0 }]);
});

it('GREATEST 4 arguments', () => {
expect(many(`select GREATEST(3, 8, 10, 4);`))
.to.deep.equal([{ greatest: 10 }]);
});

it('LEAST 2 arguments', () => {
expect(many(`select LEAST(0, -1);`))
.to.deep.equal([{ least: -1 }]);
});

it('LEAST 4 arguments', () => {
expect(many(`select LEAST(3, 8, 1, 4);`))
.to.deep.equal([{ least: 1 }]);
});

it('can declare & call function', () => {
db.registerLanguage('mylang', ({ code, args, returns }) => {
expect(code).to.equal('some code');
Expand Down

0 comments on commit d08fc7d

Please sign in to comment.