Skip to content

Commit

Permalink
[INLONG-10866][SDK] Transform SQL support sign function (#10924)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zkplo authored Aug 28, 2024
1 parent fab6756 commit 268ae4d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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;

import java.math.BigDecimal;

/**
* SignFunction
* description: sign(x): Return the sign of x, where x is a negative number, 0, and positive numbers return -1, 0, and 1, respectively
*/
public class SignFunction implements ValueParser {

private ValueParser numberParser;

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

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object numberObj = numberParser.parse(sourceData, rowIndex, context);
BigDecimal numberValue = OperatorTools.parseBigDecimal(numberObj);
double value = numberValue.doubleValue();
if (value > 0) {
return 1;
} else if (value < 0) {
return -1;
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.inlong.sdk.transform.process.function.ReverseFunction;
import org.apache.inlong.sdk.transform.process.function.RightFunction;
import org.apache.inlong.sdk.transform.process.function.RoundFunction;
import org.apache.inlong.sdk.transform.process.function.SignFunction;
import org.apache.inlong.sdk.transform.process.function.SinFunction;
import org.apache.inlong.sdk.transform.process.function.SinhFunction;
import org.apache.inlong.sdk.transform.process.function.SqrtFunction;
Expand Down Expand Up @@ -134,6 +135,7 @@ public class OperatorTools {
functionMap.put("reverse", ReverseFunction::new);
functionMap.put("substring", SubstringFunction::new);
functionMap.put("trim", TrimFunction::new);
functionMap.put("sign", SignFunction::new);
functionMap.put("replicate", ReplicateFunction::new);
functionMap.put("locate", LocateFunction::new);
functionMap.put("to_date", ToDateFunction::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ public class TestTransformArithmeticFunctionsProcessor {
kvSink = new KvSinkInfo("UTF-8", dstFields);
}

@Test
public void testSignFunction() throws Exception {
String transformSql = "select sign(numeric1) from source";
TransformConfig config = new TransformConfig(transformSql);
// case1: sign(3.14159265358979323846)
TransformProcessor<String, String> processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
List<String> output1 = processor.transform("3.14159265358979323846|4|6|8");
Assert.assertEquals(1, output1.size());
Assert.assertEquals(output1.get(0), "result=1");
// case2: sign(-3.5)
List<String> output2 = processor.transform("-3.5|4|6|8");
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=-1");
// case3: sign(0)
List<String> output3 = processor.transform("0|4|6|8");
Assert.assertEquals(1, output3.size());
Assert.assertEquals(output3.get(0), "result=0");
}

@Test
public void testModuloFunction() throws Exception {
String transformFunctionSql = "select mod(numeric1,100) from source";
Expand Down

0 comments on commit 268ae4d

Please sign in to comment.