You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by dl...@apache.org on 2019/03/18 18:03:50 UTC

[asterixdb] branch master updated: [NO ISSUE][FUN] Add aliases for stddev_*(), var_*() functions

This is an automated email from the ASF dual-hosted git repository.

dlych pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/asterixdb.git


The following commit(s) were added to refs/heads/master by this push:
     new e5ea42d  [NO ISSUE][FUN] Add aliases for stddev_*(), var_*() functions
e5ea42d is described below

commit e5ea42db0f0b0925db7da7b11e998e8c9733b9f5
Author: Dmitry Lychagin <dm...@couchbase.com>
AuthorDate: Fri Mar 15 11:38:09 2019 -0700

    [NO ISSUE][FUN] Add aliases for stddev_*(), var_*() functions
    
    - user model changes: yes
    - storage format changes: no
    - interface changes: no
    
    Details:
    - Support aliases for SQL aggregate functions
    - Add function aliases: stddev() -> stddev_samp(),
       variance() -> var_samp(), variance_samp() -> var_samp(),
       variance_pop() -> var_pop()
    - stddev_pop() and var_pop() must return 0 if input is a single item
    
    Change-Id: I0fe04164164726cfb4e17f951f0fd6c2a823c162
    Reviewed-on: https://asterix-gerrit.ics.uci.edu/3278
    Sonar-Qube: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Tested-by: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Contrib: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Integration-Tests: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Reviewed-by: Ali Alsuliman <al...@gmail.com>
---
 .../stddev/stddev.1.query.sqlpp                    | 29 ++++++++++++
 .../stddev/stddev.2.query.sqlpp                    | 29 ++++++++++++
 .../stddev/stddev.3.query.sqlpp                    | 33 ++++++++++++++
 .../aggregate-sql-sugar/var/var.1.query.sqlpp      | 31 +++++++++++++
 .../aggregate-sql-sugar/var/var.2.query.sqlpp      | 32 +++++++++++++
 .../aggregate-sql-sugar/var/var.3.query.sqlpp      | 33 ++++++++++++++
 .../stddev_misc/stddev_misc.1.query.sqlpp          | 28 ++++++++++++
 .../stddev_pop_misc/stddev_pop_misc.1.query.sqlpp  | 28 ++++++++++++
 .../aggregate-sql/var_misc/var_misc.1.query.sqlpp  | 28 ++++++++++++
 .../var_pop_misc/var_pop_misc.1.query.sqlpp        | 28 ++++++++++++
 .../stddev_misc/stddev_misc.1.query.sqlpp          | 28 ++++++++++++
 .../stddev_pop_misc/stddev_pop_misc.1.query.sqlpp  | 28 ++++++++++++
 .../aggregate/var_misc/var_misc.1.query.sqlpp      | 28 ++++++++++++
 .../var_pop_misc/var_pop_misc.1.query.sqlpp        | 28 ++++++++++++
 .../aggregate-sql-sugar/stddev/stddev.1.adm        |  1 +
 .../aggregate-sql-sugar/stddev/stddev.2.adm        |  1 +
 .../aggregate-sql-sugar/stddev/stddev.3.adm        |  1 +
 .../results/aggregate-sql-sugar/var/var.1.adm      |  1 +
 .../results/aggregate-sql-sugar/var/var.2.adm      |  1 +
 .../results/aggregate-sql-sugar/var/var.3.adm      |  1 +
 .../aggregate-sql/stddev_misc/stddev_misc.1.adm    |  1 +
 .../stddev_pop_misc/stddev_pop_misc.1.adm          |  1 +
 .../results/aggregate-sql/var_misc/var_misc.1.adm  |  1 +
 .../aggregate-sql/var_pop_misc/var_pop_misc.1.adm  |  1 +
 .../aggregate/stddev_misc/stddev_misc.1.adm        |  1 +
 .../stddev_pop_misc/stddev_pop_misc.1.adm          |  1 +
 .../results/aggregate/var_misc/var_misc.1.adm      |  1 +
 .../aggregate/var_pop_misc/var_pop_misc.1.adm      |  1 +
 .../test/resources/runtimets/testsuite_sqlpp.xml   | 52 ++++++++++++++++++++++
 .../asterix-doc/src/main/markdown/sqlpp/3_query.md | 14 +++++-
 .../lang/common/util/CommonFunctionMapUtil.java    | 12 ++++-
 .../asterix/lang/sqlpp/util/FunctionMapUtil.java   | 31 +++++++++++--
 .../asterix-lang-sqlpp/src/main/javacc/SQLPP.jj    |  2 +-
 ...stractSingleVarStatisticsAggregateFunction.java | 10 +++--
 34 files changed, 534 insertions(+), 12 deletions(-)

diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/stddev/stddev.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/stddev/stddev.1.query.sqlpp
new file mode 100644
index 0000000..57d04d2
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/stddev/stddev.1.query.sqlpp
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : stddev_samp(), stddev_pop(), stddev()
+* Expected Res : Success
+*/
+
+from range(1, 10) x
+select
+  round_half_to_even(stddev_samp(x), 2) as stddev_samp,
+  round_half_to_even(stddev_pop(x), 2) as stddev_pop,
+  round_half_to_even(stddev(x), 2) as stddev
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/stddev/stddev.2.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/stddev/stddev.2.query.sqlpp
new file mode 100644
index 0000000..04f8982
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/stddev/stddev.2.query.sqlpp
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : distinct stddev_samp(), stddev_pop(), stddev()
+* Expected Res : Success
+*/
+
+from range(1, 10) x, range(1, 3) y
+select
+  round_half_to_even(stddev_samp(distinct x), 2) as stddev_samp,
+  round_half_to_even(stddev_pop(distinct x), 2) as stddev_pop,
+  round_half_to_even(stddev(distinct x), 2) as stddev
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/stddev/stddev.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/stddev/stddev.3.query.sqlpp
new file mode 100644
index 0000000..ab0a828
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/stddev/stddev.3.query.sqlpp
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : stddev(), stddev_samp(), stddev_pop() over 0 and 1 items
+* Expected Res : Success
+*/
+
+{
+ "stddev_0": (from range(1, 10) x where x < 1 select value stddev(x)),
+ "stddev_1": (from range(1, 10) x where x = 1 select value stddev(x)),
+
+ "stddev_samp_0": (from range(1, 10) x where x < 1 select value stddev_samp(x)),
+ "stddev_samp_1": (from range(1, 10) x where x = 1 select value stddev_samp(x)),
+
+ "stddev_pop_0": (from range(1, 10) x where x < 1 select value stddev_pop(x)),
+ "stddev_pop_1": (from range(1, 10) x where x = 1 select value stddev_pop(x))
+}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/var/var.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/var/var.1.query.sqlpp
new file mode 100644
index 0000000..70ccde1
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/var/var.1.query.sqlpp
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : var_samp(), var_pop(), variance_samp(), variance_pop(), variance()
+* Expected Res : Success
+*/
+
+from range(1, 10) x
+select
+  round_half_to_even(var_samp(x), 2) as var_samp,
+  round_half_to_even(var_pop(x), 2) as var_pop,
+  round_half_to_even(variance_samp(x), 2) as variance_samp,
+  round_half_to_even(variance_pop(x), 2) as variance_pop,
+  round_half_to_even(variance(x), 2) as variance
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/var/var.2.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/var/var.2.query.sqlpp
new file mode 100644
index 0000000..fd30535
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/var/var.2.query.sqlpp
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : distinct var_samp(), var_pop(), variance_samp(),
+*              :          variance_pop(), variance()
+* Expected Res : Success
+*/
+
+from range(1, 10) x, range (1, 3) y
+select
+  round_half_to_even(var_samp(distinct x), 2) as var_samp,
+  round_half_to_even(var_pop(distinct x), 2) as var_pop,
+  round_half_to_even(variance_samp(distinct x), 2) as variance_samp,
+  round_half_to_even(variance_pop(distinct x), 2) as variance_pop,
+  round_half_to_even(variance(distinct x), 2) as variance
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/var/var.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/var/var.3.query.sqlpp
new file mode 100644
index 0000000..108e2bd
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql-sugar/var/var.3.query.sqlpp
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : variance(), var_samp(), var_pop() over 0 and 1 items
+* Expected Res : Success
+*/
+
+{
+ "variance_0": (from range(1, 10) x where x < 1 select value variance(x)),
+ "variance_1": (from range(1, 10) x where x = 1 select value variance(x)),
+
+ "var_samp_0": (from range(1, 10) x where x < 1 select value var_samp(x)),
+ "var_samp_1": (from range(1, 10) x where x = 1 select value var_samp(x)),
+
+ "var_pop_0": (from range(1, 10) x where x < 1 select value var_pop(x)),
+ "var_pop_1": (from range(1, 10) x where x = 1 select value var_pop(x))
+}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/stddev_misc/stddev_misc.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/stddev_misc/stddev_misc.1.query.sqlpp
new file mode 100644
index 0000000..30f1479
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/stddev_misc/stddev_misc.1.query.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : array_stddev_samp() over 0 and 1 items
+* Expected Res : Success
+*/
+
+{
+ "t0": array_stddev_samp([]),
+ "t1": array_stddev_samp([2])
+}
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/stddev_pop_misc/stddev_pop_misc.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/stddev_pop_misc/stddev_pop_misc.1.query.sqlpp
new file mode 100644
index 0000000..5566938
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/stddev_pop_misc/stddev_pop_misc.1.query.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : array_stddev_pop() over 0 and 1 items
+* Expected Res : Success
+*/
+
+{
+ "t0": array_stddev_pop([]),
+ "t1": array_stddev_pop([2])
+}
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/var_misc/var_misc.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/var_misc/var_misc.1.query.sqlpp
new file mode 100644
index 0000000..91b6aa8
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/var_misc/var_misc.1.query.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : array_var_samp() over 0 and 1 items
+* Expected Res : Success
+*/
+
+{
+ "t0": array_var_samp([]),
+ "t1": array_var_samp([2])
+}
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/var_pop_misc/var_pop_misc.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/var_pop_misc/var_pop_misc.1.query.sqlpp
new file mode 100644
index 0000000..0c30935
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/var_pop_misc/var_pop_misc.1.query.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : array_var_pop() over 0 and 1 items
+* Expected Res : Success
+*/
+
+{
+ "t0": array_var_pop([]),
+ "t1": array_var_pop([2])
+}
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/stddev_misc/stddev_misc.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/stddev_misc/stddev_misc.1.query.sqlpp
new file mode 100644
index 0000000..cc40d89
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/stddev_misc/stddev_misc.1.query.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : strict_stddev_samp() over 0 and 1 items
+* Expected Res : Success
+*/
+
+{
+ "t0": strict_stddev_samp([]),
+ "t1": strict_stddev_samp([2])
+}
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/stddev_pop_misc/stddev_pop_misc.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/stddev_pop_misc/stddev_pop_misc.1.query.sqlpp
new file mode 100644
index 0000000..a855540
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/stddev_pop_misc/stddev_pop_misc.1.query.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : strict_stddev_pop() over 0 and 1 items
+* Expected Res : Success
+*/
+
+{
+ "t0": strict_stddev_pop([]),
+ "t1": strict_stddev_pop([2])
+}
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/var_misc/var_misc.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/var_misc/var_misc.1.query.sqlpp
new file mode 100644
index 0000000..c81fca9
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/var_misc/var_misc.1.query.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : strict_var_samp() over 0 and 1 items
+* Expected Res : Success
+*/
+
+{
+ "t0": strict_var_samp([]),
+ "t1": strict_var_samp([2])
+}
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/var_pop_misc/var_pop_misc.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/var_pop_misc/var_pop_misc.1.query.sqlpp
new file mode 100644
index 0000000..4df9b60
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/var_pop_misc/var_pop_misc.1.query.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+* Description  : strict_var_pop() over 0 and 1 items
+* Expected Res : Success
+*/
+
+{
+ "t0": strict_var_pop([]),
+ "t1": strict_var_pop([2])
+}
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/stddev/stddev.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/stddev/stddev.1.adm
new file mode 100644
index 0000000..1eb655c
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/stddev/stddev.1.adm
@@ -0,0 +1 @@
+{ "stddev_samp": 3.03, "stddev_pop": 2.87, "stddev": 3.03 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/stddev/stddev.2.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/stddev/stddev.2.adm
new file mode 100644
index 0000000..1eb655c
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/stddev/stddev.2.adm
@@ -0,0 +1 @@
+{ "stddev_samp": 3.03, "stddev_pop": 2.87, "stddev": 3.03 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/stddev/stddev.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/stddev/stddev.3.adm
new file mode 100644
index 0000000..b49752a
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/stddev/stddev.3.adm
@@ -0,0 +1 @@
+{ "stddev_0": [ null ], "stddev_1": [ null ], "stddev_samp_0": [ null ], "stddev_samp_1": [ null ], "stddev_pop_0": [ null ], "stddev_pop_1": [ 0.0 ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/var/var.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/var/var.1.adm
new file mode 100644
index 0000000..8dcba0d
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/var/var.1.adm
@@ -0,0 +1 @@
+{ "var_samp": 9.17, "var_pop": 8.25, "variance_samp": 9.17, "variance_pop": 8.25, "variance": 9.17 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/var/var.2.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/var/var.2.adm
new file mode 100644
index 0000000..8dcba0d
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/var/var.2.adm
@@ -0,0 +1 @@
+{ "var_samp": 9.17, "var_pop": 8.25, "variance_samp": 9.17, "variance_pop": 8.25, "variance": 9.17 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/var/var.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/var/var.3.adm
new file mode 100644
index 0000000..e8ecdd4
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql-sugar/var/var.3.adm
@@ -0,0 +1 @@
+{ "variance_0": [ null ], "variance_1": [ null ], "var_samp_0": [ null ], "var_samp_1": [ null ], "var_pop_0": [ null ], "var_pop_1": [ 0.0 ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/stddev_misc/stddev_misc.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/stddev_misc/stddev_misc.1.adm
new file mode 100644
index 0000000..3ecac4f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/stddev_misc/stddev_misc.1.adm
@@ -0,0 +1 @@
+{ "t0": null, "t1": null }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/stddev_pop_misc/stddev_pop_misc.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/stddev_pop_misc/stddev_pop_misc.1.adm
new file mode 100644
index 0000000..4b6e74b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/stddev_pop_misc/stddev_pop_misc.1.adm
@@ -0,0 +1 @@
+{ "t0": null, "t1": 0.0 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/var_misc/var_misc.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/var_misc/var_misc.1.adm
new file mode 100644
index 0000000..3ecac4f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/var_misc/var_misc.1.adm
@@ -0,0 +1 @@
+{ "t0": null, "t1": null }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/var_pop_misc/var_pop_misc.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/var_pop_misc/var_pop_misc.1.adm
new file mode 100644
index 0000000..4b6e74b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/var_pop_misc/var_pop_misc.1.adm
@@ -0,0 +1 @@
+{ "t0": null, "t1": 0.0 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/stddev_misc/stddev_misc.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/stddev_misc/stddev_misc.1.adm
new file mode 100644
index 0000000..3ecac4f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/stddev_misc/stddev_misc.1.adm
@@ -0,0 +1 @@
+{ "t0": null, "t1": null }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/stddev_pop_misc/stddev_pop_misc.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/stddev_pop_misc/stddev_pop_misc.1.adm
new file mode 100644
index 0000000..4b6e74b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/stddev_pop_misc/stddev_pop_misc.1.adm
@@ -0,0 +1 @@
+{ "t0": null, "t1": 0.0 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/var_misc/var_misc.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/var_misc/var_misc.1.adm
new file mode 100644
index 0000000..3ecac4f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/var_misc/var_misc.1.adm
@@ -0,0 +1 @@
+{ "t0": null, "t1": null }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/var_pop_misc/var_pop_misc.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/var_pop_misc/var_pop_misc.1.adm
new file mode 100644
index 0000000..4b6e74b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/var_pop_misc/var_pop_misc.1.adm
@@ -0,0 +1 @@
+{ "t0": null, "t1": 0.0 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index 0b221bf..313ed78 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -1250,6 +1250,16 @@
       </compilation-unit>
     </test-case>
     <test-case FilePath="aggregate">
+      <compilation-unit name="stddev_misc">
+        <output-dir compare="Text">stddev_misc</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate">
+      <compilation-unit name="stddev_pop_misc">
+        <output-dir compare="Text">stddev_pop_misc</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate">
       <compilation-unit name="sum_double">
         <output-dir compare="Text">sum_double</output-dir>
       </compilation-unit>
@@ -1420,6 +1430,16 @@
         <output-dir compare="Text">var_distinct</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="aggregate">
+      <compilation-unit name="var_misc">
+        <output-dir compare="Text">var_misc</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate">
+      <compilation-unit name="var_pop_misc">
+        <output-dir compare="Text">var_pop_misc</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="aggregate-sql">
     <test-case FilePath="aggregate-sql">
@@ -2413,6 +2433,16 @@
       </compilation-unit>
     </test-case>
     <test-case FilePath="aggregate-sql">
+      <compilation-unit name="stddev_misc">
+        <output-dir compare="Text">stddev_misc</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate-sql">
+      <compilation-unit name="stddev_pop_misc">
+        <output-dir compare="Text">stddev_pop_misc</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate-sql">
       <compilation-unit name="sum_double">
         <output-dir compare="Text">sum_double</output-dir>
       </compilation-unit>
@@ -2578,6 +2608,28 @@
         <output-dir compare="Text">var_distinct</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="aggregate-sql">
+      <compilation-unit name="var_misc">
+        <output-dir compare="Text">var_misc</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate-sql">
+      <compilation-unit name="var_pop_misc">
+        <output-dir compare="Text">var_pop_misc</output-dir>
+      </compilation-unit>
+    </test-case>
+  </test-group>
+  <test-group name="aggregate-sql-sugar">
+    <test-case FilePath="aggregate-sql-sugar">
+      <compilation-unit name="stddev">
+        <output-dir compare="Text">stddev</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate-sql-sugar">
+      <compilation-unit name="var">
+        <output-dir compare="Text">var</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="array_fun">
     <test-case FilePath="array_fun">
diff --git a/asterixdb/asterix-doc/src/main/markdown/sqlpp/3_query.md b/asterixdb/asterix-doc/src/main/markdown/sqlpp/3_query.md
index 365c6e3..c266d40 100644
--- a/asterixdb/asterix-doc/src/main/markdown/sqlpp/3_query.md
+++ b/asterixdb/asterix-doc/src/main/markdown/sqlpp/3_query.md
@@ -1238,7 +1238,8 @@ This query returns:
 
 ### <a id="SQL-92_aggregation_functions">SQL-92 Aggregation Functions</a>
 For compatibility with the traditional SQL aggregation functions, the query language also offers SQL-92's
-aggregation function symbols (`COUNT`, `SUM`, `MAX`, `MIN`, and `AVG`) as supported syntactic sugar.
+aggregation function symbols (`COUNT`, `SUM`, `MAX`, `MIN`, `AVG`, `STDDEV_SAMP`, `STDDEV_POP`, `VAR_SAMP`, `VAR_POP`) 
+as supported syntactic sugar.
 The query compiler rewrites queries that utilize these function symbols into queries that only
 use the collection aggregate functions of the query language. The following example uses the SQL-92 syntax approach
 to compute a result that is identical to that of the more explicit example above:
@@ -1259,12 +1260,21 @@ will rewrite as follows:
     GROUP AS `$1`(msg AS msg);
 
 
-The same sort of rewritings apply to the function symbols `SUM`, `MAX`, `MIN`, and `AVG`.
+The same sort of rewritings apply to the function symbols `SUM`, `MAX`, `MIN`, `AVG`, `STDDEV_SAMP`, `STDDEV_POP`, 
+`VAR_SAMP`, and `VAR_POP`. 
 In contrast to the collection aggregate functions of the query language, these special SQL-92 function symbols
 can only be used in the same way they are in standard SQL (i.e., with the same restrictions).
 
 DISTINCT modifier is also supported for these aggregate functions.
 
+The following aggregate function aliases are supported
+ 
+| Function       | Aliases                 | 
+|----------------|-------------------------|
+| STDDEV_SAMP    | STDDEV                  |
+| VAR_SAMP       | VARIANCE, VARIANCE_SAMP |
+| VAR_POP        | VARIANCE_POP            |
+
 ### <a id="SQL-92_compliant_gby">SQL-92 Compliant GROUP BY Aggregations</a>
 The query language provides full support for SQL-92 `GROUP BY` aggregation queries.
 The following query is such an example:
diff --git a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/util/CommonFunctionMapUtil.java b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/util/CommonFunctionMapUtil.java
index 631bb7b..e2e259e 100644
--- a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/util/CommonFunctionMapUtil.java
+++ b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/util/CommonFunctionMapUtil.java
@@ -114,6 +114,12 @@ public class CommonFunctionMapUtil {
 
         // Array/Mutliset functions
         addFunctionMapping("array_length", "len");
+
+        // Aggregate functions
+        addFunctionMapping("stddev", "stddev_samp");
+        addFunctionMapping("variance", "var_samp");
+        addFunctionMapping("variance_samp", "var_samp");
+        addFunctionMapping("variance_pop", "var_pop");
     }
 
     private CommonFunctionMapUtil() {
@@ -132,7 +138,7 @@ public class CommonFunctionMapUtil {
             throws CompilationException {
         String name = fs.getName();
         String lowerCaseName = name.toLowerCase();
-        String mappedName = FUNCTION_NAME_MAP.get(lowerCaseName);
+        String mappedName = getFunctionMapping(lowerCaseName);
         if (mappedName != null) {
             return new FunctionSignature(fs.getNamespace(), mappedName, fs.getArity());
         }
@@ -141,6 +147,10 @@ public class CommonFunctionMapUtil {
         return BuiltinFunctions.isBuiltinCompilerFunction(newFs, true) ? newFs : fs;
     }
 
+    public static String getFunctionMapping(String alias) {
+        return FUNCTION_NAME_MAP.get(alias);
+    }
+
     public static void addFunctionMapping(String alias, String functionName) {
         FUNCTION_NAME_MAP.put(alias, functionName);
     }
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/FunctionMapUtil.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/FunctionMapUtil.java
index b220d8d..daecabb 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/FunctionMapUtil.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/FunctionMapUtil.java
@@ -39,6 +39,7 @@ import org.apache.hyracks.api.exceptions.SourceLocation;
 public class FunctionMapUtil {
 
     public static final String CONCAT = "concat";
+    public static final String DISTINCT_AGGREGATE_SUFFIX = "-distinct";
     private final static String CORE_AGGREGATE_PREFIX = "strict_";
     // This is a transitional case. The ALT_CORE_AGGREGATE_PREFIX should be removed again.
     private final static String ALT_CORE_AGGREGATE_PREFIX = "coll_";
@@ -64,8 +65,9 @@ public class FunctionMapUtil {
      *         false otherwise.
      */
     public static boolean isSql92AggregateFunction(FunctionSignature signature) {
-        IFunctionInfo finfo = FunctionUtil.getFunctionInfo(new FunctionIdentifier(FunctionConstants.ASTERIX_NS,
-                signature.getName().toLowerCase(), signature.getArity()));
+        String name = applySql92AggregateNameMapping(signature.getName().toLowerCase());
+        IFunctionInfo finfo = FunctionUtil
+                .getFunctionInfo(new FunctionIdentifier(FunctionConstants.ASTERIX_NS, name, signature.getArity()));
         if (finfo == null) {
             return false;
         }
@@ -83,8 +85,8 @@ public class FunctionMapUtil {
         if (!isSql92AggregateFunction(fs)) {
             return fs;
         }
-        return new FunctionSignature(FunctionConstants.ASTERIX_NS, CORE_SQL_AGGREGATE_PREFIX + fs.getName(),
-                fs.getArity());
+        String name = applySql92AggregateNameMapping(fs.getName().toLowerCase());
+        return new FunctionSignature(FunctionConstants.ASTERIX_NS, CORE_SQL_AGGREGATE_PREFIX + name, fs.getArity());
     }
 
     /**
@@ -193,4 +195,25 @@ public class FunctionMapUtil {
                 signature.getName().toLowerCase(), signature.getArity()));
         return finfo != null ? BuiltinFunctions.getWindowFunction(finfo.getFunctionIdentifier()) : null;
     }
+
+    /**
+     * Returns original function name for a SQL-92 aggregate function alias or given name if there is no mapping
+     * defined for it
+     */
+    private static String applySql92AggregateNameMapping(String functionName) {
+        boolean distinct = functionName.endsWith(DISTINCT_AGGREGATE_SUFFIX);
+        if (distinct) {
+            String mainName = CommonFunctionMapUtil.getFunctionMapping(
+                    functionName.substring(0, functionName.length() - DISTINCT_AGGREGATE_SUFFIX.length()));
+            if (mainName != null) {
+                return mainName + DISTINCT_AGGREGATE_SUFFIX;
+            }
+        } else {
+            String mainName = CommonFunctionMapUtil.getFunctionMapping(functionName);
+            if (mainName != null) {
+                return mainName;
+            }
+        }
+        return functionName;
+    }
 }
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj b/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
index 19a4da2..e50f68f 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
+++ b/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
@@ -2726,7 +2726,7 @@ Expression FunctionCallExpr() throws ParseException:
     {
       String name = funcName.function;
       if (distinct) {
-        name += "-distinct";
+        name += FunctionMapUtil.DISTINCT_AGGREGATE_SUFFIX;
       }
       // TODO use funcName.library
       String fqFunctionName = funcName.library == null ? name : funcName.library + "#" + name;
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/AbstractSingleVarStatisticsAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/AbstractSingleVarStatisticsAggregateFunction.java
index 652af00..34e3b9f 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/AbstractSingleVarStatisticsAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/AbstractSingleVarStatisticsAggregateFunction.java
@@ -269,10 +269,11 @@ public abstract class AbstractSingleVarStatisticsAggregateFunction extends Abstr
     protected void finishStddevFinalResults(IPointable result, int delta) throws HyracksDataException {
         resultStorage.reset();
         try {
-            if (moments.getCount() <= 1 || aggType == ATypeTag.NULL) {
+            long count = moments.getCount();
+            if (count <= delta || aggType == ATypeTag.NULL) {
                 nullSerde.serialize(ANull.NULL, resultStorage.getDataOutput());
             } else {
-                aDouble.setValue(Math.sqrt(moments.getM2() / (moments.getCount() - delta)));
+                aDouble.setValue(Math.sqrt(moments.getM2() / (count - delta)));
                 doubleSerde.serialize(aDouble, resultStorage.getDataOutput());
             }
         } catch (IOException e) {
@@ -284,10 +285,11 @@ public abstract class AbstractSingleVarStatisticsAggregateFunction extends Abstr
     protected void finishVarFinalResults(IPointable result, int delta) throws HyracksDataException {
         resultStorage.reset();
         try {
-            if (moments.getCount() <= 1 || aggType == ATypeTag.NULL) {
+            long count = moments.getCount();
+            if (count <= delta || aggType == ATypeTag.NULL) {
                 nullSerde.serialize(ANull.NULL, resultStorage.getDataOutput());
             } else {
-                aDouble.setValue(moments.getM2() / (moments.getCount() - delta));
+                aDouble.setValue(moments.getM2() / (count - delta));
                 doubleSerde.serialize(aDouble, resultStorage.getDataOutput());
             }
         } catch (IOException e) {