-
Notifications
You must be signed in to change notification settings - Fork 181
[ISSUE-789] Add string functions TRIM, LPAD, RPAD #811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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()) { | ||
| 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()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
MySQL's default TRIM behavior removes spaces, so |
||
| } | ||
|
|
||
| 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'), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 '), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a case containing tabs, such as |
||
| trim('world'), | ||
| trim(' a b c ') | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String.length(),substring(), andStringBuilder.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"😀".