Skip to content

Commit

Permalink
Fix GROUP_CONCAT losing languages if equal
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Feb 29, 2024
1 parent 476a303 commit 0cf0bee
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/expression-evaluator/lib/aggregators/GroupConcat.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import type * as RDF from '@rdfjs/types';
import { string } from '../functions/Helpers';
import { langString, string } from '../functions/Helpers';
import { AggregatorComponent } from './Aggregator';

export class GroupConcat extends AggregatorComponent {
private state: string | undefined = undefined;
private lastLanguageValid = true;
private lastLanguage: string | undefined = undefined;

public static emptyValue(): RDF.Term {
return string('').toRDF();
}

public put(term: RDF.Term): void {
if (this.state === undefined) {
this.state = term.value;
if (term.termType === 'Literal') {
this.lastLanguage = term.language;
}
} else {
this.state += this.separator + term.value;
if (this.lastLanguageValid && term.termType === 'Literal' && this.lastLanguage !== term.language) {
this.lastLanguageValid = false;
this.lastLanguage = undefined;
}
}
}

public result(): RDF.Term {
if (this.state === undefined) {
return GroupConcat.emptyValue();
}
if (this.lastLanguageValid && this.lastLanguage) {
return langString(this.state, this.lastLanguage).toRDF();
}
return string(this.state).toRDF();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,32 @@ describe('an aggregate evaluator should be able to', () => {
});
await expect(result).resolves.toEqual(DF.literal('1;2;3;4'));
});

it('with equal languages', async() => {
const result = testCase({
...baseTestCaseArgs,
input: [
BF.bindings([[ DF.variable('x'), DF.literal('1', 'en') ]]),
BF.bindings([[ DF.variable('x'), DF.literal('2', 'en') ]]),
BF.bindings([[ DF.variable('x'), DF.literal('3', 'en') ]]),
BF.bindings([[ DF.variable('x'), DF.literal('4', 'en') ]]),
],
});
await expect(result).resolves.toEqual(DF.literal('1 2 3 4', 'en'));
});

it('with non-equal languages', async() => {
const result = testCase({
...baseTestCaseArgs,
input: [
BF.bindings([[ DF.variable('x'), DF.literal('1', 'en') ]]),
BF.bindings([[ DF.variable('x'), DF.literal('2', 'nl') ]]),
BF.bindings([[ DF.variable('x'), DF.literal('3', 'en') ]]),
BF.bindings([[ DF.variable('x'), DF.literal('4', 'en') ]]),
],
});
await expect(result).resolves.toEqual(DF.literal('1 2 3 4'));
});
});

it('on a put', async() => {
Expand Down

0 comments on commit 0cf0bee

Please sign in to comment.