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

dockerzhang 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 3109548a40 [INLONG-11238][SDK] Transform SQL supports FORMAT function 
(#11271)
3109548a40 is described below

commit 3109548a405afd69e72246c02d752337a4257553
Author: Zkplo <87751516+zk...@users.noreply.github.com>
AuthorDate: Tue Oct 8 12:51:25 2024 +0800

    [INLONG-11238][SDK] Transform SQL supports FORMAT function (#11271)
    
    Co-authored-by: ZKpLo <14148880+zk...@user.noreply.gitee.com>
---
 .../transform/process/function/FormatFunction.java | 78 ++++++++++++++++++++++
 .../function/string/TestFormatFunction.java        | 69 +++++++++++++++++++
 2 files changed, 147 insertions(+)

diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/FormatFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/FormatFunction.java
new file mode 100644
index 0000000000..4bf3f0027f
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/FormatFunction.java
@@ -0,0 +1,78 @@
+/*
+ * 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.math.RoundingMode;
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * FormatFunction  ->  FORMAT(X,D)
+ * description:
+ * return NULL if X or D is NULL.
+ * return the result of formatting the number X to "#,###,###.##" format, 
rounded to D decimal places.
+ */
+@TransformFunction(names = {"format"})
+public class FormatFunction implements ValueParser {
+
+    private ValueParser numberParser;
+    private ValueParser reservedDigitsParser;
+
+    public FormatFunction(Function expr) {
+        List<Expression> expressions = expr.getParameters().getExpressions();
+        numberParser = OperatorTools.buildParser(expressions.get(0));
+        reservedDigitsParser = OperatorTools.buildParser(expressions.get(1));
+    }
+
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        Object numberObj = numberParser.parse(sourceData, rowIndex, context);
+        Object reservedDigitsObj = reservedDigitsParser.parse(sourceData, 
rowIndex, context);
+        if (numberObj == null || reservedDigitsObj == null) {
+            return null;
+        }
+        BigDecimal number = OperatorTools.parseBigDecimal(numberObj);
+        int reservedDigits = 
OperatorTools.parseBigDecimal(reservedDigitsObj).intValue();
+        if (reservedDigits < 0) {
+            reservedDigits = 0;
+        }
+        // build format
+        StringBuilder pattern = new StringBuilder("#,###");
+        if (reservedDigits > 0) {
+            pattern.append(".");
+            for (int i = 0; i < reservedDigits; i++) {
+                pattern.append("0");
+            }
+        }
+        number = number.setScale(reservedDigits, RoundingMode.HALF_UP);
+        DecimalFormat df = (DecimalFormat) 
NumberFormat.getNumberInstance(Locale.US);
+        df.applyPattern(pattern.toString());
+        return df.format(number);
+    }
+}
diff --git 
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestFormatFunction.java
 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestFormatFunction.java
new file mode 100644
index 0000000000..1143873868
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestFormatFunction.java
@@ -0,0 +1,69 @@
+/*
+ * 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.string;
+
+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 TestFormatFunction extends AbstractFunctionStringTestBase {
+
+    @Test
+    public void testFormatFunction() throws Exception {
+        String transformSql = null, data = null;
+        TransformConfig config = null;
+        TransformProcessor<String, String> processor = null;
+        List<String> output = null;
+
+        transformSql = "select format(string1,string2) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        // case1: FORMAT(12332.123456, 4)
+        data = "12332.123456|4|";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=12,332.1235", output.get(0));
+
+        // case2: FORMAT(12332.1,4)
+        data = "12332.1|4|";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=12,332.1000", output.get(0));
+
+        // case3: FORMAT(12332.2,0)
+        data = "12332.1|0|";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=12,332", output.get(0));
+
+        // case4: FORMAT(12332.2,-3)
+        data = "12332.1|-3|";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=12,332", output.get(0));
+    }
+}

Reply via email to