Skip to content

Commit

Permalink
fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
morningman committed Sep 18, 2024
1 parent 392d5aa commit 56bf78e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.catalog.TableProperty;
import org.apache.doris.catalog.Type;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ClientPool;
import org.apache.doris.common.Pair;
Expand All @@ -48,6 +49,7 @@
import org.apache.doris.datasource.ExternalCatalog;
import org.apache.doris.datasource.ExternalMetaCacheMgr;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.datasource.TablePartitionValues;
import org.apache.doris.datasource.hive.HMSExternalCatalog;
import org.apache.doris.datasource.hive.HMSExternalTable;
import org.apache.doris.datasource.hive.HiveMetaStoreCache;
Expand Down Expand Up @@ -105,6 +107,7 @@
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
Expand Down Expand Up @@ -796,12 +799,32 @@ private static void filterColumns(TFetchSchemaTableDataResult result,
result.setDataBatch(filterColumnsRows);
}

// Refer to be/src/vec/runtime/vdatetime_value.h
private static long convertToDateTimeV2(
int year, int month, int day, int hour, int minute, int second, int microsecond) {
return (long) microsecond | (long) second << 20 | (long) minute << 26 | (long) hour << 32
| (long) day << 37 | (long) month << 42 | (long) year << 46;
}

// Refer to be/src/vec/runtime/vdatetime_value.h
private static long convertToDateV2(
int year, int month, int day) {
return (long) day | (long) month << 5 | (long) year << 9;
}

private static long convertStringToDateTimeV2(String dateTimeStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = TimeUtils.formatDateTimeAndFullZero(dateTimeStr, formatter);
return convertToDateTimeV2(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth(),
dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(), dateTime.getNano() / 1000);
}

private static long convertStringToDateV2(String dateStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime dateTime = TimeUtils.formatDateTimeAndFullZero(dateStr, formatter);
return convertToDateV2(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth());
}

private static TFetchSchemaTableDataResult mtmvMetadataResult(TMetadataTableRequestParams params) {
if (LOG.isDebugEnabled()) {
LOG.debug("mtmvMetadataResult() start");
Expand Down Expand Up @@ -1506,10 +1529,12 @@ private static List<TRow> partitionValuesMetadataResultForHmsTable(HMSExternalTa
throws AnalysisException {
List<Column> partitionCols = tbl.getPartitionColumns();
List<Integer> colIdxs = Lists.newArrayList();
List<Type> types = Lists.newArrayList();
for (String colName : colNames) {
for (int i = 0; i < partitionCols.size(); ++i) {
if (partitionCols.get(i).getName().equalsIgnoreCase(colName)) {
colIdxs.add(i);
types.add(partitionCols.get(i).getType());
}
}
}
Expand All @@ -1530,8 +1555,49 @@ private static List<TRow> partitionValuesMetadataResultForHmsTable(HMSExternalTa
if (values.size() != partitionCols.size()) {
continue;
}
for (Integer idx : colIdxs) {
trow.addToColumnValue(new TCell().setStringVal(values.get(idx))); // COLUMN_VALUE

for (int i = 0; i < colIdxs.size(); ++i) {
int idx = colIdxs.get(i);
String partitionValue = values.get(idx);
if (partitionValue == null && partitionValue.equals(TablePartitionValues.HIVE_DEFAULT_PARTITION)) {
trow.addToColumnValue(new TCell().setIsNull(true));
} else {
Type type = types.get(i);
switch (type.getPrimitiveType()) {
case BOOLEAN:
trow.addToColumnValue(new TCell().setBoolVal(Boolean.valueOf(partitionValue)));
break;
case TINYINT:
case SMALLINT:
case INT:
trow.addToColumnValue(new TCell().setIntVal(Integer.valueOf(partitionValue)));
break;
case BIGINT:
trow.addToColumnValue(new TCell().setLongVal(Long.valueOf(partitionValue)));
break;
case FLOAT:
trow.addToColumnValue(new TCell().setDoubleVal(Float.valueOf(partitionValue)));
break;
case DOUBLE:
trow.addToColumnValue(new TCell().setDoubleVal(Double.valueOf(partitionValue)));
break;
case VARCHAR:
case CHAR:
case STRING:
trow.addToColumnValue(new TCell().setStringVal(partitionValue));
break;
case DATE:
case DATEV2:
trow.addToColumnValue(new TCell().setLongVal(convertStringToDateV2(partitionValue)));
case DATETIME:
case DATETIMEV2:
trow.addToColumnValue(new TCell().setLongVal(convertStringToDateTimeV2(partitionValue)));
break;
default:
throw new AnalysisException(
"Unsupported partition column type for $partitions sys table " + type);
}
}
}
dataBatch.add(trow);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.DatabaseIf;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.common.ErrorCode;
Expand Down Expand Up @@ -168,12 +167,12 @@ public String getTableName() {
@Override
public List<Column> getTableColumns() throws AnalysisException {
Preconditions.checkNotNull(table);
// TODO support other type of tables
// TODO: support other type of sys tables
if (schema == null) {
List<Column> partitionColumns = ((HMSExternalTable) table).getPartitionColumns();
schema = Lists.newArrayList();
for (Column column : partitionColumns) {
schema.add(new Column(column.getName(), ScalarType.createStringType()));
schema.add(new Column(column));
}
}
return schema;
Expand Down
1 change: 1 addition & 0 deletions gensrc/thrift/Data.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ struct TCell {
3: optional i64 longVal
4: optional double doubleVal
5: optional string stringVal
6: optional bool isNull
// add type: date datetime
}

Expand Down

0 comments on commit 56bf78e

Please sign in to comment.