Browse Source

Merge pull request #8900 from nocodb/nc-fix/rollup-fns

fix: rollup functions
pull/8385/merge
Pranav C 6 months ago committed by GitHub
parent
commit
13c14b53e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      packages/nc-gui/components/virtual-cell/Rollup.vue
  2. 43
      packages/nocodb-sdk/src/lib/helperFunctions.ts
  3. 77
      packages/nocodb/src/db/genRollupSelectv2.ts
  4. 1
      packages/nocodb/src/models/RollupColumn.ts
  5. 1
      packages/nocodb/src/schema/swagger-v2.json
  6. 1
      packages/nocodb/src/schema/swagger.json

6
packages/nc-gui/components/virtual-cell/Rollup.vue

@ -41,7 +41,11 @@ const childColumn = computed(() => {
<template>
<div class="nc-cell-field" @dblclick="activateShowEditNonEditableFieldWarning">
<div v-if="['count', 'avg', 'sum', 'countDistinct', 'sumDistinct', 'avgDistinct'].includes(colOptions.rollup_function)">
<div
v-if="
['count', 'avg', 'sum', 'min', 'max', 'countDistinct', 'sumDistinct', 'avgDistinct'].includes(colOptions.rollup_function)
"
>
{{ value }}
</div>
<LazySmartsheetCell v-else v-model="value" :column="childColumn" :edit-enabled="false" :read-only="true" />

43
packages/nocodb-sdk/src/lib/helperFunctions.ts

@ -59,7 +59,11 @@ const stringifyRolesObj = (roles?: RolesObj | null): string => {
};
const getAvailableRollupForUiType = (type: string) => {
if ([UITypes.Year].includes(type as UITypes)) {
return ['count', 'min', 'max', 'countDistinct'];
}
if (isNumericCol(type as UITypes)) {
// Number, Currency, Percent, Duration, Rating, Decimal
return [
'sum',
'count',
@ -70,7 +74,8 @@ const getAvailableRollupForUiType = (type: string) => {
'sumDistinct',
'avgDistinct',
];
} else if (
}
if (
[
UITypes.Date,
UITypes.DateTime,
@ -79,7 +84,8 @@ const getAvailableRollupForUiType = (type: string) => {
].includes(type as UITypes)
) {
return ['count', 'min', 'max', 'countDistinct'];
} else if (
}
if (
[
UITypes.SingleLineText,
UITypes.LongText,
@ -87,25 +93,30 @@ const getAvailableRollupForUiType = (type: string) => {
UITypes.Email,
UITypes.PhoneNumber,
UITypes.URL,
UITypes.Checkbox,
UITypes.JSON,
].includes(type as UITypes)
) {
return ['count'];
} else if ([UITypes.Attachment].includes(type as UITypes)) {
return ['count', 'countDistinct'];
}
if ([UITypes.Checkbox].includes(type as UITypes)) {
return ['count', 'sum'];
}
if ([UITypes.Attachment].includes(type as UITypes)) {
return [];
} else {
return [
'sum',
'count',
'min',
'max',
'avg',
'countDistinct',
'sumDistinct',
'avgDistinct',
];
}
if ([UITypes.SingleSelect, UITypes.MultiSelect].includes(type as UITypes)) {
return ['count', 'countDistinct'];
}
return [
'sum',
'count',
'min',
'max',
'avg',
'countDistinct',
'sumDistinct',
'avgDistinct',
];
};
const getFileName = ({ name, count, ext }) =>

77
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(
`${alias || parentBaseModel.getTnPath(parentModel.table_name)}.${
parentCol.column_name
}`,
),
'=',
knex.ref(`${refTableAlias}.${childCol.column_name}`),
);
).where(
knex.ref(
`${alias || parentBaseModel.getTnPath(parentModel.table_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(
`${alias || parentBaseModel.getTnPath(parentModel.table_name)}.${
parentCol.column_name
}`,
),
'=',
knex.ref(`${refTableAlias}.${childCol.column_name}`),
);
).where(
knex.ref(
`${alias || parentBaseModel.getTnPath(parentModel.table_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,
};

1
packages/nocodb/src/models/RollupColumn.ts

@ -15,6 +15,7 @@ export const ROLLUP_FUNCTIONS = <const>[
'countDistinct',
'sumDistinct',
'avgDistinct',
'sum',
];
export default class RollupColumn implements RollupType {

1
packages/nocodb/src/schema/swagger-v2.json

@ -17156,6 +17156,7 @@
"min",
"max",
"avg",
"sum",
"countDistinct",
"sumDistinct",
"avgDistinct"

1
packages/nocodb/src/schema/swagger.json

@ -23461,6 +23461,7 @@
"min",
"max",
"avg",
"sum",
"countDistinct",
"sumDistinct",
"avgDistinct"

Loading…
Cancel
Save