morrySnow commented on code in PR #10981:
URL: https://github.com/apache/doris/pull/10981#discussion_r924606269


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/DateLiteral.java:
##########
@@ -0,0 +1,222 @@
+// 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.doris.nereids.trees.expressions;
+
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.nereids.types.DateTimeType;
+import org.apache.doris.nereids.types.DateType;
+import org.apache.doris.nereids.util.DateUtils;
+
+import com.google.common.base.Preconditions;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.joda.time.LocalDateTime;
+import org.joda.time.format.DateTimeFormatter;
+
+/**
+ * Date literal in Nereids.
+ */
+public class DateLiteral extends Literal {
+
+    private static final Logger LOG = LogManager.getLogger(DateLiteral.class);
+
+    private static final int DATEKEY_LENGTH = 8;
+    private static final int DATETIME_TO_MINUTE_STRING_LENGTH = 16;
+    private static final int DATETIME_TO_HOUR_STRING_LENGTH = 13;
+
+    private static DateTimeFormatter DATE_TIME_FORMATTER = null;
+    private static DateTimeFormatter DATE_TIME_FORMATTER_TO_HOUR = null;
+    private static DateTimeFormatter DATE_TIME_FORMATTER_TO_MINUTE = null;
+    private static DateTimeFormatter DATE_FORMATTER = null;
+    private static DateTimeFormatter DATE_TIME_FORMATTER_TWO_DIGIT = null;
+    private static DateTimeFormatter DATE_FORMATTER_TWO_DIGIT = null;
+    private static DateTimeFormatter DATEKEY_FORMATTER = null;
+
+    private long year;
+    private long month;
+    private long day;
+    private long hour;
+    private long minute;
+    private long second;
+    private long microsecond;
+
+    static {
+        try {
+            DATE_TIME_FORMATTER = DateUtils.formatBuilder("%Y-%m-%d 
%H:%i:%s").toFormatter();
+            DATE_TIME_FORMATTER_TO_HOUR = DateUtils.formatBuilder("%Y-%m-%d 
%H").toFormatter();
+            DATE_TIME_FORMATTER_TO_MINUTE = DateUtils.formatBuilder("%Y-%m-%d 
%H:%i").toFormatter();
+            DATE_FORMATTER = DateUtils.formatBuilder("%Y-%m-%d").toFormatter();
+            DATEKEY_FORMATTER = 
DateUtils.formatBuilder("%Y%m%d").toFormatter();
+            DATE_TIME_FORMATTER_TWO_DIGIT = DateUtils.formatBuilder("%y-%m-%d 
%H:%i:%s").toFormatter();
+            DATE_FORMATTER_TWO_DIGIT = 
DateUtils.formatBuilder("%y-%m-%d").toFormatter();
+        } catch (AnalysisException e) {
+            LOG.error("invalid date format", e);
+            System.exit(-1);
+        }
+    }
+
+    public DateLiteral(String s, DataType type) throws AnalysisException {
+        super(type);
+        init(s, type);
+    }
+
+    /**
+     * C'tor for date type.
+     */
+    public DateLiteral(long year, long month, long day) {
+        super(DateType.INSTANCE);
+        this.hour = 0;
+        this.minute = 0;
+        this.second = 0;
+        this.year = year;
+        this.month = month;
+        this.day = day;
+    }
+
+    /**
+     * C'tor for datetime type.
+     */
+    public DateLiteral(long year, long month, long day, long hour, long 
minute, long second) {
+        super(DateTimeType.INSTANCE);
+        this.hour = hour;
+        this.minute = minute;
+        this.second = second;
+        this.year = year;
+        this.month = month;
+        this.day = day;
+    }
+
+    /**
+     * C'tor for type conversion.
+     */
+    public DateLiteral(DateLiteral other, DataType type) {
+        super(type);
+        this.hour = other.hour;
+        this.minute = other.minute;
+        this.second = other.second;
+        this.year = other.year;
+        this.month = other.month;
+        this.day = other.day;
+    }
+
+    private void init(String s, DataType type) throws AnalysisException {
+        try {
+            LocalDateTime dateTime;
+            if (type.isDate()) {
+                if (s.split("-")[0].length() == 2) {
+                    dateTime = DATE_FORMATTER_TWO_DIGIT.parseLocalDateTime(s);
+                } else if (s.length() == DATEKEY_LENGTH && !s.contains("-")) {
+                    dateTime = DATEKEY_FORMATTER.parseLocalDateTime(s);
+                } else {
+                    dateTime = DATE_FORMATTER.parseLocalDateTime(s);
+                }
+            } else {
+                if (s.split("-")[0].length() == 2) {
+                    dateTime = 
DATE_TIME_FORMATTER_TWO_DIGIT.parseLocalDateTime(s);
+                } else {
+                    if (s.length() == DATETIME_TO_MINUTE_STRING_LENGTH) {
+                        dateTime = 
DATE_TIME_FORMATTER_TO_MINUTE.parseLocalDateTime(s);
+                    } else if (s.length() == DATETIME_TO_HOUR_STRING_LENGTH) {
+                        dateTime = 
DATE_TIME_FORMATTER_TO_HOUR.parseLocalDateTime(s);
+                    } else {
+                        dateTime = DATE_TIME_FORMATTER.parseLocalDateTime(s);
+                    }
+                }
+            }
+            year = dateTime.getYear();
+            month = dateTime.getMonthOfYear();
+            day = dateTime.getDayOfMonth();
+            hour = dateTime.getHourOfDay();
+            minute = dateTime.getMinuteOfHour();
+            second = dateTime.getSecondOfMinute();
+        } catch (Exception ex) {
+            throw new AnalysisException("date literal [" + s + "] is invalid");
+        }
+    }
+
+    @Override
+    protected Expression uncheckedCastTo(DataType targetType) throws 
AnalysisException {
+        if (getDataType().equals(targetType)) {
+            return this;
+        }
+        if (targetType.isDateType()) {
+            if (getDataType().equals(targetType)) {
+                return this;
+            }
+            if (targetType.equals(DateType.INSTANCE)) {
+                return new DateLiteral(this.year, this.month, this.day);
+            } else if (targetType.equals(DateTimeType.INSTANCE)) {
+                return new DateLiteral(this.year, this.month, this.day, 
this.hour, this.minute, this.second);
+            } else {
+                throw new AnalysisException("Error date literal type : " + 
type);
+            }
+        }
+        //todo other target type cast
+        return this;
+    }
+
+    public DateLiteral withDataType(DataType type) {
+        Preconditions.checkArgument(type.isDate() || type.isDateTime());
+        return new DateLiteral(this, type);
+    }
+
+    @Override
+    public Long getValue() {
+        return (year * 10000 + month * 100 + day) * 1000000L + hour * 10000 + 
minute * 100 + second;

Review Comment:
   we need to discuss later that should we return a human readable Long or a 
timestamp liked Long



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/types/DecimalType.java:
##########
@@ -0,0 +1,44 @@
+// 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.doris.nereids.types;
+
+import org.apache.doris.catalog.Type;
+
+/**
+ * Decimal type in Nereids.
+ */
+public class DecimalType extends FractionalType {
+
+    private int precision;
+    private int scale;
+
+    public DecimalType(int precision, int scale) {
+        this.precision = precision;
+        this.scale = scale;
+    }
+
+    public static DecimalType createDecimalType(int precision, int scale) {
+        return new DecimalType(precision, scale);
+    }
+
+    @Override
+    public Type toCatalogDataType() {
+        return Type.DECIMALV2;

Review Comment:
   just like CatalogType to Type, we need to 
   ```java
   return ScalarType.createDecimalType(PrimitiveType.DECIMALV2, precision, 
scale);
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/TimestampArithmetic.java:
##########
@@ -0,0 +1,154 @@
+// 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.doris.nereids.trees.expressions;
+
+import org.apache.doris.analysis.ArithmeticExpr.Operator;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+
+import com.google.common.base.Preconditions;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.List;
+
+/**
+ * Describes the addition and subtraction of time units from timestamps.
+ * Arithmetic expressions on timestamps are syntactic sugar.
+ * They are executed as function call exprs in the BE.
+ */
+public class TimestampArithmetic extends Expression implements 
BinaryExpression {

Review Comment:
   just like Arithmetic, we need to different class to represent different 
TimestampArithmetic functions



-- 
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...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to