From b77bbbb9582c87f585f703f437b140e0a2e713aa Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 15 Jul 2024 15:25:29 +0000 Subject: [PATCH] fix: cast boolean to integer when using rollup on bool type in pg (#3378) --- packages/nocodb/src/db/genRollupSelectv2.ts | 73 ++++++++++++--------- packages/nocodb/src/models/RollupColumn.ts | 1 + packages/nocodb/src/schema/swagger-v2.json | 1 + packages/nocodb/src/schema/swagger.json | 1 + 4 files changed, 46 insertions(+), 30 deletions(-) diff --git a/packages/nocodb/src/db/genRollupSelectv2.ts b/packages/nocodb/src/db/genRollupSelectv2.ts index cba41408be..f6d3828782 100644 --- a/packages/nocodb/src/db/genRollupSelectv2.ts +++ b/packages/nocodb/src/db/genRollupSelectv2.ts @@ -43,6 +43,26 @@ export default async function ({ dbDriver: knex, }); + const applyFunction = (qb: any) => { + // if postgres and rollup function is sum/sumDistinct/avgDistinct/avg, then cast the column to integer when type is boolean + if ( + baseModelSqlv2.isPg && + ['sum', 'sumDistinct', 'avgDistinct', 'avg'].includes( + columnOptions.rollup_function, + ) && + ['bool', 'boolean'].includes(rollupColumn.dt) + ) { + qb[columnOptions.rollup_function as string]?.( + knex.raw('??.??::integer', [refTableAlias, rollupColumn.column_name]), + ); + return; + } + + qb[columnOptions.rollup_function as string]?.( + knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), + ); + }; + switch (relationColumnOption.type) { case RelationTypes.HAS_MANY: { const queryBuilder: any = knex( @@ -50,19 +70,16 @@ export default async function ({ childBaseModel.getTnPath(childModel), refTableAlias, ]), - ) - [columnOptions.rollup_function as string]?.( - knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), - ) - .where( - knex.ref( + ).where( + knex.ref( `${alias || parentBaseModel.getTnPath(parentModel.table_name)}.${ - parentCol.column_name - }`, - ), - '=', - knex.ref(`${refTableAlias}.${childCol.column_name}`), - ); + parentCol.column_name + }`, + ), + '=', + knex.ref(`${refTableAlias}.${childCol.column_name}`), + ); + applyFunction(queryBuilder); return { builder: queryBuilder, @@ -75,20 +92,17 @@ export default async function ({ childBaseModel.getTnPath(childModel?.table_name), refTableAlias, ]), - ) - [columnOptions.rollup_function as string]?.( - knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), - ) - .where( - knex.ref( + ).where( + knex.ref( `${alias || parentBaseModel.getTnPath(parentModel.table_name)}.${ - parentCol.column_name - }`, - ), - '=', - knex.ref(`${refTableAlias}.${childCol.column_name}`), - ); + parentCol.column_name + }`, + ), + '=', + knex.ref(`${refTableAlias}.${childCol.column_name}`), + ); + applyFunction(qb); return { builder: qb, }; @@ -114,18 +128,15 @@ export default async function ({ refTableAlias, ]), ) - [columnOptions.rollup_function as string]?.( - knex.ref(`${refTableAlias}.${rollupColumn.column_name}`), - ) .innerJoin( - assocBaseModel.getTnPath(mmModel.table_name), + assocBaseModel.getTnPath(mmModel.table_name) as any, knex.ref( `${assocBaseModel.getTnPath(mmModel.table_name)}.${ mmParentCol.column_name }`, - ), + ) as any, '=', - knex.ref(`${refTableAlias}.${parentCol.column_name}`), + knex.ref(`${refTableAlias}.${parentCol.column_name}`) as any, ) .where( knex.ref( @@ -141,6 +152,8 @@ export default async function ({ ), ); + applyFunction(qb); + return { builder: qb, }; diff --git a/packages/nocodb/src/models/RollupColumn.ts b/packages/nocodb/src/models/RollupColumn.ts index 6978471256..43ca9ad2ec 100644 --- a/packages/nocodb/src/models/RollupColumn.ts +++ b/packages/nocodb/src/models/RollupColumn.ts @@ -15,6 +15,7 @@ export const ROLLUP_FUNCTIONS = [ 'countDistinct', 'sumDistinct', 'avgDistinct', + 'sum', ]; export default class RollupColumn implements RollupType { diff --git a/packages/nocodb/src/schema/swagger-v2.json b/packages/nocodb/src/schema/swagger-v2.json index fd640f102a..15c1ef73dd 100644 --- a/packages/nocodb/src/schema/swagger-v2.json +++ b/packages/nocodb/src/schema/swagger-v2.json @@ -17156,6 +17156,7 @@ "min", "max", "avg", + "sum", "countDistinct", "sumDistinct", "avgDistinct" diff --git a/packages/nocodb/src/schema/swagger.json b/packages/nocodb/src/schema/swagger.json index 5437020383..d8a95d91c1 100644 --- a/packages/nocodb/src/schema/swagger.json +++ b/packages/nocodb/src/schema/swagger.json @@ -23461,6 +23461,7 @@ "min", "max", "avg", + "sum", "countDistinct", "sumDistinct", "avgDistinct"