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 @@ -109,9 +109,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 @@ -123,6 +125,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 @@ -186,6 +189,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 @@ -194,10 +198,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,51 @@
/*
* 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.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 len, String pad) {
if (str == null || len == null || pad == null) {
return null;
}
if (len <= 0) {
return "";
}
if (len <= str.length()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.length(), substring(), and StringBuilder.length() operate on UTF-16 code units rather than Unicode code points. This makes LPAD incorrect for supplementary characters and can split a surrogate pair while truncating.

For example, lpad("😀", 2, "x") should return "x😀" under MySQL's character-based semantics, but this implementation returns "😀".

return str.substring(0, len);
}
if (pad.isEmpty()) {
return str;
}
StringBuilder sb = new StringBuilder();
int padLen = len - str.length();
while (sb.length() < padLen) {
sb.append(pad);
}
sb.setLength(padLen);
sb.append(str);
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.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 len, String pad) {
if (str == null || len == null || pad == null) {
return null;
}
if (len <= 0) {
return "";
}
if (len <= str.length()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the same UTF-16 code-unit issue as LPAD.

return str.substring(0, len);
}
if (pad.isEmpty()) {
return str;
}
StringBuilder sb = new StringBuilder();
sb.append(str);
while (sb.length() < len) {
sb.append(pad);
}
sb.setLength(len);
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 both leading and trailing whitespace removed.")
public class Trim extends UDF {

public String eval(String s) {
if (s == null) {
return null;
}
return StringUtils.strip(s);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringUtils.strip(s) removes tabs and other Java whitespace characters, while the BinaryString overload below removes only the ASCII space character. This creates overload-dependent behavior.

MySQL's default TRIM behavior removes spaces, so trim("\thello\t") should retain the tabs, but this overload currently returns "hello".

}

public BinaryString eval(BinaryString s) {
if (s == null) {
return null;
}
int l = 0;
int r = s.getLength() - 1;
while (l <= r && s.getByte(l) == ' ') {
l++;
}
while (r >= l && s.getByte(r) == ' ') {
r--;
}
return s.substring(l, r + 1);
}
}
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 @@
xxxhi,hello,hel,ababahi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hixxx,hello,hel,hiababa
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello,world,a b c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.
*/

CREATE TABLE output_console(
c1 varchar,
c2 varchar,
c3 varchar,
c4 varchar
) WITH (
type='file',
geaflow.dsl.file.path='${target}'
);

INSERT INTO output_console
SELECT
lpad('hi', 5, 'x'),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we extend the runtime tests with supplementary Unicode characters, null arguments, an empty padding string, and zero/negative target lengths?

lpad('hello', 5, 'x'),
lpad('hello', 3, 'x'),
lpad('hi', 7, 'ab')
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.
*/

CREATE TABLE output_console(
c1 varchar,
c2 varchar,
c3 varchar,
c4 varchar
) WITH (
type='file',
geaflow.dsl.file.path='${target}'
);

INSERT INTO output_console
SELECT
rpad('hi', 5, 'x'),
rpad('hello', 5, 'x'),
rpad('hello', 3, 'x'),
rpad('hi', 7, 'ab')
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

CREATE TABLE output_console(
c1 varchar,
c2 varchar,
c3 varchar
) WITH (
type='file',
geaflow.dsl.file.path='${target}'
);

INSERT INTO output_console
SELECT
trim(' hello '),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a case containing tabs, such as trim('\thello\t'), to define and verify the intended semantics. It would also be useful to cover null input here.

trim('world'),
trim(' a b c ')
Loading