Skip to content

Commit

Permalink
[INLONG-10823][SDK] Transform SQL support Reverse function (#10854)
Browse files Browse the repository at this point in the history
  • Loading branch information
ying-hua authored Aug 27, 2024
1 parent e30e493 commit 6ce335e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.Function;
/**
* ReverseFunction
* description: reverse(string)--returns the string with the order of the characters reversed.
* returns NULL if string is a empty string.
*/
public class ReverseFunction implements ValueParser {

private ValueParser stringParser;
public ReverseFunction(Function expr) {
stringParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
}

/**
* parse
* Parse and reverse string by reverse() in StringBuilder.
* @param sourceData
* @param rowIndex
* @param context
* @return
*/

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object stringObj = stringParser.parse(sourceData, rowIndex, context);
if (stringObj == null) {
return null;
}
String str = OperatorTools.parseString(stringObj);
return new StringBuilder(str).reverse().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.inlong.sdk.transform.process.function.NowFunction;
import org.apache.inlong.sdk.transform.process.function.PowerFunction;
import org.apache.inlong.sdk.transform.process.function.ReplicateFunction;
import org.apache.inlong.sdk.transform.process.function.ReverseFunction;
import org.apache.inlong.sdk.transform.process.function.RoundFunction;
import org.apache.inlong.sdk.transform.process.function.SinFunction;
import org.apache.inlong.sdk.transform.process.function.SinhFunction;
Expand Down Expand Up @@ -120,6 +121,7 @@ public class OperatorTools {
functionMap.put("log2", Log2Function::new);
functionMap.put("log", LogFunction::new);
functionMap.put("exp", ExpFunction::new);
functionMap.put("reverse", ReverseFunction::new);
functionMap.put("substring", SubstringFunction::new);
functionMap.put("trim", TrimFunction::new);
functionMap.put("replicate", ReplicateFunction::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class TestTransformStringFunctionsProcessor {
private static final List<FieldInfo> dstFields = new ArrayList<>();
private static final CsvSourceInfo csvSource;
private static final KvSinkInfo kvSink;

static {
for (int i = 1; i < 4; i++) {
FieldInfo field = new FieldInfo();
Expand Down Expand Up @@ -118,6 +119,7 @@ public void testLocateFunction() throws Exception {
Assert.assertEquals(1, output5.size());
Assert.assertEquals(output5.get(0), "result=null");
}

@Test
public void testReplicateFunction() throws Exception {
String transformSql1 = "select replicate(string1, numeric1) from source";
Expand Down Expand Up @@ -184,6 +186,36 @@ public void testTrimFunction() throws Exception {
Assert.assertEquals(output3.get(0), "result=in long");
}

@Test
public void testReverseFunction() throws Exception {
String transformSql1 = "select reverse(string1) from source";
TransformConfig config1 = new TransformConfig(transformSql1);
TransformProcessor<String, String> processor1 = TransformProcessor
.create(config1, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case1: reverse('apple')
List<String> output1 = processor1.transform("apple|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals(output1.get(0), "result=elppa");
// case2: reverse('ban ana ')
String transformSql2 = "select reverse(string2) from source";
TransformConfig config2 = new TransformConfig(transformSql2);
TransformProcessor<String, String> processor2 = TransformProcessor
.create(config2, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
List<String> output2 = processor2.transform("apple|ban ana |cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result= ana nab");
// case3: reverse(12345)
List<String> output3 = processor1.transform("12345|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output3.size());
Assert.assertEquals(output3.get(0), "result=54321");
// case4: reverse(null)
List<String> output4 = processor1.transform("|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output4.size());
Assert.assertEquals(output4.get(0), "result=");
}

@Test
public void testToBase64Function() throws Exception {
String transformSql = "select to_base64(string1) from source";
Expand Down

0 comments on commit 6ce335e

Please sign in to comment.