Skip to content

Commit

Permalink
[INLONG-10869][SDK] Transform SQL supports case conversion of strings (
Browse files Browse the repository at this point in the history
…#10878)

* [INLONG-10869][SDK] Transform SQL supports case conversion of strings

* [INLONG-10864][SDK] Increase the judgment of null pointers

* [INLONG-10869][SDK] Modified the handling logic for null and added relevant testing code

---------

Co-authored-by: ZKpLo <[email protected]>
  • Loading branch information
Zkplo and ZKpLo authored Aug 27, 2024
1 parent d5a4eba commit de1189f
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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;

/**
* LowerFunction
* description: LOWER(s): Convert all letters of the string s to lowercase letters
*/
public class LowerFunction implements ValueParser {

private ValueParser stringParser;

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

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object stringObj = stringParser.parse(sourceData, rowIndex, context);
if (stringObj == null)
return null;
return stringObj.toString().toLowerCase();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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;

/**
* UpperFunction
* description: UPPER(s): Convert a string to uppercase
*/
public class UpperFunction implements ValueParser {

private ValueParser stringParser;

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

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object stringObj = stringParser.parse(sourceData, rowIndex, context);
if (stringObj == null)
return null;
return stringObj.toString().toUpperCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.inlong.sdk.transform.process.function.Log10Function;
import org.apache.inlong.sdk.transform.process.function.Log2Function;
import org.apache.inlong.sdk.transform.process.function.LogFunction;
import org.apache.inlong.sdk.transform.process.function.LowerFunction;
import org.apache.inlong.sdk.transform.process.function.ModuloFunction;
import org.apache.inlong.sdk.transform.process.function.NowFunction;
import org.apache.inlong.sdk.transform.process.function.PowerFunction;
Expand All @@ -54,6 +55,7 @@
import org.apache.inlong.sdk.transform.process.function.ToTimestampFunction;
import org.apache.inlong.sdk.transform.process.function.TrimFunction;
import org.apache.inlong.sdk.transform.process.function.UnixTimestampFunction;
import org.apache.inlong.sdk.transform.process.function.UpperFunction;
import org.apache.inlong.sdk.transform.process.parser.AdditionParser;
import org.apache.inlong.sdk.transform.process.parser.ColumnParser;
import org.apache.inlong.sdk.transform.process.parser.DateParser;
Expand Down Expand Up @@ -160,6 +162,8 @@ public class OperatorTools {
functionMap.put("to_timestamp", ToTimestampFunction::new);
functionMap.put("mod", ModuloFunction::new);
functionMap.put("to_base64", ToBase64Function::new);
functionMap.put("lower", LowerFunction::new);
functionMap.put("upper", UpperFunction::new);
functionMap.put("length", LengthFunction::new);
functionMap.put("replace", ReplaceFunction::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,62 @@ public class TestTransformStringFunctionsProcessor {
kvSink = new KvSinkInfo("UTF-8", dstFields);
}

@Test
public void testLowerFunction() throws Exception {
String transformSql1 = "select lower(string1) from source";
TransformConfig config1 = new TransformConfig(transformSql1);
TransformProcessor<String, String> processor1 = TransformProcessor
.create(config1, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case1: lower("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=apple");

// case2: lower("")
List<String> output2 = processor1.transform("|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=");

// case3: lower(null)
String transformSql2 = "select lower(xxd) from source";
TransformConfig config2 = new TransformConfig(transformSql2);
TransformProcessor<String, String> processor2 = TransformProcessor
.create(config2, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
List<String> output3 = processor2.transform("ApPlE|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output3.size());
Assert.assertEquals(output3.get(0), "result=null");
}

@Test
public void testUpperFunction() throws Exception {
String transformSql1 = "select upper(string1) from source";
TransformConfig config1 = new TransformConfig(transformSql1);
TransformProcessor<String, String> processor1 = TransformProcessor
.create(config1, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case1: upper("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=APPLE");

// case2: upper("")
List<String> output2 = processor1.transform("|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=");

// case3: upper(null)
String transformSql2 = "select upper(xxd) from source";
TransformConfig config2 = new TransformConfig(transformSql2);
TransformProcessor<String, String> processor2 = TransformProcessor
.create(config2, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
List<String> output3 = processor2.transform("ApPlE|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output3.size());
Assert.assertEquals(output3.get(0), "result=null");
}

@Test
public void testSubstringFunction() throws Exception {
String transformSql1 = "select substring(string2, numeric1) from source";
Expand Down

0 comments on commit de1189f

Please sign in to comment.