[test](java-udf)add java udf RegressionTest about the currently supported data types #13972

This commit is contained in:
Liqf
2022-11-05 19:25:58 +08:00
committed by GitHub
parent d01f7c546a
commit 1724faf9a5
23 changed files with 880 additions and 65 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<String> 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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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)));
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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}")
}
}

View File

@ -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}")
}

View File

@ -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}")
}
}

View File

@ -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}")
}
}

View File

@ -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}")
}
}

View File

@ -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); """

View File

@ -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<String> 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);
}
}