forked from apache/inlong
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-11242][SDK] Transform support MAP() function (apache#11276)
- Loading branch information
1 parent
5b7764f
commit 4a2be65
Showing
2 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...sform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/MapFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
...rc/test/java/org/apache/inlong/sdk/transform/process/function/string/TestMapFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
} |