Skip to content

Commit

Permalink
[INLONG-11122][SDK] Transform SQL supports UNHEX function (#11131)
Browse files Browse the repository at this point in the history
Co-authored-by: ZKpLo <[email protected]>
  • Loading branch information
Zkplo and ZKpLo authored Sep 19, 2024
1 parent cde50b2 commit 9731701
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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;

/**
* UnHexFunction -> unhex(str)
* description: unhex(str) interprets each pair of characters in the argument as a hexadecimal number and converts it to the byte represented by the number.
* return null if str is null;
* return a binary string otherwise.
*/
@TransformFunction(names = {"unhex"})
public class UnHexFunction implements ValueParser {

private ValueParser valueParser;

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

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object valueObj = valueParser.parse(sourceData, rowIndex, context);
if (valueObj == null) {
return null;
}
return hexToString(OperatorTools.parseString(valueObj));
}

public static String hexToString(String hex) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i += 2) {
String str = hex.substring(i, i + 2);
char ch = (char) Integer.parseInt(str, 16);
output.append(ch);
}
return output.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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;

/**
* TestUnHexFunction
* description: test the unhex function in transform processor
*/
public class TestUnHexFunction extends AbstractFunctionStringTestBase {

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

transformSql = "select unhex(string1) from source";
config = new TransformConfig(transformSql);
processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case3: unhex("6")
output = processor.transform("6", new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=", output.get(0));
// case1: unhex("696E6C6F6E67")
output = processor.transform("696E6C6F6E67", new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=inlong", output.get(0));

// case2: unhex("")
output = processor.transform("|", new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals(output.get(0), "result=");

// case3: unhex("6")
output = processor.transform("6", new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=", output.get(0));

transformSql = "select unhex(hex(string1)) from source";
config = new TransformConfig(transformSql);
processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case4: unhex(hex("inlong"))
output = processor.transform("inlong", new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals("result=inlong", output.get(0));

}
}

0 comments on commit 9731701

Please sign in to comment.