Skip to content

Commit

Permalink
Added more aggregate functions and columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelotfr committed Nov 1, 2023
1 parent 48d1f7f commit 070c74f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/fetch/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 8 additions & 4 deletions src/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})))
Expand All @@ -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})))

Check failure on line 23 in src/queries.spec.ts

View workflow job for this annotation

GitHub Actions / build-and-test

error: expect(received).toBe(expected)

Expected: "SELECT count() FROM Sales WHERE (collection_name == 'pomelo')" Received: "SELECT count() FROM Sales JOIN blocks ON blocks.block_id = Sales.block_id WHERE (collection_name == 'pomelo')" at /home/runner/work/substreams-atomicmarket-api/substreams-atomicmarket-api/src/queries.spec.ts:23:4
.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`);
});
20 changes: 17 additions & 3 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 070c74f

Please sign in to comment.