use of org.apache.flink.table.types.logical.TimeType in project flink by apache.
the class PythonBridgeUtils method getPickledBytesFromJavaObject.
private static Object getPickledBytesFromJavaObject(Object obj, LogicalType dataType) throws IOException {
Pickler pickler = new Pickler();
initialize();
if (obj == null) {
return new byte[0];
} else {
if (dataType instanceof DateType) {
long time;
if (obj instanceof LocalDate) {
time = ((LocalDate) (obj)).toEpochDay();
} else {
time = ((Date) obj).toLocalDate().toEpochDay();
}
return pickler.dumps(time);
} else if (dataType instanceof TimeType) {
long time;
if (obj instanceof LocalTime) {
time = ((LocalTime) obj).toNanoOfDay();
} else {
time = ((Time) obj).toLocalTime().toNanoOfDay();
}
time = time / 1000;
return pickler.dumps(time);
} else if (dataType instanceof TimestampType) {
if (obj instanceof LocalDateTime) {
return pickler.dumps(Timestamp.valueOf((LocalDateTime) obj));
} else {
return pickler.dumps(obj);
}
} else if (dataType instanceof RowType) {
Row tmpRow = (Row) obj;
LogicalType[] tmpRowFieldTypes = ((RowType) dataType).getChildren().toArray(new LogicalType[0]);
List<Object> rowFieldBytes = new ArrayList<>(tmpRow.getArity() + 1);
rowFieldBytes.add(new byte[] { tmpRow.getKind().toByteValue() });
for (int i = 0; i < tmpRow.getArity(); i++) {
rowFieldBytes.add(getPickledBytesFromJavaObject(tmpRow.getField(i), tmpRowFieldTypes[i]));
}
return rowFieldBytes;
} else if (dataType instanceof MapType) {
List<List<Object>> serializedMapKV = new ArrayList<>(2);
MapType mapType = (MapType) dataType;
Map<Object, Object> mapObj = (Map) obj;
List<Object> keyBytesList = new ArrayList<>(mapObj.size());
List<Object> valueBytesList = new ArrayList<>(mapObj.size());
for (Map.Entry entry : mapObj.entrySet()) {
keyBytesList.add(getPickledBytesFromJavaObject(entry.getKey(), mapType.getKeyType()));
valueBytesList.add(getPickledBytesFromJavaObject(entry.getValue(), mapType.getValueType()));
}
serializedMapKV.add(keyBytesList);
serializedMapKV.add(valueBytesList);
return pickler.dumps(serializedMapKV);
} else if (dataType instanceof ArrayType) {
Object[] objects = (Object[]) obj;
List<Object> serializedElements = new ArrayList<>(objects.length);
ArrayType arrayType = (ArrayType) dataType;
LogicalType elementType = arrayType.getElementType();
for (Object object : objects) {
serializedElements.add(getPickledBytesFromJavaObject(object, elementType));
}
return pickler.dumps(serializedElements);
}
if (dataType instanceof FloatType) {
return pickler.dumps(String.valueOf(obj));
} else {
return pickler.dumps(obj);
}
}
}
use of org.apache.flink.table.types.logical.TimeType in project flink by apache.
the class TableauStyle method columnWidthsByType.
// Package private and private static methods to deal with complexity of string writing and
// formatting
/**
* Try to derive column width based on column types. If result set is not small enough to be
* stored in java heap memory, we can't determine column widths based on column values.
*/
static int[] columnWidthsByType(List<Column> columns, int maxColumnWidth, boolean printNullAsEmpty, boolean printRowKind) {
// fill width with field names first
final int[] colWidths = columns.stream().mapToInt(col -> col.getName().length()).toArray();
// determine proper column width based on types
for (int i = 0; i < columns.size(); ++i) {
LogicalType type = columns.get(i).getDataType().getLogicalType();
int len;
switch(type.getTypeRoot()) {
case TINYINT:
// extra for negative value
len = TinyIntType.PRECISION + 1;
break;
case SMALLINT:
// extra for negative value
len = SmallIntType.PRECISION + 1;
break;
case INTEGER:
// extra for negative value
len = IntType.PRECISION + 1;
break;
case BIGINT:
// extra for negative value
len = BigIntType.PRECISION + 1;
break;
case DECIMAL:
len = ((DecimalType) type).getPrecision() + // extra for negative value and decimal point
2;
break;
case BOOLEAN:
// "true" or "false"
len = 5;
break;
case DATE:
// e.g. 9999-12-31
len = 10;
break;
case TIME_WITHOUT_TIME_ZONE:
int precision = ((TimeType) type).getPrecision();
// 23:59:59[.999999999]
len = precision == 0 ? 8 : precision + 9;
break;
case TIMESTAMP_WITHOUT_TIME_ZONE:
precision = ((TimestampType) type).getPrecision();
len = timestampTypeColumnWidth(precision);
break;
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
precision = ((LocalZonedTimestampType) type).getPrecision();
len = timestampTypeColumnWidth(precision);
break;
default:
len = maxColumnWidth;
}
// adjust column width with potential null values
len = printNullAsEmpty ? len : Math.max(len, PrintStyle.NULL_VALUE.length());
colWidths[i] = Math.max(colWidths[i], len);
}
// add an extra column for row kind if necessary
if (printRowKind) {
final int[] ret = new int[columns.size() + 1];
ret[0] = ROW_KIND_COLUMN.length();
System.arraycopy(colWidths, 0, ret, 1, columns.size());
return ret;
} else {
return colWidths;
}
}
Aggregations