Browse Source

Merge pull request #7288 from nocodb/nc-fix/formula-fixes

Nc fix/formula fixes
pull/7293/head
Raju Udava 11 months ago committed by GitHub
parent
commit
c95ad10fd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      packages/nocodb-sdk/src/lib/formulaHelpers.ts
  2. 21
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

33
packages/nocodb-sdk/src/lib/formulaHelpers.ts

@ -712,8 +712,18 @@ export const formulas: Record<string, FormulaMeta> = {
validation: { validation: {
args: { args: {
rqd: 2, rqd: 2,
},
type: FormulaDataTypes.STRING, custom(argTypes: FormulaDataTypes[], parsedTree) {
if (argTypes[1] !== FormulaDataTypes.NUMERIC) {
throw new FormulaError(
FormulaErrorType.INVALID_ARG,
{
key: 'msg.formula.numericTypeIsExpected',
calleeName: parsedTree.callee?.name?.toUpperCase(),
},
'Numeric type is expected'
);
}
}, },
}, },
description: description:
@ -1767,11 +1777,20 @@ export async function validateFormulaAndExtractTreeWithType({
res.dataType = FormulaDataTypes.STRING; res.dataType = FormulaDataTypes.STRING;
} }
} else if (parsedTree.type === JSEPNode.UNARY_EXP) { } else if (parsedTree.type === JSEPNode.UNARY_EXP) {
throw new FormulaError( // only support -ve values
FormulaErrorType.NOT_SUPPORTED, if (
{}, ['-'].includes(parsedTree.operator) &&
'Unary expression is not supported' parsedTree.argument.type === JSEPNode.LITERAL &&
); typeof parsedTree.argument.value === 'number'
) {
res.dataType = FormulaDataTypes.NUMERIC;
} else {
throw new FormulaError(
FormulaErrorType.NOT_SUPPORTED,
{},
`Unary expression '${parsedTree.operator}' is not supported`
);
}
} else if (parsedTree.type === JSEPNode.BINARY_EXP) { } else if (parsedTree.type === JSEPNode.BINARY_EXP) {
res.left = await validateAndExtract(parsedTree.left); res.left = await validateAndExtract(parsedTree.left);
res.right = await validateAndExtract(parsedTree.right); res.right = await validateAndExtract(parsedTree.right);

21
packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

@ -1027,11 +1027,22 @@ async function _formulaQueryBuilder(
} }
return { builder: query }; return { builder: query };
} else if (pt.type === 'UnaryExpression') { } else if (pt.type === 'UnaryExpression') {
const query = knex.raw( let query;
`${pt.operator}${( if (
await fn(pt.argument, null, pt.operator) (pt.operator === '-' || pt.operator === '+') &&
).builder.toQuery()}${colAlias}`, pt.dataType === FormulaDataTypes.NUMERIC
); ) {
query = knex.raw('?', [
(pt.operator === '-' ? -1 : 1) * pt.argument.value,
]);
} else {
query = knex.raw(
`${pt.operator}${(
await fn(pt.argument, null, pt.operator)
).builder.toQuery()}${colAlias}`,
);
}
if (prevBinaryOp && pt.operator !== prevBinaryOp) { if (prevBinaryOp && pt.operator !== prevBinaryOp) {
query.wrap('(', ')'); query.wrap('(', ')');
} }

Loading…
Cancel
Save