-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-11263][SDK] Transform SQL supports "lcm" function (#11285)
- Loading branch information
Showing
2 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
...sform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LcmFunction.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,95 @@ | ||
/* | ||
* 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 lombok.extern.slf4j.Slf4j; | ||
import net.sf.jsqlparser.expression.Expression; | ||
import net.sf.jsqlparser.expression.Function; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.math.RoundingMode; | ||
import java.util.List; | ||
|
||
/** | ||
* LcmFunction -> lcm(numeric_type,numeric_type) | ||
* description: | ||
* - return 0 if either input is zero | ||
* - return least common multiple (the smallest strictly positive number that is an integral multiple of both inputs) | ||
* Note: numeric_type includes floating-point number and integer | ||
*/ | ||
@Slf4j | ||
@TransformFunction(names = {"lcm"}) | ||
public class LcmFunction implements ValueParser { | ||
|
||
private final ValueParser firstNumParser; | ||
private final ValueParser secondNumTypeParser; | ||
|
||
public LcmFunction(Function expr) { | ||
List<Expression> expressions = expr.getParameters().getExpressions(); | ||
firstNumParser = OperatorTools.buildParser(expressions.get(0)); | ||
secondNumTypeParser = OperatorTools.buildParser(expressions.get(1)); | ||
} | ||
|
||
@Override | ||
public Object parse(SourceData sourceData, int rowIndex, Context context) { | ||
Object firstNumObj = firstNumParser.parse(sourceData, rowIndex, context); | ||
Object secondNumObj = secondNumTypeParser.parse(sourceData, rowIndex, context); | ||
if (firstNumObj == null || secondNumObj == null) { | ||
return null; | ||
} | ||
try { | ||
BigDecimal firstNum = OperatorTools.parseBigDecimal(firstNumObj); | ||
BigDecimal secondNum = OperatorTools.parseBigDecimal(secondNumObj); | ||
return lcm(firstNum, secondNum).toPlainString(); | ||
} catch (Exception e) { | ||
log.error("Parse error", e); | ||
return null; | ||
} | ||
} | ||
|
||
public static BigInteger gcd(BigDecimal a, BigDecimal b) { | ||
BigInteger intA = a.toBigInteger(); | ||
BigInteger intB = b.toBigInteger(); | ||
return intA.gcd(intB); | ||
} | ||
|
||
public static BigDecimal lcm(BigDecimal a, BigDecimal b) { | ||
if (a.compareTo(BigDecimal.ZERO) == 0 || b.compareTo(BigDecimal.ZERO) == 0) { | ||
return BigDecimal.ZERO; | ||
} | ||
int maxScale = Math.max(a.scale(), b.scale()); | ||
|
||
BigDecimal factorA = a.movePointRight(maxScale); | ||
BigDecimal factorB = b.movePointRight(maxScale); | ||
|
||
BigInteger gcdValue = gcd(factorA, factorB); | ||
BigDecimal product = factorA.multiply(factorB); | ||
|
||
// LCM = (|a * b| / GCD(a, b)) | ||
BigDecimal lcmValue = product.divide(new BigDecimal(gcdValue), RoundingMode.HALF_UP); | ||
|
||
return lcmValue.movePointLeft(maxScale).stripTrailingZeros(); | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...est/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLcmFunction.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,70 @@ | ||
/* | ||
* 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.arithmetic; | ||
|
||
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 TestLcmFunction extends AbstractFunctionArithmeticTestBase { | ||
|
||
@Test | ||
public void testLcmFunction() throws Exception { | ||
String transformSql = null, data = null; | ||
TransformConfig config = null; | ||
TransformProcessor<String, String> processor = null; | ||
List<String> output = null; | ||
|
||
transformSql = "select lcm(numeric1,numeric2) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
|
||
// case1: lcm(3.14159265358979323846,3.2653589793) | ||
data = "3.14159265358979323846|3.2653589793||"; | ||
output = processor.transform(data, new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=512921389035117286501.7893551939", output.get(0)); | ||
|
||
// case2: lcm(3.141,3.846) | ||
data = "3.141|3.846||"; | ||
output = processor.transform(data, new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=4026.762", output.get(0)); | ||
|
||
// case3: lcm(0,3.846) | ||
data = "0|3.846||"; | ||
output = processor.transform(data, new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=0", output.get(0)); | ||
|
||
// case4: lcm(-3.141,3.846) | ||
data = "3.141|3.846||"; | ||
output = processor.transform(data, new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=4026.762", output.get(0)); | ||
} | ||
} |