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-10803][SDK] Transform SQL support Round function (apache#10810)
* [INLONG-10803][SDK] Transform SQL support Round function * [INLONG-10803][SDK] Transform SQL support Round function --------- Co-authored-by: ZKpLo <[email protected]>
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...orm-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RoundFunction.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,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()); | ||
} | ||
} |
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
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