Skip to content

Commit

Permalink
[INLONG-10813][SDK] Transform support cos function (#10814)
Browse files Browse the repository at this point in the history
Co-authored-by: Charles Zhang <dockerzhang@apache.org>
Co-authored-by: yfsn666 <61183968+yfsn666@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 19, 2024
1 parent 47c710b commit 4884510
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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;

/**
* CosFunction
* description: cos(numeric)--returns the cosine of numeric
*/
public class CosFunction implements ValueParser {

private ValueParser numberParser;

/**
* Constructor
* @param expr
*/
public CosFunction(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);
return Math.cos(numberValue.doubleValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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.DateExtractFunction;
import org.apache.inlong.sdk.transform.process.function.DateExtractFunction.DateExtractFunctionType;
import org.apache.inlong.sdk.transform.process.function.DateFormatFunction;
Expand Down Expand Up @@ -113,6 +114,7 @@ public class OperatorTools {
functionMap.put("floor", FloorFunction::new);
functionMap.put("sin", SinFunction::new);
functionMap.put("sinh", SinhFunction::new);
functionMap.put("cos", CosFunction::new);
functionMap.put("year", func -> new DateExtractFunction(DateExtractFunctionType.YEAR, func));
functionMap.put("quarter", func -> new DateExtractFunction(DateExtractFunctionType.QUARTER, func));
functionMap.put("month", func -> new DateExtractFunction(DateExtractFunctionType.MONTH, func));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,17 @@ public void testSinhFunction() throws Exception {
Assert.assertEquals(1, output3.size());
Assert.assertEquals(output3.get(0), "result=3.626860407847019");
}

@Test
public void testCosFunction() throws Exception {
String transformSql = "select cos(numeric1) from source";
TransformConfig config = new TransformConfig(transformSql);
TransformProcessor<String, String> processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case: cos(0)
List<String> output1 = processor.transform("0|4|6|8", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals(output1.get(0), "result=1.0");
}
}

0 comments on commit 4884510

Please sign in to comment.