Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.apache.doris.nereids.rules.expression.rules.SupportJavaDateFormatter;
import org.apache.doris.nereids.trees.expressions.ExecFunction;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.scalar.FromMicrosecond;
import org.apache.doris.nereids.trees.expressions.functions.scalar.FromMillisecond;
import org.apache.doris.nereids.trees.expressions.functions.scalar.FromSecond;
import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DateLiteral;
Expand Down Expand Up @@ -404,7 +407,8 @@ public static Expression date(DateTimeV2Literal dateTime) throws AnalysisExcepti
*/
@ExecFunction(name = "date_trunc")
public static Expression dateTrunc(DateTimeV2Literal date, StringLikeLiteral trunc) {
return DateTimeV2Literal.fromJavaDateType(dateTruncHelper(date.toJavaDateType(), trunc.getValue()));
return DateTimeV2Literal.fromJavaDateType(
dateTruncHelper(date.toJavaDateType(), trunc.getValue()), date.getScale());
}

@ExecFunction(name = "date_trunc")
Expand All @@ -414,7 +418,8 @@ public static Expression dateTrunc(DateV2Literal date, StringLikeLiteral trunc)

@ExecFunction(name = "date_trunc")
public static Expression dateTrunc(StringLikeLiteral trunc, DateTimeV2Literal date) {
return DateTimeV2Literal.fromJavaDateType(dateTruncHelper(date.toJavaDateType(), trunc.getValue()));
return DateTimeV2Literal.fromJavaDateType(
dateTruncHelper(date.toJavaDateType(), trunc.getValue()), date.getScale());
}

@ExecFunction(name = "date_trunc")
Expand Down Expand Up @@ -1057,27 +1062,27 @@ public static Expression monthName(DateV2Literal date) {

@ExecFunction(name = "from_second")
public static Expression fromSecond(BigIntLiteral second) {
return fromMicroSecond(second.getValue() * 1000 * 1000);
return fromMicroSecond(second.getValue() * 1000 * 1000, FromSecond.RESULT_SCALE);
}

@ExecFunction(name = "from_millisecond")
public static Expression fromMilliSecond(BigIntLiteral milliSecond) {
return fromMicroSecond(milliSecond.getValue() * 1000);
return fromMicroSecond(milliSecond.getValue() * 1000, FromMillisecond.RESULT_SCALE);
}

@ExecFunction(name = "from_microsecond")
public static Expression fromMicroSecond(BigIntLiteral microSecond) {
return fromMicroSecond(microSecond.getValue());
return fromMicroSecond(microSecond.getValue(), FromMicrosecond.RESULT_SCALE);
}

private static Expression fromMicroSecond(long microSecond) {
private static Expression fromMicroSecond(long microSecond, int scale) {
if (microSecond < 0 || microSecond > 253402271999999999L) {
throw new AnalysisException("Operation from_microsecond of " + microSecond + " out of range");
}
LocalDateTime dateTime = LocalDateTime.ofInstant(
Instant.ofEpochMilli(microSecond / 1000).plusNanos(microSecond % 1000 * 1000),
DateUtils.getTimeZone());
return new DateTimeV2Literal(DateTimeV2Type.MAX, dateTime.getYear(),
return new DateTimeV2Literal(DateTimeV2Type.of(scale), dateTime.getYear(),
dateTime.getMonthValue(), dateTime.getDayOfMonth(), dateTime.getHour(),
dateTime.getMinute(), dateTime.getSecond(), dateTime.getNano() / 1000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public class FromMicrosecond extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, PropagateNullLiteral, PropagateNullable,
FromSecondMonotonic {

public static final int RESULT_SCALE = 6;
private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DateTimeV2Type.MAX).args(BigIntType.INSTANCE));
FunctionSignature.ret(DateTimeV2Type.of(RESULT_SCALE)).args(BigIntType.INSTANCE));

public FromMicrosecond(Expression arg0) {
super("from_microsecond", arg0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
public class FromMillisecond extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, PropagateNullLiteral, PropagateNullable,
FromSecondMonotonic {
public static final DateTimeV2Type MillisecondDateTimeV2 = DateTimeV2Type.of(3);
public static final int RESULT_SCALE = 3;
private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(MillisecondDateTimeV2).args(BigIntType.INSTANCE));
FunctionSignature.ret(DateTimeV2Type.of(RESULT_SCALE)).args(BigIntType.INSTANCE));

public FromMillisecond(Expression arg0) {
super("from_millisecond", arg0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public class FromSecond extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, PropagateNullLiteral, PropagateNullable,
FromSecondMonotonic {

public static final int RESULT_SCALE = 0;
private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DateTimeV2Type.SYSTEM_DEFAULT).args(BigIntType.INSTANCE));
FunctionSignature.ret(DateTimeV2Type.of(RESULT_SCALE)).args(BigIntType.INSTANCE));

public FromSecond(Expression arg0) {
super("from_second", arg0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1367,8 +1367,8 @@ void testDateTrunc() {
String[] tags = {"year", "month", "day", "hour", "minute", "second"};

String[] answer = {
"'2001-01-01 00:00:00.000000'", "'2001-12-01 00:00:00.000000'", "'2001-12-31 00:00:00.000000'",
"'2001-12-31 01:00:00.000000'", "'2001-12-31 01:01:00.000000'", "'2001-12-31 01:01:01.000000'"
"'2001-01-01 00:00:00'", "'2001-12-01 00:00:00'", "'2001-12-31 00:00:00'",
"'2001-12-31 01:00:00'", "'2001-12-31 01:01:00'", "'2001-12-31 01:01:01'"
};
int answerIdx = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.expressions.functions;
package org.apache.doris.nereids.trees.expressions.functions.executable;

import org.apache.doris.nereids.trees.expressions.functions.executable.DateTimeExtractAndTransform;
import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DateTimeV2Literal;
import org.apache.doris.nereids.trees.expressions.literal.SmallIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
import org.apache.doris.nereids.types.DateTimeV2Type;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -124,4 +126,26 @@ void testSpecialDates() {
Assertions.assertEquals(new TinyIntLiteral((byte) 9), DateTimeExtractAndTransform.weekOfYear(dtx3));
Assertions.assertEquals(new TinyIntLiteral((byte) 9), DateTimeExtractAndTransform.weekOfYear(dtx4));
}

@Test
void testDateTruncScale() {
DateTimeV2Literal literal = new DateTimeV2Literal(DateTimeV2Type.of(3), 2025, 9, 25, 14, 57, 36, 123);
StringLiteral unit = new StringLiteral("HOUR");
DateTimeV2Literal result = (DateTimeV2Literal) DateTimeExtractAndTransform.dateTrunc(literal, unit);
Assertions.assertEquals(3, result.getScale());
result = (DateTimeV2Literal) DateTimeExtractAndTransform.dateTrunc(unit, literal);
Assertions.assertEquals(3, result.getScale());
}

@Test
void testFromSecondSacle() {
BigIntLiteral second = new BigIntLiteral(10000);
DateTimeV2Literal result;
result = (DateTimeV2Literal) DateTimeExtractAndTransform.fromSecond(second);
Assertions.assertEquals(0, result.getScale());
result = (DateTimeV2Literal) DateTimeExtractAndTransform.fromMilliSecond(second);
Assertions.assertEquals(3, result.getScale());
result = (DateTimeV2Literal) DateTimeExtractAndTransform.fromMicroSecond(second);
Assertions.assertEquals(6, result.getScale());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ suite("nereids_test_alias_function") {

test {
sql 'select cast(f1(\'2023-06-01\', 3) as string);'
result([['2023-05-29 00:00:00.000000']])
result([['2023-05-29 00:00:00']])
}
test {
sql 'select f2(f1(\'2023-05-20\', 2), 3)'
Expand Down
Loading