Merge pull request #14928 from Budibase/allow-fields-on-counts
Allow fields on count calculations.
This commit is contained in:
commit
e3b499885f
|
@ -1091,7 +1091,14 @@ class InternalBuilder {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
query = query.count(`* as ${aggregation.name}`)
|
if (this.client === SqlClient.ORACLE) {
|
||||||
|
const field = this.convertClobs(`${tableName}.${aggregation.field}`)
|
||||||
|
query = query.select(
|
||||||
|
this.knex.raw(`COUNT(??) as ??`, [field, aggregation.name])
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
query = query.count(`${aggregation.field} as ${aggregation.name}`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const fieldSchema = this.getFieldSchema(aggregation.field)
|
const fieldSchema = this.getFieldSchema(aggregation.field)
|
||||||
|
|
|
@ -12,6 +12,8 @@ import {
|
||||||
RelationSchemaField,
|
RelationSchemaField,
|
||||||
ViewFieldMetadata,
|
ViewFieldMetadata,
|
||||||
CalculationType,
|
CalculationType,
|
||||||
|
CountDistinctCalculationFieldMetadata,
|
||||||
|
CountCalculationFieldMetadata,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { builderSocket, gridSocket } from "../../../websockets"
|
import { builderSocket, gridSocket } from "../../../websockets"
|
||||||
import { helpers } from "@budibase/shared-core"
|
import { helpers } from "@budibase/shared-core"
|
||||||
|
@ -22,27 +24,31 @@ function stripUnknownFields(
|
||||||
if (helpers.views.isCalculationField(field)) {
|
if (helpers.views.isCalculationField(field)) {
|
||||||
if (field.calculationType === CalculationType.COUNT) {
|
if (field.calculationType === CalculationType.COUNT) {
|
||||||
if ("distinct" in field && field.distinct) {
|
if ("distinct" in field && field.distinct) {
|
||||||
return {
|
const strippedField: RequiredKeys<CountDistinctCalculationFieldMetadata> =
|
||||||
|
{
|
||||||
|
order: field.order,
|
||||||
|
width: field.width,
|
||||||
|
visible: field.visible,
|
||||||
|
readonly: field.readonly,
|
||||||
|
icon: field.icon,
|
||||||
|
distinct: field.distinct,
|
||||||
|
calculationType: field.calculationType,
|
||||||
|
field: field.field,
|
||||||
|
columns: field.columns,
|
||||||
|
}
|
||||||
|
return strippedField
|
||||||
|
} else {
|
||||||
|
const strippedField: RequiredKeys<CountCalculationFieldMetadata> = {
|
||||||
order: field.order,
|
order: field.order,
|
||||||
width: field.width,
|
width: field.width,
|
||||||
visible: field.visible,
|
visible: field.visible,
|
||||||
readonly: field.readonly,
|
readonly: field.readonly,
|
||||||
icon: field.icon,
|
icon: field.icon,
|
||||||
distinct: field.distinct,
|
|
||||||
calculationType: field.calculationType,
|
calculationType: field.calculationType,
|
||||||
field: field.field,
|
field: field.field,
|
||||||
columns: field.columns,
|
columns: field.columns,
|
||||||
}
|
}
|
||||||
} else {
|
return strippedField
|
||||||
return {
|
|
||||||
order: field.order,
|
|
||||||
width: field.width,
|
|
||||||
visible: field.visible,
|
|
||||||
readonly: field.readonly,
|
|
||||||
icon: field.icon,
|
|
||||||
calculationType: field.calculationType,
|
|
||||||
columns: field.columns,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const strippedField: RequiredKeys<ViewCalculationFieldMetadata> = {
|
const strippedField: RequiredKeys<ViewCalculationFieldMetadata> = {
|
||||||
|
|
|
@ -693,6 +693,12 @@ describe.each([
|
||||||
calculationType: CalculationType.COUNT,
|
calculationType: CalculationType.COUNT,
|
||||||
field: "Price",
|
field: "Price",
|
||||||
},
|
},
|
||||||
|
countDistinct: {
|
||||||
|
visible: true,
|
||||||
|
calculationType: CalculationType.COUNT,
|
||||||
|
distinct: true,
|
||||||
|
field: "Price",
|
||||||
|
},
|
||||||
min: {
|
min: {
|
||||||
visible: true,
|
visible: true,
|
||||||
calculationType: CalculationType.MIN,
|
calculationType: CalculationType.MIN,
|
||||||
|
@ -708,11 +714,6 @@ describe.each([
|
||||||
calculationType: CalculationType.AVG,
|
calculationType: CalculationType.AVG,
|
||||||
field: "Price",
|
field: "Price",
|
||||||
},
|
},
|
||||||
sum2: {
|
|
||||||
visible: true,
|
|
||||||
calculationType: CalculationType.SUM,
|
|
||||||
field: "Price",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -763,10 +764,12 @@ describe.each([
|
||||||
count: {
|
count: {
|
||||||
visible: true,
|
visible: true,
|
||||||
calculationType: CalculationType.COUNT,
|
calculationType: CalculationType.COUNT,
|
||||||
|
field: "Price",
|
||||||
},
|
},
|
||||||
count2: {
|
count2: {
|
||||||
visible: true,
|
visible: true,
|
||||||
calculationType: CalculationType.COUNT,
|
calculationType: CalculationType.COUNT,
|
||||||
|
field: "Price",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -774,7 +777,7 @@ describe.each([
|
||||||
status: 400,
|
status: 400,
|
||||||
body: {
|
body: {
|
||||||
message:
|
message:
|
||||||
'Duplicate calculation on field "*", calculation type "count"',
|
'Duplicate calculation on field "Price", calculation type "count"',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -805,7 +808,7 @@ describe.each([
|
||||||
status: 400,
|
status: 400,
|
||||||
body: {
|
body: {
|
||||||
message:
|
message:
|
||||||
'Duplicate calculation on field "Price", calculation type "count"',
|
'Duplicate calculation on field "Price", calculation type "count distinct"',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -820,6 +823,7 @@ describe.each([
|
||||||
count: {
|
count: {
|
||||||
visible: true,
|
visible: true,
|
||||||
calculationType: CalculationType.COUNT,
|
calculationType: CalculationType.COUNT,
|
||||||
|
field: "Price",
|
||||||
},
|
},
|
||||||
count2: {
|
count2: {
|
||||||
visible: true,
|
visible: true,
|
||||||
|
@ -831,6 +835,26 @@ describe.each([
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("does not confuse counts on different fields in the duplicate check", async () => {
|
||||||
|
await config.api.viewV2.create({
|
||||||
|
tableId: table._id!,
|
||||||
|
name: generator.guid(),
|
||||||
|
type: ViewV2Type.CALCULATION,
|
||||||
|
schema: {
|
||||||
|
count: {
|
||||||
|
visible: true,
|
||||||
|
calculationType: CalculationType.COUNT,
|
||||||
|
field: "Price",
|
||||||
|
},
|
||||||
|
count2: {
|
||||||
|
visible: true,
|
||||||
|
calculationType: CalculationType.COUNT,
|
||||||
|
field: "Category",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
!isLucene &&
|
!isLucene &&
|
||||||
it("does not get confused when a calculation field shadows a basic one", async () => {
|
it("does not get confused when a calculation field shadows a basic one", async () => {
|
||||||
const table = await config.api.table.save(
|
const table = await config.api.table.save(
|
||||||
|
@ -1607,6 +1631,7 @@ describe.each([
|
||||||
view.schema!.count = {
|
view.schema!.count = {
|
||||||
visible: true,
|
visible: true,
|
||||||
calculationType: CalculationType.COUNT,
|
calculationType: CalculationType.COUNT,
|
||||||
|
field: "age",
|
||||||
}
|
}
|
||||||
await config.api.viewV2.update(view)
|
await config.api.viewV2.update(view)
|
||||||
|
|
||||||
|
@ -3761,6 +3786,51 @@ describe.each([
|
||||||
expect(rows[0].sum).toEqual(55)
|
expect(rows[0].sum).toEqual(55)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should be able to count non-numeric fields", async () => {
|
||||||
|
const table = await config.api.table.save(
|
||||||
|
saveTableRequest({
|
||||||
|
schema: {
|
||||||
|
firstName: {
|
||||||
|
type: FieldType.STRING,
|
||||||
|
name: "firstName",
|
||||||
|
},
|
||||||
|
lastName: {
|
||||||
|
type: FieldType.STRING,
|
||||||
|
name: "lastName",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const view = await config.api.viewV2.create({
|
||||||
|
tableId: table._id!,
|
||||||
|
name: generator.guid(),
|
||||||
|
type: ViewV2Type.CALCULATION,
|
||||||
|
schema: {
|
||||||
|
count: {
|
||||||
|
visible: true,
|
||||||
|
calculationType: CalculationType.COUNT,
|
||||||
|
field: "firstName",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await config.api.row.bulkImport(table._id!, {
|
||||||
|
rows: [
|
||||||
|
{ firstName: "Jane", lastName: "Smith" },
|
||||||
|
{ firstName: "Jane", lastName: "Doe" },
|
||||||
|
{ firstName: "Alice", lastName: "Smith" },
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
const { rows } = await config.api.viewV2.search(view.id, {
|
||||||
|
query: {},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(rows).toHaveLength(1)
|
||||||
|
expect(rows[0].count).toEqual(3)
|
||||||
|
})
|
||||||
|
|
||||||
it("should be able to filter rows on the view itself", async () => {
|
it("should be able to filter rows on the view itself", async () => {
|
||||||
const table = await config.api.table.save(
|
const table = await config.api.table.save(
|
||||||
saveTableRequest({
|
saveTableRequest({
|
||||||
|
|
|
@ -351,6 +351,7 @@ export async function search(
|
||||||
aggregations.push({
|
aggregations.push({
|
||||||
name: key,
|
name: key,
|
||||||
calculationType: field.calculationType,
|
calculationType: field.calculationType,
|
||||||
|
field: mapToUserColumn(field.field),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -61,12 +61,68 @@ export function isView(view: any): view is ViewV2 {
|
||||||
return view.id && docIds.isViewId(view.id) && view.version === 2
|
return view.id && docIds.isViewId(view.id) && view.version === 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function guardDuplicateCalculationFields(view: Omit<ViewV2, "id" | "version">) {
|
||||||
|
const seen: Record<string, Record<CalculationType, boolean>> = {}
|
||||||
|
const calculationFields = helpers.views.calculationFields(view)
|
||||||
|
for (const name of Object.keys(calculationFields)) {
|
||||||
|
const schema = calculationFields[name]
|
||||||
|
const isCount = schema.calculationType === CalculationType.COUNT
|
||||||
|
const isDistinct = "distinct" in schema
|
||||||
|
|
||||||
|
if (isCount && isDistinct) {
|
||||||
|
// We do these separately.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (seen[schema.field]?.[schema.calculationType]) {
|
||||||
|
throw new HTTPError(
|
||||||
|
`Duplicate calculation on field "${schema.field}", calculation type "${schema.calculationType}"`,
|
||||||
|
400
|
||||||
|
)
|
||||||
|
}
|
||||||
|
seen[schema.field] ??= {} as Record<CalculationType, boolean>
|
||||||
|
seen[schema.field][schema.calculationType] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function guardDuplicateCountDistinctFields(
|
||||||
|
view: Omit<ViewV2, "id" | "version">
|
||||||
|
) {
|
||||||
|
const seen: Record<string, Record<CalculationType, boolean>> = {}
|
||||||
|
const calculationFields = helpers.views.calculationFields(view)
|
||||||
|
for (const name of Object.keys(calculationFields)) {
|
||||||
|
const schema = calculationFields[name]
|
||||||
|
const isCount = schema.calculationType === CalculationType.COUNT
|
||||||
|
if (!isCount) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
const isDistinct = "distinct" in schema
|
||||||
|
if (!isDistinct) {
|
||||||
|
// We do these separately.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (seen[schema.field]?.[schema.calculationType]) {
|
||||||
|
throw new HTTPError(
|
||||||
|
`Duplicate calculation on field "${schema.field}", calculation type "${schema.calculationType} distinct"`,
|
||||||
|
400
|
||||||
|
)
|
||||||
|
}
|
||||||
|
seen[schema.field] ??= {} as Record<CalculationType, boolean>
|
||||||
|
seen[schema.field][schema.calculationType] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function guardCalculationViewSchema(
|
async function guardCalculationViewSchema(
|
||||||
table: Table,
|
table: Table,
|
||||||
view: Omit<ViewV2, "id" | "version">
|
view: Omit<ViewV2, "id" | "version">
|
||||||
) {
|
) {
|
||||||
const calculationFields = helpers.views.calculationFields(view)
|
const calculationFields = helpers.views.calculationFields(view)
|
||||||
|
|
||||||
|
guardDuplicateCalculationFields(view)
|
||||||
|
guardDuplicateCountDistinctFields(view)
|
||||||
|
|
||||||
if (Object.keys(calculationFields).length > 5) {
|
if (Object.keys(calculationFields).length > 5) {
|
||||||
throw new HTTPError(
|
throw new HTTPError(
|
||||||
"Calculation views can only have a maximum of 5 fields",
|
"Calculation views can only have a maximum of 5 fields",
|
||||||
|
@ -74,29 +130,8 @@ async function guardCalculationViewSchema(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const seen: Record<string, Record<CalculationType, boolean>> = {}
|
|
||||||
|
|
||||||
for (const name of Object.keys(calculationFields)) {
|
for (const name of Object.keys(calculationFields)) {
|
||||||
const schema = calculationFields[name]
|
const schema = calculationFields[name]
|
||||||
const isCount = schema.calculationType === CalculationType.COUNT
|
|
||||||
const isDistinct = isCount && "distinct" in schema && schema.distinct
|
|
||||||
|
|
||||||
const field = isCount && !isDistinct ? "*" : schema.field
|
|
||||||
if (seen[field]?.[schema.calculationType]) {
|
|
||||||
throw new HTTPError(
|
|
||||||
`Duplicate calculation on field "${field}", calculation type "${schema.calculationType}"`,
|
|
||||||
400
|
|
||||||
)
|
|
||||||
}
|
|
||||||
seen[field] ??= {} as Record<CalculationType, boolean>
|
|
||||||
seen[field][schema.calculationType] = true
|
|
||||||
|
|
||||||
// Count fields that aren't distinct don't need to reference another field,
|
|
||||||
// so we don't validate it.
|
|
||||||
if (isCount && !isDistinct) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!schema.field) {
|
if (!schema.field) {
|
||||||
throw new HTTPError(
|
throw new HTTPError(
|
||||||
`Calculation field "${name}" is missing a "field" property`,
|
`Calculation field "${name}" is missing a "field" property`,
|
||||||
|
@ -112,6 +147,7 @@ async function guardCalculationViewSchema(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isCount = schema.calculationType === CalculationType.COUNT
|
||||||
if (!isCount && !isNumeric(targetSchema.type)) {
|
if (!isCount && !isNumeric(targetSchema.type)) {
|
||||||
throw new HTTPError(
|
throw new HTTPError(
|
||||||
`Calculation field "${name}" references field "${schema.field}" which is not a numeric field`,
|
`Calculation field "${name}" references field "${schema.field}" which is not a numeric field`,
|
||||||
|
|
|
@ -54,12 +54,12 @@ export interface NumericCalculationFieldMetadata
|
||||||
|
|
||||||
export interface CountCalculationFieldMetadata extends BasicViewFieldMetadata {
|
export interface CountCalculationFieldMetadata extends BasicViewFieldMetadata {
|
||||||
calculationType: CalculationType.COUNT
|
calculationType: CalculationType.COUNT
|
||||||
|
field: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CountDistinctCalculationFieldMetadata
|
export interface CountDistinctCalculationFieldMetadata
|
||||||
extends CountCalculationFieldMetadata {
|
extends CountCalculationFieldMetadata {
|
||||||
distinct: true
|
distinct: true
|
||||||
field: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ViewCalculationFieldMetadata =
|
export type ViewCalculationFieldMetadata =
|
||||||
|
|
|
@ -18,11 +18,11 @@ export interface NumericAggregation extends BaseAggregation {
|
||||||
|
|
||||||
export interface CountAggregation extends BaseAggregation {
|
export interface CountAggregation extends BaseAggregation {
|
||||||
calculationType: CalculationType.COUNT
|
calculationType: CalculationType.COUNT
|
||||||
|
field: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CountDistinctAggregation extends CountAggregation {
|
export interface CountDistinctAggregation extends CountAggregation {
|
||||||
distinct: true
|
distinct: true
|
||||||
field: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Aggregation =
|
export type Aggregation =
|
||||||
|
|
Loading…
Reference in New Issue