From 3ae515f75d173d89548ef25d2c8b40f341f21438 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 30 Aug 2024 18:04:24 +0200 Subject: [PATCH] Support GROUPING function in SOQL queries Fixes #49 --- antlr/ApexLexer.g4 | 1 + antlr/ApexParser.g4 | 3 +++ .../apexparser/SOQLParserTest.java | 19 ++++++++++++++++++ npm/src/__tests__/SOQLParserTest.ts | 20 +++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/antlr/ApexLexer.g4 b/antlr/ApexLexer.g4 index a2b3297..bf30419 100644 --- a/antlr/ApexLexer.g4 +++ b/antlr/ApexLexer.g4 @@ -170,6 +170,7 @@ CUSTOM : 'custom'; STANDARD : 'standard'; DISTANCE : 'distance'; GEOLOCATION : 'geolocation'; +GROUPING : 'grouping'; // SOQL Date functions CALENDAR_MONTH : 'calendar_month'; diff --git a/antlr/ApexParser.g4 b/antlr/ApexParser.g4 index d6a259b..ad86f34 100644 --- a/antlr/ApexParser.g4 +++ b/antlr/ApexParser.g4 @@ -636,6 +636,7 @@ soqlFunction | WEEK_IN_YEAR LPAREN dateFieldName RPAREN | FIELDS LPAREN soqlFieldsParameter RPAREN | DISTANCE LPAREN locationValue COMMA locationValue COMMA StringLiteral RPAREN + | GROUPING LPAREN fieldName RPAREN ; dateFieldName @@ -962,6 +963,7 @@ id | CUSTOM | DISTANCE | GEOLOCATION + | GROUPING // SOQL date functions | CALENDAR_MONTH | CALENDAR_QUARTER @@ -1160,6 +1162,7 @@ anyId | CUSTOM | DISTANCE | GEOLOCATION + | GROUPING // SOQL date functions | CALENDAR_MONTH | CALENDAR_QUARTER diff --git a/jvm/src/test/java/io/github/apexdevtools/apexparser/SOQLParserTest.java b/jvm/src/test/java/io/github/apexdevtools/apexparser/SOQLParserTest.java index d0d98b7..ba62131 100644 --- a/jvm/src/test/java/io/github/apexdevtools/apexparser/SOQLParserTest.java +++ b/jvm/src/test/java/io/github/apexdevtools/apexparser/SOQLParserTest.java @@ -111,4 +111,23 @@ void testSubQuery() { assertNotNull(context); assertEquals(0, parserAndCounter.getValue().getNumErrors()); } + + @Test + void testGroupingFunction() { + Map.Entry parserAndCounter = createParser( + "SELECT\n" + + " OBJ1__c O1,\n" + + " OBJ2__c O2,\n" + + " OBJ3__c O3,\n" + + " SUM(OBJ4__c) O4,\n" + + " GROUPING(OBJ1__c) O1Group,\n" + + " GROUPING(OBJ2__c) O2Group,\n" + + " GROUPING(OBJ3__c) O3Group\n" + + "FROM OBJ4__c\n" + + "GROUP BY ROLLUP(OBJ1__c, OBJ2__c, OBJ3__c)" + ); + ApexParser.QueryContext context = parserAndCounter.getKey().query(); + assertNotNull(context); + assertEquals(0, parserAndCounter.getValue().getNumErrors()); + } } diff --git a/npm/src/__tests__/SOQLParserTest.ts b/npm/src/__tests__/SOQLParserTest.ts index e2312b6..d8cb00e 100644 --- a/npm/src/__tests__/SOQLParserTest.ts +++ b/npm/src/__tests__/SOQLParserTest.ts @@ -118,3 +118,23 @@ test("SubQuery", () => { expect(context).toBeInstanceOf(QueryContext); expect(errorCounter.getNumErrors()).toEqual(0); }); + +test("Grouping function", () => { + const [parser, errorCounter] = createParser( + `SELECT + OBJ1__c O1, + OBJ2__c O2, + OBJ3__c O3, + SUM(OBJ4__c) O4, + GROUPING(OBJ1__c) O1Group, + GROUPING(OBJ2__c) O2Group, + GROUPING(OBJ3__c) O3Group + FROM OBJ4__c + GROUP BY ROLLUP(OBJ1__c, OBJ2__c, OBJ3__c)` + ); + + const context = parser.query(); + + expect(context).toBeInstanceOf(QueryContext); + expect(errorCounter.getNumErrors()).toEqual(0); +});