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 f2c5051bd5 [INLONG-11260][SDK] Transform SQL supports "num_nonnulls" 
and "num_nulls" function (#11278)
f2c5051bd5 is described below

commit f2c5051bd5b8e99eb3e8eedb4ea776222ffde2d5
Author: Zkplo <[email protected]>
AuthorDate: Tue Oct 8 12:47:29 2024 +0800

    [INLONG-11260][SDK] Transform SQL supports "num_nonnulls" and "num_nulls" 
function (#11278)
    
    Co-authored-by: ZKpLo <[email protected]>
---
 .../process/function/NumNonNullsFunction.java      | 71 +++++++++++++++++++++
 .../process/function/NumNullsFunction.java         | 72 ++++++++++++++++++++++
 .../arithmetic/TestNumNonNullsFunction.java        | 51 +++++++++++++++
 .../function/arithmetic/TestNumNullsFunction.java  | 51 +++++++++++++++
 4 files changed, 245 insertions(+)

diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/NumNonNullsFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/NumNonNullsFunction.java
new file mode 100644
index 0000000000..1e613506d8
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/NumNonNullsFunction.java
@@ -0,0 +1,71 @@
+/*
+ * 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 net.sf.jsqlparser.expression.NullValue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * NumNonNullsFunction  ->  num_nonnulls(expr1,expr2,...)
+ * description:
+ * - return the number of non-null arguments.
+ */
+@TransformFunction(names = {"num_nonnulls"})
+public class NumNonNullsFunction implements ValueParser {
+
+    private final List<ValueParser> nodeList;
+
+    public NumNonNullsFunction(Function expr) {
+        List<Expression> expressions = expr.getParameters().getExpressions();
+        this.nodeList = new ArrayList<>();
+        if (expressions == null || expressions.isEmpty()) {
+            return;
+        }
+        for (Expression expression : expressions) {
+            if (expression instanceof NullValue) {
+                nodeList.add(null);
+            } else {
+                nodeList.add(OperatorTools.buildParser(expression));
+            }
+        }
+    }
+
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        int num = 0;
+        for (ValueParser valueParser : nodeList) {
+            if (valueParser == null) {
+                continue;
+            }
+            Object value = valueParser.parse(sourceData, rowIndex, context);
+            if (value != null) {
+                num++;
+            }
+        }
+        return num;
+    }
+}
diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/NumNullsFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/NumNullsFunction.java
new file mode 100644
index 0000000000..31b528db32
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/NumNullsFunction.java
@@ -0,0 +1,72 @@
+/*
+ * 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 net.sf.jsqlparser.expression.NullValue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * NumNonNullsFunction  ->  num_nulls(expr1,expr2,...)
+ * description:
+ * - return the number of null arguments.
+ */
+@TransformFunction(names = {"num_nulls"})
+public class NumNullsFunction implements ValueParser {
+
+    private final List<ValueParser> nodeList;
+
+    public NumNullsFunction(Function expr) {
+        List<Expression> expressions = expr.getParameters().getExpressions();
+        this.nodeList = new ArrayList<>();
+        if (expressions == null || expressions.isEmpty()) {
+            return;
+        }
+        for (Expression expression : expressions) {
+            if (expression instanceof NullValue) {
+                nodeList.add(null);
+            } else {
+                nodeList.add(OperatorTools.buildParser(expression));
+            }
+        }
+    }
+
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        int num = 0;
+        for (ValueParser valueParser : nodeList) {
+            if (valueParser == null) {
+                num++;
+                continue;
+            }
+            Object value = valueParser.parse(sourceData, rowIndex, context);
+            if (value == null) {
+                num++;
+            }
+        }
+        return num;
+    }
+}
diff --git 
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestNumNonNullsFunction.java
 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestNumNonNullsFunction.java
new file mode 100644
index 0000000000..59168c48ef
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestNumNonNullsFunction.java
@@ -0,0 +1,51 @@
+/*
+ * 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.arithmetic;
+
+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 TestNumNonNullsFunction extends 
AbstractFunctionArithmeticTestBase {
+
+    @Test
+    public void testNumNonNullsFunction() throws Exception {
+        String transformSql = null, data = null;
+        TransformConfig config = null;
+        TransformProcessor<String, String> processor = null;
+        List<String> output = null;
+
+        // case1: num_nonnulls(5, 3, null, null)
+        transformSql = "select num_nonnulls(numeric1,numeric2,null,numericx) 
from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        data = "5|3|3|5";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=2", output.get(0));
+    }
+}
diff --git 
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestNumNullsFunction.java
 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestNumNullsFunction.java
new file mode 100644
index 0000000000..54e815dc0a
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestNumNullsFunction.java
@@ -0,0 +1,51 @@
+/*
+ * 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.arithmetic;
+
+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 TestNumNullsFunction extends AbstractFunctionArithmeticTestBase {
+
+    @Test
+    public void testNumNullsFunction() throws Exception {
+        String transformSql = null, data = null;
+        TransformConfig config = null;
+        TransformProcessor<String, String> processor = null;
+        List<String> output = null;
+
+        // case1: num_nulls(5, null, null, null)
+        transformSql = "select num_nulls(numeric1,numericx1,null,numericx2) 
from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        data = "5|3|3|5";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=3", output.get(0));
+    }
+}

Reply via email to