This is an automated email from the ASF dual-hosted git repository.

aloyszhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git


The following commit(s) were added to refs/heads/master by this push:
     new 89ba85ff8b [INLONG-10901][SDK] Transform support BIN(integer) function 
(#10903)
89ba85ff8b is described below

commit 89ba85ff8b1acbc8d0c699fc2412e0870e8895fd
Author: emptyOVO <[email protected]>
AuthorDate: Tue Aug 27 12:03:55 2024 +0800

    [INLONG-10901][SDK] Transform support BIN(integer) function (#10903)
---
 .../transform/process/function/BinFunction.java    | 56 ++++++++++++++++++++++
 .../transform/process/operator/OperatorTools.java  |  2 +
 .../TestTransformArithmeticFunctionsProcessor.java | 22 +++++++++
 3 files changed, 80 insertions(+)

diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/BinFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/BinFunction.java
new file mode 100644
index 0000000000..ae4dbc96e7
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/BinFunction.java
@@ -0,0 +1,56 @@
+/*
+ * 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.util.List;
+/**
+ * BinFunction
+ * description: bin(integer)--Returns a string representation of an integer in 
binary format. If the integer is NULL, NULL is returned.
+ */
+public class BinFunction implements ValueParser {
+
+    private ValueParser valueParser;
+
+    public BinFunction(Function expr) {
+        if (expr.getParameters() != null) {
+            List<Expression> expressions = 
expr.getParameters().getExpressions();
+            if (expressions != null && expressions.size() == 1) {
+                valueParser = OperatorTools.buildParser(expressions.get(0));
+            }
+        }
+    }
+
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        if (valueParser != null) {
+            Object valueObj = valueParser.parse(sourceData, rowIndex, context);
+            BigDecimal value = OperatorTools.parseBigDecimal(valueObj);
+            return Integer.toBinaryString(value.intValue());
+        }
+        return null;
+    }
+}
diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java
index c1cffbf88a..72865fa279 100644
--- 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java
@@ -18,6 +18,7 @@
 package org.apache.inlong.sdk.transform.process.operator;
 
 import org.apache.inlong.sdk.transform.process.function.AbsFunction;
+import org.apache.inlong.sdk.transform.process.function.BinFunction;
 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;
@@ -123,6 +124,7 @@ public class OperatorTools {
         functionMap.put("sinh", SinhFunction::new);
         functionMap.put("cos", CosFunction::new);
         functionMap.put("tan", TanFunction::new);
+        functionMap.put("bin", BinFunction::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));
diff --git 
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java
 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java
index 6577ac6d67..e725345558 100644
--- 
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java
+++ 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java
@@ -347,4 +347,26 @@ public class TestTransformArithmeticFunctionsProcessor {
         Assert.assertEquals(output3.get(0), "result=-2.185039863261519");
     }
 
+    @Test
+    public void testBinFunction() throws Exception {
+        String transformSql1 = "select bin(numeric1) from source";
+        TransformConfig config1 = new TransformConfig(transformSql1);
+        TransformProcessor<String, String> processor1 = TransformProcessor
+                .create(config1, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        // case: bin(4)
+        List<String> output1 = processor1.transform("4|5|6|8", new 
HashMap<>());
+        Assert.assertEquals(1, output1.size());
+        Assert.assertEquals(output1.get(0), "result=100");
+        String transformSql2 = "select bin() from source";
+        TransformConfig config2 = new TransformConfig(transformSql2);
+        TransformProcessor<String, String> processor2 = TransformProcessor
+                .create(config2, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        // case: bin()
+        List<String> output2 = processor2.transform("1|2|3|4", new 
HashMap<>());
+        Assert.assertEquals(1, output1.size());
+        Assert.assertEquals(output2.get(0), "result=null");
+    }
+
 }

Reply via email to