Skip to content

Commit

Permalink
[INLONG-10920][SDK] Transform support LocalTime function (#10922)
Browse files Browse the repository at this point in the history
  • Loading branch information
ying-hua authored Aug 27, 2024
1 parent de1189f commit 916e6c1
Show file tree
Hide file tree
Showing 3 changed files with 105 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
Expand Up @@ -31,6 +31,7 @@
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;
Expand Down Expand Up @@ -118,6 +119,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
import org.junit.Before;
import org.junit.Test;

import java.time.Duration;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -362,4 +366,47 @@ public void testToTimestampFunction() throws Exception {
Assert.assertEquals(1, output4.size());
Assert.assertEquals(output4.get(0), "result=1970-01-01 00:00:00.0");
}

@Test
public void testLocalTimeFunction() throws Exception {
String transformSql1 = "select localTime() from source";
TransformConfig config1 = new TransformConfig(transformSql1);
TransformProcessor<String, String> processor1 = TransformProcessor
.create(config1, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
DateTimeFormatter fomatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// case1: localTime()
List<String> output1 = processor1.transform("", new HashMap<>());
LocalTime expectedTime1 = LocalTime.now().withNano(0);
LocalTime actualTime1 = LocalTime.parse(output1.get(0).split("=")[1], fomatter);
Duration duration1 = Duration.between(expectedTime1, actualTime1);
Assert.assertEquals(1, output1.size());
Assert.assertTrue(duration1.getSeconds() < 1);

// case2: currentTime("UTC")
String transformSql2 = "select currentTime('UTC') from source";
TransformConfig config2 = new TransformConfig(transformSql2);
TransformProcessor<String, String> processor2 = TransformProcessor
.create(config2, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
List<String> output2 = processor2.transform("", new HashMap<>());
LocalTime expectedTime2 = LocalTime.now(ZoneId.of("UTC")).withNano(0);
LocalTime actualTime2 = LocalTime.parse(output2.get(0).split("=")[1], fomatter);
Duration duration2 = Duration.between(expectedTime2, actualTime2);
Assert.assertEquals(1, output2.size());
Assert.assertTrue(duration2.getSeconds() < 1);

// case 3: localTime("America/New_York")
String transformSql3 = "select localTime('America/New_York') from source";
TransformConfig config3 = new TransformConfig(transformSql3);
TransformProcessor<String, String> processor3 = TransformProcessor
.create(config3, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
List<String> output3 = processor3.transform("", new HashMap<>());
LocalTime expectedTime3 = LocalTime.now(ZoneId.of("America/New_York")).withNano(0);
LocalTime actualTime3 = LocalTime.parse(output3.get(0).split("=")[1], fomatter);
Duration duration3 = Duration.between(expectedTime3, actualTime3);
Assert.assertEquals(1, output3.size());
Assert.assertTrue(duration3.getSeconds() < 1);
}
}

0 comments on commit 916e6c1

Please sign in to comment.