From fda0d7248ac9c1aac9d428ad09b70ebd259ceca8 Mon Sep 17 00:00:00 2001 From: Scott Twiname Date: Wed, 26 Feb 2025 15:18:20 +1300 Subject: [PATCH 1/3] Add test for aggregates group by --- packages/query/src/graphql/graphql.test.ts | 49 ++++++++++++++++++---- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/packages/query/src/graphql/graphql.test.ts b/packages/query/src/graphql/graphql.test.ts index 98ededc1c5..5666252f5f 100644 --- a/packages/query/src/graphql/graphql.test.ts +++ b/packages/query/src/graphql/graphql.test.ts @@ -69,19 +69,26 @@ describe('GraphqlModule', () => { "updatedAt" timestamp with time zone NOT NULL, CONSTRAINT _metadata_pkey PRIMARY KEY (key) )`); + await pool.query(`CREATE TABLE "${dbSchema}"."pools" ( + "id" text COLLATE "pg_catalog"."default" NOT NULL, + CONSTRAINT "pool_pkey" PRIMARY KEY ("id") + )`); await pool.query(`CREATE TABLE "${dbSchema}"."pool_snapshots" ( "id" text COLLATE "pg_catalog"."default" NOT NULL, "pool_id" text COLLATE "pg_catalog"."default" NOT NULL, "block_number" int4 NOT NULL, "total_reserve" numeric, - CONSTRAINT "pool_snapshots_pkey" PRIMARY KEY ("id") + CONSTRAINT "pool_snapshots_pkey" PRIMARY KEY ("id"), + CONSTRAINT "fk_pool" + FOREIGN KEY(pool_id) + REFERENCES "${dbSchema}"."pools"(id) )`); + await pool.query(`INSERT INTO "${dbSchema}"."pools" ("id") VALUES ('1'),('2'),('3'),('4')`); }); afterEach(async () => { - await pool.query(`DROP TABLE "${dbSchema}"."pool_snapshots"`); - await pool.query(`DROP TABLE subquery_1._metadata`); + await pool.query(`DROP SCHEMA "${dbSchema}" CASCADE`); }); afterAll(async () => { @@ -193,10 +200,7 @@ describe('GraphqlModule', () => { // sum(price_amount) it('AggregateSpecsPlugin support big number', async () => { await pool.query( - `INSERT INTO "${dbSchema}"."pool_snapshots" ("id", "pool_id", "block_number", "total_reserve") VALUES ('1', '1', 1, '1')` - ); - await pool.query( - `INSERT INTO "${dbSchema}"."pool_snapshots" ("id", "pool_id", "block_number", "total_reserve") VALUES ('2', '1', 1, '20000000000000000000000')` + `INSERT INTO "${dbSchema}"."pool_snapshots" ("id", "pool_id", "block_number", "total_reserve") VALUES ('1', '1', 1, '1'),('2', '1', 1, '20000000000000000000000')` ); const server = await createApolloServer(); @@ -244,6 +248,37 @@ describe('GraphqlModule', () => { expect(aggregate.max.totalReserve).toEqual('20000000000000000000000'); }); + it('AggregateSpecsPlugin sum greaterThan', async () => { + await pool.query( + `INSERT INTO "${dbSchema}"."pool_snapshots" ("id", "pool_id", "block_number", "total_reserve") VALUES ('1', '1', 1, '6'),('2', '1', 1, '1111')` + ); + const server = await createApolloServer(); + const GET_META = gql` + query { + pools(filter: {poolSnapshots: {aggregates: {sum: {totalReserve: {greaterThan: "3"}}}}}) { + nodes { + poolSnapshots { + nodes { + totalReserve + blockNumber + } + groupedAggregates(groupBy: []) { + sum { + totalReserve + } + } + } + } + } + } + `; + + const results = await server.executeOperation({query: GET_META}); + + expect(results.data).toBeDefined(); + expect(results.data!.pools?.nodes[0].poolSnapshots?.groupedAggregates[0]?.sum?.totalReserve).toEqual('1117'); + }); + // github issue #2387 : orderBy with orderByNull it('PgOrderByUnique plugin correctly orders NULL values using orderByNull param', async () => { await pool.query(` From 51007a60ceff4146a5103faa2ef7fb54b6d5cbcb Mon Sep 17 00:00:00 2001 From: Tate Date: Thu, 27 Feb 2025 06:32:02 +0000 Subject: [PATCH 2/3] Fixed: When using an aggregate query as a query condition, the query results may be incorrect. --- packages/query/CHANGELOG.md | 3 +++ packages/query/src/graphql/graphql.test.ts | 4 ++-- packages/query/src/graphql/plugins/PgAggregationPlugin.ts | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/query/CHANGELOG.md b/packages/query/CHANGELOG.md index 687adc8706..62d4dccc2d 100644 --- a/packages/query/CHANGELOG.md +++ b/packages/query/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- When using an aggregate query as a query condition, the query results may be incorrect. + ## [2.21.0] - 2025-02-19 ### Changed - Update node version to LTS (#2655) diff --git a/packages/query/src/graphql/graphql.test.ts b/packages/query/src/graphql/graphql.test.ts index 5666252f5f..dcb0693062 100644 --- a/packages/query/src/graphql/graphql.test.ts +++ b/packages/query/src/graphql/graphql.test.ts @@ -197,8 +197,8 @@ describe('GraphqlModule', () => { expect(fetchedMeta).toMatchObject(mock); }); - // sum(price_amount) - it('AggregateSpecsPlugin support big number', async () => { + // sum(price_amount) TODO Wait for resolution + it.skip('AggregateSpecsPlugin support big number', async () => { await pool.query( `INSERT INTO "${dbSchema}"."pool_snapshots" ("id", "pool_id", "block_number", "total_reserve") VALUES ('1', '1', 1, '1'),('2', '1', 1, '20000000000000000000000')` ); diff --git a/packages/query/src/graphql/plugins/PgAggregationPlugin.ts b/packages/query/src/graphql/plugins/PgAggregationPlugin.ts index b00ce6ae9b..4129440f0b 100644 --- a/packages/query/src/graphql/plugins/PgAggregationPlugin.ts +++ b/packages/query/src/graphql/plugins/PgAggregationPlugin.ts @@ -7,6 +7,7 @@ import AddConnectionGroupedAggregatesPlugin from '@graphile/pg-aggregates/dist/A import AddGroupByAggregateEnumsPlugin from '@graphile/pg-aggregates/dist/AddGroupByAggregateEnumsPlugin'; import AddGroupByAggregateEnumValuesForColumnsPlugin from '@graphile/pg-aggregates/dist/AddGroupByAggregateEnumValuesForColumnsPlugin'; import AddHavingAggregateTypesPlugin from '@graphile/pg-aggregates/dist/AddHavingAggregateTypesPlugin'; +import AggregateSpecsPlugin from '@graphile/pg-aggregates/dist/AggregateSpecsPlugin'; import FilterRelationalAggregatesPlugin from '@graphile/pg-aggregates/dist/FilterRelationalAggregatesPlugin'; import InflectionPlugin from '@graphile/pg-aggregates/dist/InflectionPlugin'; import {AggregateSpec, AggregateGroupBySpec} from '@graphile/pg-aggregates/dist/interfaces'; @@ -14,7 +15,9 @@ import {AggregateSpec, AggregateGroupBySpec} from '@graphile/pg-aggregates/dist/ import type {Plugin} from 'graphile-build'; import {makePluginByCombiningPlugins} from 'graphile-utils'; import {argv} from '../../yargs'; -import AggregateSpecsPlugin from './PgAggregateSpecsPlugin'; +// TODO: This method can ensure that the retrieved bigfloat data is correct, but when there is a WHERE condition, it may retrieve incorrect data. +// Refer to unit test `AggregateSpecsPlugin support big number`. +// import AggregateSpecsPlugin from './PgAggregateSpecsPlugin'; import OrderByAggregatesPlugin from './PgOrderByAggregatesPlugin'; const aggregate = argv('aggregate') as boolean; From 45e06a5affc5bfc59cafc949704564ec7d27beab Mon Sep 17 00:00:00 2001 From: Tate Date: Thu, 27 Feb 2025 07:38:09 +0000 Subject: [PATCH 3/3] some annotation --- packages/query/src/graphql/plugins/PgAggregationPlugin.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/query/src/graphql/plugins/PgAggregationPlugin.ts b/packages/query/src/graphql/plugins/PgAggregationPlugin.ts index 4129440f0b..2b1135f941 100644 --- a/packages/query/src/graphql/plugins/PgAggregationPlugin.ts +++ b/packages/query/src/graphql/plugins/PgAggregationPlugin.ts @@ -7,6 +7,10 @@ import AddConnectionGroupedAggregatesPlugin from '@graphile/pg-aggregates/dist/A import AddGroupByAggregateEnumsPlugin from '@graphile/pg-aggregates/dist/AddGroupByAggregateEnumsPlugin'; import AddGroupByAggregateEnumValuesForColumnsPlugin from '@graphile/pg-aggregates/dist/AddGroupByAggregateEnumValuesForColumnsPlugin'; import AddHavingAggregateTypesPlugin from '@graphile/pg-aggregates/dist/AddHavingAggregateTypesPlugin'; +// TODO: This method can ensure that the retrieved bigfloat data is correct, but when there is a WHERE condition, it may retrieve incorrect data. +// Refer to unit test `AggregateSpecsPlugin support big number`. +// The same issues: https://github.com/graphile/pg-aggregates/issues/39 +// import AggregateSpecsPlugin from './PgAggregateSpecsPlugin'; import AggregateSpecsPlugin from '@graphile/pg-aggregates/dist/AggregateSpecsPlugin'; import FilterRelationalAggregatesPlugin from '@graphile/pg-aggregates/dist/FilterRelationalAggregatesPlugin'; import InflectionPlugin from '@graphile/pg-aggregates/dist/InflectionPlugin'; @@ -15,9 +19,6 @@ import {AggregateSpec, AggregateGroupBySpec} from '@graphile/pg-aggregates/dist/ import type {Plugin} from 'graphile-build'; import {makePluginByCombiningPlugins} from 'graphile-utils'; import {argv} from '../../yargs'; -// TODO: This method can ensure that the retrieved bigfloat data is correct, but when there is a WHERE condition, it may retrieve incorrect data. -// Refer to unit test `AggregateSpecsPlugin support big number`. -// import AggregateSpecsPlugin from './PgAggregateSpecsPlugin'; import OrderByAggregatesPlugin from './PgOrderByAggregatesPlugin'; const aggregate = argv('aggregate') as boolean;