Skip to content

Commit

Permalink
Simplify ORDER BY and GROUP BY clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
luxbe committed Dec 19, 2024
1 parent fad6ebf commit 2328b6c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 44 deletions.
26 changes: 13 additions & 13 deletions jopa-impl/src/main/antlr4/cz/cvut/kbss/jopa/query/soql/Soql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ querySentence: selectStatement ;

selectStatement: selectClause fromClause whereClause? groupByClause? orderByClause? ;

objectPathExpression: simplePath DOT objectField ;

simplePath: objectField (DOT simplePath)* ;

objectField: IDENTIFICATION_VARIABLE ;

selectClause: SELECT (DISTINCT)? selectItem (',' selectItem)* ;

selectItem: selectExpression;
Expand Down Expand Up @@ -128,19 +134,13 @@ functionsReturningNumerics
| 'FLOOR' '(' simpleArithmeticExpression ')'
;

orderByClause: ORDERBY orderByFullFormComma orderByFullFormComma* ;

orderByFullFormComma: orderByFullForm ','? ;

orderByFullForm: orderByParam ORDERING? ;

orderByParam: object DOT attribute (DOT attribute)* ;
orderByClause: ORDER BY orderByItem (',' orderByItem)* ;

groupByClause: GROUPBY groupByParamComma groupByParamComma* ;
orderByItem: objectPathExpression (ASC | DESC) ;

groupByParamComma: groupByParam ','? ;
groupByClause: GROUP BY groupByItem (',' groupByItem)* ;

groupByParam: object DOT attribute (DOT attribute)* ;
groupByItem: objectPathExpression ;

inputParameter: COLON IDENTIFICATION_VARIABLE ;

Expand All @@ -159,11 +159,11 @@ AND: 'AND' ;

OR: 'OR' ;

ORDERBY: 'ORDER BY' ;
BY: 'BY' ;

ORDERING: ASC | DESC ;
ORDER: 'ORDER' ;

GROUPBY: 'GROUP BY' ;
GROUP: 'GROUP' ;

ASC: 'ASC' ;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,36 @@ public void exitSelectStatement(SoqlParser.SelectStatementContext ctx) {

}

@Override
public void enterObjectPathExpression(SoqlParser.ObjectPathExpressionContext ctx) {

}

@Override
public void exitObjectPathExpression(SoqlParser.ObjectPathExpressionContext ctx) {

}

@Override
public void enterSimplePath(SoqlParser.SimplePathContext ctx) {

}

@Override
public void exitSimplePath(SoqlParser.SimplePathContext ctx) {

}

@Override
public void enterObjectField(SoqlParser.ObjectFieldContext ctx) {

}

@Override
public void exitObjectField(SoqlParser.ObjectFieldContext ctx) {

}

@Override
public void enterSelectClause(SoqlParser.SelectClauseContext ctx) {
this.typeDef = SparqlConstants.SELECT;
Expand Down Expand Up @@ -565,24 +595,8 @@ public void exitOrderByClause(SoqlParser.OrderByClauseContext ctx) {
}

@Override
public void enterOrderByFullFormComma(SoqlParser.OrderByFullFormCommaContext ctx) {
}

@Override
public void exitOrderByFullFormComma(SoqlParser.OrderByFullFormCommaContext ctx) {
}

@Override
public void enterOrderByFullForm(SoqlParser.OrderByFullFormContext ctx) {
}

@Override
public void exitOrderByFullForm(SoqlParser.OrderByFullFormContext ctx) {
}

@Override
public void enterOrderByParam(SoqlParser.OrderByParamContext ctx) {
SoqlNode firstNode = linkContextNodes(ctx);
public void enterOrderByItem(SoqlParser.OrderByItemContext ctx) {
SoqlNode firstNode = linkObjectPathExpression(ctx);
String orderingBy = getOrderingBy(ctx.getParent());
SoqlOrderParameter orderParam = new SoqlOrderParameter(firstNode, orderingBy);
boolean attrSet = false;
Expand All @@ -603,28 +617,21 @@ public void enterOrderByParam(SoqlParser.OrderByParamContext ctx) {
}

@Override
public void exitOrderByParam(SoqlParser.OrderByParamContext ctx) {
public void exitOrderByItem(SoqlParser.OrderByItemContext ctx) {
}

@Override
public void enterGroupByClause(SoqlParser.GroupByClauseContext ctx) {
}

@Override
public void exitGroupByClause(SoqlParser.GroupByClauseContext ctx) {
}

@Override
public void enterGroupByParamComma(SoqlParser.GroupByParamCommaContext ctx) {
}

@Override
public void exitGroupByParamComma(SoqlParser.GroupByParamCommaContext ctx) {
public void exitGroupByClause(SoqlParser.GroupByClauseContext ctx) {
}

@Override
public void enterGroupByParam(SoqlParser.GroupByParamContext ctx) {
SoqlNode firstNode = linkContextNodes(ctx);
public void enterGroupByItem(SoqlParser.GroupByItemContext ctx) {
SoqlNode firstNode = linkObjectPathExpression(ctx);
SoqlGroupParameter groupParam = new SoqlGroupParameter(firstNode);
boolean attrSet = false;
for (SoqlAttribute attr : attributes) {
Expand All @@ -643,6 +650,11 @@ public void enterGroupByParam(SoqlParser.GroupByParamContext ctx) {
groupAttributes.add(groupParam);
}

@Override
public void exitGroupByItem(SoqlParser.GroupByItemContext ctx) {

}

private SoqlNode linkContextNodes(ParserRuleContext ctx) {
SoqlNode firstNode = new AttributeNode(getOwnerFromParam(ctx));
SoqlNode currentNode = firstNode;
Expand All @@ -659,8 +671,20 @@ private SoqlNode linkContextNodes(ParserRuleContext ctx) {
return firstNode;
}

@Override
public void exitGroupByParam(SoqlParser.GroupByParamContext ctx) {
private SoqlNode linkObjectPathExpression(ParserRuleContext ctx) {
SoqlNode firstNode = new AttributeNode(getOwnerFromParam(ctx));
SoqlNode currentNode = firstNode;
for (int i = 2; i < ctx.getChild(0).getChildCount(); i += 2) {
SoqlNode prevNode = currentNode;
currentNode = new AttributeNode(prevNode, ctx.getChild(0).getChild(i).getText());
prevNode.setChild(currentNode);
}
setIris(firstNode);
if (currentNode.getIri().isEmpty()) {
currentNode.getParent().setChild(null);
this.isInObjectIdentifierExpression = true;
}
return firstNode;
}

@Override
Expand Down

0 comments on commit 2328b6c

Please sign in to comment.