Skip to content

Commit

Permalink
[INLONG-10971][SDK] Transform support INSERT function (#10972)
Browse files Browse the repository at this point in the history
  • Loading branch information
ying-hua authored Sep 3, 2024
1 parent 0a394f4 commit 3e7f81d
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.inlong.sdk.transform.process.function;

import org.apache.inlong.sdk.transform.decode.SourceData;
import org.apache.inlong.sdk.transform.process.Context;
import org.apache.inlong.sdk.transform.process.operator.OperatorTools;
import org.apache.inlong.sdk.transform.process.parser.ValueParser;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;

import java.util.List;
/**
* InsertFunction
*
* Description:
* Returns a string where a specified substring is replaced by another string, starting at a given position and for a specified length.
* If the position is out of the string's bounds, the original string is returned.
* If the length exceeds the remaining length of the string from the given position, the replacement continues to the end of the string.
* If any argument is null, the function returns null.
*
* Arguments:
* - str: The original string.
* - pos: The position to start the replacement (1-based index).
* - len: The number of characters to replace.
* - newstr: The string to insert.
*
* Examples:
* - INSERT('12345678', 3, 4, 'word') = '12word78'
* - INSERT('12345678', -1, 4, 'word') = '12345678'
* - INSERT('12345678', 3, 100, 'word') = '12word'
*/
@TransformFunction(names = {"insert"})
public class InsertFunction implements ValueParser {

private ValueParser strParser;
private ValueParser posParser;
private ValueParser lenParser;
private ValueParser newStrParser;

public InsertFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
strParser = OperatorTools.buildParser(expressions.get(0));
posParser = OperatorTools.buildParser(expressions.get(1));
lenParser = OperatorTools.buildParser(expressions.get(2));
newStrParser = OperatorTools.buildParser(expressions.get(3));
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object strObject = strParser.parse(sourceData, rowIndex, context);
Object posObject = posParser.parse(sourceData, rowIndex, context);
Object lenObject = lenParser.parse(sourceData, rowIndex, context);
Object newStrObject = newStrParser.parse(sourceData, rowIndex, context);

if (strObject == null || posObject == null || lenObject == null || newStrObject == null) {
return null;
}

String str = OperatorTools.parseString(strObject);
int pos = OperatorTools.parseBigDecimal(posObject).intValue();
int len = OperatorTools.parseBigDecimal(lenObject).intValue();
String newStr = OperatorTools.parseString(newStrObject);

if (str == null || newStr == null) {
return null;
}

if (pos < 1 || pos > str.length()) {
return str;
}

int startIndex = pos - 1;
int endIndex = Math.min(startIndex + len, str.length());

StringBuilder result = new StringBuilder();
result.append(str, 0, startIndex);
result.append(newStr);
if (endIndex < str.length()) {
result.append(str, endIndex, str.length());
}

return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -690,4 +690,38 @@ public void testTranslateFunction() throws Exception {
Assert.assertEquals(output3.get(0), "result=Apache Inlong");
}

@Test
public void testInsertFunction() throws Exception {
String transformSql1 = "select insert(string1, numeric1, numeric2, string2) from source";
TransformConfig config1 = new TransformConfig(transformSql1);
TransformProcessor<String, String> processor1 = TransformProcessor
.create(config1, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));

// case1: insert('12345678', 3, 4, 'word') -> '12word78'
List<String> output1 = processor1.transform("12345678|word|cloud|3|4|0", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals("result=12word78", output1.get(0));

// case2: insert('12345678', -1, 4, 'word') -> '12345678'
List<String> output2 = processor1.transform("12345678|word|cloud|-1|4|0", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals("result=12345678", output2.get(0));

// case3: insert('12345678', 3, 100, 'word') -> '12word'
List<String> output3 = processor1.transform("12345678|word|cloud|3|100|0", new HashMap<>());
Assert.assertEquals(1, output3.size());
Assert.assertEquals("result=12word", output3.get(0));

// case4: insert('', 3, 4, 'word') -> ''
List<String> output4 = processor1.transform("|word|cloud|3|4|0", new HashMap<>());
Assert.assertEquals(1, output4.size());
Assert.assertEquals("result=", output4.get(0));

// case5: insert('12345678', 3, 4, '') -> '1278'
List<String> output5 = processor1.transform("12345678||cloud|3|4|0", new HashMap<>());
Assert.assertEquals(1, output5.size());
Assert.assertEquals("result=1278", output5.get(0));
}

}

0 comments on commit 3e7f81d

Please sign in to comment.