You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/01/16 16:05:17 UTC
[groovy] branch master updated: Add built-in `var` and `varp`
functions to GINQ
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 2ef5013 Add built-in `var` and `varp` functions to GINQ
2ef5013 is described below
commit 2ef5013a181e94fb055e0e0689a80329ea344bd4
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Jan 17 00:04:22 2021 +0800
Add built-in `var` and `varp` functions to GINQ
---
.../ginq/provider/collection/GinqAstWalker.groovy | 6 ++-
.../provider/collection/runtime/Queryable.java | 18 ++++++++
.../collection/runtime/QueryableCollection.java | 18 +++++++-
.../groovy-ginq/src/spec/doc/ginq-userguide.adoc | 26 +++++++++--
.../test/org/apache/groovy/ginq/GinqTest.groovy | 50 ++++++++++++++++++++++
5 files changed, 111 insertions(+), 7 deletions(-)
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
index 801deb0..7867b65 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
@@ -1549,8 +1549,10 @@ class GinqAstWalker implements GinqAstVisitor<Expression>, SyntaxErrorReportable
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_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 String FUNCTION_ROW_NUMBER = 'rowNumber'
private static final String FUNCTION_LEAD = 'lead'
@@ -1559,7 +1561,7 @@ class GinqAstWalker implements GinqAstVisitor<Expression>, SyntaxErrorReportable
private static final String FUNCTION_LAST_VALUE = 'lastValue'
private static final String FUNCTION_RANK = 'rank'
private static final String FUNCTION_DENSE_RANK = 'denseRank'
- private static final List<String> WINDOW_FUNCTION_LIST = [FUNCTION_COUNT, FUNCTION_MIN, FUNCTION_MAX, FUNCTION_SUM, FUNCTION_AVG, FUNCTION_MEDIAN, FUNCTION_STDEV, FUNCTION_STDEVP,
+ private static final List<String> WINDOW_FUNCTION_LIST = [FUNCTION_COUNT, FUNCTION_MIN, FUNCTION_MAX, FUNCTION_SUM, FUNCTION_AVG, FUNCTION_MEDIAN, FUNCTION_STDEV, FUNCTION_STDEVP, FUNCTION_VAR, FUNCTION_VARP,
FUNCTION_ROW_NUMBER, FUNCTION_LEAD, FUNCTION_LAG, FUNCTION_FIRST_VALUE, FUNCTION_LAST_VALUE, FUNCTION_RANK, FUNCTION_DENSE_RANK]
private static final String NAMEDRECORD_CLASS_NAME = NamedRecord.class.name
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Queryable.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Queryable.java
index b73d995..bf357ee 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Queryable.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Queryable.java
@@ -436,6 +436,24 @@ public interface Queryable<T> {
BigDecimal stdevp(Function<? super T, ? extends Number> mapper);
/**
+ * Aggregate function {@code var}, similar to SQL's {@code var}
+ *
+ * @param mapper choose the field to calculate the statistical variance
+ * @return statistical variance
+ * @since 4.0.0
+ */
+ BigDecimal var(Function<? super T, ? extends Number> mapper);
+
+ /**
+ * Aggregate function {@code varp}, similar to SQL's {@code varp}
+ *
+ * @param mapper choose the field to calculate the statistical variance for the population
+ * @return statistical variance for the population
+ * @since 4.0.0
+ */
+ BigDecimal varp(Function<? super T, ? extends Number> mapper);
+
+ /**
* The most powerful aggregate function in GINQ, it will receive the grouped result({@link Queryable} instance) and apply any processing
*
* @param mapper map the grouped result({@link Queryable} instance) to aggregate result
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
index 44d5a49..4376996 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
@@ -436,7 +436,17 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
return sd(mapper, 1);
}
- private BigDecimal sd(Function<? super T, ? extends Number> mapper, int diff) {
+ @Override
+ public BigDecimal var(Function<? super T, ? extends Number> mapper) {
+ return vr(mapper, 0);
+ }
+
+ @Override
+ public BigDecimal varp(Function<? super T, ? extends Number> mapper) {
+ return vr(mapper, 1);
+ }
+
+ private BigDecimal vr(Function<? super T, ? extends Number> mapper, int diff) {
BigDecimal avg = this.avg(mapper);
Object[] result = agg(q -> q.stream()
.map(mapper)
@@ -448,8 +458,12 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
return r;
}, (o1, o2) -> o1));
+ return ((BigDecimal) result[1]).divide(toBigDecimal((Long) result[0] - diff), 16, RoundingMode.HALF_UP);
+ }
+
+ private BigDecimal sd(Function<? super T, ? extends Number> mapper, int diff) {
// `BigDecimal.sqrt` is introduced since Java9, so we can not use it for now.
- return toBigDecimal(sqrt(((BigDecimal) result[1]).divide(toBigDecimal((Long) result[0] - diff), 16, RoundingMode.HALF_UP).doubleValue()));
+ return toBigDecimal(sqrt(vr(mapper, diff).doubleValue()));
}
@Override
diff --git a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
index 3ac9260..6eeb15a 100644
--- a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
+++ b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
@@ -284,9 +284,9 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_grouping_09,in
===== Aggregate Functions
GINQ provides some built-in aggregate functions, e.g.
-`count`, `min`, `max`, `sum`, `avg`, `median`, `stdev`, `stdevp` and the most powerful function `agg`.
+`count`, `min`, `max`, `sum`, `avg`, `median`, `stdev`, `stdevp`, `var`, `varp` and the most powerful function `agg`.
[NOTE]
-`count(...)`, `min(...)`, `max(...)`, `avg(...)`, `median(...)` and `stdev(...)` just operate on non-`null` values,
+`count(...)`, `min(...)`, `max(...)`, `avg(...)`, `median(...)`, `stdev(...)`, `stdevp(...)`, `var(...)` and `varp(...)` just operate on non-`null` values,
and `count()` is similar to `count(*)` in SQL.
[source, sql]
----
@@ -348,6 +348,16 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_aggfunction_03
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_aggfunction_04,indent=0]
----
+[source, groovy]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_aggfunction_05,indent=0]
+----
+
+[source, groovy]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_aggfunction_06,indent=0]
+----
+
==== Sorting
`orderby` is equivalent to SQL's `ORDER BY`
@@ -592,7 +602,7 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_15
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_11,indent=0]
----
-===== `min`, `max`, `count`, `sum`, `avg`, `median`, `stdev` and `stdevp`
+===== `min`, `max`, `count`, `sum`, `avg`, `median`, `stdev`, `stdevp`, `var` and `varp`
[source, groovy]
----
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_22,indent=0]
@@ -653,6 +663,16 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_35
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_36,indent=0]
----
+[source, groovy]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_37,indent=0]
+----
+
+[source, groovy]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_winfunction_38,indent=0]
+----
+
=== GINQ Tips
==== Row Number
`_rn` is the implicit variable representing row number for each record in the result set. It starts with `0`
diff --git a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
index 5bfc10d..831bf11 100644
--- a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
+++ b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
@@ -4699,6 +4699,30 @@ class GinqTest {
}
@Test
+ void "testGinq - aggregate function - 15"() {
+ assertGinqScript '''
+// tag::ginq_aggfunction_05[]
+ assert [0.6666666666666667] == GQ {
+ from n in [1, 2, 3]
+ select var(n)
+ }.toList()
+// end::ginq_aggfunction_05[]
+ '''
+ }
+
+ @Test
+ void "testGinq - aggregate function - 16"() {
+ assertGinqScript '''
+// tag::ginq_aggfunction_06[]
+ assert [1] == GQ {
+ from n in [1, 2, 3]
+ select varp(n)
+ }.toList()
+// end::ginq_aggfunction_06[]
+ '''
+ }
+
+ @Test
void "testGinq - parallel - 1"() {
assertGinqScript '''
// tag::ginq_tips_08[]
@@ -5734,6 +5758,32 @@ class GinqTest {
}
@Test
+ void "testGinq - window - 80"() {
+ assertGinqScript '''
+// tag::ginq_winfunction_37[]
+ assert [[1, 0.6666666666666667],
+ [2, 0.6666666666666667],
+ [3, 0.6666666666666667]] == GQ {
+ from n in [1, 2, 3]
+ select n, (var(n) over())
+ }.toList()
+// end::ginq_winfunction_37[]
+ '''
+ }
+
+ @Test
+ void "testGinq - window - 81"() {
+ assertGinqScript '''
+// tag::ginq_winfunction_38[]
+ assert [[1, 1], [2, 1], [3, 1]] == GQ {
+ from n in [1, 2, 3]
+ select n, (varp(n) over())
+ }.toList()
+// end::ginq_winfunction_38[]
+ '''
+ }
+
+ @Test
void "testGinq - shutdown - 0"() {
assertScript '''
import org.apache.groovy.ginq.provider.collection.runtime.QueryableHelper