Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add distinct query plugin #1274

Merged
merged 5 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions packages/node-core/src/indexer/dictionary.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export type SpecVersionDictionary = {

const logger = getLogger('dictionary');

const distinctErrorEscaped = `Unknown argument \\"distinct\\"`;

function extractVar(name: string, cond: DictionaryQueryCondition): GqlVar {
let gqlType: string;
switch (typeof cond.value) {
Expand Down Expand Up @@ -96,7 +98,8 @@ function buildDictQueryFragment(
startBlock: number,
queryEndBlock: number,
conditions: DictionaryQueryCondition[][],
batchSize: number
batchSize: number,
useDistinct: boolean,
): [GqlVar[], GqlNode] {
const [gqlVars, filter] = extractVars(entity, conditions);

Expand All @@ -120,6 +123,11 @@ function buildDictQueryFragment(
first: batchSize.toString(),
},
};

if (useDistinct) {
node.args.distinct = ['BLOCK_HEIGHT'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we useDistinct, we no longer need to have

lessThan: `"${queryEndBlock}"`,

if the distinct blocks in between difference is large than queryEndBlock threshold 10000, this will become inefficient.

For example expected distinct return 1999, 8999, 28999... (total number of batch size blocks)
but due to query limit

    blockHeight: {
      greaterThanOrEqualTo: `1999`,
      lessThan: `11999`,
    },

we will only have 2 blocks in return

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to retain lessThan for the work that @bz888 has done. But thinking about that, the queryEndBlock could be set to the height when a next dataSource is introduced.

Maybe we should save this for another task?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's do it in another task

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return [gqlVars, node];
}

Expand All @@ -128,6 +136,7 @@ export class DictionaryService implements OnApplicationShutdown {
protected client: ApolloClient<NormalizedCacheObject>;
private isShutdown = false;
private mappedDictionaryQueryEntries: Map<number, DictionaryQueryEntry[]>;
private useDistinct = true;

constructor(
readonly dictionaryEndpoint: string,
Expand Down Expand Up @@ -197,6 +206,18 @@ export class DictionaryService implements OnApplicationShutdown {
batchBlocks,
};
} catch (err) {
// Check if the error is about distinct argument and disable distinct if so
if (JSON.stringify(err).includes(distinctErrorEscaped)) {
this.useDistinct = false;
logger.warn(`Dictionary doesn't support distinct query.`);
// Rerun the qeury now with distinct disabled
return this.getDictionary(
startBlock,
queryEndBlock,
batchSize,
conditions,
);
}
logger.warn(err, `failed to fetch dictionary result`);
return undefined;
}
Expand All @@ -223,7 +244,7 @@ export class DictionaryService implements OnApplicationShutdown {
},
];
for (const entity of Object.keys(mapped)) {
const [pVars, node] = buildDictQueryFragment(entity, startBlock, queryEndBlock, mapped[entity], batchSize);
const [pVars, node] = buildDictQueryFragment(entity, startBlock, queryEndBlock, mapped[entity], batchSize, this.useDistinct);
nodes.push(node);
vars.push(...pVars);
}
Expand Down
107 changes: 107 additions & 0 deletions packages/query/src/graphql/plugins/PgDistinctPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2022 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import {Build, Plugin} from 'graphile-build';
import {PgClass, QueryBuilder} from 'graphile-build-pg';
import type {GraphQLEnumType} from 'graphql';
import * as PgSql from 'pg-sql2';

type Extend = <T1, T2>(base: T1, extra: T2, hint?: string) => T1 & T2;

const getEnumName = (entityName: string): string => {
return `${entityName}_distinct_enum`;
};

export const PgDistinctPlugin: Plugin = (builder) => {
// Creates enums for each entity based on their fields
builder.hook(
'init',
(args, build) => {
const {
graphql: {GraphQLEnumType},
newWithHooks,
pgIntrospectionResultsByKind,
} = build;

pgIntrospectionResultsByKind.class.forEach((cls: PgClass) => {
if (!cls.isSelectable || build.pgOmit(cls, 'order')) return;
if (!cls.namespace) return;

const enumTypeName = getEnumName(cls.name);

const entityEnumValues: Record<string, {value: number}> = {};
cls?.attributes?.forEach((attr, index) => {
if (attr.name.indexOf('_') !== 0) {
entityEnumValues[attr.name.toUpperCase()] = {value: index};
}
});

newWithHooks(
GraphQLEnumType,
{
name: enumTypeName,
values: entityEnumValues,
},
{
__origin: `Adding connection "distinct" enum type for ${cls.name}.`,
pgIntrospection: cls,
},
true
);
});

return args;
},
['AddDistinctEnumsPlugin']
);

// Extends schema and modifies the query
builder.hook(
'GraphQLObjectType:fields:field:args',
(args, build, {addArgDataGenerator, scope: {pgFieldIntrospection}}) => {
const {
extend,
graphql: {GraphQLList},
pgSql: sql,
} = build as Build & {extend: Extend; pgSql: typeof PgSql};

const enumTypeName = getEnumName(pgFieldIntrospection?.name);
const enumType = build.getTypeByName(enumTypeName) as GraphQLEnumType;

if (!enumType) {
return args;
}

addArgDataGenerator(({distinct}) => ({
pgQuery: (queryBuilder: QueryBuilder) => {
distinct?.map((field: number) => {
const {name} = enumType.getValues()[field];
const fieldName = name.toLowerCase();
if (!pgFieldIntrospection?.attributes?.map((a) => a.name).includes(fieldName)) {
console.warn(`Distinct field ${fieldName} doesn't exist on entity ${pgFieldIntrospection?.name}`);

return;
}

const id = sql.fragment`${queryBuilder.getTableAlias()}.${sql.identifier(fieldName)}`;

// Dependent on https://github.com/graphile/graphile-engine/pull/805
(queryBuilder as any).distinctOn(id);
});
},
}));

return extend(
args,
{
distinct: {
description: 'Fields to be distinct',
defaultValue: null,
type: new GraphQLList(enumType),
},
},
'DistinctPlugin'
);
}
);
};
2 changes: 2 additions & 0 deletions packages/query/src/graphql/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {makeAddInflectorsPlugin} from 'graphile-utils';
import PgAggregationPlugin from './PgAggregationPlugin';
import {PgBlockHeightPlugin} from './PgBlockHeightPlugin';
import {PgRowByVirtualIdPlugin} from './PgRowByVirtualIdPlugin';
import {PgDistinctPlugin} from './PgDistinctPlugin';

/* eslint-enable */

Expand Down Expand Up @@ -109,6 +110,7 @@ const plugins = [
PgAggregationPlugin,
PgBlockHeightPlugin,
PgRowByVirtualIdPlugin,
PgDistinctPlugin,
makeAddInflectorsPlugin((inflectors) => {
const {constantCase: oldConstantCase} = inflectors;
const enumValues = new Set();
Expand Down