Java Code Examples for org.apache.flink.table.types.logical.LogicalType#getTypeRoot()
The following examples show how to use
org.apache.flink.table.types.logical.LogicalType#getTypeRoot() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogicalTypeCasts.java From flink with Apache License 2.0 | 6 votes |
private static boolean supportsConstructedCasting( LogicalType sourceType, LogicalType targetType, boolean allowExplicit) { final LogicalTypeRoot sourceRoot = sourceType.getTypeRoot(); final LogicalTypeRoot targetRoot = targetType.getTypeRoot(); // all constructed types can only be casted within the same type root if (sourceRoot == targetRoot) { final List<LogicalType> sourceChildren = sourceType.getChildren(); final List<LogicalType> targetChildren = targetType.getChildren(); if (sourceChildren.size() != targetChildren.size()) { return false; } for (int i = 0; i < sourceChildren.size(); i++) { if (!supportsCasting(sourceChildren.get(i), targetChildren.get(i), allowExplicit)) { return false; } } return true; } return false; }
Example 2
Source File: HBaseTableSchema.java From flink with Apache License 2.0 | 6 votes |
/** * Construct a {@link HBaseTableSchema} from a {@link TableSchema}. */ public static HBaseTableSchema fromTableSchema(TableSchema schema) { HBaseTableSchema hbaseSchema = new HBaseTableSchema(); RowType rowType = (RowType) schema.toPhysicalRowDataType().getLogicalType(); for (RowType.RowField field : rowType.getFields()) { LogicalType fieldType = field.getType(); if (fieldType.getTypeRoot() == LogicalTypeRoot.ROW) { RowType familyType = (RowType) fieldType; String familyName = field.getName(); for (RowType.RowField qualifier : familyType.getFields()) { hbaseSchema.addColumn( familyName, qualifier.getName(), fromLogicalToDataType(qualifier.getType())); } } else if (fieldType.getChildren().size() == 0) { hbaseSchema.setRowKey(field.getName(), fromLogicalToDataType(fieldType)); } else { throw new IllegalArgumentException( "Unsupported field type '" + fieldType + "' for HBase."); } } return hbaseSchema; }
Example 3
Source File: LogicalTypeMerging.java From flink with Apache License 2.0 | 6 votes |
private static @Nullable LogicalType findCommonCastableType(List<LogicalType> normalizedTypes) { LogicalType resultType = normalizedTypes.get(0); for (LogicalType type : normalizedTypes) { final LogicalTypeRoot typeRoot = type.getTypeRoot(); // NULL does not affect the result of this loop if (typeRoot == NULL) { continue; } if (supportsImplicitCast(resultType, type)) { resultType = type; } else { if (!supportsImplicitCast(type, resultType)) { return null; } } } return resultType; }
Example 4
Source File: CollectionDataType.java From flink with Apache License 2.0 | 5 votes |
private static Class<?> ensureArrayConversionClass( LogicalType logicalType, DataType elementDataType, @Nullable Class<?> clazz) { // arrays are a special case because their default conversion class depends on the // conversion class of the element type if (logicalType.getTypeRoot() == LogicalTypeRoot.ARRAY && clazz == null) { return Array.newInstance(elementDataType.getConversionClass(), 0).getClass(); } return clazz; }
Example 5
Source File: LogicalTypeMerging.java From flink with Apache License 2.0 | 5 votes |
private static LogicalType createCommonExactNumericType(LogicalType resultType, LogicalType type) { // same EXACT_NUMERIC types if (type.equals(resultType)) { return resultType; } final LogicalTypeRoot resultTypeRoot = resultType.getTypeRoot(); final LogicalTypeRoot typeRoot = type.getTypeRoot(); // no DECIMAL types involved if (resultTypeRoot != DECIMAL && typeRoot != DECIMAL) { // type root contains order of precision if (getPrecision(type) > getPrecision(resultType)) { return type; } return resultType; } // determine DECIMAL with precision (p), scale (s) and number of whole digits (d): // d = max(p1 - s1, p2 - s2) // s <= max(s1, s2) // p = s + d final int p1 = getPrecision(resultType); final int p2 = getPrecision(type); final int s1 = getScale(resultType); final int s2 = getScale(type); final int maxPrecision = DecimalType.MAX_PRECISION; int d = Math.max(p1 - s1, p2 - s2); d = Math.min(d, maxPrecision); int s = Math.max(s1, s2); s = Math.min(s, maxPrecision - d); final int p = d + s; return new DecimalType(p, s); }
Example 6
Source File: HiveFunctionUtil.java From flink with Apache License 2.0 | 5 votes |
private static boolean isPrimitive(LogicalType type) { switch (type.getTypeRoot()) { case BOOLEAN: case TINYINT: case SMALLINT: case INTEGER: case BIGINT: case FLOAT: case DOUBLE: return true; default: return false; } }
Example 7
Source File: TypeCheckUtils.java From flink with Apache License 2.0 | 5 votes |
public static boolean isTimeInterval(LogicalType type) { switch (type.getTypeRoot()) { case INTERVAL_DAY_TIME: case INTERVAL_YEAR_MONTH: return true; default: return false; } }
Example 8
Source File: LogicalTypeGeneralization.java From flink with Apache License 2.0 | 5 votes |
private static @Nullable List<LogicalType> findCommonChildrenTypes(List<LogicalType> normalizedTypes) { final LogicalType firstType = normalizedTypes.get(0); final LogicalTypeRoot typeRoot = firstType.getTypeRoot(); final int numberOfChildren = firstType.getChildren().size(); for (LogicalType type : normalizedTypes) { // all types must have the same root if (type.getTypeRoot() != typeRoot) { return null; } // all types must have the same number of children if (type.getChildren().size() != numberOfChildren) { return null; } } // recursively compute column-wise least restrictive final List<LogicalType> resultChildren = new ArrayList<>(numberOfChildren); for (int i = 0; i < numberOfChildren; i++) { final Optional<LogicalType> childType = findCommonType(new ChildTypeView(normalizedTypes, i)); if (!childType.isPresent()) { return null; } resultChildren.add(childType.get()); } // no child should be empty at this point return resultChildren; }
Example 9
Source File: SortCodeGeneratorTest.java From flink with Apache License 2.0 | 5 votes |
private Object value2(LogicalType type, Random rnd) { switch (type.getTypeRoot()) { case BOOLEAN: return false; case TINYINT: return (byte) 0; case SMALLINT: return (short) 0; case INTEGER: return 0; case BIGINT: return 0L; case FLOAT: return 0f; case DOUBLE: return 0d; case VARCHAR: return BinaryString.fromString("0"); case DECIMAL: DecimalType decimalType = (DecimalType) type; return Decimal.fromBigDecimal(new BigDecimal(0), decimalType.getPrecision(), decimalType.getScale()); case ARRAY: case VARBINARY: byte[] bytes = new byte[rnd.nextInt(7) + 10]; rnd.nextBytes(bytes); return type instanceof VarBinaryType ? bytes : BinaryArray.fromPrimitiveArray(bytes); case ROW: RowType rowType = (RowType) type; if (rowType.getFields().get(0).getType().getTypeRoot() == INTEGER) { return GenericRow.of(rnd.nextInt()); } else { return GenericRow.of(GenericRow.of(new Object[]{null})); } case ANY: return new BinaryGeneric<>(rnd.nextInt(), IntSerializer.INSTANCE); default: throw new RuntimeException("Not support!"); } }
Example 10
Source File: BinaryArrayWriter.java From flink with Apache License 2.0 | 5 votes |
/** * @deprecated Use {@link #createNullSetter(LogicalType)} for avoiding logical types during runtime. */ @Deprecated public void setNullAt(int pos, LogicalType type) { switch (type.getTypeRoot()) { case BOOLEAN: setNullBoolean(pos); break; case TINYINT: setNullByte(pos); break; case SMALLINT: setNullShort(pos); break; case INTEGER: case DATE: case TIME_WITHOUT_TIME_ZONE: case INTERVAL_YEAR_MONTH: setNullInt(pos); break; case BIGINT: case TIMESTAMP_WITHOUT_TIME_ZONE: case TIMESTAMP_WITH_LOCAL_TIME_ZONE: case INTERVAL_DAY_TIME: setNullLong(pos); break; case FLOAT: setNullFloat(pos); break; case DOUBLE: setNullDouble(pos); break; default: setNullAt(pos); } }
Example 11
Source File: SortCodeGeneratorTest.java From flink with Apache License 2.0 | 4 votes |
private Object value1(LogicalType type, Random rnd) { switch (type.getTypeRoot()) { case BOOLEAN: return false; case TINYINT: return Byte.MIN_VALUE; case SMALLINT: return Short.MIN_VALUE; case INTEGER: return Integer.MIN_VALUE; case BIGINT: return Long.MIN_VALUE; case FLOAT: return Float.MIN_VALUE; case DOUBLE: return Double.MIN_VALUE; case VARCHAR: return StringData.fromString(""); case DECIMAL: DecimalType decimalType = (DecimalType) type; return DecimalData.fromBigDecimal(new BigDecimal(Integer.MIN_VALUE), decimalType.getPrecision(), decimalType.getScale()); case TIMESTAMP_WITHOUT_TIME_ZONE: return TimestampData.fromEpochMillis(Long.MIN_VALUE); case ARRAY: byte[] bytes = new byte[rnd.nextInt(7) + 1]; rnd.nextBytes(bytes); BinaryArrayData array = BinaryArrayData.fromPrimitiveArray(bytes); for (int i = 0; i < bytes.length; i++) { array.setNullByte(i); } return array; case VARBINARY: byte[] bytes2 = new byte[rnd.nextInt(7) + 1]; rnd.nextBytes(bytes2); return bytes2; case ROW: return GenericRowData.of(new Object[]{null}); case RAW: return RawValueData.fromObject(rnd.nextInt()); default: throw new RuntimeException("Not support!"); } }
Example 12
Source File: BinaryArrayWriter.java From flink with Apache License 2.0 | 4 votes |
/** * Creates an for accessor setting the elements of an array writer to {@code null} during runtime. * * @param elementType the element type of the array */ public static NullSetter createNullSetter(LogicalType elementType) { // ordered by type root definition switch (elementType.getTypeRoot()) { case CHAR: case VARCHAR: case BINARY: case VARBINARY: case DECIMAL: case BIGINT: case TIMESTAMP_WITHOUT_TIME_ZONE: case TIMESTAMP_WITH_LOCAL_TIME_ZONE: case INTERVAL_DAY_TIME: case ARRAY: case MULTISET: case MAP: case ROW: case STRUCTURED_TYPE: case RAW: return BinaryArrayWriter::setNullLong; case BOOLEAN: return BinaryArrayWriter::setNullBoolean; case TINYINT: return BinaryArrayWriter::setNullByte; case SMALLINT: return BinaryArrayWriter::setNullShort; case INTEGER: case DATE: case TIME_WITHOUT_TIME_ZONE: case INTERVAL_YEAR_MONTH: return BinaryArrayWriter::setNullInt; case FLOAT: return BinaryArrayWriter::setNullFloat; case DOUBLE: return BinaryArrayWriter::setNullDouble; case TIMESTAMP_WITH_TIME_ZONE: throw new UnsupportedOperationException(); case DISTINCT_TYPE: return createNullSetter(((DistinctType) elementType).getSourceType()); case NULL: case SYMBOL: case UNRESOLVED: default: throw new IllegalArgumentException(); } }
Example 13
Source File: ArrayData.java From flink with Apache License 2.0 | 4 votes |
/** * Creates an accessor for getting elements in an internal array data structure at the * given position. * * @param elementType the element type of the array */ static ElementGetter createElementGetter(LogicalType elementType) { final ElementGetter elementGetter; // ordered by type root definition switch (elementType.getTypeRoot()) { case CHAR: case VARCHAR: elementGetter = ArrayData::getString; break; case BOOLEAN: elementGetter = ArrayData::getBoolean; break; case BINARY: case VARBINARY: elementGetter = ArrayData::getBinary; break; case DECIMAL: final int decimalPrecision = getPrecision(elementType); final int decimalScale = getScale(elementType); elementGetter = (array, pos) -> array.getDecimal(pos, decimalPrecision, decimalScale); break; case TINYINT: elementGetter = ArrayData::getByte; break; case SMALLINT: elementGetter = ArrayData::getShort; break; case INTEGER: case DATE: case TIME_WITHOUT_TIME_ZONE: case INTERVAL_YEAR_MONTH: elementGetter = ArrayData::getInt; break; case BIGINT: case INTERVAL_DAY_TIME: elementGetter = ArrayData::getLong; break; case FLOAT: elementGetter = ArrayData::getFloat; break; case DOUBLE: elementGetter = ArrayData::getDouble; break; case TIMESTAMP_WITHOUT_TIME_ZONE: case TIMESTAMP_WITH_LOCAL_TIME_ZONE: final int timestampPrecision = getPrecision(elementType); elementGetter = (array, pos) -> array.getTimestamp(pos, timestampPrecision); break; case TIMESTAMP_WITH_TIME_ZONE: throw new UnsupportedOperationException(); case ARRAY: elementGetter = ArrayData::getArray; break; case MULTISET: case MAP: elementGetter = ArrayData::getMap; break; case ROW: case STRUCTURED_TYPE: final int rowFieldCount = getFieldCount(elementType); elementGetter = (array, pos) -> array.getRow(pos, rowFieldCount); break; case DISTINCT_TYPE: elementGetter = createElementGetter(((DistinctType) elementType).getSourceType()); break; case RAW: elementGetter = ArrayData::getRawValue; break; case NULL: case SYMBOL: case UNRESOLVED: default: throw new IllegalArgumentException(); } if (!elementType.isNullable()) { return elementGetter; } return (array, pos) -> { if (array.isNullAt(pos)) { return null; } return elementGetter.getElementOrNull(array, pos); }; }
Example 14
Source File: TypeCheckUtils.java From flink with Apache License 2.0 | 4 votes |
public static boolean isBoolean(LogicalType type) { return type.getTypeRoot() == BOOLEAN; }
Example 15
Source File: CsvRowDataSerializationSchema.java From flink with Apache License 2.0 | 4 votes |
private ArrayElementConverter createArrayElementConverter(LogicalType fieldType) { switch (fieldType.getTypeRoot()) { case NULL: return (csvMapper, container, array, pos) -> container.nullNode(); case BOOLEAN: return (csvMapper, container, array, pos) -> container.booleanNode(array.getBoolean(pos)); case TINYINT: return (csvMapper, container, array, pos) -> container.numberNode(array.getByte(pos)); case SMALLINT: return (csvMapper, container, array, pos) -> container.numberNode(array.getShort(pos)); case INTEGER: case INTERVAL_YEAR_MONTH: return (csvMapper, container, array, pos) -> container.numberNode(array.getInt(pos)); case BIGINT: case INTERVAL_DAY_TIME: return (csvMapper, container, array, pos) -> container.numberNode(array.getLong(pos)); case FLOAT: return (csvMapper, container, array, pos) -> container.numberNode(array.getFloat(pos)); case DOUBLE: return (csvMapper, container, array, pos) -> container.numberNode(array.getDouble(pos)); case CHAR: case VARCHAR: return (csvMapper, container, array, pos) -> container.textNode(array.getString(pos).toString()); case BINARY: case VARBINARY: return (csvMapper, container, array, pos) -> container.binaryNode(array.getBinary(pos)); case DATE: return (csvMapper, container, array, pos) -> convertDate(array.getInt(pos), container); case TIME_WITHOUT_TIME_ZONE: return (csvMapper, container, array, pos) -> convertTime(array.getInt(pos), container); case TIMESTAMP_WITH_TIME_ZONE: final int zonedTimestampPrecision = ((LocalZonedTimestampType) fieldType).getPrecision(); return (csvMapper, container, array, pos) -> convertTimestamp(array.getTimestamp(pos, zonedTimestampPrecision), container); case TIMESTAMP_WITHOUT_TIME_ZONE: final int timestampPrecision = ((TimestampType) fieldType).getPrecision(); return (csvMapper, container, array, pos) -> convertTimestamp(array.getTimestamp(pos, timestampPrecision), container); case DECIMAL: return createDecimalArrayElementConverter((DecimalType) fieldType); // we don't support ARRAY and ROW in an ARRAY, see CsvRowSchemaConverter#validateNestedField case ARRAY: case ROW: case MAP: case MULTISET: case RAW: default: throw new UnsupportedOperationException("Unsupported type: " + fieldType); } }
Example 16
Source File: TypeInfoDataTypeConverter.java From flink with Apache License 2.0 | 4 votes |
public static TypeInformation<?> fromDataTypeToTypeInfo(DataType dataType) { Class<?> clazz = dataType.getConversionClass(); if (clazz.isPrimitive()) { final TypeInformation<?> foundTypeInfo = primitiveDataTypeTypeInfoMap.get(clazz.getName()); if (foundTypeInfo != null) { return foundTypeInfo; } } LogicalType logicalType = fromDataTypeToLogicalType(dataType); switch (logicalType.getTypeRoot()) { case TIMESTAMP_WITHOUT_TIME_ZONE: TimestampType timestampType = (TimestampType) logicalType; int precision = timestampType.getPrecision(); if (timestampType.getKind() == TimestampKind.REGULAR) { return clazz == TimestampData.class ? new TimestampDataTypeInfo(precision) : (clazz == LocalDateTime.class ? ((3 == precision) ? Types.LOCAL_DATE_TIME : new LegacyLocalDateTimeTypeInfo(precision)) : ((3 == precision) ? Types.SQL_TIMESTAMP : new LegacyTimestampTypeInfo(precision))); } else { return TypeConversions.fromDataTypeToLegacyInfo(dataType); } case TIMESTAMP_WITH_LOCAL_TIME_ZONE: LocalZonedTimestampType lzTs = (LocalZonedTimestampType) logicalType; int precisionLzTs = lzTs.getPrecision(); return clazz == TimestampData.class ? new TimestampDataTypeInfo(precisionLzTs) : (clazz == Instant.class ? ((3 == precisionLzTs) ? Types.INSTANT : new LegacyInstantTypeInfo(precisionLzTs)) : TypeConversions.fromDataTypeToLegacyInfo(dataType)); case DECIMAL: DecimalType decimalType = (DecimalType) logicalType; return clazz == DecimalData.class ? new DecimalDataTypeInfo(decimalType.getPrecision(), decimalType.getScale()) : new BigDecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()); case CHAR: case VARCHAR: // ignore precision return clazz == StringData.class ? StringDataTypeInfo.INSTANCE : BasicTypeInfo.STRING_TYPE_INFO; case BINARY: case VARBINARY: // ignore precision return PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO; case INTERVAL_YEAR_MONTH: return TimeIntervalTypeInfo.INTERVAL_MONTHS; case INTERVAL_DAY_TIME: return TimeIntervalTypeInfo.INTERVAL_MILLIS; case ARRAY: if (dataType instanceof CollectionDataType && !isPrimitive(((CollectionDataType) dataType).getElementDataType().getLogicalType())) { return ObjectArrayTypeInfo.getInfoFor( fromDataTypeToTypeInfo(((CollectionDataType) dataType).getElementDataType())); } else { return TypeConversions.fromDataTypeToLegacyInfo(dataType); } case MAP: KeyValueDataType mapType = (KeyValueDataType) dataType; return new MapTypeInfo( fromDataTypeToTypeInfo(mapType.getKeyDataType()), fromDataTypeToTypeInfo(mapType.getValueDataType())); case MULTISET: return MultisetTypeInfo.getInfoFor( fromDataTypeToTypeInfo(((CollectionDataType) dataType).getElementDataType())); case ROW: if (RowData.class.isAssignableFrom(dataType.getConversionClass())) { return RowDataTypeInfo.of((RowType) fromDataTypeToLogicalType(dataType)); } else if (Row.class == dataType.getConversionClass()) { RowType logicalRowType = (RowType) logicalType; return new RowTypeInfo( dataType.getChildren() .stream() .map(TypeInfoDataTypeConverter::fromDataTypeToTypeInfo) .toArray(TypeInformation[]::new), logicalRowType.getFieldNames().toArray(new String[0])); } else { return TypeConversions.fromDataTypeToLegacyInfo(dataType); } case RAW: if (logicalType instanceof RawType) { final RawType<?> rawType = (RawType<?>) logicalType; return createWrapperTypeInfo(rawType); } return TypeConversions.fromDataTypeToLegacyInfo(dataType); default: return TypeConversions.fromDataTypeToLegacyInfo(dataType); } }
Example 17
Source File: CsvRowDataSerializationSchema.java From flink with Apache License 2.0 | 4 votes |
private RowFieldConverter createRowFieldConverter(LogicalType fieldType) { switch (fieldType.getTypeRoot()) { case NULL: return (csvMapper, container, row, pos) -> container.nullNode(); case BOOLEAN: return (csvMapper, container, row, pos) -> container.booleanNode(row.getBoolean(pos)); case TINYINT: return (csvMapper, container, row, pos) -> container.numberNode(row.getByte(pos)); case SMALLINT: return (csvMapper, container, row, pos) -> container.numberNode(row.getShort(pos)); case INTEGER: case INTERVAL_YEAR_MONTH: return (csvMapper, container, row, pos) -> container.numberNode(row.getInt(pos)); case BIGINT: case INTERVAL_DAY_TIME: return (csvMapper, container, row, pos) -> container.numberNode(row.getLong(pos)); case FLOAT: return (csvMapper, container, row, pos) -> container.numberNode(row.getFloat(pos)); case DOUBLE: return (csvMapper, container, row, pos) -> container.numberNode(row.getDouble(pos)); case CHAR: case VARCHAR: return (csvMapper, container, row, pos) -> container.textNode(row.getString(pos).toString()); case BINARY: case VARBINARY: return (csvMapper, container, row, pos) -> container.binaryNode(row.getBinary(pos)); case DATE: return (csvMapper, container, row, pos) -> convertDate(row.getInt(pos), container); case TIME_WITHOUT_TIME_ZONE: return (csvMapper, container, row, pos) -> convertTime(row.getInt(pos), container); case TIMESTAMP_WITH_TIME_ZONE: final int zonedTimestampPrecision = ((LocalZonedTimestampType) fieldType).getPrecision(); return (csvMapper, container, row, pos) -> convertTimestamp(row.getTimestamp(pos, zonedTimestampPrecision), container); case TIMESTAMP_WITHOUT_TIME_ZONE: final int timestampPrecision = ((TimestampType) fieldType).getPrecision(); return (csvMapper, container, row, pos) -> convertTimestamp(row.getTimestamp(pos, timestampPrecision), container); case DECIMAL: return createDecimalRowFieldConverter((DecimalType) fieldType); case ARRAY: return createArrayRowFieldConverter((ArrayType) fieldType); case ROW: return createRowRowFieldConverter((RowType) fieldType); case MAP: case MULTISET: case RAW: default: throw new UnsupportedOperationException("Unsupported type: " + fieldType); } }
Example 18
Source File: TypeCheckUtils.java From flink with Apache License 2.0 | 4 votes |
public static boolean isInteger(LogicalType type) { return type.getTypeRoot() == INTEGER; }
Example 19
Source File: OrcSplitReaderUtil.java From flink with Apache License 2.0 | 4 votes |
/** * See {@code org.apache.flink.table.catalog.hive.util.HiveTypeUtil}. */ public static TypeDescription logicalTypeToOrcType(LogicalType type) { type = type.copy(true); switch (type.getTypeRoot()) { case CHAR: return TypeDescription.createChar().withMaxLength(((CharType) type).getLength()); case VARCHAR: int len = ((VarCharType) type).getLength(); if (len == VarCharType.MAX_LENGTH) { return TypeDescription.createString(); } else { return TypeDescription.createVarchar().withMaxLength(len); } case BOOLEAN: return TypeDescription.createBoolean(); case VARBINARY: if (type.equals(DataTypes.BYTES().getLogicalType())) { return TypeDescription.createBinary(); } else { throw new UnsupportedOperationException( "Not support other binary type: " + type); } case DECIMAL: DecimalType decimalType = (DecimalType) type; return TypeDescription.createDecimal() .withScale(decimalType.getScale()) .withPrecision(decimalType.getPrecision()); case TINYINT: return TypeDescription.createByte(); case SMALLINT: return TypeDescription.createShort(); case INTEGER: return TypeDescription.createInt(); case BIGINT: return TypeDescription.createLong(); case FLOAT: return TypeDescription.createFloat(); case DOUBLE: return TypeDescription.createDouble(); case DATE: return TypeDescription.createDate(); case TIMESTAMP_WITHOUT_TIME_ZONE: return TypeDescription.createTimestamp(); case ARRAY: ArrayType arrayType = (ArrayType) type; return TypeDescription.createList(logicalTypeToOrcType(arrayType.getElementType())); case MAP: MapType mapType = (MapType) type; return TypeDescription.createMap( logicalTypeToOrcType(mapType.getKeyType()), logicalTypeToOrcType(mapType.getValueType())); case ROW: RowType rowType = (RowType) type; TypeDescription struct = TypeDescription.createStruct(); for (int i = 0; i < rowType.getFieldCount(); i++) { struct.addField( rowType.getFieldNames().get(i), logicalTypeToOrcType(rowType.getChildren().get(i))); } return struct; default: throw new UnsupportedOperationException("Unsupported type: " + type); } }
Example 20
Source File: PrintUtils.java From flink with Apache License 2.0 | 4 votes |
/** * 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. */ public static int[] columnWidthsByType( List<TableColumn> columns, int maxColumnWidth, String nullColumn, @Nullable String rowKindColumn) { // 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).getType().getLogicalType(); int len; switch (type.getTypeRoot()) { case TINYINT: len = TinyIntType.PRECISION + 1; // extra for negative value break; case SMALLINT: len = SmallIntType.PRECISION + 1; // extra for negative value break; case INTEGER: len = IntType.PRECISION + 1; // extra for negative value break; case BIGINT: len = BigIntType.PRECISION + 1; // extra for negative value break; case DECIMAL: len = ((DecimalType) type).getPrecision() + 2; // extra for negative value and decimal point break; case BOOLEAN: len = 5; // "true" or "false" break; case DATE: len = 10; // e.g. 9999-12-31 break; case TIME_WITHOUT_TIME_ZONE: int precision = ((TimeType) type).getPrecision(); len = precision == 0 ? 8 : precision + 9; // 23:59:59[.999999999] 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 colWidths[i] = Math.max(colWidths[i], Math.max(len, nullColumn.length())); } // add an extra column for row kind if necessary if (rowKindColumn != null) { final int[] ret = new int[columns.size() + 1]; ret[0] = rowKindColumn.length(); System.arraycopy(colWidths, 0, ret, 1, columns.size()); return ret; } else { return colWidths; } }