Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] informationschema.task_runs timezone fix (backport #52123) #52264

Merged
merged 1 commit into from
Oct 24, 2024
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 @@ -51,6 +51,7 @@
import org.apache.thrift.meta_data.FieldValueMetaData;
import org.apache.thrift.protocol.TType;

import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -162,7 +163,7 @@ private static ConstantOperator mayCast(ConstantOperator value, Type schemaType)
}
// From timestamp to DATETIME
if (value.getType().isBigint() && schemaType.isDatetime()) {
return ConstantOperator.createDatetime(DateUtils.fromEpochMillis(value.getBigint() * 1000));
return ConstantOperator.createDatetime(DateUtils.fromEpochMillis(value.getBigint() * 1000, ZoneId.systemDefault()));
}
return value.castTo(schemaType)
.orElseThrow(() -> new NotImplementedException(String.format("unsupported type cast from %s to %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
Expand Down Expand Up @@ -118,8 +117,8 @@ public static String formatDateTimeUnix(LocalDateTime dateTime) {
return dateTime.format(DATE_TIME_FORMATTER_UNIX);
}

public static LocalDateTime fromEpochMillis(long epochMilli) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneOffset.UTC);
public static LocalDateTime fromEpochMillis(long epochMilli, ZoneId zoneId) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), zoneId);
}

public static LocalDateTime parseUnixDateTime(String str) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -96,7 +97,7 @@ public static PipeFileRecord fromHdfsFile(FileStatus file) {
} else {
record.fileVersion = String.valueOf(file.getModificationTime());
}
record.lastModified = DateUtils.fromEpochMillis(file.getModificationTime());
record.lastModified = DateUtils.fromEpochMillis(file.getModificationTime(), ZoneOffset.UTC);
record.stagedTime = LocalDateTime.now();
record.loadState = FileListRepo.PipeFileState.UNLOADED;
return record;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;


import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

public class InformationSchemaDataSourceTest {

Expand Down Expand Up @@ -296,6 +299,14 @@ public void testDynamicPartition() throws Exception {
Assert.assertEquals("1", props.get("replication_num"));
}

public static ZoneOffset offset(ZoneId id) {
return ZoneOffset.ofTotalSeconds((int)
TimeUnit.MILLISECONDS.toSeconds(
TimeZone.getTimeZone(id).getRawOffset() // Returns offset in milliseconds
)
);
}

@Test
public void testTaskRunsEvaluation() throws Exception {
starRocksAssert.withDatabase("d1").useDatabase("d1");
Expand All @@ -306,10 +317,12 @@ public void testTaskRunsEvaluation() throws Exception {
taskRun.setTaskName("t_1024");
taskRun.setState(Constants.TaskRunState.SUCCESS);
taskRun.setDbName("d1");
taskRun.setCreateTime(DateUtils.parseDatTimeString("2024-01-02 03:04:05").toEpochSecond(ZoneOffset.UTC) * 1000);
taskRun.setProcessStartTime(
DateUtils.parseDatTimeString("2024-01-02 03:04:05").toEpochSecond(ZoneOffset.UTC) * 1000);
taskRun.setFinishTime(DateUtils.parseDatTimeString("2024-01-02 03:04:05").toEpochSecond(ZoneOffset.UTC) * 1000);
taskRun.setCreateTime(DateUtils.parseDatTimeString("2024-01-02 03:04:05")
.toEpochSecond(offset(ZoneId.systemDefault())) * 1000);
taskRun.setFinishTime(DateUtils.parseDatTimeString("2024-01-02 03:04:05")
.toEpochSecond(offset(ZoneId.systemDefault())) * 1000);
taskRun.setExpireTime(DateUtils.parseDatTimeString("2024-01-02 03:04:05")
.toEpochSecond(offset(ZoneId.systemDefault())) * 1000);
new MockUp<TaskManager>() {
@Mock
public List<TaskRunStatus> getMatchedTaskRunStatus(TGetTasksParams params) {
Expand All @@ -320,7 +333,7 @@ public List<TaskRunStatus> getMatchedTaskRunStatus(TGetTasksParams params) {
starRocksAssert.query("select * from information_schema.task_runs where task_name = 't_1024' ")
.explainContains(" constant exprs: ",
"NULL | 't_1024' | '2024-01-02 03:04:05' | '2024-01-02 03:04:05' | 'SUCCESS' | " +
"NULL | 'd1' | 'insert into t1 select * from t1' | '1970-01-01 00:00:00' | 0 | " +
"NULL | 'd1' | 'insert into t1 select * from t1' | '2024-01-02 03:04:05' | 0 | " +
"NULL | '0%' | '' | NULL");
starRocksAssert.query("select state, error_message" +
" from information_schema.task_runs where task_name = 't_1024' ")
Expand Down
Loading