danny0405 commented on code in PR #5830: URL: https://github.com/apache/hudi/pull/5830#discussion_r1033192441
########## hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/table/format/TestCastMap.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.hudi.table.format; + +import org.apache.flink.table.data.DecimalData; +import org.apache.flink.table.data.binary.BinaryStringData; +import org.apache.flink.table.types.logical.BigIntType; +import org.apache.flink.table.types.logical.DateType; +import org.apache.flink.table.types.logical.DecimalType; +import org.apache.flink.table.types.logical.DoubleType; +import org.apache.flink.table.types.logical.FloatType; +import org.apache.flink.table.types.logical.IntType; +import org.apache.flink.table.types.logical.VarCharType; + +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.time.LocalDate; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests for {@link CastMap}. + */ +public class TestCastMap { + + @Test + public void testCastInt() { + CastMap castMap = new CastMap(); + castMap.add(0, new IntType(), new BigIntType()); + castMap.add(1, new IntType(), new FloatType()); + castMap.add(2, new IntType(), new DoubleType()); + castMap.add(3, new IntType(), new DecimalType()); + castMap.add(4, new IntType(), new VarCharType()); + int val = 1; + assertEquals(1L, castMap.castIfNeeded(0, val)); + assertEquals(1.0F, castMap.castIfNeeded(1, val)); + assertEquals(1.0, castMap.castIfNeeded(2, val)); + assertEquals(DecimalData.fromBigDecimal(BigDecimal.ONE, 1, 0), castMap.castIfNeeded(3, val)); + assertEquals(BinaryStringData.fromString("1"), castMap.castIfNeeded(4, val)); + } + + @Test + public void testCastLong() { + CastMap castMap = new CastMap(); + castMap.add(0, new BigIntType(), new FloatType()); + castMap.add(1, new BigIntType(), new DoubleType()); + castMap.add(2, new BigIntType(), new DecimalType()); + castMap.add(3, new BigIntType(), new VarCharType()); + long val = 1L; + assertEquals(1.0F, castMap.castIfNeeded(0, val)); + assertEquals(1.0, castMap.castIfNeeded(1, val)); + assertEquals(DecimalData.fromBigDecimal(BigDecimal.ONE, 1, 0), castMap.castIfNeeded(2, val)); + assertEquals(BinaryStringData.fromString("1"), castMap.castIfNeeded(3, val)); + } + + @Test + public void testCastFloat() { + CastMap castMap = new CastMap(); + castMap.add(0, new FloatType(), new DoubleType()); + castMap.add(1, new FloatType(), new DecimalType()); + castMap.add(2, new FloatType(), new VarCharType()); + float val = 1F; + assertEquals(1.0, castMap.castIfNeeded(0, val)); + assertEquals(DecimalData.fromBigDecimal(BigDecimal.ONE, 1, 0), castMap.castIfNeeded(1, val)); + assertEquals(BinaryStringData.fromString("1.0"), castMap.castIfNeeded(2, val)); + } + + @Test + public void testCastDouble() { + CastMap castMap = new CastMap(); + castMap.add(0, new DoubleType(), new DecimalType()); + castMap.add(1, new DoubleType(), new VarCharType()); + double val = 1; + assertEquals(DecimalData.fromBigDecimal(BigDecimal.ONE, 1, 0), castMap.castIfNeeded(0, val)); + assertEquals(BinaryStringData.fromString("1.0"), castMap.castIfNeeded(1, val)); + } + + @Test + public void testCastDecimal() { + CastMap castMap = new CastMap(); + castMap.add(0, new DecimalType(2, 1), new DecimalType(3, 2)); + castMap.add(1, new DecimalType(), new VarCharType()); + DecimalData val = DecimalData.fromBigDecimal(BigDecimal.ONE, 2, 1); + assertEquals(DecimalData.fromBigDecimal(BigDecimal.ONE, 3, 2), castMap.castIfNeeded(0, val)); + assertEquals(BinaryStringData.fromString("1.0"), castMap.castIfNeeded(1, val)); Review Comment: Thanks, i see we return null when CastMap cast a type that is not in its precedence list, is that reasonable ? -- 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]
