From 070c74fba63d846e139c029470cc694b8e104e17 Mon Sep 17 00:00:00 2001 From: Pelotfr Date: Wed, 1 Nov 2023 15:50:38 +0100 Subject: [PATCH] Added more aggregate functions and columns --- src/fetch/openapi.ts | 2 +- src/queries.spec.ts | 12 ++++++++---- src/queries.ts | 20 +++++++++++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/fetch/openapi.ts b/src/fetch/openapi.ts index e8f4950..ce2c760 100644 --- a/src/fetch/openapi.ts +++ b/src/fetch/openapi.ts @@ -173,7 +173,7 @@ export default new OpenApiBuilder() in: "query", description: "Aggregate column", required: false, - schema: {enum: ['listing_price_amount'] }, + schema: {enum: ['listing_price_amount', 'sale_id', 'asset_ids', 'listing_price_value'] }, }, { name: "collection_name", diff --git a/src/queries.spec.ts b/src/queries.spec.ts index 2eac5e0..f09d760 100644 --- a/src/queries.spec.ts +++ b/src/queries.spec.ts @@ -4,8 +4,6 @@ import { expect, test } from "bun:test"; import { getSale, getAggregate } from "./queries.js"; const collection_name = "pomelo"; -const aggregate_function = "min"; -const aggregate_column = "listing_price_amount"; test("getSale", () => { expect(getSale(new URLSearchParams({collection_name}))) @@ -22,6 +20,12 @@ JOIN blocks ON blocks.block_id = s.block_id WHERE (collection_name == '${collect }); test("getAggregate", () => { - expect(getAggregate(new URLSearchParams({aggregate_function, aggregate_column}))) - .toBe(`SELECT ${aggregate_function}(${aggregate_column}) FROM Sales`); + expect(getAggregate(new URLSearchParams({aggregate_function: 'count', collection_name}))) + .toBe(`SELECT count() FROM Sales WHERE (collection_name == '${collection_name}')`); + + expect(getAggregate(new URLSearchParams({aggregate_function: 'count', aggregate_column: 'sale_id'}))) + .toBe(`SELECT count(sale_id) FROM Sales`); + + expect(getAggregate(new URLSearchParams({aggregate_function: 'max', aggregate_column: 'listing_price_amount'}))) + .toBe(`SELECT max(listing_price_amount) FROM Sales`); }); \ No newline at end of file diff --git a/src/queries.ts b/src/queries.ts index fa4ec35..f53d58c 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -81,14 +81,28 @@ export function getAggregate(searchParams: URLSearchParams) { let query = `SELECT`; // Aggregate Function + const valid_agg_functions = ["min", "max", "avg", "sum", "count", "median"]; const aggregate_function = searchParams.get("aggregate_function"); + if(!aggregate_function) throw new Error("Aggregate function is required"); + if (aggregate_function && !valid_agg_functions.includes(aggregate_function)) throw new Error("Aggregate function not supported"); + + // Aggregate Column + const valid_agg_columns = ["sale_id", "asset_ids", "listing_price_amount", "listing_price_value"]; const aggregate_column = searchParams.get("aggregate_column"); - if (aggregate_function == "count" && !aggregate_column) query += ` count()`; - else if (aggregate_function && aggregate_column) query += ` ${aggregate_function}(${aggregate_column})`; - else throw new Error("Invalid aggregate function or column"); + if (aggregate_column && !valid_agg_columns.includes(aggregate_column)) throw new Error("Aggregate column not supported"); + if (aggregate_function == "count") { + if (aggregate_column) query += ` count(${aggregate_column})`; + else query += ` count()`; + } + else if (aggregate_column != "sale_id" && aggregate_column != "asset_ids") query += ` ${aggregate_function}(${aggregate_column})` + else throw new Error("Invalid aggregate column with given aggregate function"); query += ` FROM Sales`; + + // JOIN block table + query += ` JOIN blocks ON blocks.block_id = Sales.block_id`; + const where = []; // Clickhouse Operators // https://clickhouse.com/docs/en/sql-reference/operators