From 86702eda64383ff55fc43498eb2abb6ca0c8e9be Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 22 Nov 2023 07:17:56 +0000 Subject: [PATCH] feat: formula - add rounddown and roundup support --- packages/nc-gui/utils/formulaUtils.ts | 26 +++++++++++++++- packages/nocodb/src/db/functionMappings/pg.ts | 30 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/packages/nc-gui/utils/formulaUtils.ts b/packages/nc-gui/utils/formulaUtils.ts index 391af7d641..1aa158d64a 100644 --- a/packages/nc-gui/utils/formulaUtils.ts +++ b/packages/nc-gui/utils/formulaUtils.ts @@ -532,7 +532,7 @@ const formulas: Record = { COUNTA: { validation: { args: { - min:1, + min: 1, }, }, description: '', @@ -559,6 +559,30 @@ const formulas: Record = { syntax: 'COUNTALL()', examples: ['COUNTALL()'], }, + ROUNDDOWN: { + type: formulaTypes.NUMERIC, + validation: { + args: { + min: 1, + max: 2, + }, + }, + description: '', + syntax: 'ROUNDDOWN()', + examples: ['ROUNDDOWN()'], + }, + ROUNDUP: { + type: formulaTypes.NUMERIC, + validation: { + args: { + min: 1, + max: 2, + }, + }, + description: '', + syntax: 'ROUNDUP()', + examples: ['ROUNDUP()'], + }, } const formulaList = Object.keys(formulas) diff --git a/packages/nocodb/src/db/functionMappings/pg.ts b/packages/nocodb/src/db/functionMappings/pg.ts index 594ba775d3..de506c6811 100644 --- a/packages/nocodb/src/db/functionMappings/pg.ts +++ b/packages/nocodb/src/db/functionMappings/pg.ts @@ -253,6 +253,36 @@ const pg = { ), }; }, + + // todo: move to common and use existing functions + ROUNDDOWN: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { + const { builder: valueBuilder } = await fn(pt.arguments[0]); + let precisionBuilder = knex.raw('0'); + if (pt.arguments[1]) { + const { builder } = await fn(pt.arguments[1]); + precisionBuilder = builder; + } + + return { + builder: knex.raw( + `ROUND(FLOOR(${valueBuilder} * POWER(10, ${precisionBuilder})) / POWER(10, ${precisionBuilder}))${colAlias}`, + ), + }; + }, + ROUNDUP: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { + const { builder: valueBuilder } = await fn(pt.arguments[0]); + let precisionBuilder = knex.raw('0'); + if (pt.arguments[1]) { + const { builder } = await fn(pt.arguments[1]); + precisionBuilder = builder; + } + + return { + builder: knex.raw( + `ROUND(CEIL(${valueBuilder} * POWER(10, ${precisionBuilder})) / POWER(10, ${precisionBuilder}))${colAlias}`, + ), + }; + }, }; export default pg;