Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1573,13 +1573,14 @@ class GinqAstWalker implements GinqAstVisitor<Expression>, SyntaxErrorReportable
private static final String FUNCTION_MAX = 'max'
private static final String FUNCTION_SUM = 'sum'
private static final String FUNCTION_AVG = 'avg'
private static final String FUNCTION_LIST = 'list'
private static final String FUNCTION_MEDIAN = 'median'
private static final String FUNCTION_STDEV = 'stdev'
private static final String FUNCTION_STDEVP = 'stdevp'
private static final String FUNCTION_VAR = 'var'
private static final String FUNCTION_VARP = 'varp'
private static final String FUNCTION_AGG = 'agg'
private static final List<String> AGG_FUNCTION_NAME_LIST = [FUNCTION_COUNT, FUNCTION_MIN, FUNCTION_MAX, FUNCTION_SUM, FUNCTION_AVG, FUNCTION_MEDIAN, FUNCTION_STDEV, FUNCTION_STDEVP, FUNCTION_VAR, FUNCTION_VARP, FUNCTION_AGG]
private static final List<String> AGG_FUNCTION_NAME_LIST = [FUNCTION_COUNT, FUNCTION_MIN, FUNCTION_MAX, FUNCTION_SUM, FUNCTION_AVG, FUNCTION_MEDIAN, FUNCTION_STDEV, FUNCTION_STDEVP, FUNCTION_VAR, FUNCTION_VARP, FUNCTION_LIST, FUNCTION_AGG]

private static final String FUNCTION_ROW_NUMBER = 'rowNumber'
private static final String FUNCTION_LEAD = 'lead'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,17 @@ default Queryable<T> union(Queryable<? extends T> queryable) {
*/
<U extends Comparable<? super U>> U max(Function<? super T, ? extends U> mapper);

/**
* Aggregate function {@code list}. There is no equivalent in standard SQL
* but various databases support a similar concept named: {@code list}, {@code listagg}, or {@code array_agg}.
*
* @param mapper choose the field to include in the list
* @param <U> the field type
* @return aggregate list result
* @since 4.0.14
*/
<U> List<U> list(Function<? super T, ? extends U> mapper);

/**
* Aggregate function {@code median}, similar to SQL's {@code median}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,14 @@ public <U extends Comparable<? super U>> U max(Function<? super T, ? extends U>
.orElse(null));
}

@Override
public <U> List<U> list(Function<? super T, ? extends U> mapper) {
return agg(q -> q.stream()
.map(mapper)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
}

@Override
public BigDecimal median(Function<? super T, ? extends Number> mapper) {
List<BigDecimal> sortedNumList = agg(q -> q.stream()
Expand Down
15 changes: 15 additions & 0 deletions subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ GINQ provides some built-in aggregate functions:
|java.math.BigDecimal
|the average (arithmetic mean) of all non-null values

|list(_expression_)
|any
|java.util.List
|the aggregated list of all non-null values

|median(_expression_)
|java.lang.Number
|java.math.BigDecimal
Expand Down Expand Up @@ -424,6 +429,16 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_grouping_11,in
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_grouping_12,indent=0]
----

[source, sql]
----
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_grouping_14,indent=0]
----

[source, sql]
----
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_grouping_15,indent=0]
----

[source, sql]
----
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_grouping_03,indent=0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3018,6 +3018,49 @@ class GinqTest {
'''
}

@Test
void "testGinq - from groupby select - 32"() {
assertGinqScript '''
// tag::ginq_grouping_14[]
assert [['A', ['APPLE', 'APRICOT']],
['B', ['BANANA']],
['C', ['CANTALOUPE']]] == GQL {
from fruit in ['Apple', 'Apricot', 'Banana', 'Cantaloupe']
groupby fruit[0] as firstChar
select firstChar, list(fruit.toUpperCase()) as fruit_list
}
// end::ginq_grouping_14[]
'''
}

@Test
void "testGinq - from groupby select - 33"() {
assertGinqScript '''
@groovy.transform.EqualsAndHashCode
class Person {
String name
int weight
String gender

Person(String name, int weight, String gender) {
this.name = name
this.weight = weight
this.gender = gender
}
}
// tag::ginq_grouping_15[]
def persons = [new Person('Linda', 100, 'Female'),
new Person('Daniel', 135, 'Male'),
new Person('David', 122, 'Male')]
assert [['Male', ['Daniel', 'David']], ['Female', ['Linda']]] == GQL {
from p in persons
groupby p.gender
select p.gender, list(p.name)
}
// end::ginq_grouping_15[]
'''
}

@Test
void "testGinq - from where groupby select - 1"() {
assertGinqScript '''
Expand Down Expand Up @@ -4462,9 +4505,9 @@ class GinqTest {
assert '3' == filterExpr.rightExpression.text

assert ginqExpression.fromExpression.dataSourceExpr instanceof GinqExpression
BinaryExpression contructedFilterExpr1 = ((GinqExpression) ginqExpression.fromExpression.dataSourceExpr).whereExpression.filterExpr
assert Types.COMPARE_GREATER_THAN == contructedFilterExpr1.operation.type
assert '1' == contructedFilterExpr1.rightExpression.text
BinaryExpression constructedFilterExpr1 = ((GinqExpression) ginqExpression.fromExpression.dataSourceExpr).whereExpression.filterExpr
assert Types.COMPARE_GREATER_THAN == constructedFilterExpr1.operation.type
assert '1' == constructedFilterExpr1.rightExpression.text

assert ginqExpression.joinExpressionList[0].dataSourceExpr !instanceof GinqExpression
}
Expand Down Expand Up @@ -4611,8 +4654,8 @@ class GinqTest {
}.ast

MethodNode methodNode = ast.classes[0].methods.grep(e -> e.name == 'hello')[0]
ExpressionStatement delcareStatement = ((BlockStatement) methodNode.getCode()).getStatements()[0]
DeclarationExpression declarationExpression = delcareStatement.getExpression()
ExpressionStatement declareStatement = ((BlockStatement) methodNode.getCode()).getStatements()[0]
DeclarationExpression declarationExpression = declareStatement.getExpression()
ClosureExpression closureException = declarationExpression.rightExpression

GinqAstBuilder ginqAstBuilder = new GinqAstBuilder(sourceUnit)
Expand Down