Skip to content

Commit

Permalink
[INLONG-11229][SDK] Transform Support ELT Function (apache#11248)
Browse files Browse the repository at this point in the history
  • Loading branch information
ying-hua authored Oct 11, 2024
1 parent 1a96ec4 commit adb74d4
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.ArrayList;
import java.util.List;

/**
* EltFunction -> elt(index, expr[, exprs]*)
* description:
* - Returns the index-th expression.
* - index must be an integer between 1 and the number of expressions.
* - Returns NULL if index is NULL or out of range.
*/
@TransformFunction(names = {"elt"})
public class EltFunction implements ValueParser {

private ValueParser indexParser;
private List<ValueParser> exprParsers;

public EltFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
indexParser = OperatorTools.buildParser(expressions.get(0));
exprParsers = new ArrayList<>();
for (int i = 1; i < expressions.size(); i++) {
exprParsers.add(OperatorTools.buildParser(expressions.get(i)));
}
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object indexObject = indexParser.parse(sourceData, rowIndex, context);
if (indexObject == null) {
return null;
}

int index = OperatorTools.parseBigDecimal(indexObject).intValue();
if (index < 1 || index > exprParsers.size()) {
return null;
}

return exprParsers.get(index - 1).parse(sourceData, rowIndex, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.string;

import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory;
import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory;
import org.apache.inlong.sdk.transform.pojo.TransformConfig;
import org.apache.inlong.sdk.transform.process.TransformProcessor;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

public class TestEltFunction extends AbstractFunctionStringTestBase {

@Test
public void testEltFunction() throws Exception {
String transformSql = null, data = null;
TransformConfig config = null;
TransformProcessor<String, String> processor = null;
List<String> output = null;

// case1: elt(2, 'a', 'b', 'c')
transformSql = "select elt(numeric1, string1, string2, string3) from source";
config = new TransformConfig(transformSql);
processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
data = "a|b|c|2";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=b", output.get(0));

// case2: elt(4, 'a', 'b', 'c') - index out of range
data = "a|b|c|4";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=", output.get(0));

// case3: elt(null, 'a', 'b', 'c') - index is null
data = "a|b|c|";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=", output.get(0));

// case4: elt(2, 1, 2, 3) - expressions are numbers
transformSql = "select elt(numeric1, numeric1, numeric2, numeric3) from source";
config = new TransformConfig(transformSql);
processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
data = "a|b|c|2|2|3";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=2", output.get(0));

// case5: elt(1, 'a', 2, 'c') - mixed types
transformSql = "select elt(numeric1, string1, numeric1, string2) from source";
config = new TransformConfig(transformSql);
processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
data = "a|2|c|1";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=a", output.get(0));
}
}

0 comments on commit adb74d4

Please sign in to comment.