DERBY-7041: Do not create persistent dependencies between views and the system-supplied statistical aggregates; commit derby-7041-01-aa-omitDependencyOnSystemSuppliedAggregate.diff.

git-svn-id: https://svn.apache.org/repos/asf/db/derby/code/trunk@1856730 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/java/org.apache.derby.engine/org/apache/derby/impl/sql/compile/AggregateNode.java b/java/org.apache.derby.engine/org/apache/derby/impl/sql/compile/AggregateNode.java
index 7717442..2bc9cc6 100644
--- a/java/org.apache.derby.engine/org/apache/derby/impl/sql/compile/AggregateNode.java
+++ b/java/org.apache.derby.engine/org/apache/derby/impl/sql/compile/AggregateNode.java
@@ -398,8 +398,12 @@
             }
 
             // set up dependency on the user-defined aggregate and compile a check for USAGE
-            // priv if needed
-            getCompilerContext().createDependency( ad );
+            // priv if needed. no need for a dependency if this is a builtin, system-supplied
+            // aggregate
+            if ( !isModernBuiltinAggregate )
+            {
+                getCompilerContext().createDependency( ad );
+            }
 
             if ( isPrivilegeCollectionRequired() )
             {
diff --git a/java/org.apache.derby.tests/org/apache/derbyTesting/functionTests/tests/lang/AggBuiltinTest.java b/java/org.apache.derby.tests/org/apache/derbyTesting/functionTests/tests/lang/AggBuiltinTest.java
index 502b2ef..c62ab36 100644
--- a/java/org.apache.derby.tests/org/apache/derbyTesting/functionTests/tests/lang/AggBuiltinTest.java
+++ b/java/org.apache.derby.tests/org/apache/derbyTesting/functionTests/tests/lang/AggBuiltinTest.java
@@ -80,6 +80,9 @@
             stddev_pop();
             // Standard deviation sample (n - 1)
         	stddev_samp();
+
+            // bugs
+            derby7041();
         	
         } finally {
             try {
@@ -94,6 +97,71 @@
         }
     }
 
+    /**
+     * Bug 7041 was an NPE raised when trying to record a persistent dependency
+     * from a view to a builtin aggregate which had been created with the user-defined
+     * aggregate machinery.
+     */
+    private void derby7041() throws SQLException
+    {
+        x
+            (
+             "CREATE TABLE T1_7041\n" +
+             "(\n" +
+             "	T1_7041_KEY INTEGER,\n" +
+             "	T1_7041_TAG INTEGER\n" +
+             ")\n"
+             );
+        x
+            (
+             "CREATE TABLE T2_7041\n" +
+             "(\n" +
+             "	T2_7041_KEY INTEGER\n" +
+             ")\n"
+             );
+        x
+            (
+             "CREATE TABLE T3_7041\n" +
+             "(\n" +
+             "	T3_7041_KEY1 INTEGER,\n" +
+             "	T3_7041_KEY2 INTEGER,\n" +
+             "	T3_7041_VAL INTEGER\n" +
+             ")\n"
+             );
+        x
+            (
+             "CREATE VIEW V1_7041 (T1_7041_TAG, VALUE) AS\n" +
+             "(\n" +
+             "  SELECT T1_7041_TAG, T3_7041_VAL AS VALUE\n" +
+             "  FROM T1_7041, T2_7041, T3_7041\n" +
+             "  WHERE\n" +
+             "    T1_7041_KEY = T3_7041_KEY2\n" +
+             "    AND T2_7041_KEY = T3_7041_KEY1\n" +
+             ")\n"
+             );
+        x
+            (
+             "CREATE VIEW V2_7041 AS\n" +
+             "(\n" +
+             "  SELECT\n" +
+             "    A.T1_7041_TAG AS A_T1_7041_TAG,\n" +
+             "    A.VALUE AS A_VALUE\n" +
+             "  FROM V1_7041 AS A, V1_7041 AS B\n" +
+             ")\n"
+             );
+        x
+            (
+             "CREATE VIEW V3_7041 (A_T1_7041_TAG, STD_DEV_A_VALUE) AS\n" +
+             "(\n" +
+             "  SELECT\n" +
+             "    A_T1_7041_TAG,\n" +
+             "    STDDEV_SAMP(A_VALUE) AS STD_DEV_A_VALUE\n" +
+             "  FROM V2_7041\n" +
+             "  GROUP BY A_T1_7041_TAG\n" +
+             ")\n"
+             );
+    }
+
 
     private void avg() throws SQLException {
         x("create table t (i int, s smallint, l bigint,"