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

Customize group by and select #48

Merged
merged 2 commits into from
Sep 2, 2024
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@comake/skl-js-engine",
"version": "0.19.0",
"version": "0.20.0",
"description": "Standard Knowledge Language Javascript Engine",
"keywords": [
"skl",
Expand Down
3 changes: 3 additions & 0 deletions src/storage/FindOptionsTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { Variable } from 'sparqljs';
import type { OrArray, JSONArray, JSONObject } from '../util/Types';
import type { FindOperator } from './FindOperator';
import type { InverseRelationOperatorValue } from './operator/InverseRelation';
Expand All @@ -12,6 +13,8 @@ export interface FindOneOptions {
relations?: FindOptionsRelations;
order?: FindOptionsOrder;
skipFraming?: boolean;
group?: Variable;
entitySelectVariable?: Variable;
}

export type FindOptionsRelationsValue = |
Expand Down
5 changes: 3 additions & 2 deletions src/storage/query-adapter/sparql/SparqlQueryAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ export class SparqlQueryAdapter implements QueryAdapter {
const queryData = queryBuilder.buildEntitySelectPatternsFromOptions(entityVariable, options);
const entitySelectQuery = queryData.where.length > 0
? createSparqlSelectQuery(
entityVariable,
options?.entitySelectVariable ?? entityVariable,
queryData.where,
queryData.orders,
queryData.group,
// FIXME: This will not work if queryData.group is defined, figure out what can make it defined.
queryData.group ?? options?.group,
options?.limit,
options?.offset,
)
Expand Down
2 changes: 1 addition & 1 deletion src/storage/query-adapter/sparql/SparqlQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ export class SparqlQueryBuilder {
return createSparqlNotEqualOperation(leftSide, rightSide as Expression);
}

private resolveValueToTerm(value: FieldPrimitiveValue | ValueWhereFieldObject): NamedNode | Literal {
private resolveValueToTerm(value: FieldPrimitiveValue | ValueWhereFieldObject): NamedNode | Literal | Variable {
if (typeof value === 'object' && '@value' in value) {
return valueToLiteral(
(value as ValueWhereFieldObject)['@value'],
Expand Down
7 changes: 5 additions & 2 deletions src/util/TripleUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import DataFactory from '@rdfjs/data-model';
import type { Quad, Quad_Object, Quad_Subject, Literal } from '@rdfjs/types';
import type { Quad, Quad_Object, Quad_Subject, Literal, Variable } from '@rdfjs/types';
import * as jsonld from 'jsonld';
import type { ContextDefinition, GraphObject, NodeObject, ValueObject } from 'jsonld';
import type { Frame } from 'jsonld/jsonld-spec';
Expand Down Expand Up @@ -254,7 +254,7 @@ export async function triplesToJsonldWithFrame(
export function valueToLiteral(
value: string | boolean | number | Date | JSONObject | JSONArray,
datatype?: string,
): Literal {
): Literal | Variable {
if (datatype) {
if (datatype === '@json' || datatype === RDF.JSON) {
return DataFactory.literal(JSON.stringify(value), RDF.JSON);
Expand All @@ -270,6 +270,9 @@ export function valueToLiteral(
if (typeof value === 'boolean') {
return DataFactory.literal(value.toString(), XSD.boolean);
}
if (typeof value === 'string' && value.startsWith('?') && value.length > 1) {
return DataFactory.variable(value.slice(1));
}
if (value instanceof Date) {
return DataFactory.literal(value.toISOString(), XSD.dateTime);
}
Expand Down
33 changes: 33 additions & 0 deletions test/unit/storage/query-adapter/sparql/SparqlQueryAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,39 @@
'}',
]);
});

it('executes a group by query with custom entity select variable.', async(): Promise<void> => {
await adapter.findAll({
where: {
type: 'https://schema.org/Place',
'https://standardknowledge.com/ontologies/core/deduplicationGroup': '?deduplicationGroup'

Check failure on line 709 in test/unit/storage/query-adapter/sparql/SparqlQueryAdapter.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
},
group: DataFactory.variable('deduplicationGroup'),
entitySelectVariable: {
variable: DataFactory.variable('entity'),
expression: {
type: 'aggregate',
aggregation: 'MIN',
expression: DataFactory.variable('entity'),
},
},
});
expect(select).toHaveBeenCalledTimes(1);
expect(select.mock.calls[0][0].split('\n')).toEqual([
'CONSTRUCT { ?subject ?predicate ?object. }',
'WHERE {',
' {',
' SELECT DISTINCT (MIN(?entity) AS ?entity) WHERE {',
' ?entity (<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>/(<http://www.w3.org/2000/01/rdf-schema#subClassOf>*)) <https://schema.org/Place>;',
' <https://standardknowledge.com/ontologies/core/deduplicationGroup> ?deduplicationGroup.',
' FILTER(EXISTS { GRAPH ?entity { ?entity ?c1 ?c2. } })',
' }',
' GROUP BY ?deduplicationGroup',
' }',
' GRAPH ?entity { ?subject ?predicate ?object. }',
'}',
]);
});
});

describe('findAllBy', (): void => {
Expand Down
Loading