Skip to content

Commit

Permalink
Refactor UDF management process to support table model user-defined f…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
Cpaulyz authored Nov 27, 2024
1 parent b072f9e commit d751364
Show file tree
Hide file tree
Showing 57 changed files with 1,211 additions and 475 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.iotdb.confignode.it;

import org.apache.iotdb.common.rpc.thrift.Model;
import org.apache.iotdb.common.rpc.thrift.TSStatus;
import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
Expand All @@ -39,6 +40,7 @@
import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema;
import org.apache.iotdb.confignode.rpc.thrift.TGetTriggerTableResp;
import org.apache.iotdb.confignode.rpc.thrift.TGetUDFTableResp;
import org.apache.iotdb.confignode.rpc.thrift.TGetUdfTableReq;
import org.apache.iotdb.confignode.rpc.thrift.TSchemaPartitionReq;
import org.apache.iotdb.confignode.rpc.thrift.TSchemaPartitionTableResp;
import org.apache.iotdb.confignode.rpc.thrift.TShowCQResp;
Expand Down Expand Up @@ -71,9 +73,11 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.apache.iotdb.confignode.it.utils.ConfigNodeTestUtils.generatePatternTreeBuffer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(IoTDBTestRunner.class)
@Category({ClusterIT.class})
Expand Down Expand Up @@ -183,7 +187,7 @@ public void testPartitionInfoSnapshot() throws Exception {
}

assertTriggerInformation(createTriggerReqs, client.getTriggerTable());
assertUDFInformation(createFunctionReqs, client.getUDFTable());
assertUDFInformation(createFunctionReqs, client.getUDFTable(new TGetUdfTableReq(Model.TREE)));

TShowCQResp showCQResp = client.showCQ();
assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(), showCQResp.getStatus().getCode());
Expand Down Expand Up @@ -283,12 +287,14 @@ private List<TCreateFunctionReq> createUDF(SyncConfigNodeIServiceClient client)

TCreateFunctionReq createFunctionReq1 =
new TCreateFunctionReq("test1", "org.apache.iotdb.udf.UDTFExample", true)
.setModel(Model.TREE)
.setJarName(jarName)
.setJarFile(jarFile)
.setJarMD5(jarMD5);

TCreateFunctionReq createFunctionReq2 =
new TCreateFunctionReq("test2", "org.apache.iotdb.udf.UDTFExample", true)
.setModel(Model.TREE)
.setJarName(jarName)
.setJarFile(jarFile)
.setJarMD5(jarMD5);
Expand All @@ -307,11 +313,13 @@ private List<TCreateFunctionReq> createUDF(SyncConfigNodeIServiceClient client)
}

private void assertUDFInformation(List<TCreateFunctionReq> req, TGetUDFTableResp resp) {
Map<String, TCreateFunctionReq> nameToReqMap =
req.stream().collect(Collectors.toMap(r -> r.getUdfName().toUpperCase(), r -> r));
for (int i = 0; i < req.size(); i++) {
TCreateFunctionReq createFunctionReq = req.get(i);
UDFInformation udfInformation =
UDFInformation.deserialize(resp.getAllUDFInformation().get(i));

assertTrue(nameToReqMap.containsKey(udfInformation.getFunctionName()));
TCreateFunctionReq createFunctionReq = nameToReqMap.get(udfInformation.getFunctionName());
assertEquals(createFunctionReq.getUdfName().toUpperCase(), udfInformation.getFunctionName());
assertEquals(createFunctionReq.getClassName(), udfInformation.getClassName());
assertEquals(createFunctionReq.getJarName(), udfInformation.getJarName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1115,8 +1115,8 @@ public void showFunctions(CloseableHttpClient httpClient) {
Map map = queryMetaData(httpClient, sql);
List<String> columnNamesResult = (List<String>) map.get("columnNames");
List<List<Object>> valuesResult = (List<List<Object>>) map.get("values");
assertEquals(3, columnNamesResult.size());
assertEquals(3, valuesResult.size());
assertEquals(4, columnNamesResult.size());
assertEquals(4, valuesResult.size());
}

public void showTimeseries(CloseableHttpClient httpClient) {
Expand Down Expand Up @@ -1767,8 +1767,8 @@ public void showFunctionsV2(CloseableHttpClient httpClient) {
Map map = queryMetaDataV2(httpClient, sql);
List<String> columnNamesResult = (List<String>) map.get("column_names");
List<List<Object>> valuesResult = (List<List<Object>>) map.get("values");
assertEquals(3, columnNamesResult.size());
assertEquals(3, valuesResult.size());
assertEquals(4, columnNamesResult.size());
assertEquals(4, valuesResult.size());
}

public void showTimeseriesV2(CloseableHttpClient httpClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public void testUDFClassName() {

// executed correctly
try (ResultSet resultSet = statement.executeQuery("show functions")) {
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
int count = 0;
while (resultSet.next()) {
StringBuilder stringBuilder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void createReflectShowDropUDAFTest() {
statement.executeQuery("SELECT udaf(*) FROM root.vehicle");

try (ResultSet resultSet = statement.executeQuery("SHOW FUNCTIONS")) {
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
int count = 0;
while (resultSet.next()) {
++count;
Expand All @@ -108,7 +108,7 @@ public void createAndDropUDAFSeveralTimesTest() {
++count;
}
Assert.assertEquals(1 + FUNCTIONS_COUNT, count);
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
statement.execute("DROP FUNCTION udaf");

statement.execute(
Expand All @@ -122,7 +122,7 @@ public void createAndDropUDAFSeveralTimesTest() {
++count;
}
Assert.assertEquals(1 + FUNCTIONS_COUNT, count);
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
statement.execute("DROP FUNCTION udaf");
}
} catch (SQLException throwable) {
Expand Down Expand Up @@ -222,7 +222,7 @@ public void createFunctionWithURITest() throws SQLException {
++count;
}
Assert.assertEquals(2 + FUNCTIONS_COUNT, count);
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
statement.execute("DROP FUNCTION udaf1");
statement.execute("DROP FUNCTION udaf2");
} catch (Exception e) {
Expand Down Expand Up @@ -309,7 +309,7 @@ public void testShowBuiltinFunction() {
"CREATE FUNCTION udaf AS 'org.apache.iotdb.db.query.udf.example.UDAFCount'");

try (ResultSet resultSet = statement.executeQuery("SHOW FUNCTIONS")) {
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
int count = 0;
while (resultSet.next()) {
StringBuilder stringBuilder = new StringBuilder();
Expand All @@ -320,7 +320,7 @@ public void testShowBuiltinFunction() {
if (result.contains(FUNCTION_TYPE_EXTERNAL_UDAF)) {
Assert.assertEquals(
String.format(
"UDAF,%s,org.apache.iotdb.db.query.udf.example.UDAFCount,",
"UDAF,%s,org.apache.iotdb.db.query.udf.example.UDAFCount,AVAILABLE,",
FUNCTION_TYPE_EXTERNAL_UDAF),
result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void testCreateReflectShowDrop() {
statement.executeQuery("select udf(*, *) from root.vehicle");

try (ResultSet resultSet = statement.executeQuery("show functions")) {
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
int count = 0;
while (resultSet.next()) {
StringBuilder stringBuilder = new StringBuilder();
Expand Down Expand Up @@ -123,7 +123,7 @@ public void testCreateAndDropSeveralTimes() {
Assert.assertEquals(
1 + NATIVE_FUNCTIONS_COUNT + BUILTIN_FUNCTIONS_COUNT + BUILTIN_SCALAR_FUNCTIONS_COUNT,
count);
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
statement.execute("drop function udf");

statement.execute("create function udf as 'org.apache.iotdb.db.query.udf.example.Adder'");
Expand All @@ -138,7 +138,7 @@ public void testCreateAndDropSeveralTimes() {
Assert.assertEquals(
1 + NATIVE_FUNCTIONS_COUNT + BUILTIN_FUNCTIONS_COUNT + BUILTIN_SCALAR_FUNCTIONS_COUNT,
count);
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
statement.execute("drop function udf");
}
} catch (SQLException throwable) {
Expand Down Expand Up @@ -249,7 +249,7 @@ public void testCreateFunctionWithURI() throws SQLException {
Assert.assertEquals(
2 + NATIVE_FUNCTIONS_COUNT + BUILTIN_FUNCTIONS_COUNT + BUILTIN_SCALAR_FUNCTIONS_COUNT,
count);
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
statement.execute("drop function udf");
statement.execute("drop function udf1");
} catch (Exception e) {
Expand Down Expand Up @@ -353,7 +353,10 @@ public void testCreateBuiltinFunction() {
statement.execute("create function sin as 'org.apache.iotdb.db.query.udf.example.Adder'");
fail();
} catch (SQLException throwable) {
assertTrue(throwable.getMessage().contains("the same name UDF has been created"));
assertTrue(
throwable
.getMessage()
.contains("the given function name conflicts with the built-in function name"));
}
}

Expand All @@ -376,7 +379,7 @@ public void testShowBuiltinFunction() {
statement.execute("create function udf as 'org.apache.iotdb.db.query.udf.example.Adder'");

try (ResultSet resultSet = statement.executeQuery("show functions")) {
assertEquals(3, resultSet.getMetaData().getColumnCount());
assertEquals(4, resultSet.getMetaData().getColumnCount());
int count = 0;
while (resultSet.next()) {
StringBuilder stringBuilder = new StringBuilder();
Expand All @@ -391,7 +394,7 @@ public void testShowBuiltinFunction() {
if (result.contains(FUNCTION_TYPE_EXTERNAL_UDTF)) {
Assert.assertEquals(
String.format(
"UDF,%s,org.apache.iotdb.db.query.udf.example.Adder,",
"UDF,%s,org.apache.iotdb.db.query.udf.example.Adder,AVAILABLE,",
FUNCTION_TYPE_EXTERNAL_UDTF),
result);
++count;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.iotdb.udf.api.relational;

public interface AggregationFunction extends SQLFunction {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.iotdb.udf.api.relational;

public interface SQLFunction {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.iotdb.udf.api.relational;

public interface ScalarFunction extends SQLFunction {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.iotdb.udf.api.relational;

public interface TableFunction extends SQLFunction {}
Loading

0 comments on commit d751364

Please sign in to comment.