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

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


The following commit(s) were added to refs/heads/master by this push:
     new c09f293  [CALCITE-3349] Add CREATE FUNCTION and DROP FUNCTION ddl 
(Zhenqiu Huang)
c09f293 is described below

commit c09f293b4666b38e41a0724aab73565f8c61dbac
Author: hpeter <[email protected]>
AuthorDate: Sun Sep 15 23:01:52 2019 -0700

    [CALCITE-3349] Add CREATE FUNCTION and DROP FUNCTION ddl (Zhenqiu Huang)
    
    Close #1455
---
 .../apache/calcite/runtime/CalciteResource.java    |  3 +
 .../main/java/org/apache/calcite/sql/SqlKind.java  |  1 +
 .../calcite/runtime/CalciteResource.properties     |  1 +
 .../apache/calcite/sql/ddl/SqlCreateFunction.java  |  9 ++-
 .../org/apache/calcite/sql/ddl/SqlDropObject.java  |  7 +++
 .../java/org/apache/calcite/test/ServerTest.java   | 67 ++++++++++++++++++++++
 6 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java 
b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
index 3c39422..da277e1 100644
--- a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
+++ b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
@@ -788,6 +788,9 @@ public interface CalciteResource {
   @BaseMessage("Type ''{0}'' not found")
   ExInst<SqlValidatorException> typeNotFound(String name);
 
+  @BaseMessage("Function ''{0}'' not found")
+  ExInst<SqlValidatorException> functionNotFound(String name);
+
   @BaseMessage("Dialect does not support feature: ''{0}''")
   ExInst<SqlValidatorException> dialectDoesNotSupportFeature(String 
featureName);
 
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlKind.java 
b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
index cb29032..7041f4b 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlKind.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlKind.java
@@ -1167,6 +1167,7 @@ public enum SqlKind {
       EnumSet.of(COMMIT, ROLLBACK, ALTER_SESSION,
           CREATE_SCHEMA, CREATE_FOREIGN_SCHEMA, DROP_SCHEMA,
           CREATE_TABLE, ALTER_TABLE, DROP_TABLE,
+          CREATE_FUNCTION, DROP_FUNCTION,
           CREATE_VIEW, ALTER_VIEW, DROP_VIEW,
           CREATE_MATERIALIZED_VIEW, ALTER_MATERIALIZED_VIEW,
           DROP_MATERIALIZED_VIEW,
diff --git 
a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties 
b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
index 1b670b6..f4852a2 100644
--- 
a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
+++ 
b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
@@ -256,6 +256,7 @@ ViewExists=View ''{0}'' already exists and REPLACE not 
specified
 SchemaNotFound=Schema ''{0}'' not found
 ViewNotFound=View ''{0}'' not found
 TypeNotFound=Type ''{0}'' not found
+FunctionNotFound=Function ''{0}'' not found
 DialectDoesNotSupportFeature=Dialect does not support feature: ''{0}''
 IllegalNegativeSubstringLength=Substring error: negative substring length not 
allowed
 TrimError=Trim error: trim character must be exactly 1 character
diff --git 
a/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateFunction.java 
b/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateFunction.java
index 29ccc6b..32ce7bf 100644
--- a/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateFunction.java
+++ b/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateFunction.java
@@ -16,7 +16,9 @@
  */
 package org.apache.calcite.sql.ddl;
 
+import org.apache.calcite.jdbc.CalcitePrepare;
 import org.apache.calcite.sql.SqlCreate;
+import org.apache.calcite.sql.SqlExecutableStatement;
 import org.apache.calcite.sql.SqlIdentifier;
 import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.sql.SqlLiteral;
@@ -38,7 +40,8 @@ import java.util.Objects;
 /**
  * Parse tree for {@code CREATE FUNCTION} statement.
  */
-public class SqlCreateFunction extends SqlCreate {
+public class SqlCreateFunction extends SqlCreate
+    implements SqlExecutableStatement {
   private final SqlIdentifier name;
   private final SqlNode className;
   private final SqlNodeList usingList;
@@ -80,6 +83,10 @@ public class SqlCreateFunction extends SqlCreate {
     }
   }
 
+  @Override public void execute(CalcitePrepare.Context context) {
+    throw new UnsupportedOperationException("CREATE FUNCTION is not supported 
yet.");
+  }
+
   @SuppressWarnings("unchecked")
   private List<Pair<SqlLiteral, SqlLiteral>> pairs() {
     return Util.pairs((List) usingList.getList());
diff --git a/server/src/main/java/org/apache/calcite/sql/ddl/SqlDropObject.java 
b/server/src/main/java/org/apache/calcite/sql/ddl/SqlDropObject.java
index ed6e6d4..5bdc8ec 100644
--- a/server/src/main/java/org/apache/calcite/sql/ddl/SqlDropObject.java
+++ b/server/src/main/java/org/apache/calcite/sql/ddl/SqlDropObject.java
@@ -91,6 +91,13 @@ abstract class SqlDropObject extends SqlDrop
             RESOURCE.typeNotFound(name.getSimple()));
       }
       break;
+    case DROP_FUNCTION:
+      existed = schema.removeFunction(name.getSimple());
+      if (!existed && !ifExists) {
+        throw SqlUtil.newContextException(name.getParserPosition(),
+            RESOURCE.functionNotFound(name.getSimple()));
+      }
+      break;
     case OTHER_DDL:
     default:
       throw new AssertionError(getKind());
diff --git a/server/src/test/java/org/apache/calcite/test/ServerTest.java 
b/server/src/test/java/org/apache/calcite/test/ServerTest.java
index 37847e0..e012e7e 100644
--- a/server/src/test/java/org/apache/calcite/test/ServerTest.java
+++ b/server/src/test/java/org/apache/calcite/test/ServerTest.java
@@ -17,6 +17,9 @@
 package org.apache.calcite.test;
 
 import org.apache.calcite.config.CalciteConnectionProperty;
+import org.apache.calcite.jdbc.CalciteConnection;
+import org.apache.calcite.schema.Function;
+import org.apache.calcite.schema.FunctionParameter;
 import org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl;
 
 import org.junit.Ignore;
@@ -29,6 +32,8 @@ import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
 
 import static org.apache.calcite.test.Matchers.isLinux;
 
@@ -166,6 +171,68 @@ public class ServerTest {
     }
   }
 
+  @Test public void testCreateFunction() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      boolean b = s.execute("create schema s");
+      assertThat(b, is(false));
+      try {
+        boolean f = s.execute("create function if not exists s.t \n"
+                + "as 'org.apache.calcite.udf.TableFun.demoUdf'\n"
+                + "using jar 'file:/path/udf/udf-0.0.1-SNAPSHOT.jar'");
+      } catch (SQLException e) {
+        assertThat(e.getMessage(),
+                containsString("CREATE FUNCTION is not supported yet"));
+      }
+    }
+  }
+
+  @Test public void testDropFunction() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      boolean b = s.execute("create schema s");
+      assertThat(b, is(false));
+
+      boolean f = s.execute("drop function if exists t");
+      assertThat(f, is(false));
+
+      try {
+        boolean f2 = s.execute("drop function t");
+        assertThat(f2, is(false));
+      } catch (SQLException e) {
+        assertThat(e.getMessage(),
+                containsString("Error while executing SQL \"drop function t\":"
+                        + " At line 1, column 15: Function 'T' not found"));
+      }
+
+      CalciteConnection calciteConnection = (CalciteConnection) c;
+      calciteConnection.getRootSchema().add("T", new Function() {
+        @Override public List<FunctionParameter> getParameters() {
+          return new ArrayList<>();
+        }
+      });
+
+      boolean f3 = s.execute("drop function t");
+      assertThat(f3, is(false));
+
+      // case sensitive function name
+      calciteConnection.getRootSchema().add("t", new Function() {
+        @Override public List<FunctionParameter> getParameters() {
+          return new ArrayList<>();
+        }
+      });
+
+      try {
+        boolean f4 = s.execute("drop function t");
+        assertThat(f4, is(false));
+      } catch (SQLException e) {
+        assertThat(e.getMessage(),
+                containsString("Error while executing SQL \"drop function t\":"
+                        + " At line 1, column 15: Function 'T' not found"));
+      }
+    }
+  }
+
   @Test public void testInsertCastedValueOfCompositeUdt() throws Exception {
     try (Connection c = connect();
          Statement s = c.createStatement()) {

Reply via email to