JingsongLi commented on code in PR #423: URL: https://github.com/apache/flink-table-store/pull/423#discussion_r1044197565
########## flink-table-store-core/src/main/java/org/apache/flink/table/store/file/schema/DataValueConverter.java: ########## @@ -0,0 +1,327 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.store.file.schema; + +import org.apache.flink.table.data.DecimalData; +import org.apache.flink.table.data.StringData; +import org.apache.flink.table.data.TimestampData; +import org.apache.flink.table.store.utils.DateTimeUtils; +import org.apache.flink.table.types.logical.DecimalType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.utils.LogicalTypeCasts; + +import org.apache.commons.lang3.StringUtils; + +import java.math.BigDecimal; +import java.sql.Date; + +import static org.apache.flink.table.types.logical.LogicalTypeRoot.BINARY; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.CHAR; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.DATE; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.DECIMAL; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.VARBINARY; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.VARCHAR; +import static org.apache.flink.util.Preconditions.checkState; + +/** Data value converter for column type evolution. */ +public class DataValueConverter { + private final LogicalType sourceType; + private final LogicalType targetType; + + public DataValueConverter(LogicalType sourceType, LogicalType targetType) { + this.sourceType = sourceType; + this.targetType = targetType; + } + + /** + * Convert the source value with {@link LogicalType} to target {@link LogicalType} according to + * {@Code LogicalTypeCasts#implicitCastingRules}. + * + * @param sourceValue the source value + * @return the target value + */ + public Object convert(Object sourceValue) { + checkState( + LogicalTypeCasts.supportsImplicitCast(sourceType, targetType), + String.format( + "Cant convert value[%s] from type[%s] to [%s] without loosing information", + sourceValue, sourceType, targetType)); + + if (sourceType.equals(targetType) || sourceValue == null) { + return sourceValue; + } + + switch (targetType.getTypeRoot()) { + case VARCHAR: + { + // Convert from CHAR + if (sourceType.getTypeRoot() == VARCHAR) { + return sourceValue; + } else if (sourceType.getTypeRoot() == CHAR) { + return StringData.fromString( + StringUtils.stripEnd(sourceValue.toString(), " ")); + } + + throw new UnsupportedOperationException( + String.format( + "Cannot convert value[%s] from %s to %s type", + sourceType, sourceType, targetType)); + } + case VARBINARY: + { + // Convert from BINARY + if (sourceType.getTypeRoot() == VARBINARY + || sourceType.getTypeRoot() == BINARY) { + return sourceValue; + } + + throw new UnsupportedOperationException( + String.format( + "Cannot convert value[%s] from %s to %s type", + sourceType, sourceType, targetType)); + } + case DECIMAL: + { + DecimalType decimalType = (DecimalType) targetType; + // Convert from TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE + switch (sourceType.getTypeRoot()) { + case DECIMAL: + { + return DecimalData.fromBigDecimal( + getDecimal(sourceValue), + decimalType.getPrecision(), + decimalType.getScale()); + } + case TINYINT: + { + return DecimalData.fromBigDecimal( + new BigDecimal(getByte(sourceValue)), + decimalType.getPrecision(), + decimalType.getScale()); + } + case SMALLINT: + { + return DecimalData.fromBigDecimal( + new BigDecimal(getShort(sourceValue)), + decimalType.getPrecision(), + decimalType.getScale()); + } + case INTEGER: + { + return DecimalData.fromBigDecimal( + new BigDecimal(getInt(sourceValue)), + decimalType.getPrecision(), + decimalType.getScale()); + } + case BIGINT: + { + return DecimalData.fromBigDecimal( + new BigDecimal(getLong(sourceValue)), + decimalType.getPrecision(), + decimalType.getScale()); + } + case FLOAT: + { + return DecimalData.fromBigDecimal( + BigDecimal.valueOf(getFloat(sourceValue)), + decimalType.getPrecision(), + decimalType.getScale()); + } + case DOUBLE: + { + return DecimalData.fromBigDecimal( + BigDecimal.valueOf(getDouble(sourceValue)), + decimalType.getPrecision(), + decimalType.getScale()); + } + } + + throw new UnsupportedOperationException( + String.format( + "Cannot convert value[%s] from %s to %s type", + sourceType, sourceType, targetType)); + } + case SMALLINT: + { + // Convert from TINYINT + switch (sourceType.getTypeRoot()) { + case TINYINT: + return (short) getByte(sourceValue); + case SMALLINT: + return getShort(sourceValue); + } + + throw new UnsupportedOperationException( + String.format( + "Cannot convert value[%s] from %s to %s type", + sourceType, sourceType, targetType)); + } + case INTEGER: + { + // Convert from TINYINT, SMALLINT + switch (sourceType.getTypeRoot()) { + case TINYINT: + return (int) getByte(sourceValue); + case SMALLINT: + return (int) getShort(sourceValue); + case INTEGER: + return getInt(sourceValue); + } + + throw new UnsupportedOperationException( + String.format( + "Cannot convert value[%s] from %s to %s type", + sourceType, sourceType, targetType)); + } + case BIGINT: + { + // Convert from logical types TINYINT, SMALLINT, INTEGER + switch (sourceType.getTypeRoot()) { + case TINYINT: + return (long) getByte(sourceValue); + case SMALLINT: + return (long) getShort(sourceValue); + case INTEGER: + return (long) getInt(sourceValue); + case BIGINT: + return getLong(sourceValue); + } + + throw new UnsupportedOperationException( + String.format( + "Cannot convert value[%s] from %s to %s type", + sourceType, sourceType, targetType)); + } + case FLOAT: + { + // Convert from TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DECIMAL + switch (sourceType.getTypeRoot()) { + case TINYINT: + return (float) getByte(sourceValue); Review Comment: Maybe we can use `((Number) sourceValue).floatValue()`. WDYT? ########## flink-table-store-core/src/main/java/org/apache/flink/table/store/file/schema/DataValueConverter.java: ########## @@ -0,0 +1,327 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.store.file.schema; + +import org.apache.flink.table.data.DecimalData; +import org.apache.flink.table.data.StringData; +import org.apache.flink.table.data.TimestampData; +import org.apache.flink.table.store.utils.DateTimeUtils; +import org.apache.flink.table.types.logical.DecimalType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.utils.LogicalTypeCasts; + +import org.apache.commons.lang3.StringUtils; + +import java.math.BigDecimal; +import java.sql.Date; + +import static org.apache.flink.table.types.logical.LogicalTypeRoot.BINARY; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.CHAR; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.DATE; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.DECIMAL; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.VARBINARY; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.VARCHAR; +import static org.apache.flink.util.Preconditions.checkState; + +/** Data value converter for column type evolution. */ +public class DataValueConverter { + private final LogicalType sourceType; + private final LogicalType targetType; + + public DataValueConverter(LogicalType sourceType, LogicalType targetType) { + this.sourceType = sourceType; + this.targetType = targetType; + } + + /** + * Convert the source value with {@link LogicalType} to target {@link LogicalType} according to + * {@Code LogicalTypeCasts#implicitCastingRules}. + * + * @param sourceValue the source value + * @return the target value + */ + public Object convert(Object sourceValue) { + checkState( Review Comment: Move this check to the constructor? ########## flink-table-store-core/src/main/java/org/apache/flink/table/store/file/schema/SchemaEvolutionUtil.java: ########## @@ -304,4 +305,52 @@ private static int indexOf(DataField dataField, LinkedHashMap<Integer, DataField throw new IllegalArgumentException( String.format("Can't find data field %s", dataField.name())); } + + /** + * Create converter mapping from table fields to underlying data fields. For example, the table + * and data fields are as follows + * + * <ul> + * <li>table fields: 1->c INT, 6->b STRING, 3->a BIGINT + * <li>data fields: 1->a BIGINT, 3->c DOUBLE + * </ul> + * + * <p>We can get the column types (1->a BIGINT), (3->c DOUBLE) from data fields for (1->c INT) + * and (3->a BIGINT) in table fields through index mapping [0, -1, 1], then compare the data + * type and create converter mapping. + * + * <p>/// TODO should support nest index mapping when nest schema evolution is supported. + * + * @param tableFields the fields of table + * @param dataFields the fields of underlying data + * @param indexMapping the index mapping from table fields to data fields + * @return the index mapping + */ + public static DataValueConverter[] createConvertMapping( Review Comment: Maybe we can make converters consistent with indexMapping? - if (dataField.type().equals(tableField.type())), we can return an identity converter. - If all converters are identity, we can make this returning null array. In this way, we can remove null check for the element of this array. ########## flink-table-store-core/src/main/java/org/apache/flink/table/store/file/schema/DataValueConverter.java: ########## @@ -0,0 +1,327 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.store.file.schema; + +import org.apache.flink.table.data.DecimalData; +import org.apache.flink.table.data.StringData; +import org.apache.flink.table.data.TimestampData; +import org.apache.flink.table.store.utils.DateTimeUtils; +import org.apache.flink.table.types.logical.DecimalType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.utils.LogicalTypeCasts; + +import org.apache.commons.lang3.StringUtils; + +import java.math.BigDecimal; +import java.sql.Date; + +import static org.apache.flink.table.types.logical.LogicalTypeRoot.BINARY; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.CHAR; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.DATE; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.DECIMAL; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.VARBINARY; +import static org.apache.flink.table.types.logical.LogicalTypeRoot.VARCHAR; +import static org.apache.flink.util.Preconditions.checkState; + +/** Data value converter for column type evolution. */ +public class DataValueConverter { Review Comment: Can we introduce a `CastExecutor` maybe? We can introduce a static method `CastExecutor create(LogicalType sourceType, LogicalType targetType)`. Just like in Flink `CodeGeneratorCastRule`, the difference is that we don't use codegen here. Maybe we can copy code to here from Flink codegen. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
