|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.functions; |
| 15 | + |
| 16 | +import com.facebook.presto.common.type.TimeZoneKey; |
| 17 | +import com.facebook.presto.common.type.Type; |
| 18 | +import com.facebook.presto.server.testing.TestingPrestoServer; |
| 19 | +import com.facebook.presto.spi.Plugin; |
| 20 | +import com.facebook.presto.testing.MaterializedResult; |
| 21 | +import com.facebook.presto.tests.TestingPrestoClient; |
| 22 | +import com.google.common.collect.ImmutableSet; |
| 23 | +import org.intellij.lang.annotations.Language; |
| 24 | +import org.testng.annotations.BeforeClass; |
| 25 | +import org.testng.annotations.Test; |
| 26 | + |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import static com.facebook.presto.common.type.BigintType.BIGINT; |
| 30 | +import static com.facebook.presto.common.type.IntegerType.INTEGER; |
| 31 | +import static com.facebook.presto.common.type.VarcharType.VARCHAR; |
| 32 | +import static com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.JAVA_BUILTIN_NAMESPACE; |
| 33 | +import static com.facebook.presto.testing.TestingSession.testSessionBuilder; |
| 34 | +import static java.lang.String.format; |
| 35 | +import static org.testng.Assert.assertEquals; |
| 36 | +import static org.testng.Assert.assertThrows; |
| 37 | +import static org.testng.Assert.fail; |
| 38 | + |
| 39 | +public class TestPluginLoadedSqlInvokedFunctions |
| 40 | +{ |
| 41 | + protected TestingPrestoServer server; |
| 42 | + protected TestingPrestoClient client; |
| 43 | + |
| 44 | + private static final String CATALOG_NAME = JAVA_BUILTIN_NAMESPACE.getCatalogName(); |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + public void setup() |
| 48 | + throws Exception |
| 49 | + { |
| 50 | + server = new TestingPrestoServer(); |
| 51 | + server.installPlugin(new TestFunctionsPlugin()); |
| 52 | + client = new TestingPrestoClient(server, testSessionBuilder() |
| 53 | + .setTimeZoneKey(TimeZoneKey.getTimeZoneKey("America/Bahia_Banderas")) |
| 54 | + .build()); |
| 55 | + } |
| 56 | + |
| 57 | + public void assertInvalidFunction(String expr, String exceptionPattern) |
| 58 | + { |
| 59 | + try { |
| 60 | + client.execute("SELECT " + expr); |
| 61 | + fail("Function expected to fail but not"); |
| 62 | + } |
| 63 | + catch (Exception e) { |
| 64 | + if (!(e.getMessage().matches(exceptionPattern))) { |
| 65 | + fail(format("Expected exception message '%s' to match '%s' but not", |
| 66 | + e.getMessage(), exceptionPattern)); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static class TestFunctionsPlugin |
| 72 | + implements Plugin |
| 73 | + { |
| 74 | + @Override |
| 75 | + public Set<Class<?>> getSqlInvokedFunctions() |
| 76 | + { |
| 77 | + return ImmutableSet.<Class<?>>builder() |
| 78 | + .add(TestSqlInvokedFunctionsPlugin.class) |
| 79 | + .build(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Set<Class<?>> getFunctions() |
| 84 | + { |
| 85 | + return ImmutableSet.<Class<?>>builder() |
| 86 | + .add(TestFunctions.class) |
| 87 | + .build(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public void check(@Language("SQL") String query, Type expectedType, Object expectedValue) |
| 92 | + { |
| 93 | + MaterializedResult result = client.execute(query).getResult(); |
| 94 | + assertEquals(result.getRowCount(), 1); |
| 95 | + assertEquals(result.getTypes().get(0), expectedType); |
| 96 | + Object actual = result.getMaterializedRows().get(0).getField(0); |
| 97 | + assertEquals(actual, expectedValue); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testNewFunctionNamespaceFunction() |
| 102 | + { |
| 103 | + check("SELECT " + JAVA_BUILTIN_NAMESPACE + ".modulo(10,3)", BIGINT, 1L); |
| 104 | + check("SELECT " + JAVA_BUILTIN_NAMESPACE + ".identity('test-functions')", VARCHAR, "test-functions"); |
| 105 | + check("SELECT " + JAVA_BUILTIN_NAMESPACE + ".custom_square(2, 3)", INTEGER, 4); |
| 106 | + check("SELECT " + JAVA_BUILTIN_NAMESPACE + ".custom_square(null, 3)", INTEGER, 9); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testInvalidFunctionAndNamespace() |
| 111 | + { |
| 112 | + assertInvalidFunction(CATALOG_NAME + ".namespace.modulo(10,3)", format("line 1:8: Function %s.namespace.modulo not registered", CATALOG_NAME)); |
| 113 | + assertInvalidFunction(CATALOG_NAME + ".system.some_func(10)", format("line 1:8: Function %s.system.some_func not registered", CATALOG_NAME)); |
| 114 | + } |
| 115 | + |
| 116 | + @Test(dependsOnMethods = |
| 117 | + {"testNewFunctionNamespaceFunction", |
| 118 | + "testInvalidFunctionAndNamespace"}) |
| 119 | + public void testDuplicateFunctionsLoaded() |
| 120 | + { |
| 121 | + // Because we trigger the conflict check as soon as the plugins are loaded, |
| 122 | + // this will throw an Exception: Function already registered: presto.default.modulo(bigint,bigint):bigint while installing the plugin itself |
| 123 | + assertThrows(IllegalArgumentException.class, () -> server.installPlugin(new TestFunctionsPlugin())); |
| 124 | + } |
| 125 | +} |
0 commit comments