Skip to content

Commit

Permalink
[INLONG-11064][SDK] Transform SQL supports NULLIF function (apache#11078
Browse files Browse the repository at this point in the history
)

Co-authored-by: ZKpLo <[email protected]>
  • Loading branch information
2 people authored and wohainilaodou committed Oct 8, 2024
1 parent c38260e commit 3e881b9
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 3e881b9

Please sign in to comment.