From 1724faf9a5b9be4ec65780490bbd5e7d709dc56d Mon Sep 17 00:00:00 2001 From: Liqf <109049295+LemonLiTree@users.noreply.github.com> Date: Sat, 5 Nov 2022 19:25:58 +0800 Subject: [PATCH] [test](java-udf)add java udf RegressionTest about the currently supported data types #13972 --- .../data/javaudf_p0/test_javaudf_addone.out | 25 ---- .../data/javaudf_p0/test_javaudf_boolean.out | 28 +++++ .../data/javaudf_p0/test_javaudf_case.out | 17 +++ .../data/javaudf_p0/test_javaudf_decimal.out | 23 ++++ .../data/javaudf_p0/test_javaudf_float.out | 40 ++++++ .../data/javaudf_p0/test_javaudf_int.out | 88 +++++++++++++ .../data/javaudf_p0/test_javaudf_string.out | 32 +++-- .../java/org/apache/doris/udf/BigintTest.java | 26 ++++ .../org/apache/doris/udf/BooleanTest.java | 26 ++++ .../apache/doris/udf/DateWindowRollup.java | 52 ++++++++ .../org/apache/doris/udf/DecimalTest.java | 26 ++++ .../java/org/apache/doris/udf/FloatTest.java | 25 ++++ .../doris/udf/{AddOne.java => IntTest.java} | 2 +- .../org/apache/doris/udf/LargeintTest.java | 28 +++++ .../org/apache/doris/udf/SmallintTest.java | 26 ++++ .../org/apache/doris/udf/TinyintTest.java | 26 ++++ .../javaudf_p0/test_javaudf_boolean.groovy | 70 +++++++++++ ...addone.groovy => test_javaudf_case.groovy} | 41 +++--- .../javaudf_p0/test_javaudf_decimal.groovy | 70 +++++++++++ .../javaudf_p0/test_javaudf_float.groovy | 87 +++++++++++++ .../suites/javaudf_p0/test_javaudf_int.groovy | 118 ++++++++++++++++++ .../javaudf_p0/test_javaudf_string.groovy | 15 ++- .../apache/doris/udf/DateWindowRollup.java | 54 ++++++++ 23 files changed, 880 insertions(+), 65 deletions(-) delete mode 100644 regression-test/data/javaudf_p0/test_javaudf_addone.out create mode 100644 regression-test/data/javaudf_p0/test_javaudf_boolean.out create mode 100644 regression-test/data/javaudf_p0/test_javaudf_case.out create mode 100644 regression-test/data/javaudf_p0/test_javaudf_decimal.out create mode 100644 regression-test/data/javaudf_p0/test_javaudf_float.out create mode 100644 regression-test/data/javaudf_p0/test_javaudf_int.out create mode 100644 regression-test/java-udf-src/src/main/java/org/apache/doris/udf/BigintTest.java create mode 100644 regression-test/java-udf-src/src/main/java/org/apache/doris/udf/BooleanTest.java create mode 100644 regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DateWindowRollup.java create mode 100644 regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DecimalTest.java create mode 100644 regression-test/java-udf-src/src/main/java/org/apache/doris/udf/FloatTest.java rename regression-test/java-udf-src/src/main/java/org/apache/doris/udf/{AddOne.java => IntTest.java} (96%) create mode 100644 regression-test/java-udf-src/src/main/java/org/apache/doris/udf/LargeintTest.java create mode 100644 regression-test/java-udf-src/src/main/java/org/apache/doris/udf/SmallintTest.java create mode 100644 regression-test/java-udf-src/src/main/java/org/apache/doris/udf/TinyintTest.java create mode 100644 regression-test/suites/javaudf_p0/test_javaudf_boolean.groovy rename regression-test/suites/javaudf_p0/{test_javaudf_addone.groovy => test_javaudf_case.groovy} (59%) create mode 100644 regression-test/suites/javaudf_p0/test_javaudf_decimal.groovy create mode 100644 regression-test/suites/javaudf_p0/test_javaudf_float.groovy create mode 100644 regression-test/suites/javaudf_p0/test_javaudf_int.groovy create mode 100644 samples/doris-demo/java-udf-demo/src/main/java/org/apache/doris/udf/DateWindowRollup.java diff --git a/regression-test/data/javaudf_p0/test_javaudf_addone.out b/regression-test/data/javaudf_p0/test_javaudf_addone.out deleted file mode 100644 index 8f579a5f8b..0000000000 --- a/regression-test/data/javaudf_p0/test_javaudf_addone.out +++ /dev/null @@ -1,25 +0,0 @@ --- This file is automatically generated. You should know what you did if you want to edit this --- !select_default -- -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 - --- !select -- -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 - diff --git a/regression-test/data/javaudf_p0/test_javaudf_boolean.out b/regression-test/data/javaudf_p0/test_javaudf_boolean.out new file mode 100644 index 0000000000..7a293780a0 --- /dev/null +++ b/regression-test/data/javaudf_p0/test_javaudf_boolean.out @@ -0,0 +1,28 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select_default -- +111 true +112 false +113 false +114 true + +-- !select -- +false + +-- !select -- +true + +-- !select -- +false + +-- !select -- +true + +-- !select -- +\N + +-- !select -- +111 false +112 true +113 true +114 false + diff --git a/regression-test/data/javaudf_p0/test_javaudf_case.out b/regression-test/data/javaudf_p0/test_javaudf_case.out new file mode 100644 index 0000000000..3db56045a2 --- /dev/null +++ b/regression-test/data/javaudf_p0/test_javaudf_case.out @@ -0,0 +1,17 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select_default -- +2022-10-21 -3 0 +2022-10-25 -7 -3 +2022-10-26 -7 \N + +-- !select -- +2022-10-17,2022-10-18,2022-10-19,2022-10-20,2022-10-21,2022-10-22,2022-10-23 + +-- !select -- +\N + +-- !select -- +2022-10-21 2022-10-18,2022-10-19,2022-10-20 +2022-10-25 2022-10-18,2022-10-19,2022-10-20,2022-10-21 +2022-10-26 \N + diff --git a/regression-test/data/javaudf_p0/test_javaudf_decimal.out b/regression-test/data/javaudf_p0/test_javaudf_decimal.out new file mode 100644 index 0000000000..50b61279c7 --- /dev/null +++ b/regression-test/data/javaudf_p0/test_javaudf_decimal.out @@ -0,0 +1,23 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select_default -- +111 11111.111110000 222222.333333300 +112 1234556.111110000 222222.333333300 +113 87654321.111110000 \N + +-- !select -- +113.947561100 + +-- !select -- +113.947561100 + +-- !select -- +\N + +-- !select -- +\N + +-- !select -- +111 233333.444443300 +112 1456778.444443300 +113 \N + diff --git a/regression-test/data/javaudf_p0/test_javaudf_float.out b/regression-test/data/javaudf_p0/test_javaudf_float.out new file mode 100644 index 0000000000..d806cbd546 --- /dev/null +++ b/regression-test/data/javaudf_p0/test_javaudf_float.out @@ -0,0 +1,40 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select_default -- +111 11111.111 222222.33 1.234567834455677E7 1111112.0 +112 1234556.1 222222.33 2.2222222233333334E8 4.444444444444556E12 +113 8.765432E7 \N 6.666666666666667E9 \N + +-- !select -- +-108.27467 + +-- !select -- +-108.27467 + +-- !select -- +\N + +-- !select -- +\N + +-- !select -- +111 -211111.22 +112 1012333.8 +113 \N + +-- !select -- +113.9475611 + +-- !select -- +113.9475611 + +-- !select -- +\N + +-- !select -- +\N + +-- !select -- +111 2.469135668911354E7 +112 4.444444446666667E8 +113 1.3333333333333334E10 + diff --git a/regression-test/data/javaudf_p0/test_javaudf_int.out b/regression-test/data/javaudf_p0/test_javaudf_int.out new file mode 100644 index 0000000000..561ba219da --- /dev/null +++ b/regression-test/data/javaudf_p0/test_javaudf_int.out @@ -0,0 +1,88 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select_default -- +1 2 3 4 5 +2 4 6 8 10 +3 6 9 12 15 +4 8 12 16 20 +5 10 15 20 25 +6 12 18 24 30 +7 14 21 28 35 +8 16 24 32 40 +9 18 27 36 45 +10 20 30 40 50 + +-- !select -- +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 + +-- !select -- +\N + +-- !select -- +2 +4 +6 +8 +10 +12 +14 +16 +18 +20 + +-- !select -- +\N + +-- !select -- +3 +6 +9 +12 +15 +18 +21 +24 +27 +30 + +-- !select -- +\N + +-- !select -- +5 +9 +13 +17 +21 +25 +29 +33 +37 +41 + +-- !select -- +\N + +-- !select -- +6 +11 +16 +21 +26 +31 +36 +41 +46 +51 + +-- !select -- +\N + diff --git a/regression-test/data/javaudf_p0/test_javaudf_string.out b/regression-test/data/javaudf_p0/test_javaudf_string.out index 60c8689fca..892c524582 100644 --- a/regression-test/data/javaudf_p0/test_javaudf_string.out +++ b/regression-test/data/javaudf_p0/test_javaudf_string.out @@ -1,18 +1,16 @@ -- This file is automatically generated. You should know what you did if you want to edit this -- !select_default -- -1 abcdefg1 -2 abcdefg2 -3 abcdefg3 -4 abcdefg4 -5 abcdefg5 -6 abcdefg6 -7 abcdefg7 -8 abcdefg8 -9 abcdefg9 -10 abcdefg10 +1 1 abcdefg1 poiuytre1abcdefg +2 2 abcdefg2 poiuytre2abcdefg +3 3 abcdefg3 poiuytre3abcdefg +4 4 abcdefg4 poiuytre4abcdefg +5 5 abcdefg5 poiuytre5abcdefg +6 6 abcdefg6 poiuytre6abcdefg +7 7 abcdefg7 poiuytre7abcdefg +8 8 abcdefg8 poiuytre8abcdefg +9 9 abcdefg9 poiuytre9abcdefg -- !select -- -ab****g10 ab***fg1 ab***fg2 ab***fg3 @@ -24,7 +22,17 @@ ab***fg8 ab***fg9 -- !select -- -ab*def ab**efg +po***********efg +po***********efg +po***********efg +po***********efg +po***********efg +po***********efg +po***********efg +po***********efg +po***********efg + +-- !select -- ab*def ab**efg ab*def ab**efg ab*def ab**efg diff --git a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/BigintTest.java b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/BigintTest.java new file mode 100644 index 0000000000..e6cfdbba8f --- /dev/null +++ b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/BigintTest.java @@ -0,0 +1,26 @@ +// 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.doris.udf; + +import org.apache.hadoop.hive.ql.exec.UDF; + +public class BigintTest extends UDF { + public Long evaluate(Long value) { + return value == null? null: value + 1; + } +} diff --git a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/BooleanTest.java b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/BooleanTest.java new file mode 100644 index 0000000000..773fbeea36 --- /dev/null +++ b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/BooleanTest.java @@ -0,0 +1,26 @@ +// 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.doris.udf; + +import org.apache.hadoop.hive.ql.exec.UDF; + +public class BooleanTest extends UDF { + public Boolean evaluate(Boolean boo) { + return boo == null ? null : !boo; + } +} diff --git a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DateWindowRollup.java b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DateWindowRollup.java new file mode 100644 index 0000000000..2dd44b651a --- /dev/null +++ b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DateWindowRollup.java @@ -0,0 +1,52 @@ +// 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.doris.udf; + +import org.apache.hadoop.hive.ql.exec.UDF; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; + +public class DateWindowRollup extends UDF { + + /** + * @param startTime yyyy-MM-dd + * @param start 从startTime 前-start天 + * @param end 至startTime 前-end天 + * @return ${yyyy-MM-dd}+end ,${yyyy-MM-dd}+(end+1),...,${yyyy-MM-dd}+(start) + */ + public String evaluate(LocalDate startTime, Integer start, Integer end) { + if (startTime == null || start == null || end == null){ + return null; + } + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDate yesterday = LocalDate.now().minusDays(1); + List ret = new ArrayList<>(); + for (int i = start; i <= end - 1; i++) { + LocalDate groupDate = startTime.plusDays(i); + if (groupDate.isAfter(yesterday)) { + break; + } + String dateFormat = formatter.format(groupDate); + ret.add(dateFormat); + } + return String.join(",", ret); + } + +} diff --git a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DecimalTest.java b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DecimalTest.java new file mode 100644 index 0000000000..c8bd39a7f1 --- /dev/null +++ b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DecimalTest.java @@ -0,0 +1,26 @@ +// 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.doris.udf; +import java.math.BigDecimal; +import org.apache.hadoop.hive.ql.exec.UDF; + +public class DecimalTest extends UDF { + public BigDecimal evaluate(BigDecimal dec, BigDecimal dec2) { + return dec == null || dec2 == null ? null : dec.add(dec2); + } +} diff --git a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/FloatTest.java b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/FloatTest.java new file mode 100644 index 0000000000..b22c3755bd --- /dev/null +++ b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/FloatTest.java @@ -0,0 +1,25 @@ +// 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.doris.udf; +import org.apache.hadoop.hive.ql.exec.UDF; + +public class FloatTest extends UDF { + public Float evaluate(Float flo, Float flo2) { + return flo == null || flo2 == null ? null : flo - flo2; + } +} diff --git a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/AddOne.java b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/IntTest.java similarity index 96% rename from regression-test/java-udf-src/src/main/java/org/apache/doris/udf/AddOne.java rename to regression-test/java-udf-src/src/main/java/org/apache/doris/udf/IntTest.java index d468925d75..823d35db69 100644 --- a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/AddOne.java +++ b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/IntTest.java @@ -19,7 +19,7 @@ package org.apache.doris.udf; import org.apache.hadoop.hive.ql.exec.UDF; -public class AddOne extends UDF { +public class IntTest extends UDF { public Integer evaluate(Integer value) { return value == null? null: value + 1; } diff --git a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/LargeintTest.java b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/LargeintTest.java new file mode 100644 index 0000000000..ce03febe33 --- /dev/null +++ b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/LargeintTest.java @@ -0,0 +1,28 @@ +// 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.doris.udf; + +import org.apache.hadoop.hive.ql.exec.UDF; +import java.math.BigInteger; + +public class LargeintTest extends UDF { + public BigInteger evaluate(BigInteger value) { + Integer a = 1; + return value == null? null: value.add(new BigInteger(String.valueOf(a))); + } +} diff --git a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/SmallintTest.java b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/SmallintTest.java new file mode 100644 index 0000000000..950d2f9a43 --- /dev/null +++ b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/SmallintTest.java @@ -0,0 +1,26 @@ +// 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.doris.udf; + +import org.apache.hadoop.hive.ql.exec.UDF; + +public class SmallintTest extends UDF { + public Short evaluate(Short value) { + return value == null? null: value; + } +} diff --git a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/TinyintTest.java b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/TinyintTest.java new file mode 100644 index 0000000000..2d8c42f26d --- /dev/null +++ b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/TinyintTest.java @@ -0,0 +1,26 @@ +// 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.doris.udf; + +import org.apache.hadoop.hive.ql.exec.UDF; + +public class TinyintTest extends UDF { + public Byte evaluate(Byte value) { + return value == null? null: value; + } +} diff --git a/regression-test/suites/javaudf_p0/test_javaudf_boolean.groovy b/regression-test/suites/javaudf_p0/test_javaudf_boolean.groovy new file mode 100644 index 0000000000..f45caf31f6 --- /dev/null +++ b/regression-test/suites/javaudf_p0/test_javaudf_boolean.groovy @@ -0,0 +1,70 @@ +// 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. + +import org.codehaus.groovy.runtime.IOGroovyMethods + +import java.nio.charset.StandardCharsets +import java.nio.file.Files +import java.nio.file.Paths + +suite("test_javaudf_boolean") { + def tableName = "test_javaudf_boolean" + def jarPath = """${context.file.parent}/jars/java-udf-case-jar-with-dependencies.jar""" + + log.info("Jar path: ${jarPath}".toString()) + try { + sql """ DROP TABLE IF EXISTS ${tableName} """ + sql """ + CREATE TABLE IF NOT EXISTS ${tableName} ( + `user_id` INT NOT NULL COMMENT "", + `boo_1` BOOLEAN NOT NULL COMMENT "" + ) + DISTRIBUTED BY HASH(user_id) PROPERTIES("replication_num" = "1"); + """ + + sql """ INSERT INTO ${tableName} (`user_id`,`boo_1`) VALUES + (111,true), + (112,false), + (113,0), + (114,1) + """ + qt_select_default """ SELECT * FROM ${tableName} t ORDER BY user_id; """ + + File path = new File(jarPath) + if (!path.exists()) { + throw new IllegalStateException("""${jarPath} doesn't exist! """) + } + + sql """ CREATE FUNCTION java_udf_boolean_test(BOOLEAN) RETURNS BOOLEAN PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.BooleanTest", + "type"="JAVA_UDF" + ); """ + + qt_select """ SELECT java_udf_boolean_test(1) as result; """ + qt_select """ SELECT java_udf_boolean_test(0) as result ; """ + qt_select """ SELECT java_udf_boolean_test(true) as result ; """ + qt_select """ SELECT java_udf_boolean_test(false) as result ; """ + qt_select """ SELECT java_udf_boolean_test(null) as result ; """ + qt_select """ SELECT user_id,java_udf_boolean_test(boo_1) as result FROM ${tableName} order by user_id; """ + + + sql """ DROP FUNCTION java_udf_boolean_test(BOOLEAN); """ + } finally { + try_sql("DROP TABLE IF EXISTS ${tableName}") + } +} diff --git a/regression-test/suites/javaudf_p0/test_javaudf_addone.groovy b/regression-test/suites/javaudf_p0/test_javaudf_case.groovy similarity index 59% rename from regression-test/suites/javaudf_p0/test_javaudf_addone.groovy rename to regression-test/suites/javaudf_p0/test_javaudf_case.groovy index 2c89814842..fd7e5d0178 100644 --- a/regression-test/suites/javaudf_p0/test_javaudf_addone.groovy +++ b/regression-test/suites/javaudf_p0/test_javaudf_case.groovy @@ -21,8 +21,8 @@ import java.nio.charset.StandardCharsets import java.nio.file.Files import java.nio.file.Paths -suite("test_javaudf_addone") { - def tableName = "test_javaudf_addone" +suite("test_javaudf_case") { + def tableName = "test_javaudf_dateWindowRollup" def jarPath = """${context.file.parent}/jars/java-udf-case-jar-with-dependencies.jar""" log.info("Jar path: ${jarPath}".toString()) @@ -30,39 +30,38 @@ suite("test_javaudf_addone") { sql """ DROP TABLE IF EXISTS ${tableName} """ sql """ CREATE TABLE IF NOT EXISTS ${tableName} ( - `user_id` INT NOT NULL COMMENT "用户id" + `starttime` Date NOT NULL COMMENT "", + `start` int NOT NULL COMMENT "", + `end` int COMMENT "" ) - DISTRIBUTED BY HASH(user_id) PROPERTIES("replication_num" = "1"); + DISTRIBUTED BY HASH(starttime) PROPERTIES("replication_num" = "1"); """ - StringBuilder sb = new StringBuilder() - int i = 1 - for (; i < 10; i ++) { - sb.append(""" - (${i}), - """) - } - sb.append(""" - (${i}) - """) - sql """ INSERT INTO ${tableName} VALUES - ${sb.toString()} + + + sql """ INSERT INTO ${tableName} (`starttime`,`start`,`end`) VALUES + ("2022-10-21",-3,0), + ("2022-10-25",-7,-3), + ("2022-10-26",-7,null) """ - qt_select_default """ SELECT * FROM ${tableName} t ORDER BY user_id; """ + qt_select_default """ SELECT * FROM ${tableName} t ORDER BY starttime; """ File path = new File(jarPath) if (!path.exists()) { throw new IllegalStateException("""${jarPath} doesn't exist! """) } - sql """ CREATE FUNCTION java_udf_add_one(int) RETURNS int PROPERTIES ( + sql """ CREATE FUNCTION java_udf_dateWindowRollup_test(date,int,int) RETURNS String PROPERTIES ( "file"="file://${jarPath}", - "symbol"="org.apache.doris.udf.AddOne", + "symbol"="org.apache.doris.udf.DateWindowRollup", "type"="JAVA_UDF" ); """ - qt_select """ SELECT java_udf_add_one(user_id) result FROM ${tableName} ORDER BY result; """ + qt_select """ SELECT java_udf_dateWindowRollup_test("2022-10-24",-7,0) as result; """ + qt_select """ SELECT java_udf_dateWindowRollup_test(null,-7,0) as result ; """ + qt_select """ SELECT starttime,java_udf_dateWindowRollup_test(starttime,start,end) as sum FROM ${tableName} order by starttime; """ + - sql """ DROP FUNCTION java_udf_add_one(int); """ + sql """ DROP FUNCTION java_udf_dateWindowRollup_test(date,int,int); """ } finally { try_sql("DROP TABLE IF EXISTS ${tableName}") } diff --git a/regression-test/suites/javaudf_p0/test_javaudf_decimal.groovy b/regression-test/suites/javaudf_p0/test_javaudf_decimal.groovy new file mode 100644 index 0000000000..2ca1121eb8 --- /dev/null +++ b/regression-test/suites/javaudf_p0/test_javaudf_decimal.groovy @@ -0,0 +1,70 @@ +// 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. + +import org.codehaus.groovy.runtime.IOGroovyMethods + +import java.nio.charset.StandardCharsets +import java.nio.file.Files +import java.nio.file.Paths + +suite("test_javaudf_decimal") { + def tableName = "test_javaudf_decimal" + def jarPath = """${context.file.parent}/jars/java-udf-case-jar-with-dependencies.jar""" + + log.info("Jar path: ${jarPath}".toString()) + try { + sql """ DROP TABLE IF EXISTS ${tableName} """ + sql """ + CREATE TABLE IF NOT EXISTS ${tableName} ( + `user_id` INT NOT NULL COMMENT "", + `cost_1` decimal(27,9) NOT NULL COMMENT "", + `cost_2` decimal(27,9) COMMENT "" + ) + DISTRIBUTED BY HASH(user_id) PROPERTIES("replication_num" = "1"); + """ + + + sql """ INSERT INTO ${tableName} (`user_id`,`cost_1`,`cost_2`) VALUES + (111,11111.11111,222222.3333333), + (112,1234556.11111,222222.3333333), + (113,87654321.11111,null) + """ + qt_select_default """ SELECT * FROM ${tableName} t ORDER BY user_id; """ + + File path = new File(jarPath) + if (!path.exists()) { + throw new IllegalStateException("""${jarPath} doesn't exist! """) + } + + sql """ CREATE FUNCTION java_udf_decimal_test(decimal(27,9),decimal(27,9)) RETURNS decimal(27,9) PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.DecimalTest", + "type"="JAVA_UDF" + ); """ + + qt_select """ SELECT java_udf_decimal_test(cast(2.83645 as decimal(27,9)),cast(111.1111111 as decimal(27,9))) as result; """ + qt_select """ SELECT java_udf_decimal_test(2.83645,111.1111111) as result ; """ + qt_select """ SELECT java_udf_decimal_test(2.83645,null) as result ; """ + qt_select """ SELECT java_udf_decimal_test(cast(2.83645 as decimal(27,9)),null) as result ; """ + qt_select """ SELECT user_id,java_udf_decimal_test(cost_1, cost_2) as sum FROM ${tableName} order by user_id; """ + + + sql """ DROP FUNCTION java_udf_decimal_test(decimal(27,9),decimal(27,9)); """ + } finally { + try_sql("DROP TABLE IF EXISTS ${tableName}") + } +} diff --git a/regression-test/suites/javaudf_p0/test_javaudf_float.groovy b/regression-test/suites/javaudf_p0/test_javaudf_float.groovy new file mode 100644 index 0000000000..a1bd2426c9 --- /dev/null +++ b/regression-test/suites/javaudf_p0/test_javaudf_float.groovy @@ -0,0 +1,87 @@ +// 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. + +import org.codehaus.groovy.runtime.IOGroovyMethods + +import java.nio.charset.StandardCharsets +import java.nio.file.Files +import java.nio.file.Paths + +suite("test_javaudf_float") { + def tableName = "test_javaudf_float" + def jarPath = """${context.file.parent}/jars/java-udf-case-jar-with-dependencies.jar""" + + log.info("Jar path: ${jarPath}".toString()) + try { + sql """ DROP TABLE IF EXISTS ${tableName} """ + sql """ + CREATE TABLE IF NOT EXISTS ${tableName} ( + `user_id` INT NOT NULL COMMENT "", + `float_1` FLOAT NOT NULL COMMENT "", + `float_2` FLOAT COMMENT "", + `double_1` DOUBLE NOT NULL COMMENT "", + `double_2` DOUBLE COMMENT "" + ) + DISTRIBUTED BY HASH(user_id) PROPERTIES("replication_num" = "1"); + """ + + + sql """ INSERT INTO ${tableName} (`user_id`,`float_1`,`float_2`,double_1,double_2) VALUES + (111,11111.11111,222222.3333333,12345678.34455677,1111111.999999999999), + (112,1234556.11111,222222.3333333,222222222.3333333333333,4444444444444.555555555555), + (113,87654321.11111,null,6666666666.6666666666,null) + """ + qt_select_default """ SELECT * FROM ${tableName} t ORDER BY user_id; """ + + File path = new File(jarPath) + if (!path.exists()) { + throw new IllegalStateException("""${jarPath} doesn't exist! """) + } + + sql """ CREATE FUNCTION java_udf_float_test(FLOAT,FLOAT) RETURNS FLOAT PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.FloatTest", + "type"="JAVA_UDF" + ); """ + + qt_select """ SELECT java_udf_float_test(cast(2.83645 as float),cast(111.1111111 as float)) as result; """ + qt_select """ SELECT java_udf_float_test(2.83645,111.1111111) as result ; """ + qt_select """ SELECT java_udf_float_test(2.83645,null) as result ; """ + qt_select """ SELECT java_udf_float_test(cast(2.83645 as float),null) as result ; """ + qt_select """ SELECT user_id,java_udf_float_test(float_1, float_2) as sum FROM ${tableName} order by user_id; """ + + + sql """ DROP FUNCTION java_udf_float_test(FLOAT,FLOAT); """ + + sql """ CREATE FUNCTION java_udf_double_test(DOUBLE,DOUBLE) RETURNS DOUBLE PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.DoubleTest", + "type"="JAVA_UDF" + ); """ + + qt_select """ SELECT java_udf_double_test(cast(2.83645 as DOUBLE),cast(111.1111111 as DOUBLE)) as result; """ + qt_select """ SELECT java_udf_double_test(2.83645,111.1111111) as result ; """ + qt_select """ SELECT java_udf_double_test(2.83645,null) as result ; """ + qt_select """ SELECT java_udf_double_test(cast(2.83645 as DOUBLE),null) as result ; """ + qt_select """ SELECT user_id,java_udf_double_test(double_1, double_1) as sum FROM ${tableName} order by user_id; """ + + + sql """ DROP FUNCTION java_udf_double_test(DOUBLE,DOUBLE); """ + } finally { + try_sql("DROP TABLE IF EXISTS ${tableName}") + } +} diff --git a/regression-test/suites/javaudf_p0/test_javaudf_int.groovy b/regression-test/suites/javaudf_p0/test_javaudf_int.groovy new file mode 100644 index 0000000000..6e8a590b5d --- /dev/null +++ b/regression-test/suites/javaudf_p0/test_javaudf_int.groovy @@ -0,0 +1,118 @@ +// 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. + +import org.codehaus.groovy.runtime.IOGroovyMethods + +import java.nio.charset.StandardCharsets +import java.nio.file.Files +import java.nio.file.Paths + +suite("test_javaudf_int") { + def tableName = "test_javaudf_int" + def jarPath = """${context.file.parent}/jars/java-udf-case-jar-with-dependencies.jar""" + + log.info("Jar path: ${jarPath}".toString()) + try { + sql """ DROP TABLE IF EXISTS ${tableName} """ + sql """ + CREATE TABLE IF NOT EXISTS ${tableName} ( + `user_id` INT NOT NULL COMMENT "", + `tinyint_col` TINYINT NOT NULL COMMENT "", + `smallint_col` SMALLINT NOT NULL COMMENT "", + `bigint_col` BIGINT NOT NULL COMMENT "", + `largeint_col` LARGEINT NOT NULL COMMENT "" + ) + DISTRIBUTED BY HASH(user_id) PROPERTIES("replication_num" = "1"); + """ + StringBuilder sb = new StringBuilder() + int i = 1 + for (; i < 10; i ++) { + sb.append(""" + (${i},${i}*2,${i}*3,${i}*4,${i}*5), + """) + } + sb.append(""" + (${i},${i}*2,${i}*3,${i}*4,${i}*5) + """) + sql """ INSERT INTO ${tableName} VALUES + ${sb.toString()} + """ + qt_select_default """ SELECT * FROM ${tableName} t ORDER BY user_id; """ + + File path = new File(jarPath) + if (!path.exists()) { + throw new IllegalStateException("""${jarPath} doesn't exist! """) + } + + sql """ CREATE FUNCTION java_udf_int_test(int) RETURNS int PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.IntTest", + "type"="JAVA_UDF" + ); """ + + qt_select """ SELECT java_udf_int_test(user_id) result FROM ${tableName} ORDER BY result; """ + qt_select """ SELECT java_udf_int_test(null) result ; """ + + sql """ DROP FUNCTION java_udf_int_test(int); """ + + sql """ CREATE FUNCTION java_udf_tinyint_test(tinyint) RETURNS tinyint PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.TinyintTest", + "type"="JAVA_UDF" + ); """ + + qt_select """ SELECT java_udf_tinyint_test(tinyint_col) result FROM ${tableName} ORDER BY result; """ + qt_select """ SELECT java_udf_tinyint_test(null) result ; """ + + sql """ DROP FUNCTION java_udf_tinyint_test(tinyint); """ + + sql """ CREATE FUNCTION java_udf_smallint_test(smallint) RETURNS smallint PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.SmallintTest", + "type"="JAVA_UDF" + ); """ + + qt_select """ SELECT java_udf_smallint_test(smallint_col) result FROM ${tableName} ORDER BY result; """ + qt_select """ SELECT java_udf_smallint_test(null) result ; """ + + sql """ DROP FUNCTION java_udf_smallint_test(smallint); """ + + sql """ CREATE FUNCTION java_udf_bigint_test(bigint) RETURNS bigint PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.BigintTest", + "type"="JAVA_UDF" + ); """ + + qt_select """ SELECT java_udf_bigint_test(bigint_col) result FROM ${tableName} ORDER BY result; """ + qt_select """ SELECT java_udf_bigint_test(null) result ; """ + + sql """ DROP FUNCTION java_udf_bigint_test(bigint); """ + + sql """ CREATE FUNCTION java_udf_largeint_test(largeint) RETURNS largeint PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.LargeintTest", + "type"="JAVA_UDF" + ); """ + + qt_select """ SELECT java_udf_largeint_test(largeint_col) result FROM ${tableName} ORDER BY result; """ + qt_select """ SELECT java_udf_largeint_test(null) result ; """ + + sql """ DROP FUNCTION java_udf_largeint_test(largeint); """ + } finally { + try_sql("DROP TABLE IF EXISTS ${tableName}") + } +} diff --git a/regression-test/suites/javaudf_p0/test_javaudf_string.groovy b/regression-test/suites/javaudf_p0/test_javaudf_string.groovy index 32977e6b85..479acb5699 100644 --- a/regression-test/suites/javaudf_p0/test_javaudf_string.groovy +++ b/regression-test/suites/javaudf_p0/test_javaudf_string.groovy @@ -30,20 +30,22 @@ suite("test_javaudf_string") { sql """ DROP TABLE IF EXISTS ${tableName} """ sql """ CREATE TABLE IF NOT EXISTS ${tableName} ( - `user_id` INT NOT NULL COMMENT "用户id", - `string_col` VARCHAR(10) NOT NULL COMMENT "用户id" + `user_id` INT NOT NULL COMMENT "用户id", + `char_col` CHAR NOT NULL COMMENT "", + `varchar_col` VARCHAR(10) NOT NULL COMMENT "", + `string_col` STRING NOT NULL COMMENT "" ) DISTRIBUTED BY HASH(user_id) PROPERTIES("replication_num" = "1"); """ StringBuilder sb = new StringBuilder() int i = 1 - for (; i < 10; i ++) { + for (; i < 9; i ++) { sb.append(""" - (${i}, 'abcdefg${i}'), + (${i}, '${i}','abcdefg${i}','poiuytre${i}abcdefg'), """) } sb.append(""" - (${i}, 'abcdefg${i}') + (${i}, '${i}','abcdefg${i}','poiuytre${i}abcdefg') """) sql """ INSERT INTO ${tableName} VALUES ${sb.toString()} @@ -61,7 +63,8 @@ suite("test_javaudf_string") { "type"="JAVA_UDF" ); """ - qt_select """ SELECT java_udf_string_test(string_col, 2, 3) result FROM ${tableName} ORDER BY result; """ + qt_select """ SELECT java_udf_string_test(varchar_col, 2, 3) result FROM ${tableName} ORDER BY result; """ + qt_select """ SELECT java_udf_string_test(string_col, 2, 3) result FROM ${tableName} ORDER BY result; """ qt_select """ SELECT java_udf_string_test('abcdef', 2, 3), java_udf_string_test('abcdefg', 2, 3) result FROM ${tableName} ORDER BY result; """ sql """ DROP FUNCTION java_udf_string_test(string, int, int); """ diff --git a/samples/doris-demo/java-udf-demo/src/main/java/org/apache/doris/udf/DateWindowRollup.java b/samples/doris-demo/java-udf-demo/src/main/java/org/apache/doris/udf/DateWindowRollup.java new file mode 100644 index 0000000000..76bb2c0e75 --- /dev/null +++ b/samples/doris-demo/java-udf-demo/src/main/java/org/apache/doris/udf/DateWindowRollup.java @@ -0,0 +1,54 @@ +// 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.doris.udf; + +import org.apache.hadoop.hive.ql.exec.UDF; + +import org.apache.hadoop.hive.ql.exec.UDF; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; + +public class DateWindowRollup extends UDF { + + /** + * @param startTime yyyy-MM-dd + * @param start 从startTime 前-start天 + * @param end 至startTime 前-end天 + * @return ${yyyy-MM-dd}+end ,${yyyy-MM-dd}+(end+1),...,${yyyy-MM-dd}+(start) + */ + public String evaluate(LocalDate startTime, Integer start, Integer end) { + if (startTime == null || start == null || end == null){ + return null; + } + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDate yesterday = LocalDate.now().minusDays(1); + List ret = new ArrayList<>(); + for (int i = start; i <= end - 1; i++) { + LocalDate groupDate = startTime.plusDays(i); + if (groupDate.isAfter(yesterday)) { + break; + } + String dateFormat = formatter.format(groupDate); + ret.add(dateFormat); + } + return String.join(",", ret); + } + +} \ No newline at end of file