diff --git a/be/src/udf/java/java_udf.cpp b/be/src/udf/java/java_udf.cpp index d1a03cc7f1e41..a2175cb3ac2ca 100644 --- a/be/src/udf/java/java_udf.cpp +++ b/be/src/udf/java/java_udf.cpp @@ -399,14 +399,11 @@ jobject JVMFunctionHelper::newString(const char* data, size_t size) { return nstr; } -size_t JVMFunctionHelper::string_length(jstring jstr) { - return _env->GetStringUTFLength(jstr); -} - Slice JVMFunctionHelper::sliceVal(jstring jstr, std::string* buffer) { - size_t length = this->string_length(jstr); - buffer->resize(length); - _env->GetStringUTFRegion(jstr, 0, length, buffer->data()); + const size_t utf_length = _env->GetStringUTFLength(jstr); + buffer->resize(utf_length); + const size_t string_length = _env->GetStringLength(jstr); + _env->GetStringUTFRegion(jstr, 0, string_length, buffer->data()); return {buffer->data(), buffer->length()}; } diff --git a/test/sql/test_udf/R/test_jvm_udf b/test/sql/test_udf/R/test_jvm_udf new file mode 100644 index 0000000000000..53039e414b98b --- /dev/null +++ b/test/sql/test_udf/R/test_jvm_udf @@ -0,0 +1,141 @@ +-- name: test_jvm_udf +set enable_group_execution = true; +-- result: +-- !result +CREATE AGGREGATE FUNCTION sumbigint(bigint) +RETURNS bigint +symbol = "Sumbigint" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FSumbigint.jar"; +-- result: +-- !result +CREATE TABLE FUNCTION udtfstring(string) +RETURNS string +symbol = "UDTFstring" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFstring.jar"; +-- result: +-- !result +CREATE TABLE FUNCTION udtfstring_wrong_match(string) +RETURNS int +symbol = "UDTFstring" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFstring.jar"; +-- result: +-- !result +CREATE TABLE FUNCTION udtfint(int) +RETURNS int +symbol = "UDTFint" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFint.jar"; +-- result: +-- !result +CREATE TABLE FUNCTION udtfbigint(bigint) +RETURNS bigint +symbol = "UDTFbigint" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFbigint.jar"; +-- result: +-- !result +CREATE TABLE FUNCTION udtffloat(float) +RETURNS float +symbol = "UDTFfloat" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFfloat.jar"; +-- result: +-- !result +CREATE TABLE FUNCTION udtfdouble(double) +RETURNS double +symbol = "UDTFdouble" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFdouble.jar"; +-- result: +-- !result +CREATE TABLE `t0` ( + `c0` int(11) NULL COMMENT "", + `c1` varchar(20) NULL COMMENT "", + `c2` varchar(200) NULL COMMENT "", + `c3` int(11) NULL COMMENT "" +) ENGINE=OLAP +DUPLICATE KEY(`c0`, `c1`) +COMMENT "OLAP" +PROPERTIES ( +"replication_num" = "1" +); +-- result: +-- !result +insert into t0 SELECT generate_series, generate_series, generate_series, generate_series FROM TABLE(generate_series(1, 40960)); +-- result: +-- !result +select count(udtfstring) from t0, udtfstring(c1); +-- result: +81920 +-- !result +select count(udtfstring_wrong_match) from t0, udtfstring_wrong_match(c1); +-- result: +E: (1064, 'Type not matched, expect class java.lang.Integer, but got class java.lang.String') +-- !result +select count(udtfint) from t0, udtfint(c1); +-- result: +81920 +-- !result +select count(udtfbigint) from t0, udtfbigint(c1); +-- result: +81920 +-- !result +select count(udtffloat) from t0, udtffloat(c1); +-- result: +81920 +-- !result +select count(udtfdouble) from t0, udtfdouble(c1); +-- result: +81920 +-- !result +select * from TABLE(udtfstring("")); +-- result: +-- !result +select * from TABLE(udtfstring("▁▂▃▄▅▆▇█")); +-- result: +▁▂▃▄▅▆▇█ +▁▂▃▄▅▆▇█ +-- !result +select * from TABLE(udtfstring("中文测试")); +-- result: +中文测试 +中文测试 +-- !result +select * from TABLE(udtfstring("∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა")); +-- result: +∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა +∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა +-- !result +select * from TABLE(udtfstring("2H₂ + O₂ ⇌ 2H₂O")); +-- result: +2H₂ + O₂ ⇌ 2H₂O +2H₂ + O₂ ⇌ 2H₂O +-- !result +select * from TABLE(udtfstring("ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ᚩᚾ ᚦᚫᛗ ᛚᚪᚾᛞᛖ ᚾᚩᚱᚦᚹᛖᚪᚱᛞᚢᛗ ᚹᛁᚦ ᚦᚪ ᚹᛖᛥᚫ")); +-- result: +ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ᚩᚾ ᚦᚫᛗ ᛚᚪᚾᛞᛖ ᚾᚩᚱᚦᚹᛖᚪᚱᛞᚢᛗ ᚹᛁᚦ ᚦᚪ ᚹᛖᛥᚫ +ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ᚩᚾ ᚦᚫᛗ ᛚᚪᚾᛞᛖ ᚾᚩᚱᚦᚹᛖᚪᚱᛞᚢᛗ ᚹᛁᚦ ᚦᚪ ᚹᛖᛥᚫ +-- !result +set streaming_preaggregation_mode="force_streaming"; +-- result: +-- !result +select sum(delta), count(*), count(delta) from (select (sum(c3) - sumbigint(c3)) as delta from t0 group by c0,c1 limit 10) tb; +-- result: +0 10 10 +-- !result +set streaming_preaggregation_mode="auto"; +-- result: +-- !result +set enable_spill=true; +-- result: +-- !result +set spill_mode="force"; +-- result: +-- !result +select sum(delta), count(*), count(delta) from (select (sum(c3) - sumbigint(c3)) as delta from t0 group by c0,c1) tb; +-- result: +0 40960 40960 +-- !result \ No newline at end of file diff --git a/test/sql/test_udf/T/test_jvm_udf b/test/sql/test_udf/T/test_jvm_udf new file mode 100644 index 0000000000000..30b2a3706de4d --- /dev/null +++ b/test/sql/test_udf/T/test_jvm_udf @@ -0,0 +1,87 @@ +-- name: test_jvm_udf + +set enable_group_execution = true; + +CREATE AGGREGATE FUNCTION sumbigint(bigint) +RETURNS bigint +symbol = "Sumbigint" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FSumbigint.jar"; + +CREATE TABLE FUNCTION udtfstring(string) +RETURNS string +symbol = "UDTFstring" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFstring.jar"; + +CREATE TABLE FUNCTION udtfstring_wrong_match(string) +RETURNS int +symbol = "UDTFstring" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFstring.jar"; + +CREATE TABLE FUNCTION udtfint(int) +RETURNS int +symbol = "UDTFint" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFint.jar"; + +CREATE TABLE FUNCTION udtfbigint(bigint) +RETURNS bigint +symbol = "UDTFbigint" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFbigint.jar"; + +CREATE TABLE FUNCTION udtffloat(float) +RETURNS float +symbol = "UDTFfloat" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFfloat.jar"; + +CREATE TABLE FUNCTION udtfdouble(double) +RETURNS double +symbol = "UDTFdouble" +type = "StarrocksJar" +file = "${udf_url}/starrocks-jdbc%2FUDTFdouble.jar"; + + +CREATE TABLE `t0` ( + `c0` int(11) NULL COMMENT "", + `c1` varchar(20) NULL COMMENT "", + `c2` varchar(200) NULL COMMENT "", + `c3` int(11) NULL COMMENT "" +) ENGINE=OLAP +DUPLICATE KEY(`c0`, `c1`) +COMMENT "OLAP" +PROPERTIES ( +"replication_num" = "1" +); + +insert into t0 SELECT generate_series, generate_series, generate_series, generate_series FROM TABLE(generate_series(1, 40960)); + +-- test udtf cases +select count(udtfstring) from t0, udtfstring(c1); +select count(udtfstring_wrong_match) from t0, udtfstring_wrong_match(c1); +select count(udtfint) from t0, udtfint(c1); +select count(udtfbigint) from t0, udtfbigint(c1); +select count(udtffloat) from t0, udtffloat(c1); +select count(udtfdouble) from t0, udtfdouble(c1); +-- test udtf with utf8 case +select * from TABLE(udtfstring("")); +select * from TABLE(udtfstring("▁▂▃▄▅▆▇█")); +select * from TABLE(udtfstring("中文测试")); +select * from TABLE(udtfstring("∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა")); +select * from TABLE(udtfstring("2H₂ + O₂ ⇌ 2H₂O")); +select * from TABLE(udtfstring("ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ᚩᚾ ᚦᚫᛗ ᛚᚪᚾᛞᛖ ᚾᚩᚱᚦᚹᛖᚪᚱᛞᚢᛗ ᚹᛁᚦ ᚦᚪ ᚹᛖᛥᚫ")); + + +-- test group by limit case: +set streaming_preaggregation_mode="force_streaming"; +select sum(delta), count(*), count(delta) from (select (sum(c3) - sumbigint(c3)) as delta from t0 group by c0,c1 limit 10) tb; + +-- test group by spill case: +set streaming_preaggregation_mode="auto"; +set enable_spill=true; +set spill_mode="force"; + +select sum(delta), count(*), count(delta) from (select (sum(c3) - sumbigint(c3)) as delta from t0 group by c0,c1) tb;