Skip to content

Commit

Permalink
[BugFix] informationschema.task_runs timezone fix (backport #52123) (#…
Browse files Browse the repository at this point in the history
…52264)

Co-authored-by: Rohit Satardekar <rohitrs1983@gmail.com>
  • Loading branch information
mergify[bot] and rohitrs1983 authored Oct 24, 2024
1 parent 4933c4e commit dcfb2d9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
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

0 comments on commit dcfb2d9

Please sign in to comment.