Skip to content

Commit

Permalink
[INLONG-10905][SDK] Transform support Length() function (apache#10908)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zkplo authored and PeterZh6 committed Aug 27, 2024
1 parent 3a1f267 commit fbc4fd4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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;

/**
* LengthFunction
* description: length(string)
* - return the length of the string
* - return NULL if the string is NULL
*/
public class LengthFunction implements ValueParser {

private final ValueParser stringParser;

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

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object stringObject = stringParser.parse(sourceData, rowIndex, context);
if (stringObject == null) {
return null;
}
return OperatorTools.parseString(stringObject).length();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.inlong.sdk.transform.process.function.ExpFunction;
import org.apache.inlong.sdk.transform.process.function.FloorFunction;
import org.apache.inlong.sdk.transform.process.function.FromUnixTimeFunction;
import org.apache.inlong.sdk.transform.process.function.LengthFunction;
import org.apache.inlong.sdk.transform.process.function.LnFunction;
import org.apache.inlong.sdk.transform.process.function.LocateFunction;
import org.apache.inlong.sdk.transform.process.function.Log10Function;
Expand Down Expand Up @@ -144,6 +145,7 @@ public class OperatorTools {
functionMap.put("unix_timestamp", UnixTimestampFunction::new);
functionMap.put("to_timestamp", ToTimestampFunction::new);
functionMap.put("to_base64", ToBase64Function::new);
functionMap.put("length", LengthFunction::new);
}

public static ExpressionOperator buildOperator(Expression expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,27 @@ public void testToBase64Function() throws Exception {
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=aGVsbG8gd29ybGQ=");
}

@Test
public void testLengthFunction() throws Exception {
String transformSql = "select length(string1) from source";
TransformConfig config = new TransformConfig(transformSql);
TransformProcessor<String, String> processor1 = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case1: length('hello world')
List<String> output1 = processor1.transform("hello world|apple|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals("result=11", output1.get(0));

transformSql = "select length(xxd) from source";
config = new TransformConfig(transformSql);
processor1 = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case2: length(null)
output1 = processor1.transform("hello world|apple|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals("result=null", output1.get(0));
}
}

0 comments on commit fbc4fd4

Please sign in to comment.