Skip to content

Commit

Permalink
Merge branch 'master' into dev4
Browse files Browse the repository at this point in the history
  • Loading branch information
Zkplo authored Aug 27, 2024
2 parents 11e6b66 + 916e6c1 commit f64aa20
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.Function;

import java.time.LocalTime;
import java.time.ZoneId;

/**
* LocalTimeFunction
* description:
* localTime([string1]) returns the current time in the specified time zone.
* (by default: the current time in the system time zone)
*/
public class LocalTimeFunction implements ValueParser {

private ValueParser stringParser;

public LocalTimeFunction(Function expr) {
if (expr.getParameters() != null) {
stringParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
}
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
if (stringParser != null) {
String zoneString = OperatorTools.parseString(stringParser.parse(sourceData, rowIndex, context));
return LocalTime.now(ZoneId.of(zoneString)).withNano(0);
} else {
return LocalTime.now(ZoneId.systemDefault()).withNano(0);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.Function;

/**
* LowerFunction
* description: LOWER(s): Convert all letters of the string s to lowercase letters
*/
public class LowerFunction implements ValueParser {

private ValueParser stringParser;

public LowerFunction(Function expr) {
stringParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object stringObj = stringParser.parse(sourceData, rowIndex, context);
if (stringObj == null)
return null;
return stringObj.toString().toLowerCase();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 java.math.BigDecimal;
import java.util.List;
import java.util.Random;

/**
* RandFunction
* description: Rand()--Returns a pseudo-random double precision value in the range [0.0, 1.0)
* Rand(Integer)--Returns a pseudo-random double precision value in the range [0.0, 1.0) with an initial seed of Integer.
*/
public class RandFunction implements ValueParser {

private ValueParser seedParser;

private Random random;

public RandFunction(Function expr) {
random = new Random();
if (expr.getParameters() != null) {
List<Expression> expressions = expr.getParameters().getExpressions();
if (expressions != null && expressions.size() == 1) {
seedParser = OperatorTools.buildParser(expressions.get(0));
}
}
}
@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
if (seedParser != null) {
Object seedObj = seedParser.parse(sourceData, rowIndex, context);
BigDecimal seedValue = OperatorTools.parseBigDecimal(seedObj);
random.setSeed(seedValue.intValue());
}
return random.nextDouble();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.Function;

/**
* UpperFunction
* description: UPPER(s): Convert a string to uppercase
*/
public class UpperFunction implements ValueParser {

private ValueParser stringParser;

public UpperFunction(Function expr) {
stringParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object stringObj = stringParser.parse(sourceData, rowIndex, context);
if (stringObj == null)
return null;
return stringObj.toString().toUpperCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
import org.apache.inlong.sdk.transform.process.function.FromUnixTimeFunction;
import org.apache.inlong.sdk.transform.process.function.LengthFunction;
import org.apache.inlong.sdk.transform.process.function.LnFunction;
import org.apache.inlong.sdk.transform.process.function.LocalTimeFunction;
import org.apache.inlong.sdk.transform.process.function.LocateFunction;
import org.apache.inlong.sdk.transform.process.function.Log10Function;
import org.apache.inlong.sdk.transform.process.function.Log2Function;
import org.apache.inlong.sdk.transform.process.function.LogFunction;
import org.apache.inlong.sdk.transform.process.function.LowerFunction;
import org.apache.inlong.sdk.transform.process.function.ModuloFunction;
import org.apache.inlong.sdk.transform.process.function.NowFunction;
import org.apache.inlong.sdk.transform.process.function.PowerFunction;
import org.apache.inlong.sdk.transform.process.function.RandFunction;
import org.apache.inlong.sdk.transform.process.function.ReplaceFunction;
import org.apache.inlong.sdk.transform.process.function.ReplicateFunction;
import org.apache.inlong.sdk.transform.process.function.ReverseFunction;
Expand All @@ -54,6 +57,7 @@
import org.apache.inlong.sdk.transform.process.function.ToTimestampFunction;
import org.apache.inlong.sdk.transform.process.function.TrimFunction;
import org.apache.inlong.sdk.transform.process.function.UnixTimestampFunction;
import org.apache.inlong.sdk.transform.process.function.UpperFunction;
import org.apache.inlong.sdk.transform.process.parser.AdditionParser;
import org.apache.inlong.sdk.transform.process.parser.ColumnParser;
import org.apache.inlong.sdk.transform.process.parser.DateParser;
Expand Down Expand Up @@ -116,6 +120,8 @@ public class OperatorTools {
static {
functionMap.put("concat", ConcatFunction::new);
functionMap.put("now", NowFunction::new);
functionMap.put("localtime", LocalTimeFunction::new);
functionMap.put("currenttime", LocalTimeFunction::new);
functionMap.put("power", PowerFunction::new);
functionMap.put("abs", AbsFunction::new);
functionMap.put("sqrt", SqrtFunction::new);
Expand All @@ -132,6 +138,7 @@ public class OperatorTools {
functionMap.put("to_date", ToDateFunction::new);
functionMap.put("date_format", DateFormatFunction::new);
functionMap.put("ceil", CeilFunction::new);
functionMap.put("rand", RandFunction::new);
functionMap.put("floor", FloorFunction::new);
functionMap.put("sin", SinFunction::new);
functionMap.put("sinh", SinhFunction::new);
Expand Down Expand Up @@ -160,6 +167,8 @@ public class OperatorTools {
functionMap.put("mod", ModuloFunction::new);
functionMap.put("to_base64", ToBase64Function::new);
functionMap.put("sign", SignFunction::new);
functionMap.put("lower", LowerFunction::new);
functionMap.put("upper", UpperFunction::new);
functionMap.put("length", LengthFunction::new);
functionMap.put("replace", ReplaceFunction::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
* TestArithmeticFunctionsTransformProcessor
Expand Down Expand Up @@ -558,4 +559,31 @@ public void testBinFunction() throws Exception {
Assert.assertEquals(output2.get(0), "result=null");
}

@Test
public void testRandFunction() throws Exception {
String transformSql1 = "select rand(numeric1) from source";
TransformConfig config1 = new TransformConfig(transformSql1);
TransformProcessor<String, String> processor1 = TransformProcessor
.create(config1, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case: rand(1)
List<String> output1 = processor1.transform("1|4|6|8", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals(output1.get(0), "result=" + new Random(1).nextDouble());
// case: rand(2)
List<String> output2 = processor1.transform("2|4|6|8", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=" + new Random(2).nextDouble());
String transformSql2 = "select rand() from source";
TransformConfig config2 = new TransformConfig(transformSql2);
TransformProcessor<String, String> processor2 = TransformProcessor
.create(config2, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case: rand()
List<String> output3 = processor2.transform("|||", new HashMap<>());
Assert.assertEquals(1, output3.size());
double result = Double.parseDouble(output3.get(0).substring(7));
Assert.assertTrue(result >= 0.0 && result < 1.0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,62 @@ public class TestTransformStringFunctionsProcessor {
kvSink = new KvSinkInfo("UTF-8", dstFields);
}

@Test
public void testLowerFunction() throws Exception {
String transformSql1 = "select lower(string1) from source";
TransformConfig config1 = new TransformConfig(transformSql1);
TransformProcessor<String, String> processor1 = TransformProcessor
.create(config1, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case1: lower("ApPlE")
List<String> output1 = processor1.transform("ApPlE|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals(output1.get(0), "result=apple");

// case2: lower("")
List<String> output2 = processor1.transform("|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=");

// case3: lower(null)
String transformSql2 = "select lower(xxd) from source";
TransformConfig config2 = new TransformConfig(transformSql2);
TransformProcessor<String, String> processor2 = TransformProcessor
.create(config2, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
List<String> output3 = processor2.transform("ApPlE|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output3.size());
Assert.assertEquals(output3.get(0), "result=null");
}

@Test
public void testUpperFunction() throws Exception {
String transformSql1 = "select upper(string1) from source";
TransformConfig config1 = new TransformConfig(transformSql1);
TransformProcessor<String, String> processor1 = TransformProcessor
.create(config1, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case1: upper("ApPlE")
List<String> output1 = processor1.transform("ApPlE|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals(output1.get(0), "result=APPLE");

// case2: upper("")
List<String> output2 = processor1.transform("|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=");

// case3: upper(null)
String transformSql2 = "select upper(xxd) from source";
TransformConfig config2 = new TransformConfig(transformSql2);
TransformProcessor<String, String> processor2 = TransformProcessor
.create(config2, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
List<String> output3 = processor2.transform("ApPlE|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output3.size());
Assert.assertEquals(output3.get(0), "result=null");
}

@Test
public void testSubstringFunction() throws Exception {
String transformSql1 = "select substring(string2, numeric1) from source";
Expand Down
Loading

0 comments on commit f64aa20

Please sign in to comment.