forked from apache/inlong
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-11064][SDK] Transform SQL supports NULLIF function (apache#11078
) Co-authored-by: ZKpLo <[email protected]>
- Loading branch information
1 parent
c38260e
commit 3e881b9
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...rm-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/NullIfFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...java/org/apache/inlong/sdk/transform/process/function/flowcontrol/TestNullIfFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |