Skip to content

Commit

Permalink
apacheGH-1615: ElementLateral: Parse and visit
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Nov 16, 2022
1 parent 578c961 commit 512a8c9
Show file tree
Hide file tree
Showing 26 changed files with 1,686 additions and 1,839 deletions.
9 changes: 9 additions & 0 deletions jena-arq/Grammar/arq.jj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import org.apache.jena.sparql.expr.aggregate.lib.* ;
import org.apache.jena.update.* ;
import org.apache.jena.sparql.modify.request.* ;
import org.apache.jena.sparql.core.Quad ;
import java.util.List;
import java.util.ArrayList;
public class ARQParser extends ARQParserBase
{}
PARSER_END(ARQParser)
Expand Down Expand Up @@ -612,6 +614,8 @@ Element GraphPatternNotTriples() : { Element el = null ; }
el = GroupOrUnionGraphPattern()
|
el = OptionalGraphPattern()
|
el = LateralGraphPattern()
|
el = MinusGraphPattern()
|
Expand All @@ -637,6 +641,10 @@ Element OptionalGraphPattern() : { Element el ; }
{ <OPTIONAL> el = GroupGraphPattern()
{ return new ElementOptional(el) ; }
}
Element LateralGraphPattern() : { Element el ; }
{ <LATERAL> el = GroupGraphPattern()
{ return new ElementLateral(el) ; }
}
Element GraphGraphPattern() : { Element el ; Node n ;}
{
<GRAPH> n = VarOrIri() el = GroupGraphPattern()
Expand Down Expand Up @@ -1783,6 +1791,7 @@ TOKEN [IGNORE_CASE] :
| < SUBJECT: "SUBJECT" >
| < PREDICATE: "PREDICATE" >
| < OBJECT: "OBJECT" >
| < LATERAL: "LATERAL" >
| < EXISTS: "exists" >
| < NOT: "not" >
| < AS: "as" >
Expand Down
15 changes: 14 additions & 1 deletion jena-arq/Grammar/main.jj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ import org.apache.jena.sparql.modify.request.* ;
#endif
#ifdef ARQ
import org.apache.jena.sparql.core.Quad ;
import java.util.List;
import java.util.ArrayList;
#endif

public class CLASS extends PARSERBASE
Expand Down Expand Up @@ -879,6 +881,10 @@ Element GraphPatternNotTriples() : { Element el = null ; }
el = GroupOrUnionGraphPattern()
|
el = OptionalGraphPattern()
#ifdef ARQ
|
el = LateralGraphPattern()
#endif
|
el = MinusGraphPattern()
|
Expand Down Expand Up @@ -910,13 +916,19 @@ Element OptionalGraphPattern() : { Element el ; }
{ return new ElementOptional(el) ; }
}

#ifdef ARQ
Element LateralGraphPattern() : { Element el ; }
{ <LATERAL> el = GroupGraphPattern()
{ return new ElementLateral(el) ; }
}
#endif

Element GraphGraphPattern() : { Element el ; Node n ;}
{
<GRAPH> n = VarOrIri() el = GroupGraphPattern()
{ return new ElementNamedGraph(n, el) ; }
}


Element ServiceGraphPattern() : { Element el ; Node n ; boolean silent = false ; }
{
<SERVICE>
Expand Down Expand Up @@ -2522,6 +2534,7 @@ TOKEN [IGNORE_CASE] :
| < SUBJECT: "SUBJECT" >
| < PREDICATE: "PREDICATE" >
| < OBJECT: "OBJECT" >
| < LATERAL: "LATERAL" >
#endif
| < EXISTS: "exists" >
| < NOT: "not" >
Expand Down
1 change: 1 addition & 0 deletions jena-arq/Grammar/tokens.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
[<GRAPH>] ::= 'GRAPH'
[<UNION>] ::= 'UNION'
[<OPTIONAL>] ::= 'OPTIONAL'
[<LATERAL>] ::= 'LATERAL'
[<BOUND>] ::= 'BOUND'
[<COALESCE>] ::= 'COALESCE'
[<IF>] ::= 'IF'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ protected Op compileOneInGroup(Element elt, Op current, Deque<Op> acc) {
return compileElementOptional(eltOpt, current);
}

if ( elt instanceof ElementLateral ) {
ElementLateral eltLateral = (ElementLateral)elt;
return compileElementLateral(eltLateral, current);
}

if ( elt instanceof ElementMinus ) {
ElementMinus elt2 = (ElementMinus)elt;
Op op = compileElementMinus(current, elt2);
Expand Down Expand Up @@ -397,6 +402,12 @@ protected Op compileElementOptional(ElementOptional eltOpt, Op current) {
return current;
}

protected Op compileElementLateral(ElementLateral eltLateral, Op current) {
Element subElt = eltLateral.getLateralElement();
Op op = compileElement(subElt);
return OpLateral.create(current, op);
}

protected Op compileBasicPattern(BasicPattern pattern) {
return new OpBGP(pattern);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

/**
* Special purpose base class for a single transformation.
*
*
* When writing {@link Transform}s to be applied to a tree,
* extend {@link TransformCopy}, not this class.
*/
Expand Down Expand Up @@ -76,6 +76,8 @@ public class TransformSingle implements Transform
@Override
public Op transform(OpUnion opUnion, Op left, Op right) { return opUnion ; }
@Override
public Op transform(OpLateral opCopLateral, Op left, Op right) { return opCopLateral ; }
@Override
public Op transform(OpConditional opCond, Op left, Op right) { return opCond ; }

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,20 @@

package org.apache.jena.sparql.algebra.op;

import java.util.List;

import org.apache.jena.atlas.lib.InternalErrorException;
import org.apache.jena.sparql.algebra.Op;
import org.apache.jena.sparql.algebra.OpVisitor;
import org.apache.jena.sparql.algebra.Transform;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.sse.Tags;
import org.apache.jena.sparql.util.NodeIsomorphismMap;

public class OpLateral extends Op2
{
public static Op create(Op left, Op right) {
return new OpLateral(left, right, null, false);
}

public static Op create(Op left, Op right, List<Var> vars) {
return new OpLateral(left, right, vars, false);
}

public static Op create(Op left, Op right, boolean withStar) {
return new OpLateral(left, right, null, true);
return new OpLateral(left, right);
}

private final List<Var> vars;
/// Was it LATERAL(*) / LATERAL * (as opposed to LATERAL { ... }
// which means the same.
private final boolean withStar;

private OpLateral(Op left, Op right, List<Var> vars, boolean withStar) {
private OpLateral(Op left, Op right) {
super(left, right);
if ( vars != null && withStar )
throw new InternalErrorException("Inconsistent: OpLateral: Both a variable list and has a *");
this.vars = vars;
this.withStar = withStar;
}

/** LATERAL(vars) - null means "no vars" */
public List<Var> vars() {
return vars;
}

public boolean withStar() {
return withStar;
}

@Override
Expand All @@ -76,7 +46,7 @@ public Op apply(Transform transform, Op left, Op right)

@Override
public Op2 copy(Op newLeft, Op newRight)
{ return new OpLateral(newLeft, newRight, List.copyOf(vars), withStar); }
{ return new OpLateral(newLeft, newRight); }

@Override
public boolean equalTo(Op op2, NodeIsomorphismMap labelMap) {
Expand Down
Loading

0 comments on commit 512a8c9

Please sign in to comment.