Zkplo commented on code in PR #10880: URL: https://github.com/apache/inlong/pull/10880#discussion_r1730795921
########## inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/TimestampAddFunction.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; + +/** + * TimestampAddFunction + * Description: Add integer expression intervals to the date or date time expression expr. + * The unit of the time interval is specified by the unit parameter, which should be one of the following values: + * FRAC_SECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR. + */ +public class TimestampAddFunction implements ValueParser { + + private ValueParser intervalParser; + private ValueParser amountParser; + private ValueParser datetimeParser; + private static final String DEFAULT_FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss"; + private static final String DEFAULT_FORMAT_DATE = "yyyy-MM-dd"; + + public TimestampAddFunction(Function expr) { + List<Expression> expressions = expr.getParameters().getExpressions(); + intervalParser = OperatorTools.buildParser(expressions.get(0)); + amountParser = OperatorTools.buildParser(expressions.get(1)); + datetimeParser = OperatorTools.buildParser(expressions.get(2)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + String interval = intervalParser.parse(sourceData, rowIndex, context).toString(); + Long amount = Long.parseLong(amountParser.parse(sourceData, rowIndex, context).toString()); + String dateString = datetimeParser.parse(sourceData, rowIndex, context).toString(); + return evalDate(dateString, interval, amount); + } + + private String evalDate(String dateString, String interval, Long amount) { + DateTimeFormatter formatter = null; + LocalDateTime dateTime = null; + boolean hasTime = true; + if (dateString.indexOf(' ') != -1) { + formatter = DateTimeFormatter.ofPattern(DEFAULT_FORMAT_DATE_TIME); Review Comment: Thank you for your suggestion. DateTimeFormatter is thread safe, so is it feasible to directly use it as a member variable? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@inlong.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org