Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INLONG-11242][SDK] Transform support MAP() function #11276

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* MapFunction
* description: MAP(ANY1, ANY2, ANY3, ANY4, ...)--Returns a map created from a list of
* key-value pairs ((value1, value2), (value3, value4), …).
* for example: Map('he',7,'xxd')--return null
* Map('he',1,'xxd','cloud')--return {he=1, xxd=cloud}
* Map('xxd','cloud',map(1,2),map(3,'apple'))--return {xxd=cloud, {1=2}={3=apple}}
*/
@TransformFunction(names = {"map"})
public class MapFunction implements ValueParser {

private List<ValueParser> parserList;

public MapFunction(Function expr) {
if (expr.getParameters() == null) {
this.parserList = new ArrayList<>();
} else {
List<Expression> params = expr.getParameters().getExpressions();
parserList = new ArrayList<>(params.size());
for (Expression param : params) {
ValueParser node = OperatorTools.buildParser(param);
parserList.add(node);
}
}
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
if (parserList.size() % 2 != 0) {
throw new IllegalArgumentException("Input values must be in key-value pairs.");
}
Map<Object, Object> res = new LinkedHashMap<>();

for (int i = 0; i < parserList.size(); i += 2) {
Object key = parserList.get(i).parse(sourceData, rowIndex, context);
Object value = parserList.get(i + 1).parse(sourceData, rowIndex, context);
res.put(key, value);
}
return res;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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 TestMapFunction extends AbstractFunctionStringTestBase {

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

transformSql = "select map(string1,numeric1,string2) from source";
config = new TransformConfig(transformSql);
processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));

// case1: Map('he',7,'xxd')
data = "he|xxd|cloud|7|3|3";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=", output.get(0));

transformSql = "select map(string1,numeric1,string2,string3) from source";
config = new TransformConfig(transformSql);
processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));

// case2: Map('he',1,'xxd','cloud')
data = "he|xxd|cloud|1|3|3";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result={he=1, xxd=cloud}", output.get(0));

transformSql = "select map(numeric1,numeric2,string1,string2) from source";
config = new TransformConfig(transformSql);
processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));

// case2: Map(1,2,'cloud','xxd')
data = "1|2|3|xxd|cloud|3";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result={xxd=cloud, 1=2}", output.get(0));

transformSql = "select map(numeric1,numeric2,map(string1,string2),map(string3,numeric3)) from source";
config = new TransformConfig(transformSql);
processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));

// case2: Map('xxd','cloud',map(1,2),map(3,'apple'))
data = "1|2|3|xxd|cloud|apple";
output = processor.transform(data, new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result={xxd=cloud, {1=2}={3=apple}}", output.get(0));

}
}
Loading