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] Fix incorrect result for date formatting #366

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -38,15 +38,15 @@
import org.apache.flink.table.types.logical.RowType;
import org.apache.flink.table.types.logical.TimestampType;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;

public class StarRocksTableRowTransformer implements StarRocksIRowTransformer<RowData> {

private static final long serialVersionUID = 1L;
Expand All @@ -56,7 +56,6 @@ public class StarRocksTableRowTransformer implements StarRocksIRowTransformer<Ro
private String[] columnNames;
private DataType[] columnDataTypes;
private Map<String, StarRocksDataType> columns;
private final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");

private transient JsonWrapper jsonWrapper;

Expand Down Expand Up @@ -143,7 +142,7 @@ private Object typeConvertion(LogicalType type, RowData record, int pos) {
}
return sValue;
case DATE:
return dateFormatter.format(Date.valueOf(LocalDate.ofEpochDay(record.getInt(pos))));
return ISO_LOCAL_DATE.format(LocalDate.ofEpochDay(record.getInt(pos)));
case TIMESTAMP_WITHOUT_TIME_ZONE:
final int timestampPrecision =((TimestampType) type).getPrecision();
return record.getTimestamp(pos, timestampPrecision).toLocalDateTime().toString();
Expand Down Expand Up @@ -205,7 +204,7 @@ private List<Object> convertNestedArray(ArrayData arrData, LogicalType type) {
return data.parallelStream().map(m -> convertNestedMap((MapData)m, lt)).collect(Collectors.toList());
}
if (LogicalTypeRoot.DATE.equals(lt.getTypeRoot())) {
return data.parallelStream().map(date -> dateFormatter.format(Date.valueOf(LocalDate.ofEpochDay((Integer)date)))).collect(Collectors.toList());
return data.parallelStream().map(date -> ISO_LOCAL_DATE.format(LocalDate.ofEpochDay((Integer)date))).collect(Collectors.toList());
}
if (LogicalTypeRoot.ARRAY.equals(lt.getTypeRoot())) {
// traversal of the nested array
Expand Down Expand Up @@ -235,7 +234,7 @@ private Map<Object, Object> convertNestedMap(MapData mapData, LogicalType type)
continue;
}
if (LogicalTypeRoot.DATE.equals(valType.getTypeRoot())) {
result.put(en.getKey().toString(), dateFormatter.format(Date.valueOf(LocalDate.ofEpochDay((Integer)en.getValue()))));
result.put(en.getKey().toString(), ISO_LOCAL_DATE.format(LocalDate.ofEpochDay((Integer)en.getValue())));
continue;
}
if (LogicalTypeRoot.ARRAY.equals(valType.getTypeRoot())) {
Expand Down