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 2af7480220 [INLONG-11064][SDK] Transform SQL supports NULLIF function
(#11078)
2af7480220 is described below
commit 2af74802209b36197cb2f4938122d4c52132d1bf
Author: Zkplo <[email protected]>
AuthorDate: Thu Sep 12 14:44:12 2024 +0800
[INLONG-11064][SDK] Transform SQL supports NULLIF function (#11078)
Co-authored-by: ZKpLo <[email protected]>
---
.../transform/process/function/NullIfFunction.java | 63 ++++++++++++++++++++
.../function/flowcontrol/TestNullIfFunction.java | 69 ++++++++++++++++++++++
2 files changed, 132 insertions(+)
diff --git
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/NullIfFunction.java
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/NullIfFunction.java
new file mode 100644
index 0000000000..f64f8faba5
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/NullIfFunction.java
@@ -0,0 +1,63 @@
+/*
+ * 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 lombok.extern.slf4j.Slf4j;
+import net.sf.jsqlparser.expression.Expression;
+import net.sf.jsqlparser.expression.Function;
+
+import java.util.List;
+
+/**
+ * NullIfFunction
+ * description: NULLIF(expr1,expr2)
+ * - return NULL if expr1 = expr2 is true
+ * - returns expr1 otherwise
+ */
+@Slf4j
+@TransformFunction(names = {"nullif", "null_if"})
+public class NullIfFunction implements ValueParser {
+
+ private final ValueParser firstExprParser;
+ private final ValueParser secondExprParser;
+
+ public NullIfFunction(Function expr) {
+ List<Expression> expressions = expr.getParameters().getExpressions();
+ firstExprParser = OperatorTools.buildParser(expressions.get(0));
+ secondExprParser = OperatorTools.buildParser(expressions.get(1));
+ }
+
+ @Override
+ public Object parse(SourceData sourceData, int rowIndex, Context context) {
+ Object firstExprObj = firstExprParser.parse(sourceData, rowIndex,
context);
+ if (firstExprObj == null) {
+ return null;
+ }
+ Object secondExprObj = secondExprParser.parse(sourceData, rowIndex,
context);
+ if (secondExprObj == null) {
+ return firstExprObj;
+ }
+ int cmp = OperatorTools.compareValue((Comparable) firstExprObj,
(Comparable) secondExprObj);
+ return cmp == 0 ? null : firstExprObj;
+ }
+}
diff --git
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/flowcontrol/TestNullIfFunction.java
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/flowcontrol/TestNullIfFunction.java
new file mode 100644
index 0000000000..868e922cc9
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/flowcontrol/TestNullIfFunction.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.flowcontrol;
+
+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.apache.inlong.sdk.transform.process.function.arithmetic.AbstractFunctionArithmeticTestBase;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.List;
+
+public class TestNullIfFunction extends AbstractFunctionArithmeticTestBase {
+
+ @Test
+ public void testNullIfFunction() throws Exception {
+ String transformSql = null, data = null;
+ TransformConfig config = null;
+ TransformProcessor<String, String> processor = null;
+ List<String> output = null;
+
+ // case1: nullif(5, 3)
+ transformSql = "select nullif(numeric1,numeric2) 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=5", output.get(0));
+
+ // case2: nullif(5, 5)
+ data = "5|5|3|5";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=null", output.get(0));
+
+ // case3: nullif(null,3)
+ transformSql = "select nullif(xxd,numeric2) 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=null", output.get(0));
+ }
+}