@@ -2,17 +2,17 @@ import type * as RDF from '@rdfjs/types';
22import type {
33 DatasetClauses ,
44 Pattern ,
5- PatternBgp ,
5+ PatternGroup ,
6+ QuerySelect ,
67 TermBlank ,
78 TermIri ,
89 TermLiteral ,
910 TermVariable ,
1011 TripleNesting ,
1112} from '@traqula/rules-sparql-1-1' ;
12- import type * as Algebra from '../algebra ' ;
13+ import type { Algebra } from '../index ' ;
1314import Util from '../util' ;
1415import type { AstContext } from './core' ;
15- import { translatePathComponent } from './path' ;
1616import { translateExpression , translateOperation } from './pattern' ;
1717
1818type RdfTermToAst < T extends RDF . Term > = T extends RDF . Variable ? TermVariable :
@@ -41,6 +41,12 @@ export function translateTerm<T extends RDF.Term>(c: AstContext, term: T): RdfTe
4141 throw new Error ( `invalid term type: ${ term . termType } ` ) ;
4242}
4343
44+ /**
45+ * Extend is for example a bind, or an aggregator.
46+ * The result is thus registered to be tackled at the project level,
47+ * or if we are not in project scope, we give it as a patternBind
48+ * - of course, the pattern bind is scoped with the other operations at this level
49+ */
4450export function translateExtend ( c : AstContext , op : Algebra . Extend ) : Pattern [ ] {
4551 const F = c . astFactory ;
4652 if ( c . project ) {
@@ -69,22 +75,14 @@ export function translateDatasetClauses(
6975 ] , F . gen ( ) ) ;
7076}
7177
72- export function translateOrderBy ( c : AstContext , op : Algebra . OrderBy ) : any {
78+ /**
79+ * An order by is just registered to be handled in the creation of your QueryBase
80+ */
81+ export function translateOrderBy ( c : AstContext , op : Algebra . OrderBy ) : ReturnType < typeof translateOperation > {
7382 c . order . push ( ...op . expressions ) ;
7483 return translateOperation ( c , op . input ) ;
7584}
7685
77- export function translatePath ( c : AstContext , op : Algebra . Path ) : PatternBgp {
78- const F = c . astFactory ;
79- return F . patternBgp ( [
80- F . triple (
81- translateTerm ( c , op . subject ) ,
82- translatePathComponent ( c , op . predicate ) ,
83- translateTerm ( c , op . object ) ,
84- ) ,
85- ] , F . gen ( ) ) ;
86- }
87-
8886export function translatePattern ( c : AstContext , op : Algebra . Pattern ) : TripleNesting {
8987 const F = c . astFactory ;
9088 return F . triple (
@@ -94,9 +92,22 @@ export function translatePattern(c: AstContext, op: Algebra.Pattern): TripleNest
9492 ) ;
9593}
9694
97- export function translateReduced ( c : AstContext , op : Algebra . Reduced ) : Pattern {
98- const result = translateOperation ( c , op . input ) ;
99- // Project is nested in group object
100- result . patterns [ 0 ] . reduced = true ;
95+ /**
96+ * Reduced is wrapped around a project, set the query contained to be distinct
97+ */
98+ export function translateReduced ( c : AstContext , op : Algebra . Reduced ) : PatternGroup {
99+ const result : PatternGroup = translateOperation ( c , op . input ) ;
100+ const select = < QuerySelect > result . patterns [ 0 ] ;
101+ select . reduced = true ;
102+ return result ;
103+ }
104+
105+ /**
106+ * District is wrapped around a project, set the query contained to be distinct
107+ */
108+ export function translateDistinct ( c : AstContext , op : Algebra . Distinct ) : PatternGroup {
109+ const result : PatternGroup = translateOperation ( c , op . input ) ;
110+ const select = < QuerySelect > result . patterns [ 0 ] ;
111+ select . distinct = true ;
101112 return result ;
102113}
0 commit comments