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 088a19e846 [INLONG-11226][SDK] Transform support parse IS DISTINCT
FROM (#11272)
088a19e846 is described below
commit 088a19e8467706764ac1dd4df19d92f1d41aadbd
Author: emptyOVO <[email protected]>
AuthorDate: Tue Oct 8 12:51:05 2024 +0800
[INLONG-11226][SDK] Transform support parse IS DISTINCT FROM (#11272)
---
.../process/operator/DistinctOperator.java | 66 +++++++++
.../process/operator/TestDistinctOperator.java | 158 +++++++++++++++++++++
2 files changed, 224 insertions(+)
diff --git
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/DistinctOperator.java
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/DistinctOperator.java
new file mode 100644
index 0000000000..2b3425203a
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/DistinctOperator.java
@@ -0,0 +1,66 @@
+/*
+ * 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.operator;
+
+import org.apache.inlong.sdk.transform.decode.SourceData;
+import org.apache.inlong.sdk.transform.process.Context;
+import org.apache.inlong.sdk.transform.process.parser.ValueParser;
+
+import net.sf.jsqlparser.expression.operators.relational.IsDistinctExpression;
+/**
+ * DistinctOperator
+ * description: value1 IS (NOT) DISTINCT FROM value2--Returns TRUE if two
values are different.
+ * NULL values are treated as identical here.
+ * for example: 1 IS DISTINCT FROM NULL returns TRUE;
+ * NULL IS DISTINCT FROM NULL returns FALSE.
+ * 1 IS NOT DISTINCT FROM NULL returns FALSE;
+ * NULL IS NOT DISTINCT FROM NULL returns TRUE.
+ */
+@TransformOperator(values = IsDistinctExpression.class)
+public class DistinctOperator implements ExpressionOperator {
+
+ private final ValueParser leftParser;
+
+ private final ValueParser rightParser;
+
+ private final boolean isNot;
+
+ public DistinctOperator(IsDistinctExpression expr) {
+ this.leftParser = OperatorTools.buildParser(expr.getLeftExpression());
+ this.rightParser =
OperatorTools.buildParser(expr.getRightExpression());
+ this.isNot = expr.isNot();
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ public boolean check(SourceData sourceData, int rowIndex, Context context)
{
+ Object leftValue = this.leftParser.parse(sourceData, rowIndex,
context);
+ Object rightValue = this.rightParser.parse(sourceData, rowIndex,
context);
+ boolean res;
+
+ if (leftValue == null && rightValue == null) {
+ res = false;
+ } else if (leftValue == null || rightValue == null) {
+ res = true;
+ } else {
+ res = OperatorTools.compareValue((Comparable) leftValue,
(Comparable) rightValue) != 0;
+ }
+
+ return isNot != res;
+ }
+}
diff --git
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestDistinctOperator.java
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestDistinctOperator.java
new file mode 100644
index 0000000000..b572af3807
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestDistinctOperator.java
@@ -0,0 +1,158 @@
+/*
+ * 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.operator;
+
+import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory;
+import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory;
+import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo;
+import org.apache.inlong.sdk.transform.pojo.FieldInfo;
+import org.apache.inlong.sdk.transform.pojo.KvSinkInfo;
+import org.apache.inlong.sdk.transform.pojo.TransformConfig;
+import org.apache.inlong.sdk.transform.process.TransformProcessor;
+import org.apache.inlong.sdk.transform.process.converter.BooleanConverter;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+public class TestDistinctOperator extends AbstractOperatorTestBase {
+
+ private static final List<FieldInfo> srcFields = new ArrayList<>();
+ private static final List<FieldInfo> dstFields = new ArrayList<>();
+ private static final CsvSourceInfo csvSource;
+ private static final KvSinkInfo kvSink;
+
+ static {
+ for (int i = 1; i < 5; i++) {
+ FieldInfo field = new FieldInfo();
+ field.setName("numeric" + i);
+ srcFields.add(field);
+ }
+ srcFields.add(new FieldInfo("booleanVal1", new BooleanConverter()));
+ srcFields.add(new FieldInfo("booleanVal2", new BooleanConverter()));
+ FieldInfo field = new FieldInfo();
+ field.setName("result");
+ dstFields.add(field);
+ csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields);
+ kvSink = new KvSinkInfo("UTF-8", dstFields);
+ }
+
+ @Test
+ public void testDistinctOperator() throws Exception {
+ String transformSql = null, data = null;
+ TransformConfig config = null;
+ TransformProcessor<String, String> processor = null;
+ List<String> output = null;
+
+ transformSql = "select if(numeric1 is distinct from
numeric3,'true','false') from source";
+ config = new TransformConfig(transformSql);
+ processor = TransformProcessor
+ .create(config,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+
+ // case1: if(5 is distinct from 3,'true','false')
+ data = "5|2|3|4|1";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=true", output.get(0));
+
+ // case2: if(null is distinct from 3,'true','false')
+ data = "|2|3|4|1";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=true", output.get(0));
+
+ // case3: if(null is distinct from null,'true','false')
+ data = "|2||4|1";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=false", output.get(0));
+
+ // case4: if(3 is distinct from null,'true','false')
+ data = "3|2||4|1";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=true", output.get(0));
+
+ transformSql = "select if(booleanVal1 is distinct from
booleanVal2,'true','false') from source";
+ config = new TransformConfig(transformSql);
+ processor = TransformProcessor
+ .create(config,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+
+ // case5: if(false is distinct from true,'true','false')
+ data = "5|3|||false|true";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=true", output.get(0));
+ }
+
+ @Test
+ public void testNotDistinctOperator() throws Exception {
+ String transformSql = null, data = null;
+ TransformConfig config = null;
+ TransformProcessor<String, String> processor = null;
+ List<String> output = null;
+
+ transformSql = "select if(numeric1 is not distinct from
numeric3,'true','false') from source";
+ config = new TransformConfig(transformSql);
+ processor = TransformProcessor
+ .create(config,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+
+ // case6: if(5 is not distinct from 3,'true','false')
+ data = "5|2|3|4|1";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=false", output.get(0));
+
+ // case7: if(null is not distinct from 3,'true','false')
+ data = "|2|3|4|1";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=false", output.get(0));
+
+ // case8: if(null is not distinct from null,'true','false')
+ data = "|2||4|1";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=true", output.get(0));
+
+ // case9: if(3 is not distinct from null,'true','false')
+ data = "3|2||4|1";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=false", output.get(0));
+
+ transformSql = "select if(booleanVal1 is not distinct from
booleanVal2,'true','false') from source";
+ config = new TransformConfig(transformSql);
+ processor = TransformProcessor
+ .create(config,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+
+ // case10: if(false is not distinct from true,'true','false')
+ data = "5|3|||false|true";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=false", output.get(0));
+ }
+
+}