You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2018/04/26 05:44:30 UTC

[2/3] calcite git commit: [CALCITE-2264] In JDBC adapter, do not push down a call to a user-defined function (UDF) (Piotr Bojko)

[CALCITE-2264] In JDBC adapter, do not push down a call to a user-defined function (UDF) (Piotr Bojko)

Add new check whether JdbcProjectRule is trying to convert project with
user-defined function through JDBC.

This change can be optimized - maybe - to cascade projections, in which
inner can be pushed to JDBC and the outer cannot.

Close apache/calcite#663


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/b0f47047
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/b0f47047
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/b0f47047

Branch: refs/heads/master
Commit: b0f470473087982c48518860b9199a7735d57a5f
Parents: bd7e66a
Author: Piotr Bojko <pt...@gmail.com>
Authored: Fri Apr 13 23:50:04 2018 +0200
Committer: Julian Hyde <jh...@apache.org>
Committed: Wed Apr 25 22:43:43 2018 -0700

----------------------------------------------------------------------
 .../apache/calcite/adapter/jdbc/JdbcRules.java  | 46 +++++++++++++++++++-
 .../calcite/chinook/StringConcatFunction.java   | 30 +++++++++++++
 plus/src/main/resources/chinook/chinook.json    | 14 ++++++
 plus/src/test/resources/sql/functions.iq        | 30 +++++++++++++
 4 files changed, 118 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/b0f47047/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcRules.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcRules.java b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcRules.java
index a3dbd74..dcfdb1a 100644
--- a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcRules.java
+++ b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcRules.java
@@ -60,10 +60,13 @@ import org.apache.calcite.rex.RexMultisetUtil;
 import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.rex.RexOver;
 import org.apache.calcite.rex.RexProgram;
+import org.apache.calcite.rex.RexVisitorImpl;
 import org.apache.calcite.runtime.PredicateImpl;
 import org.apache.calcite.schema.ModifiableTable;
 import org.apache.calcite.sql.SqlAggFunction;
 import org.apache.calcite.sql.SqlDialect;
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlOperator;
 import org.apache.calcite.tools.RelBuilderFactory;
 import org.apache.calcite.util.ImmutableBitSet;
 import org.apache.calcite.util.Util;
@@ -381,13 +384,52 @@ public class JdbcRules {
           new PredicateImpl<Project>() {
             public boolean test(@Nullable Project project) {
               assert project != null;
-              return out.dialect.supportsWindowFunctions()
-                  || !RexOver.containsOver(project.getProjects(), null);
+              return (out.dialect.supportsWindowFunctions()
+                  || !RexOver.containsOver(project.getProjects(), null))
+                  && !userDefinedFunctionInProject(project);
             }
+
           },
           Convention.NONE, out, relBuilderFactory, "JdbcProjectRule");
     }
 
+    private static boolean userDefinedFunctionInProject(Project project) {
+      CheckingUserDefinedFunctionVisitor visitor = new CheckingUserDefinedFunctionVisitor();
+      for (RexNode node : project.getChildExps()) {
+        node.accept(visitor);
+        if (visitor.containsUserDefinedFunction()) {
+          return true;
+        }
+      }
+      return false;
+    }
+
+    /**
+     * Visitor for checking whether part of projection is a user defined function or not
+     */
+    private static class CheckingUserDefinedFunctionVisitor extends RexVisitorImpl<Void> {
+
+      private boolean containsUsedDefinedFunction = false;
+
+      CheckingUserDefinedFunctionVisitor() {
+        super(true);
+      }
+
+      public boolean containsUserDefinedFunction() {
+        return containsUsedDefinedFunction;
+      }
+
+      @Override public Void visitCall(RexCall call) {
+        SqlOperator operator = call.getOperator();
+        if (operator instanceof SqlFunction
+            && ((SqlFunction) operator).getFunctionType().isUserDefined()) {
+          containsUsedDefinedFunction |= true;
+        }
+        return super.visitCall(call);
+      }
+
+    }
+
     public RelNode convert(RelNode rel) {
       final Project project = (Project) rel;
 

http://git-wip-us.apache.org/repos/asf/calcite/blob/b0f47047/plus/src/main/java/org/apache/calcite/chinook/StringConcatFunction.java
----------------------------------------------------------------------
diff --git a/plus/src/main/java/org/apache/calcite/chinook/StringConcatFunction.java b/plus/src/main/java/org/apache/calcite/chinook/StringConcatFunction.java
new file mode 100644
index 0000000..0978aac
--- /dev/null
+++ b/plus/src/main/java/org/apache/calcite/chinook/StringConcatFunction.java
@@ -0,0 +1,30 @@
+/*
+ * 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.
+ */
+package org.apache.calcite.chinook;
+
+/**
+ * Example query for checking query projections
+ */
+public class StringConcatFunction {
+
+  public String eval(String first, String second) {
+    return "CONCAT = [" + first + "+" + second + "]";
+  }
+
+}
+
+// End StringConcatFunction.java

http://git-wip-us.apache.org/repos/asf/calcite/blob/b0f47047/plus/src/main/resources/chinook/chinook.json
----------------------------------------------------------------------
diff --git a/plus/src/main/resources/chinook/chinook.json b/plus/src/main/resources/chinook/chinook.json
index 11b3956..f506439 100644
--- a/plus/src/main/resources/chinook/chinook.json
+++ b/plus/src/main/resources/chinook/chinook.json
@@ -43,6 +43,14 @@
           ]
         },
         {
+          "name" : "SIMPLE_CUSTOMER",
+          "type" : "view",
+          "sql" : [
+            "SELECT c.firstname, c.lastname, c.email ",
+            "FROM chinook.customer AS c"
+          ]
+        },
+        {
           "name": "PREFERRED_GENRES",
           "type": "table",
           "factory": "org.apache.calcite.chinook.PreferredGenresTableFactory"
@@ -52,6 +60,12 @@
           "type": "table",
           "factory": "org.apache.calcite.chinook.PreferredAlbumsTableFactory"
         }
+      ],
+      "functions": [
+        {
+          "name": "ASCONCATOFPARAMS",
+          "className": "org.apache.calcite.chinook.StringConcatFunction"
+        }
       ]
     }
   ]

http://git-wip-us.apache.org/repos/asf/calcite/blob/b0f47047/plus/src/test/resources/sql/functions.iq
----------------------------------------------------------------------
diff --git a/plus/src/test/resources/sql/functions.iq b/plus/src/test/resources/sql/functions.iq
new file mode 100644
index 0000000..803b238
--- /dev/null
+++ b/plus/src/test/resources/sql/functions.iq
@@ -0,0 +1,30 @@
+# 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 CALCITE_AS_ADMIN
+!set outputformat mysql
+
+# Checks whether ASCONCATOFPARAMS function is properly computed and not passed to subschema, like jdbc
+SELECT email, ASCONCATOFPARAMS(firstname, lastname) AS joined FROM SIMPLE_CUSTOMER limit 3;
++-----------------------+------------------------------+
+| email                 | joined                       |
++-----------------------+------------------------------+
+| ftremblay@gmail.com   | CONCAT = [François+Tremblay] |
+| leonekohler@surfeu.de | CONCAT = [Leonie+Köhler]     |
+| luisg@embraer.com.br  | CONCAT = [Luís+Gonçalves]    |
++-----------------------+------------------------------+
+(3 rows)
+
+!ok