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: {
args: {
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:
@ -1767,11 +1777,20 @@ export async function validateFormulaAndExtractTreeWithType({
res.dataType = FormulaDataTypes.STRING;
}
} else if (parsedTree.type === JSEPNode.UNARY_EXP) {
throw new FormulaError(
FormulaErrorType.NOT_SUPPORTED,
{},
'Unary expression is not supported'
);
// only support -ve values
if (
['-'].includes(parsedTree.operator) &&
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) {
res.left = await validateAndExtract(parsedTree.left);
res.right = await validateAndExtract(parsedTree.right);

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

@ -1027,11 +1027,22 @@ async function _formulaQueryBuilder(
}
return { builder: query };
} else if (pt.type === 'UnaryExpression') {
const query = knex.raw(
`${pt.operator}${(
await fn(pt.argument, null, pt.operator)
).builder.toQuery()}${colAlias}`,
);
let query;
if (
(pt.operator === '-' || pt.operator === '+') &&
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) {
query.wrap('(', ')');
}

Loading…
Cancel
Save