Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@
import org.apache.geaflow.dsl.udf.table.string.Instr;
import org.apache.geaflow.dsl.udf.table.string.IsBlank;
import org.apache.geaflow.dsl.udf.table.string.KeyValue;
import org.apache.geaflow.dsl.udf.table.string.LPad;
import org.apache.geaflow.dsl.udf.table.string.LTrim;
import org.apache.geaflow.dsl.udf.table.string.Length;
import org.apache.geaflow.dsl.udf.table.string.Like;
import org.apache.geaflow.dsl.udf.table.string.RPad;
import org.apache.geaflow.dsl.udf.table.string.RTrim;
import org.apache.geaflow.dsl.udf.table.string.RegExp;
import org.apache.geaflow.dsl.udf.table.string.RegExpExtract;
Expand All @@ -128,6 +130,7 @@
import org.apache.geaflow.dsl.udf.table.string.Space;
import org.apache.geaflow.dsl.udf.table.string.SplitEx;
import org.apache.geaflow.dsl.udf.table.string.Substr;
import org.apache.geaflow.dsl.udf.table.string.Trim;
import org.apache.geaflow.dsl.udf.table.string.UrlDecode;
import org.apache.geaflow.dsl.udf.table.string.UrlEncode;
import org.apache.geaflow.dsl.util.FunctionUtil;
Expand Down Expand Up @@ -196,6 +199,7 @@ public class BuildInSqlFunctionTable extends ListSqlOperatorTable {
.add(GeaFlowFunction.of(KeyValue.class))
.add(GeaFlowFunction.of(Length.class))
.add(GeaFlowFunction.of(Like.class))
.add(GeaFlowFunction.of(LPad.class))
.add(GeaFlowFunction.of(LTrim.class))
.add(GeaFlowFunction.of(RegExp.class))
.add(GeaFlowFunction.of(RegexpCount.class))
Expand All @@ -204,10 +208,12 @@ public class BuildInSqlFunctionTable extends ListSqlOperatorTable {
.add(GeaFlowFunction.of(Repeat.class))
.add(GeaFlowFunction.of(Replace.class))
.add(GeaFlowFunction.of(Reverse.class))
.add(GeaFlowFunction.of(RPad.class))
.add(GeaFlowFunction.of(RTrim.class))
.add(GeaFlowFunction.of(Space.class))
.add(GeaFlowFunction.of(SplitEx.class))
.add(GeaFlowFunction.of(Substr.class))
.add(GeaFlowFunction.of(Trim.class))
.add(GeaFlowFunction.of(UrlDecode.class))
.add(GeaFlowFunction.of(UrlEncode.class))
.add(GeaFlowFunction.of(GetJsonObject.class))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.geaflow.dsl.udf.table.string;

import org.apache.geaflow.common.binary.BinaryString;
import org.apache.geaflow.dsl.common.function.Description;
import org.apache.geaflow.dsl.common.function.UDF;

@Description(name = "lpad", description = "Returns the string left-padded to the given length "
+ "with the specified pad string.")
public class LPad extends UDF {

public String eval(String str, Integer length, String pad) {
return StringPadUtil.pad(str, length, pad, true);
}

public BinaryString eval(BinaryString str, Integer length, BinaryString pad) {
if (str == null || length == null || pad == null) {
return null;
}
return BinaryString.fromString(StringPadUtil.pad(
str.toString(), length, pad.toString(), true));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.geaflow.dsl.udf.table.string;

import org.apache.geaflow.common.binary.BinaryString;
import org.apache.geaflow.dsl.common.function.Description;
import org.apache.geaflow.dsl.common.function.UDF;

@Description(name = "rpad", description = "Returns the string right-padded to the given length "
+ "with the specified pad string.")
public class RPad extends UDF {

public String eval(String str, Integer length, String pad) {
return StringPadUtil.pad(str, length, pad, false);
}

public BinaryString eval(BinaryString str, Integer length, BinaryString pad) {
if (str == null || length == null || pad == null) {
return null;
}
return BinaryString.fromString(StringPadUtil.pad(
str.toString(), length, pad.toString(), false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.geaflow.dsl.udf.table.string;

final class StringPadUtil {

private StringPadUtil() {
}

static String pad(String str, Integer length, String pad, boolean left) {
if (str == null || length == null || pad == null) {
return null;
}
if (length <= 0) {
return "";
}

int strLength = str.codePointCount(0, str.length());
if (length <= strLength) {
return substring(str, length);
}
if (pad.isEmpty()) {
return str;
}

String padding = repeat(pad, length - strLength);
return left ? padding + str : str + padding;
}

private static String repeat(String pad, int length) {
int padLength = pad.codePointCount(0, pad.length());
int repeatCount = length / padLength;
int remainder = length % padLength;
StringBuilder result = new StringBuilder();
for (int i = 0; i < repeatCount; i++) {
result.append(pad);
}
if (remainder > 0) {
result.append(substring(pad, remainder));
}
return result.toString();
}

private static String substring(String value, int codePointCount) {
return value.substring(0, value.offsetByCodePoints(0, codePointCount));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.geaflow.dsl.udf.table.string;

import org.apache.commons.lang3.StringUtils;
import org.apache.geaflow.common.binary.BinaryString;
import org.apache.geaflow.dsl.common.function.Description;
import org.apache.geaflow.dsl.common.function.UDF;

@Description(name = "trim", description = "Returns a string with leading and trailing "
+ "spaces removed.")
public class Trim extends UDF {

public String eval(String str) {
return StringUtils.strip(str, " ");
}

public BinaryString eval(BinaryString str) {
if (str == null) {
return null;
}
return BinaryString.fromString(StringUtils.strip(str.toString(), " "));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.geaflow.dsl.udf.string;

import org.apache.geaflow.common.binary.BinaryString;
import org.apache.geaflow.dsl.udf.table.string.LPad;
import org.apache.geaflow.dsl.udf.table.string.RPad;
import org.testng.Assert;
import org.testng.annotations.Test;

public class PadTest {

private static final BinaryString PAD = BinaryString.fromString("xy");

@Test
public void testLPad() {
LPad lPad = new LPad();
Assert.assertEquals(lPad.eval("hi", 5, "xy"), "xyxhi");
Assert.assertEquals(lPad.eval("hello", 3, "x"), "hel");
Assert.assertEquals(lPad.eval("hi", 0, "x"), "");
Assert.assertEquals(lPad.eval("hi", -1, "x"), "");
Assert.assertEquals(lPad.eval("hi", 5, ""), "hi");
Assert.assertNull(lPad.eval((String) null, 5, "x"));
Assert.assertNull(lPad.eval("hi", null, "x"));
Assert.assertNull(lPad.eval("hi", 5, (String) null));

Assert.assertEquals(lPad.eval(BinaryString.fromString("hi"), 5, PAD),
BinaryString.fromString("xyxhi"));
Assert.assertNull(lPad.eval((BinaryString) null, 5, PAD));
}

@Test
public void testRPad() {
RPad rPad = new RPad();
Assert.assertEquals(rPad.eval("hi", 5, "xy"), "hixyx");
Assert.assertEquals(rPad.eval("hello", 3, "x"), "hel");
Assert.assertEquals(rPad.eval("hi", 0, "x"), "");
Assert.assertEquals(rPad.eval("hi", -1, "x"), "");
Assert.assertEquals(rPad.eval("hi", 5, ""), "hi");
Assert.assertNull(rPad.eval((String) null, 5, "x"));
Assert.assertNull(rPad.eval("hi", null, "x"));
Assert.assertNull(rPad.eval("hi", 5, (String) null));

Assert.assertEquals(rPad.eval(BinaryString.fromString("hi"), 5, PAD),
BinaryString.fromString("hixyx"));
Assert.assertNull(rPad.eval((BinaryString) null, 5, PAD));
}

@Test
public void testUnicodeCodePoints() {
LPad lPad = new LPad();
RPad rPad = new RPad();
Assert.assertEquals(lPad.eval("\u80a1\u7968", 4, "\u661f"),
"\u661f\u661f\u80a1\u7968");
Assert.assertEquals(rPad.eval("\u80a1\u7968", 4, "\u661f"),
"\u80a1\u7968\u661f\u661f");
Assert.assertEquals(lPad.eval("\ud83d\ude00x", 1, "y"), "\ud83d\ude00");
Assert.assertEquals(rPad.eval("x", 3, "\ud83d\ude00"),
"x\ud83d\ude00\ud83d\ude00");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.geaflow.common.binary.BinaryString;
import org.apache.geaflow.dsl.udf.table.string.LTrim;
import org.apache.geaflow.dsl.udf.table.string.RTrim;
import org.apache.geaflow.dsl.udf.table.string.Trim;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand All @@ -43,4 +44,17 @@ public void testRLTrim() {
Assert.assertEquals(rTrim.eval(BinaryString.fromString("abc ")), BinaryString.fromString("abc"));
Assert.assertEquals(rTrim.eval(BinaryString.fromString(" ")), BinaryString.fromString(""));
}

@Test
public void testTrim() {
Trim trim = new Trim();
Assert.assertEquals(trim.eval(" abc "), "abc");
Assert.assertEquals(trim.eval("\tabc\t"), "\tabc\t");
Assert.assertEquals(trim.eval(BinaryString.fromString(" abc ")),
BinaryString.fromString("abc"));
Assert.assertEquals(trim.eval(BinaryString.fromString("\tabc\t")),
BinaryString.fromString("\tabc\t"));
Assert.assertNull(trim.eval((String) null));
Assert.assertNull(trim.eval((BinaryString) null));
}
}
Original file line number Diff line number Diff line change
@@ -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.geaflow.dsl.runtime.query;

import org.testng.annotations.Test;

public class StringFunctionTest {

@Test
public void testTrim() throws Exception {
QueryTester
.build()
.withQueryPath("/query/function_trim.sql")
.execute()
.checkSinkResult();
}

@Test
public void testLPad() throws Exception {
QueryTester
.build()
.withQueryPath("/query/function_lpad.sql")
.execute()
.checkSinkResult();
}

@Test
public void testRPad() throws Exception {
QueryTester
.build()
.withQueryPath("/query/function_rpad.sql")
.execute()
.checkSinkResult();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xyxhi,hel,,hi,星星股票,null,x😀,
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hixyx,hel,,hi,股票星星,null,😀x,
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello,world,a b c,null,x hello x
Loading
Loading