Implement group by and add a test for it.
This commit is contained in:
parent
987a24fabc
commit
ae4f7ae4b4
|
@ -799,6 +799,14 @@ class InternalBuilder {
|
||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isSqs(t?: Table): boolean {
|
||||||
|
const table = t || this.table
|
||||||
|
return (
|
||||||
|
table.sourceType === TableSourceType.INTERNAL ||
|
||||||
|
table.sourceId === INTERNAL_TABLE_SOURCE_ID
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
getTableName(t?: Table | string): string {
|
getTableName(t?: Table | string): string {
|
||||||
let table: Table
|
let table: Table
|
||||||
if (typeof t === "string") {
|
if (typeof t === "string") {
|
||||||
|
@ -813,11 +821,7 @@ class InternalBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = table.name
|
let name = table.name
|
||||||
if (
|
if (this.isSqs(table) && table._id) {
|
||||||
(table.sourceType === TableSourceType.INTERNAL ||
|
|
||||||
table.sourceId === INTERNAL_TABLE_SOURCE_ID) &&
|
|
||||||
table._id
|
|
||||||
) {
|
|
||||||
// SQS uses the table ID rather than the table name
|
// SQS uses the table ID rather than the table name
|
||||||
name = table._id
|
name = table._id
|
||||||
}
|
}
|
||||||
|
@ -830,7 +834,7 @@ class InternalBuilder {
|
||||||
throw new Error("SQL counting requires primary key to be supplied")
|
throw new Error("SQL counting requires primary key to be supplied")
|
||||||
}
|
}
|
||||||
return query.countDistinct(
|
return query.countDistinct(
|
||||||
`${this.getTableName(this.table)}.${this.table.primary[0]} as total`
|
`${this.getTableName()}.${this.table.primary[0]} as total`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -842,10 +846,11 @@ class InternalBuilder {
|
||||||
const tableName = this.getTableName()
|
const tableName = this.getTableName()
|
||||||
if (fields.length > 0) {
|
if (fields.length > 0) {
|
||||||
query = query.groupBy(fields.map(field => `${tableName}.${field}`))
|
query = query.groupBy(fields.map(field => `${tableName}.${field}`))
|
||||||
|
query = query.select(fields.map(field => `${tableName}.${field}`))
|
||||||
}
|
}
|
||||||
for (const aggregation of aggregations) {
|
for (const aggregation of aggregations) {
|
||||||
const op = aggregation.calculationType
|
const op = aggregation.calculationType
|
||||||
const field = `${this.table.name}.${aggregation.field} as ${aggregation.name}`
|
const field = `${tableName}.${aggregation.field} as ${aggregation.name}`
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case CalculationType.COUNT:
|
case CalculationType.COUNT:
|
||||||
query = query.count(field)
|
query = query.count(field)
|
||||||
|
|
|
@ -39,13 +39,13 @@ import {
|
||||||
} from "@budibase/backend-core"
|
} from "@budibase/backend-core"
|
||||||
|
|
||||||
describe.each([
|
describe.each([
|
||||||
// ["lucene", undefined],
|
["lucene", undefined],
|
||||||
// ["sqs", undefined],
|
["sqs", undefined],
|
||||||
[DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)],
|
[DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)],
|
||||||
// [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)],
|
[DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)],
|
||||||
// [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)],
|
[DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)],
|
||||||
// [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)],
|
[DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)],
|
||||||
// [DatabaseName.ORACLE, getDatasource(DatabaseName.ORACLE)],
|
[DatabaseName.ORACLE, getDatasource(DatabaseName.ORACLE)],
|
||||||
])("/v2/views (%s)", (name, dsProvider) => {
|
])("/v2/views (%s)", (name, dsProvider) => {
|
||||||
const config = setup.getConfig()
|
const config = setup.getConfig()
|
||||||
const isSqs = name === "sqs"
|
const isSqs = name === "sqs"
|
||||||
|
@ -2459,7 +2459,7 @@ describe.each([
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it.only("should be able to group by a basic field", async () => {
|
it("should be able to group by a basic field", async () => {
|
||||||
const view = await config.api.viewV2.create({
|
const view = await config.api.viewV2.create({
|
||||||
tableId: table._id!,
|
tableId: table._id!,
|
||||||
name: generator.guid(),
|
name: generator.guid(),
|
||||||
|
@ -2480,9 +2480,14 @@ describe.each([
|
||||||
query: {},
|
query: {},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const priceByQuantity: Record<number, number> = {}
|
||||||
|
for (const row of rows) {
|
||||||
|
priceByQuantity[row.quantity] ??= 0
|
||||||
|
priceByQuantity[row.quantity] += row.price
|
||||||
|
}
|
||||||
|
|
||||||
for (const row of response.rows) {
|
for (const row of response.rows) {
|
||||||
expect(row["quantity"]).toBeGreaterThan(0)
|
expect(row["Total Price"]).toEqual(priceByQuantity[row.quantity])
|
||||||
expect(row["Total Price"]).toBeGreaterThan(0)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue