Skip to content

Commit

Permalink
The draft of the test code has not been implemented1
Browse files Browse the repository at this point in the history
  • Loading branch information
cat-with-cat committed Dec 17, 2024
1 parent e6607b8 commit 1fca6b6
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ void register_function_date_time_computation(SimpleFunctionFactory& factory) {
factory.register_function<FunctionMicroSecToDateTime>();
factory.register_function<FunctionMilliSecToDateTime>();
factory.register_function<FunctionSecToDateTime>();
factory.register_function<FunctionNextDay>();
factory.register_function<FunctionPreviousDay>();

// alias
factory.register_alias("days_add", "date_add");
Expand Down
17 changes: 17 additions & 0 deletions be/src/vec/functions/function_date_or_datetime_computation.h
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,22 @@ struct Sec {
static constexpr auto name = "from_second";
static constexpr Int64 ratio = 1;
};
struct NextDayImpl {
static constexpr auto name = "next_day";
static constexpr int64_t day_seconds = 86400;
template <typename DateType>
static void calculate(const DateType& date, DateType& result) {
result = date + day_seconds;
}
};
struct PreviousDayImpl {
static constexpr auto name = "previous_day";
static constexpr int64_t day_seconds = 86400;
template <typename DateType>
static void calculate(const DateType& date, DateType& result) {
result = date - day_seconds;
}
};
template <typename Impl>
struct TimestampToDateTime : IFunction {
using ReturnType = DataTypeDateTimeV2;
Expand Down Expand Up @@ -1090,6 +1106,7 @@ struct TimestampToDateTime : IFunction {

if (dt.is_valid_date()) [[likely]] {
dt.set_microsecond((value % Impl::ratio) * ratio_to_micro);
Impl::calculate(dt, dt);
} else {
null_map[i] = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
import org.apache.doris.nereids.types.DateV2Type;
import org.apache.doris.nereids.types.DateTimeV2Type;
import org.apache.doris.nereids.types.DecimalV3Type;

import java.math.BigDecimal;
Expand Down Expand Up @@ -1154,4 +1156,5 @@ public static Expression truncate(DecimalV3Literal first, IntegerLiteral second)
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,103 @@
* ScalarFunction 'last_day'. This class is generated by GenerateFunction.
*/
public class LastDay extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullableOnDateLikeV2Args {
implements ExplicitlyCastableSignature, PropagateNullableOnDateLikeV2Args {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DateV2Type.INSTANCE).args(DateV2Type.INSTANCE, StringType.INSTANCE),
FunctionSignature.ret(DateV2Type.INSTANCE).args(DateTimeV2Type.SYSTEM_DEFAULT, StringType.INSTANCE),
FunctionSignature.ret(DateType.INSTANCE).args(DateType.INSTANCE, StringType.INSTANCE),
FunctionSignature.ret(DateType.INSTANCE).args(DateTimeType.INSTANCE, StringType.INSTANCE),
FunctionSignature.ret(DateV2Type.INSTANCE).args(DateV2Type.INSTANCE),
FunctionSignature.ret(DateV2Type.INSTANCE).args(DateTimeV2Type.SYSTEM_DEFAULT),
FunctionSignature.ret(DateType.INSTANCE).args(DateType.INSTANCE),
FunctionSignature.ret(DateType.INSTANCE).args(DateTimeType.INSTANCE)
);

/**
* constructor with 1 argument.
* constructor with 2 argument.
*/
public LastDay(Expression arg) {
super("last_day", arg);
public LastDay(Expression arg, Expression timeUnit) {
super("last_day", arg, timeUnit);
}

/**
* withChildren.
*/
@Override
public LastDay withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new LastDay(children.get(0));
Preconditions.checkArgument(children.size() == 1 || children.size() == 2);
if (children.size() == 1) {
return new LastDay(children.get(0), null);
} else {
return new LastDay(children.get(0), children.get(1));
}
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitLastDay(this, context);
if (getChildren().size() == 2) {
Expression timeUnit = getChildren().get(1);
String unit = timeUnit.toString();
switch (unit.toLowerCase()) {
case "day":
return calculateLastDayOfDay(visitor, context);
case "week":
return calculateLastDayOfWeek(visitor, context);
case "quarter":
return calculateLastDayOfQuarter(visitor, context);
case "year":
return calculateLastDayOfYear(visitor, context);
case "month":
return calculateLastDayOfMonth(visitor, context);
}
} else {

return calculateLastDayOfMonth(visitor, context);
}
}

private <R, C> R calculateLastDayOfDay(ExpressionVisitor<R, C> visitor, C context) {
LocalDate currentDate = toJavaDateType().toLocalDate();
return (R) new DateLiteral(currentDate.getYear(), currentDate.getMonthValue(), currentDate.getDayOfMonth());
}

private <R, C> R calculateLastDayOfWeek(ExpressionVisitor<R, C> visitor, C context) {
LocalDate currentDate = toJavaDateType().toLocalDate();
int dayOfWeek = currentDate.getDayOfWeek().getValue();
LocalDate lastDayOfWeek = currentDate.plusDays(7 - dayOfWeek);
return (R) new DateLiteral(lastDayOfWeek.getYear(), lastDayOfWeek.getMonthValue(), lastDayOfWeek.getDayOfMonth());
}

private <R, C> R calculateLastDayOfMonth(ExpressionVisitor<R, C> visitor, C context) {
LocalDate currentDate = toJavaDateType().toLocalDate();
LocalDate lastDayOfMonth = currentDate.withDayOfMonth(currentDate.lengthOfMonth());
return (R) new DateLiteral(lastDayOfMonth.getYear(), lastDayOfMonth.getMonthValue(), lastDayOfMonth.getDayOfMonth());
}

private <R, C> R calculateLastDayOfQuarter(ExpressionVisitor<R, C> visitor, C context) {
LocalDate currentDate = toJavaDateType().toLocalDate();
int month = currentDate.getMonthValue();
int lastMonthOfQuarter = ((month - 1) / 3 + 1) * 3;
LocalDate lastDayOfQuarter;
if (lastMonthOfQuarter == 3) {
lastDayOfQuarter = LocalDate.of(currentDate.getYear(), 3, 31);
} else if (lastMonthOfQuarter == 6) {
lastDayOfQuarter = LocalDate.of(currentDate.getYear(), 6, 30);
} else if (lastMonthOfQuarter == 9) {
lastDayOfQuarter = LocalDate.of(currentDate.getYear(), 9, 30);
} else {
lastDayOfQuarter = LocalDate.of(currentDate.getYear(), 12, 31);
}
return (R) new DateLiteral(lastDayOfQuarter.getYear(), lastDayOfQuarter.getMonthValue(), lastDayOfQuarter.getDayOfMonth());
}

private <R, C> R calculateLastDayOfYear(ExpressionVisitor<R, C> visitor, C context) {
LocalDate currentDate = toJavaDateType().toLocalDate();
LocalDate lastDayOfYear = LocalDate.of(currentDate.getYear(), 12, 31);
return (R) new DateLiteral(lastDayOfYear.getYear(), lastDayOfYear.getMonthValue(), lastDayOfYear.getDayOfMonth());
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullableOnDateLikeV2Args;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DateTimeType;
import org.apache.doris.nereids.types.DateTimeV2Type;
import org.apache.doris.nereids.types.DateType;
import org.apache.doris.nereids.types.DateV2Type;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.time.LocalDate;
import java.util.List;

/**
* ScalarFunction 'next_day'. This class is generated by GenerateFunction.
*/
public class NextDay extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullableOnDateLikeV2Args {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DateV2Type.INSTANCE).args(DateV2Type.INSTANCE),
FunctionSignature.ret(DateV2Type.INSTANCE).args(DateTimeV2Type.SYSTEM_DEFAULT),
FunctionSignature.ret(DateType.INSTANCE).args(DateType.INSTANCE),
FunctionSignature.ret(DateType.INSTANCE).args(DateTimeType.INSTANCE)
);

/**
* Constructor with 1 argument.
*/
public NextDay(Expression arg) {
super("next_day", arg);
}

/**
* withChildren.
*/
@Override
public NextDay withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new NextDay(children.get(0));
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return calculateNextDay(visitor, context);
}

private <R, C> R calculateNextDay(ExpressionVisitor<R, C> visitor, C context) {
LocalDate currentDate = toJavaDateType().toLocalDate();
LocalDate nextDay = currentDate.plusDays(1);
return (R) new DateLiteral(nextDay.getYear(), nextDay.getMonthValue(), nextDay.getDayOfMonth());
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullableOnDateLikeV2Args;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DateTimeType;
import org.apache.doris.nereids.types.DateTimeV2Type;
import org.apache.doris.nereids.types.DateType;
import org.apache.doris.nereids.types.DateV2Type;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.time.LocalDate;
import java.util.List;

/**
* ScalarFunction 'previous_day'. This class is generated by GenerateFunction.
*/
public class PreviousDay extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullableOnDateLikeV2Args {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DateV2Type.INSTANCE).args(DateV2Type.INSTANCE),
FunctionSignature.ret(DateV2Type.INSTANCE).args(DateTimeV2Type.SYSTEM_DEFAULT),
FunctionSignature.ret(DateType.INSTANCE).args(DateType.INSTANCE),
FunctionSignature.ret(DateType.INSTANCE).args(DateTimeType.INSTANCE)
);

/**
* Constructor with 1 argument.
*/
public PreviousDay(Expression arg) {
super("previous_day", arg);
}

/**
* withChildren.
*/
@Override
public PreviousDay withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new PreviousDay(children.get(0));
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return calculatePreviousDay(visitor, context);
}

private <R, C> R calculatePreviousDay(ExpressionVisitor<R, C> visitor, C context) {
LocalDate currentDate = toJavaDateType().toLocalDate();
LocalDate previousDay = currentDate.minusDays(1);
return (R) new DateLiteral(previousDay.getYear(), previousDay.getMonthValue(), previousDay.getDayOfMonth());
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}
}

0 comments on commit 1fca6b6

Please sign in to comment.