feat(reasoner): add udf split_part (#378)

This commit is contained in:
wenchengyao 2024-10-29 14:46:38 +08:00 committed by GitHub
parent 74ded0a7f5
commit 0253af6cd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,32 @@
/*
* Copyright 2023 OpenSPG Authors
*
* Licensed 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.
*/
package com.antgroup.openspg.reasoner.udf.builtin.udf;
import com.antgroup.openspg.reasoner.udf.model.UdfDefine;
import org.apache.commons.lang3.StringUtils;
public class SplitPart {
@UdfDefine(name = "split_part", compatibleName = "SplitPart")
public String split(String str, String separator, Integer partNum) throws Exception {
String[] strings = StringUtils.split(str, separator);
int length = strings.length;
if (partNum < 0) {
partNum = length - 1;
}
if (partNum >= length) {
return "";
}
return strings[partNum];
}
}

View File

@ -169,6 +169,23 @@ public class UdfTest {
Assert.assertEquals(rst2, "");
}
@Test
public void testSplitPart() {
UdfMng mng = UdfMngFactory.getUdfMng();
IUdfMeta udfMeta =
mng.getUdfMeta(
"split_part",
Lists.newArrayList(KTString$.MODULE$, KTString$.MODULE$, KTInteger$.MODULE$));
Object rst1 = udfMeta.invoke("Hello,World!", ",", 0);
Assert.assertEquals("Hello", rst1);
Object rst2 = udfMeta.invoke("Hello,Ni,Hao", ",", -1);
Assert.assertEquals("Hao", rst2);
Object rst3 = udfMeta.invoke("A省B市C村XXX", "", 0);
Assert.assertEquals("A省B市C", rst3);
Object rst4 = udfMeta.invoke("A省B市C村XXX", "", 5);
Assert.assertEquals("", rst4);
}
@Test
public void testCast() {
UdfMng mng = UdfMngFactory.getUdfMng();