Browse Source

fix: handle negative values properly

pull/7288/head
Pranav C 10 months ago
parent
commit
702051fced
  1. 10
      packages/nocodb-sdk/src/lib/formulaHelpers.ts
  2. 21
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

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

@ -1777,8 +1777,14 @@ export async function validateFormulaAndExtractTreeWithType({
res.dataType = FormulaDataTypes.STRING;
}
} else if (parsedTree.type === JSEPNode.UNARY_EXP) {
// only support unary +/-
if (!['-', '+'].includes(parsedTree.operator)) {
// 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,
{},

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