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-10873][SDK] Transform support factorial function #10874

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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,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;

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;

/**
* FactorialFunction
* description: factorial(numeric)--returns the factorial of a non-negative
* integer
*/
public class FactorialFunction implements ValueParser {

private ValueParser numberParser;

/**
* Constructor
*
* @param expr
*/
public FactorialFunction(Function expr) {
numberParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
}

/**
* parse
*
* @param sourceData
* @param rowIndex
* @return
*/
@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object numberObj = numberParser.parse(sourceData, rowIndex, context);
BigDecimal numberValue = OperatorTools.parseBigDecimal(numberObj);

// Ensure the number is a non-negative integer
if (numberValue.scale() > 0 || numberValue.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Factorial is only defined for non-negative integers.");
}

return factorial(numberValue.intValue());
}

/**
* Helper method to calculate factorial
*
* @param n
* @return
*/
private BigDecimal factorial(int n) {
BigDecimal result = BigDecimal.ONE;
for (int i = 2; i <= n; i++) {
result = result.multiply(BigDecimal.valueOf(i));
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.inlong.sdk.transform.process.function.ToDateFunction;
import org.apache.inlong.sdk.transform.process.function.ToTimestampFunction;
import org.apache.inlong.sdk.transform.process.function.UnixTimestampFunction;
import org.apache.inlong.sdk.transform.process.function.FactorialFunction;
import org.apache.inlong.sdk.transform.process.parser.AdditionParser;
import org.apache.inlong.sdk.transform.process.parser.ColumnParser;
import org.apache.inlong.sdk.transform.process.parser.DateParser;
Expand All @@ -55,6 +56,7 @@
import org.apache.inlong.sdk.transform.process.parser.TimestampParser;
import org.apache.inlong.sdk.transform.process.parser.ValueParser;


import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,41 @@ public void testLog10Function() throws Exception {
Assert.assertEquals(output2.get(0), "result=3.0");
}

@Test
public void testFactorialFunction() throws Exception {
String transformSql = "select factorial(numeric1) from source";
TransformConfig config = new TransformConfig(transformSql);
TransformProcessor<String, String> processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case1: 测试非负整数的阶乘
dockerzhang marked this conversation as resolved.
Show resolved Hide resolved
List<String> output1 = processor.transform("5|4|6|8");
Assert.assertEquals(1, output1.size());
Assert.assertEquals(output1.get(0), "result=120");

// case2: 测试0的阶乘
List<String> output2 = processor.transform("0|4|6|8");
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=1");

// case3: 测试带有小数的输入,应抛出异常
try {
List<String> output3 = processor.transform("5.5|4|6|8");
Assert.fail("Expected an exception for non-integer input");
} catch (IllegalArgumentException e) {
Assert.assertEquals("Factorial is only defined for non-negative integers.", e.getMessage());
}

// case4: 测试负数的输入,应抛出异常
try {
List<String> output4 = processor.transform("-5|4|6|8");
Assert.fail("Expected an exception for negative input");
} catch (IllegalArgumentException e) {
Assert.assertEquals("Factorial is only defined for non-negative integers.", e.getMessage());
}
}
}

@Test
public void testLog2Function() throws Exception {
String transformSql = "select log2(numeric1) from source";
Expand Down
Loading