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-11002][SDK] Transform SQL support Fibonacci function #11003

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
96702e0
add(member):张天瑞
youzhi886 Aug 22, 2024
35eceba
[SDK] Transform support factorial function #10859
youzhi886 Aug 22, 2024
15cf7ee
[INLONG-10873][SDK] Transform support factorial function
youzhi886 Aug 25, 2024
8a79da6
[INLONG-10873][SDK] Transform support factorial function
youzhi886 Aug 25, 2024
4ba6306
[INLONG-10873][SDK] Transform support factorial function
youzhi886 Aug 25, 2024
1579c5c
[INLONG-10873][SDK] Transform support factorial function
youzhi886 Aug 25, 2024
ef7d6f9
[INLONG-10873][SDK] Transform support factorial function
youzhi886 Aug 25, 2024
411b257
fix some bugs
youzhi886 Sep 1, 2024
b004ac4
[INLONG-10873][SDK]Transform support factorial function
youzhi886 Sep 1, 2024
ef0ca01
[INLONG-10873][SDK]Transform support factorial function
youzhi886 Sep 2, 2024
e5ce841
[INLONG-10873][SDK]Transform support factorial function
youzhi886 Sep 2, 2024
a9843f6
[INLONG-10873][SDK]Transform support factorial function
youzhi886 Sep 2, 2024
dd039f6
[INLONG-10873][SDK]Transform support factorial function
youzhi886 Sep 2, 2024
8c1b6ec
[INLONG-10873][SDK]Transform support factorial function
youzhi886 Sep 2, 2024
b6dec70
[INLONG-10873][SDK]Transform support factorial function
youzhi886 Sep 2, 2024
31f4d8a
[INLONG-10873][SDK]Transform support factorial function
youzhi886 Sep 2, 2024
a7dcba6
[INLONG-10873][SDK]Transform support factorial function
youzhi886 Sep 3, 2024
e1ac5e7
[INLONG-10873][SDK]Transform support factorial function
youzhi886 Sep 3, 2024
4e00c68
[INLONG-11002][SDK]Transform SQL support Fibonacci function
youzhi886 Sep 3, 2024
a5d1176
Merge branch 'master' of https://github.com/youzhi886/inlong into INL…
youzhi886 Sep 3, 2024
e46810b
[INLONG-11002][SDK]Transform SQL support Fibonacci function
youzhi886 Sep 4, 2024
5103fa6
[INLONG-11002][SDK]Transform SQL support Fibonacci function
youzhi886 Sep 4, 2024
467d901
[INLONG-11002][SDK]Transform SQL support Fibonacci function
youzhi886 Sep 5, 2024
947c54b
[INLONG-11002][SDK]Transform SQL support Fibonacci function
youzhi886 Sep 5, 2024
951651d
[INLONG-11002][SDK]Transform SQL support Fibonacci function
youzhi886 Sep 5, 2024
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;

/**
* FibonacciFunction
* description: fibonacci(numeric)--returns the nth Fibonacci number
*/

public class FibonacciFunction implements ValueParser {

private final ValueParser numberParser;

/**
* Constructor
*
* @param expr
*/
public FibonacciFunction(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);
int n = numberValue.intValue();
return fibonacci(n);
}

/**
* Calculate the nth Fibonacci number.
*
* @param n the position in the Fibonacci sequence
* @return the nth Fibonacci number
*/
private long fibonacci(int n) {
if (n <= 1)
return n;
long prev = 0, curr = 1;
for (int i = 2; i <= n; i++) {
long temp = curr;
curr = curr + prev;
prev = temp;
}
return curr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,44 @@

package org.apache.inlong.sdk.transform.process.operator;

import org.apache.inlong.sdk.transform.process.function.FunctionTools;
import org.apache.inlong.sdk.transform.process.function.AbsFunction;
import org.apache.inlong.sdk.transform.process.function.CeilFunction;
import org.apache.inlong.sdk.transform.process.function.ConcatFunction;
import org.apache.inlong.sdk.transform.process.function.CosFunction;
import org.apache.inlong.sdk.transform.process.function.DateFormatFunction;
import org.apache.inlong.sdk.transform.process.function.ExpFunction;
import org.apache.inlong.sdk.transform.process.function.FibonacciFunction;
import org.apache.inlong.sdk.transform.process.function.FloorFunction;
import org.apache.inlong.sdk.transform.process.function.FromUnixTimeFunction;
import org.apache.inlong.sdk.transform.process.function.LnFunction;
import org.apache.inlong.sdk.transform.process.function.LocateFunction;
import org.apache.inlong.sdk.transform.process.function.Log10Function;
import org.apache.inlong.sdk.transform.process.function.Log2Function;
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;
import org.apache.inlong.sdk.transform.process.function.SubstringFunction;
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.parser.ColumnParser;
import org.apache.inlong.sdk.transform.process.parser.ParserTools;
import org.apache.inlong.sdk.transform.process.parser.ValueParser;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
import org.apache.commons.lang.ObjectUtils;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;

import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* OperatorTools
Expand All @@ -52,55 +71,35 @@ public class OperatorTools {

public static final String CHILD_KEY = "$child";

static {
init();
}

private static void init() {
Reflections reflections = new Reflections(OPERATOR_PATH, Scanners.TypesAnnotated);
Set<Class<?>> clazzSet = reflections.getTypesAnnotatedWith(TransformOperator.class);
for (Class<?> clazz : clazzSet) {
if (ExpressionOperator.class.isAssignableFrom(clazz)) {
TransformOperator annotation = clazz.getAnnotation(TransformOperator.class);
if (annotation == null) {
continue;
}
Class<?>[] values = annotation.values();
for (Class<?> value : values) {
operatorMap.compute(value, (key, former) -> {
if (former != null) {
log.warn("find a conflict for parser class [{}], the former one is [{}], new one is [{}]",
key, former.getName(), clazz.getName());
}
return clazz;
});
}
}
}
}
private static final Map<String, java.util.function.Function<Function, ValueParser>> functionMap = new HashMap<>();

public static ExpressionOperator getTransformOperator(Expression expr) {
Class<?> clazz = operatorMap.get(expr.getClass());
if (clazz == null) {
return null;
}
try {
Constructor<?> constructor = clazz.getDeclaredConstructor(expr.getClass());
return (ExpressionOperator) constructor.newInstance(expr);
} catch (NoSuchMethodException e) {
log.error("transform operator {} needs one constructor that accept one params whose type is {}",
clazz.getName(), expr.getClass().getName(), e);
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static {
functionMap.put("concat", ConcatFunction::new);
functionMap.put("now", NowFunction::new);
functionMap.put("power", PowerFunction::new);
functionMap.put("abs", AbsFunction::new);
functionMap.put("sqrt", SqrtFunction::new);
functionMap.put("ln", LnFunction::new);
functionMap.put("log10", Log10Function::new);
functionMap.put("log2", Log2Function::new);
functionMap.put("log", LogFunction::new);
functionMap.put("exp", ExpFunction::new);
functionMap.put("substring", SubstringFunction::new);
functionMap.put("locate", LocateFunction::new);
functionMap.put("to_date", ToDateFunction::new);
functionMap.put("date_format", DateFormatFunction::new);
functionMap.put("ceil", CeilFunction::new);
functionMap.put("floor", FloorFunction::new);
functionMap.put("sin", SinFunction::new);
functionMap.put("sinh", SinhFunction::new);
functionMap.put("cos", CosFunction::new);

functionMap.put("round", RoundFunction::new);
functionMap.put("from_unixtime", FromUnixTimeFunction::new);
functionMap.put("unix_timestamp", UnixTimestampFunction::new);
functionMap.put("to_timestamp", ToTimestampFunction::new);
functionMap.put("fibonacci", FibonacciFunction::new);

public static ExpressionOperator buildOperator(Expression expr) {
if (expr != null) {
return getTransformOperator(expr);
}
return null;
}

public static ValueParser buildParser(Expression expr) {
Expand All @@ -109,14 +108,23 @@ public static ValueParser buildParser(Expression expr) {
if (exprString.startsWith(ROOT_KEY) || exprString.startsWith(CHILD_KEY)) {
return new ColumnParser((Function) expr);
} else {
return FunctionTools.getTransformFunction((Function) expr);
// TODO
Function func = (Function) expr;
java.util.function.Function<Function, ValueParser> valueParserConstructor = functionMap
.get(func.getName().toLowerCase());
if (valueParserConstructor != null) {
return valueParserConstructor.apply(func);
} else {
return new ColumnParser(func);
}
}
}
return ParserTools.getTransformParser(expr);
return null;
}

/**
* parseBigDecimal
*
* @param value
* @return
*/
Expand Down Expand Up @@ -150,6 +158,7 @@ public static Timestamp parseTimestamp(Object value) {

/**
* compareValue
*
* @param left
* @param right
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,35 @@ public void testLog10Function() throws Exception {
Assert.assertEquals(output2.get(0), "result=3.0");
}

@Test
public void testFibonacciFunction() throws Exception {
String transformSql = "select fibonacci(numeric1) from source";
TransformConfig config = new TransformConfig(transformSql);
TransformProcessor<String, String> processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));

// case1: fibonacci(0)
List<String> output1 = processor.transform("0|4|6|8", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals(output1.get(0), "result=0");

// case2: fibonacci(1)
List<String> output2 = processor.transform("1|4|6|8", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=1");

// case3: fibonacci(7)
List<String> output3 = processor.transform("7|4|6|8", new HashMap<>());
Assert.assertEquals(1, output3.size());
Assert.assertEquals(output3.get(0), "result=13");

// case4: fibonacci(10)
List<String> output4 = processor.transform("10|4|6|8", new HashMap<>());
Assert.assertEquals(1, output4.size());
Assert.assertEquals(output4.get(0), "result=55");
}

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading