You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@asterixdb.apache.org by "Yingyi Bu (Code Review)" <do...@asterixdb.incubator.apache.org> on 2016/02/24 19:46:01 UTC

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Yingyi Bu has uploaded a new change for review.

  https://asterix-gerrit.ics.uci.edu/663

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................

ASTERIXDB-1322: fix the AQL translator and add tests.

Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
---
A asterix-app/data/csv/55.csv
A asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-1.aql
A asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-2.aql
A asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
A asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.ddl.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-2.2.update.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-3.3.query.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.ddl.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.2.update.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.3.query.aql
A asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.adm
A asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.adm
M asterix-app/src/test/resources/runtimets/testsuite.xml
M asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java
15 files changed, 518 insertions(+), 1 deletion(-)


  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/63/663/1

diff --git a/asterix-app/data/csv/55.csv b/asterix-app/data/csv/55.csv
new file mode 100644
index 0000000..d154dc6
--- /dev/null
+++ b/asterix-app/data/csv/55.csv
@@ -0,0 +1,55 @@
+11,55,20140108,1,11:45:06,90,0.666666667
+12,55,20140108,1,11:45:07,90,0.666666667
+13,55,20140108,1,11:45:08,90,0.666666667
+14,55,20140108,1,11:45:08,90,0.666666667
+15,55,20140108,1,11:45:09,90,0.666666667
+16,55,20140108,1,11:45:10,90,0.666666667
+17,55,20140108,1,11:45:10,90,0.666666667
+18,55,20140108,1,11:45:11,90,0.666666667
+19,55,20140108,1,11:45:12,89,0.674157303
+20,55,20140108,1,11:45:12,90,0.666666667
+21,55,20140108,1,11:45:13,87,0.689655172
+22,55,20140108,1,11:45:14,86,0.697674419
+23,55,20140108,1,11:45:14,87,0.689655172
+24,55,20140108,1,11:45:15,92,0.652173913
+25,55,20140108,1,11:45:16,94,0.638297872
+26,55,20140108,1,11:45:16,97,0.618556701
+27,55,20140108,1,11:45:17,97,0.618556701
+28,55,20140108,1,11:45:17,95,0.631578947
+29,55,20140108,1,11:45:18,97,0.618556701
+30,55,20140108,1,11:45:19,99,0.606060606
+31,55,20140108,1,11:45:19,101,0.594059406
+32,55,20140108,1,11:45:20,103,0.582524272
+33,55,20140108,1,11:45:21,110,0.545454545
+34,55,20140108,1,11:45:21,104,0.576923077
+35,55,20140108,1,11:45:22,110,0.545454545
+36,55,20140108,1,11:45:22,117,0.512820513
+37,55,20140108,1,11:45:23,121,0.495867769
+38,55,20140108,1,11:45:23,125,0.48
+39,55,20140108,1,11:45:24,124,0.483870968
+40,55,20140108,1,11:45:24,123,0.487804878
+41,55,20140108,1,11:45:25,122,0.491803279
+42,55,20140108,1,11:45:25,121,0.495867769
+43,55,20140108,1,11:45:26,120,0.5
+44,55,20140108,1,11:45:26,119,0.504201681
+45,55,20140108,1,11:45:27,118,0.508474576
+46,55,20140108,1,11:45:27,117,0.512820513
+47,55,20140108,1,11:45:28,116,0.517241379
+48,55,20140108,1,11:45:28,115,0.52173913
+49,55,20140108,1,11:45:29,113,0.530973451
+50,55,20140108,1,11:45:29,113,0.530973451
+51,55,20140108,1,11:45:30,112,0.535714286
+52,55,20140108,1,11:45:30,111,0.540540541
+53,55,20140108,1,11:45:31,117,0.512820513
+54,55,20140108,1,11:45:31,115,0.52173913
+55,55,20140108,1,11:45:32,114,0.526315789
+56,55,20140108,1,11:45:32,113,0.530973451
+57,55,20140108,1,11:45:33,112,0.535714286
+58,55,20140108,1,11:45:33,105,0.571428571
+59,55,20140108,1,11:45:34,103,0.582524272
+60,55,20140108,1,11:45:35,103,0.582524272
+61,55,20140108,1,11:45:35,103,0.582524272
+62,55,20140108,1,11:45:36,103,0.582524272
+63,55,20140108,1,11:45:36,103,0.582524272
+64,55,20140108,1,11:45:37,103,0.582524272
+65,55,20140108,1,11:45:38,103,0.582524272
diff --git a/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-1.aql b/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-1.aql
new file mode 100644
index 0000000..2f56f85
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-1.aql
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+
+declare function median($x)
+{
+   let $c := count($x)
+   return avg(
+                for $i at $p in (for $j in $x order by $j return $j)
+                where $p >= floor(($c-1)/2.0) and $p <= $c/2
+                return $i
+             )
+}
+
+
+declare function MAD($x)
+{
+     let $m := median($x)
+     return median(for $j in $x order by abs($j-$m) return abs($j-$m))
+}
+
+MAD([2, 3, 4, 1, 5, 6, 10, 0])
+
diff --git a/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-2.aql b/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-2.aql
new file mode 100644
index 0000000..43238b0
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-2.aql
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type HRMType as closed {
+  row_id: int32,
+  sid: int32,
+  date: date,
+  day: int32,
+  time: time,
+  bpm: int32,
+  RR: float
+};
+
+create dataset HRM(HRMType) primary key row_id;
+
+declare function median($x)
+{
+     let $c := count($x)
+     return avg(
+                for $i at $p in (for $j in $x order by $j return $j)
+                where $p >= floor(($c-1)/2.0) and $p <= $c/2
+                return $i
+               )
+}
+
+
+declare function MAD($x)
+{
+     let $m := median($x)
+     return median(for $j in $x order by abs($j-$m) return abs($j-$m))
+}
+
+for $i in dataset HRM
+group by $sid := $i.sid, $gdate := $i.date, $gday := $i.day,
+$timebin := interval-bin($i.time, time("00:00:00"), day-time-duration("PT15M")) with $i
+return
+{ 
+  "sid": $sid,
+  "gdate": $gdate,
+  "gday": $gday,
+  "timebin": $timebin,
+  "stdv": (avg(for $ii in $i return $ii.RR * $ii.RR) - 
+          avg(for $ii in $i return $ii.RR) * avg(for $ii in $i return $ii.RR))^(0.5),
+  "MAD": MAD(for $ii in $i return $ii.RR)
+}
+
diff --git a/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
new file mode 100644
index 0000000..ebbee79
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
@@ -0,0 +1,45 @@
+-- DISTRIBUTE_RESULT  |LOCAL|
+  -- ONE_TO_ONE_EXCHANGE  |LOCAL|
+    -- AGGREGATE  |LOCAL|
+      -- AGGREGATE  |LOCAL|
+        -- STREAM_PROJECT  |UNPARTITIONED|
+          -- STREAM_SELECT  |UNPARTITIONED|
+            -- STREAM_PROJECT  |UNPARTITIONED|
+              -- ASSIGN  |UNPARTITIONED|
+                -- STREAM_PROJECT  |UNPARTITIONED|
+                  -- UNNEST  |UNPARTITIONED|
+                    -- SUBPLAN  |UNPARTITIONED|
+                            {
+                              -- AGGREGATE  |UNPARTITIONED|
+                                -- IN_MEMORY_STABLE_SORT [$$28(ASC)]  |UNPARTITIONED|
+                                  -- UNNEST  |UNPARTITIONED|
+                                    -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                            }
+                      -- AGGREGATE  |UNPARTITIONED|
+                        -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
+                          -- STABLE_SORT [$$59(ASC)]  |UNPARTITIONED|
+                            -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
+                              -- STREAM_PROJECT  |UNPARTITIONED|
+                                -- ASSIGN  |UNPARTITIONED|
+                                  -- STREAM_PROJECT  |UNPARTITIONED|
+                                    -- UNNEST  |UNPARTITIONED|
+                                      -- SUBPLAN  |UNPARTITIONED|
+                                              {
+                                                -- SUBPLAN  |UNPARTITIONED|
+                                                        {
+                                                          -- AGGREGATE  |UNPARTITIONED|
+                                                            -- STREAM_SELECT  |UNPARTITIONED|
+                                                              -- UNNEST  |UNPARTITIONED|
+                                                                -- SUBPLAN  |UNPARTITIONED|
+                                                                        {
+                                                                          -- AGGREGATE  |UNPARTITIONED|
+                                                                            -- IN_MEMORY_STABLE_SORT [$$21(ASC)]  |UNPARTITIONED|
+                                                                              -- UNNEST  |UNPARTITIONED|
+                                                                                -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                                                                        }
+                                                                  -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                                                        }
+                                                  -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                                              }
+                                        -- ASSIGN  |UNPARTITIONED|
+                                          -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
diff --git a/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
new file mode 100644
index 0000000..a6e7496
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
@@ -0,0 +1,75 @@
+-- DISTRIBUTE_RESULT  |PARTITIONED|
+  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+    -- STREAM_PROJECT  |PARTITIONED|
+      -- ASSIGN  |PARTITIONED|
+        -- STREAM_PROJECT  |PARTITIONED|
+          -- SUBPLAN  |PARTITIONED|
+                  {
+                    -- SUBPLAN  |LOCAL|
+                            {
+                              -- SUBPLAN  |LOCAL|
+                                      {
+                                        -- SUBPLAN  |LOCAL|
+                                                {
+                                                  -- AGGREGATE  |LOCAL|
+                                                    -- STREAM_SELECT  |LOCAL|
+                                                      -- ASSIGN  |LOCAL|
+                                                        -- UNNEST  |LOCAL|
+                                                          -- SUBPLAN  |LOCAL|
+                                                                  {
+                                                                    -- AGGREGATE  |LOCAL|
+                                                                      -- IN_MEMORY_STABLE_SORT [$$38(ASC)]  |LOCAL|
+                                                                        -- UNNEST  |LOCAL|
+                                                                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                                                  }
+                                                            -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                                }
+                                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                      }
+                                -- SUBPLAN  |LOCAL|
+                                        {
+                                          -- AGGREGATE  |LOCAL|
+                                            -- IN_MEMORY_STABLE_SORT [$$98(ASC)]  |LOCAL|
+                                              -- ASSIGN  |LOCAL|
+                                                -- UNNEST  |LOCAL|
+                                                  -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                        }
+                                  -- NESTED_TUPLE_SOURCE  |LOCAL|
+                            }
+                      -- SUBPLAN  |LOCAL|
+                              {
+                                -- SUBPLAN  |LOCAL|
+                                        {
+                                          -- AGGREGATE  |LOCAL|
+                                            -- STREAM_SELECT  |LOCAL|
+                                              -- ASSIGN  |LOCAL|
+                                                -- UNNEST  |LOCAL|
+                                                  -- SUBPLAN  |LOCAL|
+                                                          {
+                                                            -- AGGREGATE  |LOCAL|
+                                                              -- IN_MEMORY_STABLE_SORT [$$31(ASC)]  |LOCAL|
+                                                                -- UNNEST  |LOCAL|
+                                                                  -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                                          }
+                                                    -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                        }
+                                  -- NESTED_TUPLE_SOURCE  |LOCAL|
+                              }
+                        -- NESTED_TUPLE_SOURCE  |LOCAL|
+                  }
+            -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+              -- PRE_CLUSTERED_GROUP_BY[$$94, $$95, $$96, $$97]  |PARTITIONED|
+                      {
+                        -- AGGREGATE  |LOCAL|
+                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                      }
+                -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                  -- STABLE_SORT [$$94(ASC), $$95(ASC), $$96(ASC), $$97(ASC)]  |PARTITIONED|
+                    -- HASH_PARTITION_EXCHANGE [$$94, $$95, $$96, $$97]  |PARTITIONED|
+                      -- STREAM_PROJECT  |PARTITIONED|
+                        -- ASSIGN  |PARTITIONED|
+                          -- STREAM_PROJECT  |PARTITIONED|
+                            -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                              -- DATASOURCE_SCAN  |PARTITIONED|
+                                -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                  -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.ddl.aql
new file mode 100644
index 0000000..042f3ce
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.ddl.aql
@@ -0,0 +1,18 @@
+/*
+ * 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.
+ */
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-2.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-2.2.update.aql
new file mode 100644
index 0000000..042f3ce
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-2.2.update.aql
@@ -0,0 +1,18 @@
+/*
+ * 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.
+ */
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-3.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-3.3.query.aql
new file mode 100644
index 0000000..9f7b9ec
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-3.3.query.aql
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+
+declare function median($x)
+{
+   let $c := count($x)
+   return avg(
+                for $i at $p in (for $j in $x order by $j return $j)
+                where $p-1 >= floor(($c-1)/2.0) and $p-1<= $c/2
+                return $i
+             )
+}
+
+
+declare function MAD($x)
+{
+     let $m := median($x)
+     return median(for $j in $x order by abs($j-$m) return abs($j-$m))
+}
+
+
+median([2.0, 3.0, 4.0, 1.0, 5.0, 6.0, 10.0, 0.0])
+median([2.0, 3.0, 4.0, 1.0, 5.0, 6.0, 10.0])
+
+MAD([2.0, 3.0, 4.0, 1.0, 5.0, 6.0, 10.0, 0.0])
+MAD([2.0, 3.0, 4.0, 1.0, 5.0, 6.0, 10.0])
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.ddl.aql
new file mode 100644
index 0000000..6c4ede7
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.ddl.aql
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type HRMType_Raw as closed {
+  row_id: int32,
+  sid: int32,
+  date: string,
+  day: int32,
+  time: string,
+  bpm: int32,
+  RR: float
+};
+
+create dataset HRM_Raw(HRMType_Raw) primary key row_id;
+
+
+create type HRMType as closed {
+  row_id: int32,
+  sid: int32,
+  date: date,
+  day: int32,
+  time: time,
+  bpm: int32,
+  RR: float
+};
+
+create dataset HRM(HRMType) primary key row_id;
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.2.update.aql
new file mode 100644
index 0000000..20f47ee
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.2.update.aql
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+
+use dataverse test;
+
+load dataset HRM_Raw using localfs 
+    (("path"="asterix_nc1://data/csv/55.csv"), 
+    ("format"="delimited-text"), 
+    ("header"="true"));
+
+insert into dataset HRM
+(
+    for $t in dataset HRM_Raw
+    return {
+      "row_id": $t.row_id,
+      "sid" : $t.sid,
+      "date" : parse-date($t.date, "YMD"),
+      "time" : parse-time($t.time, "h:m:s"),
+      "day" : $t.day,
+      "bpm" : $t.bpm,
+      "RR" : $t.RR
+})
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.3.query.aql
new file mode 100644
index 0000000..237ba30
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.3.query.aql
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+use dataverse test;
+
+declare function median($x)
+{
+     let $c := count($x)
+     return avg(
+                for $i at $p in (for $j in $x order by $j return $j)
+                where $p >= floor(($c-1)/2.0) and $p <= $c/2
+                return $i
+               )
+}
+
+
+declare function MAD($x)
+{
+     let $m := median($x)
+     return median(for $j in $x order by abs($j-$m) return abs($j-$m))
+}
+
+
+for $i in dataset HRM
+group by $sid := $i.sid, $gdate := $i.date, $gday := $i.day,
+$timebin := interval-bin($i.time, time("00:00:00"), day-time-duration("PT15M")) with $i
+return
+{ 
+  "sid": $sid,
+  "gdate": $gdate,
+  "gday": $gday,
+  "timebin": $timebin,
+  "stdv": (avg(for $ii in $i return $ii.RR * $ii.RR) - 
+          avg(for $ii in $i return $ii.RR) * avg(for $ii in $i return $ii.RR))^(0.5),
+  "MAD": MAD(for $ii in $i return $ii.RR)
+}
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.adm b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.adm
new file mode 100644
index 0000000..c867567
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.adm
@@ -0,0 +1,4 @@
+3.5d
+4.0d
+2.0d
+2.0d
diff --git a/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.adm b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.adm
new file mode 100644
index 0000000..94ebaf9
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.adm
@@ -0,0 +1,4 @@
+1
+2
+3
+4
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index a3a1fba..02e486e 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -6043,6 +6043,16 @@
                 <output-dir compare="Text">query-ASTERIXDB-1298</output-dir>
             </compilation-unit>
         </test-case>
+        <test-case FilePath="user-defined-functions">
+            <compilation-unit name="query-ASTERIXDB-1308-1">
+                <output-dir compare="Text">query-ASTERIXDB-1308-1</output-dir>
+            </compilation-unit>
+        </test-case>
+        <!-- <test-case FilePath="user-defined-functions">
+            <compilation-unit name="query-ASTERIXDB-1308-2">
+                <output-dir compare="Text">query-ASTERIXDB-1308-2</output-dir>
+            </compilation-unit>
+        </test-case>  -->
         <!-- This test case is not valid anymore since we do not required "IMPORT_PRIVATE_FUNCTIONS" flag anymore -->
         <!-- <test-case FilePath="user-defined-functions">
             <compilation-unit name="invoke-private-function">
diff --git a/asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java b/asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java
index da50463..c736d83 100644
--- a/asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java
+++ b/asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java
@@ -57,11 +57,13 @@
         resultEnv.removeSubstitution(varExpr);
 
         VariableExpr posVarExpr = null;
+        VariableExpr newPosVarExpr = null;
         if (fc.hasPosVar()) {
             posVarExpr = fc.getPosVarExpr();
+            newPosVarExpr = generateNewVariable(context, posVarExpr);
             resultEnv.removeSubstitution(posVarExpr);
         }
-        ForClause newFor = new ForClause(newVe, (Expression) p1.first, posVarExpr);
+        ForClause newFor = new ForClause(newVe, (Expression) p1.first, newPosVarExpr);
         return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newFor, resultEnv);
     }
 

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


Patch Set 2:

Build Started https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-topic/743/

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-HasComments: No

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Yingyi Bu (Code Review)" <do...@asterixdb.incubator.apache.org>.
Hello Till Westmann, Jenkins,

I'd like you to reexamine a change.  Please visit

    https://asterix-gerrit.ics.uci.edu/663

to look at the new patch set (#3).

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................

ASTERIXDB-1322: fix the AQL translator and add tests.

Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
---
A asterix-app/data/csv/55.csv
A asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-1.aql
A asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-2.aql
A asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
A asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.ddl.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-2.2.update.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-3.3.query.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.ddl.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.2.update.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.3.query.aql
A asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.adm
M asterix-app/src/test/resources/runtimets/testsuite.xml
M asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java
14 files changed, 515 insertions(+), 3 deletions(-)


  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/63/663/3
-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 3
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Yingyi Bu <bu...@gmail.com>

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Till Westmann (Code Review)" <do...@asterixdb.incubator.apache.org>.
Till Westmann has posted comments on this change.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


Patch Set 2: Code-Review+2

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-HasComments: No

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


Patch Set 3: -Verified

Build Started https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-notopic/896/

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 3
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Yingyi Bu <bu...@gmail.com>
Gerrit-HasComments: No

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


Patch Set 2: Verified+1

Build Successful 

https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-topic/743/ : SUCCESS

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-HasComments: No

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Till Westmann (Code Review)" <do...@asterixdb.incubator.apache.org>.
Till Westmann has posted comments on this change.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


Patch Set 3: Code-Review+2

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 3
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Yingyi Bu <bu...@gmail.com>
Gerrit-HasComments: No

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


Patch Set 3: Verified+1

Build Successful 

https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-notopic/896/ : SUCCESS

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 3
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Yingyi Bu <bu...@gmail.com>
Gerrit-HasComments: No

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Yingyi Bu (Code Review)" <do...@asterixdb.incubator.apache.org>.
Yingyi Bu has uploaded a new patch set (#2).

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................

ASTERIXDB-1322: fix the AQL translator and add tests.

Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
---
A asterix-app/data/csv/55.csv
A asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-1.aql
A asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-2.aql
A asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
A asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.ddl.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-2.2.update.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-3.3.query.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.ddl.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.2.update.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.3.query.aql
A asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.adm
M asterix-app/src/test/resources/runtimets/testsuite.xml
M asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java
14 files changed, 515 insertions(+), 3 deletions(-)


  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/63/663/2
-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 2
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


Patch Set 1: Verified-1

Build Unstable 

https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-notopic/874/ : UNSTABLE

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-HasComments: No

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Yingyi Bu (Code Review)" <do...@asterixdb.incubator.apache.org>.
Yingyi Bu has submitted this change and it was merged.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


ASTERIXDB-1322: fix the AQL translator and add tests.

Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Reviewed-on: https://asterix-gerrit.ics.uci.edu/663
Tested-by: Jenkins <je...@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <ti...@apache.org>
---
A asterix-app/data/csv/55.csv
A asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-1.aql
A asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-2.aql
A asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
A asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.ddl.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-2.2.update.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-3.3.query.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.ddl.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.2.update.aql
A asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.3.query.aql
A asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.adm
M asterix-app/src/test/resources/runtimets/testsuite.xml
M asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java
14 files changed, 515 insertions(+), 3 deletions(-)

Approvals:
  Till Westmann: Looks good to me, approved
  Jenkins: Verified



diff --git a/asterix-app/data/csv/55.csv b/asterix-app/data/csv/55.csv
new file mode 100644
index 0000000..d154dc6
--- /dev/null
+++ b/asterix-app/data/csv/55.csv
@@ -0,0 +1,55 @@
+11,55,20140108,1,11:45:06,90,0.666666667
+12,55,20140108,1,11:45:07,90,0.666666667
+13,55,20140108,1,11:45:08,90,0.666666667
+14,55,20140108,1,11:45:08,90,0.666666667
+15,55,20140108,1,11:45:09,90,0.666666667
+16,55,20140108,1,11:45:10,90,0.666666667
+17,55,20140108,1,11:45:10,90,0.666666667
+18,55,20140108,1,11:45:11,90,0.666666667
+19,55,20140108,1,11:45:12,89,0.674157303
+20,55,20140108,1,11:45:12,90,0.666666667
+21,55,20140108,1,11:45:13,87,0.689655172
+22,55,20140108,1,11:45:14,86,0.697674419
+23,55,20140108,1,11:45:14,87,0.689655172
+24,55,20140108,1,11:45:15,92,0.652173913
+25,55,20140108,1,11:45:16,94,0.638297872
+26,55,20140108,1,11:45:16,97,0.618556701
+27,55,20140108,1,11:45:17,97,0.618556701
+28,55,20140108,1,11:45:17,95,0.631578947
+29,55,20140108,1,11:45:18,97,0.618556701
+30,55,20140108,1,11:45:19,99,0.606060606
+31,55,20140108,1,11:45:19,101,0.594059406
+32,55,20140108,1,11:45:20,103,0.582524272
+33,55,20140108,1,11:45:21,110,0.545454545
+34,55,20140108,1,11:45:21,104,0.576923077
+35,55,20140108,1,11:45:22,110,0.545454545
+36,55,20140108,1,11:45:22,117,0.512820513
+37,55,20140108,1,11:45:23,121,0.495867769
+38,55,20140108,1,11:45:23,125,0.48
+39,55,20140108,1,11:45:24,124,0.483870968
+40,55,20140108,1,11:45:24,123,0.487804878
+41,55,20140108,1,11:45:25,122,0.491803279
+42,55,20140108,1,11:45:25,121,0.495867769
+43,55,20140108,1,11:45:26,120,0.5
+44,55,20140108,1,11:45:26,119,0.504201681
+45,55,20140108,1,11:45:27,118,0.508474576
+46,55,20140108,1,11:45:27,117,0.512820513
+47,55,20140108,1,11:45:28,116,0.517241379
+48,55,20140108,1,11:45:28,115,0.52173913
+49,55,20140108,1,11:45:29,113,0.530973451
+50,55,20140108,1,11:45:29,113,0.530973451
+51,55,20140108,1,11:45:30,112,0.535714286
+52,55,20140108,1,11:45:30,111,0.540540541
+53,55,20140108,1,11:45:31,117,0.512820513
+54,55,20140108,1,11:45:31,115,0.52173913
+55,55,20140108,1,11:45:32,114,0.526315789
+56,55,20140108,1,11:45:32,113,0.530973451
+57,55,20140108,1,11:45:33,112,0.535714286
+58,55,20140108,1,11:45:33,105,0.571428571
+59,55,20140108,1,11:45:34,103,0.582524272
+60,55,20140108,1,11:45:35,103,0.582524272
+61,55,20140108,1,11:45:35,103,0.582524272
+62,55,20140108,1,11:45:36,103,0.582524272
+63,55,20140108,1,11:45:36,103,0.582524272
+64,55,20140108,1,11:45:37,103,0.582524272
+65,55,20140108,1,11:45:38,103,0.582524272
diff --git a/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-1.aql b/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-1.aql
new file mode 100644
index 0000000..2f56f85
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-1.aql
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+
+declare function median($x)
+{
+   let $c := count($x)
+   return avg(
+                for $i at $p in (for $j in $x order by $j return $j)
+                where $p >= floor(($c-1)/2.0) and $p <= $c/2
+                return $i
+             )
+}
+
+
+declare function MAD($x)
+{
+     let $m := median($x)
+     return median(for $j in $x order by abs($j-$m) return abs($j-$m))
+}
+
+MAD([2, 3, 4, 1, 5, 6, 10, 0])
+
diff --git a/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-2.aql b/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-2.aql
new file mode 100644
index 0000000..028e436
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/udfs/query-ASTERIXDB-1308-2.aql
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type HRMType as closed {
+  row_id: int32,
+  sid: int32,
+  date: date,
+  day: int32,
+  time: time,
+  bpm: int32,
+  RR: float
+};
+
+create dataset HRM(HRMType) primary key row_id;
+
+declare function median($x)
+{
+     let $c := count($x)
+     return avg(
+                for $i at $p in (for $j in $x order by $j return $j)
+                where $p >= floor(($c-1)/2.0) and $p <= $c/2
+                return $i
+               )
+}
+
+
+declare function MAD($x)
+{
+     let $m := median($x)
+     return median(for $j in $x order by abs($j-$m) return abs($j-$m))
+}
+
+for $i in dataset HRM
+group by $sid := $i.sid, $gdate := $i.date, $gday := $i.day,
+$timebin := interval-bin($i.time, time("00:00:00"), day-time-duration("PT15M")) with $i
+return
+{
+  "sid": $sid,
+  "gdate": $gdate,
+  "gday": $gday,
+  "timebin": $timebin,
+  "stdv": (avg(for $ii in $i return $ii.RR * $ii.RR) -
+          avg(for $ii in $i return $ii.RR) * avg(for $ii in $i return $ii.RR))^(0.5),
+  "MAD": MAD(for $ii in $i return $ii.RR)
+}
+
diff --git a/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
new file mode 100644
index 0000000..ebbee79
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
@@ -0,0 +1,45 @@
+-- DISTRIBUTE_RESULT  |LOCAL|
+  -- ONE_TO_ONE_EXCHANGE  |LOCAL|
+    -- AGGREGATE  |LOCAL|
+      -- AGGREGATE  |LOCAL|
+        -- STREAM_PROJECT  |UNPARTITIONED|
+          -- STREAM_SELECT  |UNPARTITIONED|
+            -- STREAM_PROJECT  |UNPARTITIONED|
+              -- ASSIGN  |UNPARTITIONED|
+                -- STREAM_PROJECT  |UNPARTITIONED|
+                  -- UNNEST  |UNPARTITIONED|
+                    -- SUBPLAN  |UNPARTITIONED|
+                            {
+                              -- AGGREGATE  |UNPARTITIONED|
+                                -- IN_MEMORY_STABLE_SORT [$$28(ASC)]  |UNPARTITIONED|
+                                  -- UNNEST  |UNPARTITIONED|
+                                    -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                            }
+                      -- AGGREGATE  |UNPARTITIONED|
+                        -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
+                          -- STABLE_SORT [$$59(ASC)]  |UNPARTITIONED|
+                            -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
+                              -- STREAM_PROJECT  |UNPARTITIONED|
+                                -- ASSIGN  |UNPARTITIONED|
+                                  -- STREAM_PROJECT  |UNPARTITIONED|
+                                    -- UNNEST  |UNPARTITIONED|
+                                      -- SUBPLAN  |UNPARTITIONED|
+                                              {
+                                                -- SUBPLAN  |UNPARTITIONED|
+                                                        {
+                                                          -- AGGREGATE  |UNPARTITIONED|
+                                                            -- STREAM_SELECT  |UNPARTITIONED|
+                                                              -- UNNEST  |UNPARTITIONED|
+                                                                -- SUBPLAN  |UNPARTITIONED|
+                                                                        {
+                                                                          -- AGGREGATE  |UNPARTITIONED|
+                                                                            -- IN_MEMORY_STABLE_SORT [$$21(ASC)]  |UNPARTITIONED|
+                                                                              -- UNNEST  |UNPARTITIONED|
+                                                                                -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                                                                        }
+                                                                  -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                                                        }
+                                                  -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                                              }
+                                        -- ASSIGN  |UNPARTITIONED|
+                                          -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
diff --git a/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
new file mode 100644
index 0000000..a6e7496
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
@@ -0,0 +1,75 @@
+-- DISTRIBUTE_RESULT  |PARTITIONED|
+  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+    -- STREAM_PROJECT  |PARTITIONED|
+      -- ASSIGN  |PARTITIONED|
+        -- STREAM_PROJECT  |PARTITIONED|
+          -- SUBPLAN  |PARTITIONED|
+                  {
+                    -- SUBPLAN  |LOCAL|
+                            {
+                              -- SUBPLAN  |LOCAL|
+                                      {
+                                        -- SUBPLAN  |LOCAL|
+                                                {
+                                                  -- AGGREGATE  |LOCAL|
+                                                    -- STREAM_SELECT  |LOCAL|
+                                                      -- ASSIGN  |LOCAL|
+                                                        -- UNNEST  |LOCAL|
+                                                          -- SUBPLAN  |LOCAL|
+                                                                  {
+                                                                    -- AGGREGATE  |LOCAL|
+                                                                      -- IN_MEMORY_STABLE_SORT [$$38(ASC)]  |LOCAL|
+                                                                        -- UNNEST  |LOCAL|
+                                                                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                                                  }
+                                                            -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                                }
+                                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                      }
+                                -- SUBPLAN  |LOCAL|
+                                        {
+                                          -- AGGREGATE  |LOCAL|
+                                            -- IN_MEMORY_STABLE_SORT [$$98(ASC)]  |LOCAL|
+                                              -- ASSIGN  |LOCAL|
+                                                -- UNNEST  |LOCAL|
+                                                  -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                        }
+                                  -- NESTED_TUPLE_SOURCE  |LOCAL|
+                            }
+                      -- SUBPLAN  |LOCAL|
+                              {
+                                -- SUBPLAN  |LOCAL|
+                                        {
+                                          -- AGGREGATE  |LOCAL|
+                                            -- STREAM_SELECT  |LOCAL|
+                                              -- ASSIGN  |LOCAL|
+                                                -- UNNEST  |LOCAL|
+                                                  -- SUBPLAN  |LOCAL|
+                                                          {
+                                                            -- AGGREGATE  |LOCAL|
+                                                              -- IN_MEMORY_STABLE_SORT [$$31(ASC)]  |LOCAL|
+                                                                -- UNNEST  |LOCAL|
+                                                                  -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                                          }
+                                                    -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                        }
+                                  -- NESTED_TUPLE_SOURCE  |LOCAL|
+                              }
+                        -- NESTED_TUPLE_SOURCE  |LOCAL|
+                  }
+            -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+              -- PRE_CLUSTERED_GROUP_BY[$$94, $$95, $$96, $$97]  |PARTITIONED|
+                      {
+                        -- AGGREGATE  |LOCAL|
+                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                      }
+                -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                  -- STABLE_SORT [$$94(ASC), $$95(ASC), $$96(ASC), $$97(ASC)]  |PARTITIONED|
+                    -- HASH_PARTITION_EXCHANGE [$$94, $$95, $$96, $$97]  |PARTITIONED|
+                      -- STREAM_PROJECT  |PARTITIONED|
+                        -- ASSIGN  |PARTITIONED|
+                          -- STREAM_PROJECT  |PARTITIONED|
+                            -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                              -- DATASOURCE_SCAN  |PARTITIONED|
+                                -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                  -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.ddl.aql
new file mode 100644
index 0000000..042f3ce
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.ddl.aql
@@ -0,0 +1,18 @@
+/*
+ * 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.
+ */
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-2.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-2.2.update.aql
new file mode 100644
index 0000000..042f3ce
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-2.2.update.aql
@@ -0,0 +1,18 @@
+/*
+ * 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.
+ */
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-3.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-3.3.query.aql
new file mode 100644
index 0000000..9f7b9ec
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-3.3.query.aql
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+
+declare function median($x)
+{
+   let $c := count($x)
+   return avg(
+                for $i at $p in (for $j in $x order by $j return $j)
+                where $p-1 >= floor(($c-1)/2.0) and $p-1<= $c/2
+                return $i
+             )
+}
+
+
+declare function MAD($x)
+{
+     let $m := median($x)
+     return median(for $j in $x order by abs($j-$m) return abs($j-$m))
+}
+
+
+median([2.0, 3.0, 4.0, 1.0, 5.0, 6.0, 10.0, 0.0])
+median([2.0, 3.0, 4.0, 1.0, 5.0, 6.0, 10.0])
+
+MAD([2.0, 3.0, 4.0, 1.0, 5.0, 6.0, 10.0, 0.0])
+MAD([2.0, 3.0, 4.0, 1.0, 5.0, 6.0, 10.0])
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.ddl.aql
new file mode 100644
index 0000000..6c4ede7
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308-2.1.ddl.aql
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type HRMType_Raw as closed {
+  row_id: int32,
+  sid: int32,
+  date: string,
+  day: int32,
+  time: string,
+  bpm: int32,
+  RR: float
+};
+
+create dataset HRM_Raw(HRMType_Raw) primary key row_id;
+
+
+create type HRMType as closed {
+  row_id: int32,
+  sid: int32,
+  date: date,
+  day: int32,
+  time: time,
+  bpm: int32,
+  RR: float
+};
+
+create dataset HRM(HRMType) primary key row_id;
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.2.update.aql
new file mode 100644
index 0000000..e97b903
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.2.update.aql
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+
+use dataverse test;
+
+load dataset HRM_Raw using localfs
+    (("path"="asterix_nc1://data/csv/55.csv"),
+    ("format"="delimited-text"),
+    ("header"="true"));
+
+insert into dataset HRM
+(
+    for $t in dataset HRM_Raw
+    return {
+      "row_id": $t.row_id,
+      "sid" : $t.sid,
+      "date" : parse-date($t.date, "YMD"),
+      "time" : parse-time($t.time, "h:m:s"),
+      "day" : $t.day,
+      "bpm" : $t.bpm,
+      "RR" : $t.RR
+})
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.3.query.aql
new file mode 100644
index 0000000..2d1ba32
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-ASTERIXDB-1308-2/query-ASTERIXDB-1308.3.query.aql
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+use dataverse test;
+
+declare function median($x)
+{
+     let $c := count($x)
+     return avg(
+                for $i at $p in (for $j in $x order by $j return $j)
+                where $p >= floor(($c-1)/2.0) and $p <= $c/2
+                return $i
+               )
+}
+
+
+declare function MAD($x)
+{
+     let $m := median($x)
+     return median(for $j in $x order by abs($j-$m) return abs($j-$m))
+}
+
+
+for $i in dataset HRM
+group by $sid := $i.sid, $gdate := $i.date, $gday := $i.day,
+$timebin := interval-bin($i.time, time("00:00:00"), day-time-duration("PT15M")) with $i
+return
+{
+  "sid": $sid,
+  "gdate": $gdate,
+  "gday": $gday,
+  "timebin": $timebin,
+  "stdv": (avg(for $ii in $i return $ii.RR * $ii.RR) -
+          avg(for $ii in $i return $ii.RR) * avg(for $ii in $i return $ii.RR))^(0.5),
+  "MAD": MAD(for $ii in $i return $ii.RR)
+}
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.adm b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.adm
new file mode 100644
index 0000000..c867567
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-ASTERIXDB-1308-1/query-ASTERIXDB-1308-1.1.adm
@@ -0,0 +1,4 @@
+3.5d
+4.0d
+2.0d
+2.0d
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index 96a2c37..758c393 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -6053,6 +6053,16 @@
                 <output-dir compare="Text">query-ASTERIXDB-1298</output-dir>
             </compilation-unit>
         </test-case>
+        <test-case FilePath="user-defined-functions">
+            <compilation-unit name="query-ASTERIXDB-1308-1">
+                <output-dir compare="Text">query-ASTERIXDB-1308-1</output-dir>
+            </compilation-unit>
+        </test-case>
+        <!-- <test-case FilePath="user-defined-functions">
+            <compilation-unit name="query-ASTERIXDB-1308-2">
+                <output-dir compare="Text">query-ASTERIXDB-1308-2</output-dir>
+            </compilation-unit>
+        </test-case>  -->
         <!-- This test case is not valid anymore since we do not required "IMPORT_PRIVATE_FUNCTIONS" flag anymore -->
         <!-- <test-case FilePath="user-defined-functions">
             <compilation-unit name="invoke-private-function">
diff --git a/asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java b/asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java
index da50463..3352125 100644
--- a/asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java
+++ b/asterix-lang-aql/src/main/java/org/apache/asterix/lang/aql/visitor/AQLCloneAndSubstituteVariablesVisitor.java
@@ -56,12 +56,13 @@
         VariableSubstitutionEnvironment resultEnv = new VariableSubstitutionEnvironment(env);
         resultEnv.removeSubstitution(varExpr);
 
-        VariableExpr posVarExpr = null;
+        VariableExpr newPosVarExpr = null;
         if (fc.hasPosVar()) {
-            posVarExpr = fc.getPosVarExpr();
+            VariableExpr posVarExpr = fc.getPosVarExpr();
+            newPosVarExpr = generateNewVariable(context, posVarExpr);
             resultEnv.removeSubstitution(posVarExpr);
         }
-        ForClause newFor = new ForClause(newVe, (Expression) p1.first, posVarExpr);
+        ForClause newFor = new ForClause(newVe, (Expression) p1.first, newPosVarExpr);
         return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newFor, resultEnv);
     }
 

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 4
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Yingyi Bu <bu...@gmail.com>

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


Patch Set 3: Verified-1

Build Failed 

https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-topic/749/ : FAILURE

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 3
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Yingyi Bu <bu...@gmail.com>
Gerrit-HasComments: No

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


Patch Set 1:

Build Started https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-notopic/874/

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-HasComments: No

Change in asterixdb[master]: ASTERIXDB-1322: fix the AQL translator and add tests.

Posted by "Jenkins (Code Review)" <do...@asterixdb.incubator.apache.org>.
Jenkins has posted comments on this change.

Change subject: ASTERIXDB-1322: fix the AQL translator and add tests.
......................................................................


Patch Set 3:

Build Started https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-topic/749/

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/663
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I994bb69ca989c8c9e2ce417d62739a859e5ae94b
Gerrit-PatchSet: 3
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <bu...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: Yingyi Bu <bu...@gmail.com>
Gerrit-HasComments: No