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-10803][SDK] Transform SQL support Round function #10810

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,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.Expression;
import net.sf.jsqlparser.expression.Function;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
* RoundFunction
* description: ROUND(x [,y]) -- Return the nearest integer to x, with optional parameter y indicating the number of decimal places to be rounded. If omitted, return the integer.
*/
public class RoundFunction implements ValueParser {

private ValueParser numberParser;
private ValueParser reservedDigitsParser;

public RoundFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
numberParser = OperatorTools.buildParser(expressions.get(0));
if (expressions.size() == 2) {
reservedDigitsParser = OperatorTools.buildParser(expressions.get(1));
}
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object numberObj = numberParser.parse(sourceData, rowIndex, context);
BigDecimal number = OperatorTools.parseBigDecimal(numberObj);
if (reservedDigitsParser != null) {
Object reservedDigitsObj = reservedDigitsParser.parse(sourceData, rowIndex, context);
int reservedDigits = OperatorTools.parseBigDecimal(reservedDigitsObj).intValue();
return number.setScale(reservedDigits, RoundingMode.HALF_UP).doubleValue();
}
return Math.round(number.doubleValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.inlong.sdk.transform.process.function.LogFunction;
import org.apache.inlong.sdk.transform.process.function.NowFunction;
import org.apache.inlong.sdk.transform.process.function.PowerFunction;
import org.apache.inlong.sdk.transform.process.function.RoundFunction;
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 @@ -124,6 +125,8 @@ public class OperatorTools {
functionMap.put("second",
func -> new TimestampExtractFunction(TimestampExtractFunction.TimestampExtractFunctionType.SECOND,
func));
// TODO Add Function
Zkplo marked this conversation as resolved.
Show resolved Hide resolved
functionMap.put("round", RoundFunction::new);
}

public static ExpressionOperator buildOperator(Expression expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class TestTransformArithmeticFunctionsProcessor {
private static final List<FieldInfo> dstFields = new ArrayList<>();
private static final CsvSourceInfo csvSource;
private static final KvSinkInfo kvSink;

static {
for (int i = 1; i < 5; i++) {
FieldInfo field = new FieldInfo();
Expand All @@ -54,6 +55,36 @@ public class TestTransformArithmeticFunctionsProcessor {
kvSink = new KvSinkInfo("UTF-8", dstFields);
}

@Test
public void testRoundFunction() throws Exception {
String transformSql = "select round(numeric1) from source";
TransformConfig config = new TransformConfig(transformSql);
// case1: round(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=3");
// case2: round(3.5)
List<String> output2 = processor.transform("3.5|4|6|8");
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=4");

transformSql = "select round(numeric1,numeric2) from source";
config = new TransformConfig(transformSql);
// case3: round(3.14159265358979323846,10)
processor = TransformProcessor.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
List<String> output3 = processor.transform("3.14159265358979323846|10|6|8");
Assert.assertEquals(1, output3.size());
Assert.assertEquals(output3.get(0), "result=3.1415926536");
// case4: round(13.14159265358979323846,-1)
List<String> output4 = processor.transform("13.14159265358979323846|-1|6|8");
Assert.assertEquals(1, output4.size());
Assert.assertEquals(output4.get(0), "result=10.0");
}

@Test
public void testPowerFunction() throws Exception {
String transformSql = "select power(numeric1, numeric2) from source";
Expand Down
Loading