Copilot commented on code in PR #7400:
URL: https://github.com/apache/ignite-3/pull/7400#discussion_r2689656224
##########
modules/api/src/testFixtures/java/org/apache/ignite/table/AbstractImmutableTupleTest.java:
##########
@@ -334,6 +340,375 @@ public void nullPointerWhenReadingNullByNameAsPrimitive(
assertEquals(String.format(NULL_TO_PRIMITIVE_NAMED_ERROR_MESSAGE,
"VAL"), err.getMessage());
}
+ @Test
+ void testReadAsByte() {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT8, "INT8",
Byte.MAX_VALUE);
+
+ assertThat(tuple.byteValue("INT8"), is(Byte.MAX_VALUE));
+ assertThat(tuple.shortValue("INT8"), is((short) Byte.MAX_VALUE));
+ assertThat(tuple.intValue("INT8"), is((int) Byte.MAX_VALUE));
+ assertThat(tuple.longValue("INT8"), is((long) Byte.MAX_VALUE));
+
+ assertThat(tuple.byteValue(0), is(Byte.MAX_VALUE));
+ assertThat(tuple.shortValue(0), is((short) Byte.MAX_VALUE));
+ assertThat(tuple.intValue(0), is((int) Byte.MAX_VALUE));
+ assertThat(tuple.longValue(0), is((long) Byte.MAX_VALUE));
+ }
+
+ @Test
+ void testReadAsShort() {
+ // The field value is within the byte range
+ {
+ String columnName = "INT16";
+ short value = Byte.MAX_VALUE;
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT16,
columnName, value);
+
+ assertThat(tuple.byteValue(columnName), is((byte) value));
+ assertThat(tuple.shortValue(columnName), is(value));
+ assertThat(tuple.intValue(columnName), is((int) value));
+ assertThat(tuple.longValue(columnName), is((long) value));
+
+ assertThat(tuple.byteValue(0), is((byte) value));
+ assertThat(tuple.shortValue(0), is(value));
+ assertThat(tuple.intValue(0), is((int) value));
+ assertThat(tuple.longValue(0), is((long) value));
+ }
+
+ // The field value is out of the byte range.
+ {
+ String columnName = "INT16";
+ short value = Byte.MAX_VALUE + 1;
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT16,
columnName, value);
+
+ ArithmeticException ex0 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(columnName));
+ assertThat(ex0.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(columnName), is(value));
+ assertThat(tuple.intValue(columnName), is((int) value));
+ assertThat(tuple.longValue(columnName), is((long) value));
+
+ ArithmeticException ex1 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(0));
+ assertThat(ex1.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(0), is(value));
+ assertThat(tuple.intValue(0), is((int) value));
+ assertThat(tuple.longValue(0), is((long) value));
+ }
+ }
+
+ @Test
+ void testReadAsInt() {
+ {
+ int value = Byte.MAX_VALUE;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT32,
columnName, value);
+
+ assertThat(tuple.byteValue(columnName), is((byte) value));
+ assertThat(tuple.shortValue(columnName), is((short) value));
+ assertThat(tuple.intValue(columnName), is(value));
+ assertThat(tuple.longValue(columnName), is((long) value));
+
+ assertThat(tuple.byteValue(0), is((byte) value));
+ assertThat(tuple.shortValue(0), is((short) value));
+ assertThat(tuple.intValue(0), is(value));
+ assertThat(tuple.longValue(0), is((long) value));
+ }
+
+ {
+ int value = Byte.MAX_VALUE + 1;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT32,
columnName, value);
+
+ ArithmeticException ex0 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(columnName));
+ assertThat(ex0.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(columnName), is((short) value));
+ assertThat(tuple.intValue(columnName), is(value));
+ assertThat(tuple.longValue(columnName), is((long) value));
+
+ ArithmeticException ex1 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(0));
+ assertThat(ex1.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(0), is((short) value));
+ assertThat(tuple.intValue(0), is(value));
+ assertThat(tuple.longValue(0), is((long) value));
+ }
+
+ {
+ int value = Short.MAX_VALUE + 1;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT32,
columnName, value);
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ assertThat(tuple.intValue(columnName), is(value));
+ assertThat(tuple.longValue(columnName), is((long) value));
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(0));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(0));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ assertThat(tuple.intValue(0), is(value));
+ assertThat(tuple.longValue(0), is((long) value));
+ }
+ }
+
+ @Test
+ void testReadAsLong() {
+ {
+ long value = Byte.MAX_VALUE;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT64,
columnName, value);
+
+ assertThat(tuple.byteValue(columnName), is((byte) value));
+ assertThat(tuple.shortValue(columnName), is((short) value));
+ assertThat(tuple.intValue(columnName), is((int) value));
+ assertThat(tuple.longValue(columnName), is(value));
+
+ assertThat(tuple.byteValue(0), is((byte) value));
+ assertThat(tuple.shortValue(0), is((short) value));
+ assertThat(tuple.intValue(0), is((int) value));
+ assertThat(tuple.longValue(0), is(value));
+ }
+
+ {
+ long value = Byte.MAX_VALUE + 1;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT64,
columnName, value);
+
+ ArithmeticException ex0 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(columnName));
+ assertThat(ex0.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(columnName), is((short) value));
+ assertThat(tuple.intValue(columnName), is((int) value));
+ assertThat(tuple.longValue(columnName), is(value));
+
+ ArithmeticException ex1 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(0));
+ assertThat(ex1.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(0), is((short) value));
+ assertThat(tuple.intValue(0), is((int) value));
+ assertThat(tuple.longValue(0), is(value));
+ }
+
+ {
+ long value = Short.MAX_VALUE + 1;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT64,
columnName, value);
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ assertThat(tuple.intValue(columnName), is((int) value));
+ assertThat(tuple.longValue(columnName), is(value));
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(0));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(0));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ assertThat(tuple.intValue(0), is((int) value));
+ assertThat(tuple.longValue(0), is(value));
+ }
+
+ {
+ long value = Integer.MAX_VALUE + 1L;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT64,
columnName, value);
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.intValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Int value overflow"));
+ }
+
+ assertThat(tuple.longValue(columnName), is(value));
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(0));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(0));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.intValue(0));
+ assertThat(ex.getMessage(), equalTo("Int value overflow"));
+ }
+
+ assertThat(tuple.longValue(0), is(value));
+ }
+ }
+
+ @Test
+ void testReadAsFloat() {
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT, "FLOAT",
Float.MAX_VALUE);
+
+ assertThat(tuple.floatValue("FLOAT"), is(Float.MAX_VALUE));
+ assertThat(tuple.doubleValue("FLOAT"), is((double)
Float.MAX_VALUE));
+
+ assertThat(tuple.floatValue(0), is(Float.MAX_VALUE));
+ assertThat(tuple.doubleValue(0), is((double) Float.MAX_VALUE));
+ }
+
+ // NaN
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT, "FLOAT",
Float.NaN);
+
+ assertThat(Float.isNaN(tuple.floatValue("FLOAT")), is(true));
+ assertThat(Double.isNaN(tuple.doubleValue("FLOAT")), is(true));
+
+ assertThat(Float.isNaN(tuple.floatValue(0)), is(true));
+ assertThat(Double.isNaN(tuple.doubleValue(0)), is(true));
+ }
+
+ // Positive infinity
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT, "FLOAT",
Float.POSITIVE_INFINITY);
+
+ assertThat(tuple.floatValue("FLOAT"), is(Float.POSITIVE_INFINITY));
+ assertThat(tuple.doubleValue("FLOAT"),
is(Double.POSITIVE_INFINITY));
+
+ assertThat(tuple.floatValue(0), is(Float.POSITIVE_INFINITY));
+ assertThat(tuple.doubleValue(0), is(Double.POSITIVE_INFINITY));
+ }
+
+ // Negative infinity
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT, "FLOAT",
Float.NEGATIVE_INFINITY);
+
+ assertThat(tuple.floatValue("FLOAT"), is(Float.NEGATIVE_INFINITY));
+ assertThat(tuple.doubleValue("FLOAT"),
is(Double.NEGATIVE_INFINITY));
+
+ assertThat(tuple.floatValue(0), is(Float.NEGATIVE_INFINITY));
+ assertThat(tuple.doubleValue(0), is(Double.NEGATIVE_INFINITY));
+ }
+ }
+
+ @Test
+ void testReadAsDouble() {
+ String columnName = "DOUBLE";
+
+ // The field value can be represented as float.
+ {
+ double value = Float.MAX_VALUE;
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.DOUBLE,
columnName, value);
+
+ assertThat(tuple.floatValue(columnName), is((float) value));
+ assertThat(tuple.floatValue(0), is((float) value));
+
+ assertThat(tuple.doubleValue(columnName), is(value));
+ assertThat(tuple.doubleValue(0), is(value));
+ }
+
+ // The field value cannot be represented as float.
+ {
+ double value = Double.MAX_VALUE;
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.DOUBLE,
columnName, value);
+
+ ArithmeticException ex0 = assertThrows(ArithmeticException.class,
() -> tuple.floatValue(columnName));
+ assertThat(ex0.getMessage(), equalTo("Float value overflow"));
+
+ ArithmeticException ex1 = assertThrows(ArithmeticException.class,
() -> tuple.floatValue(0));
+ assertThat(ex1.getMessage(), equalTo("Float value overflow"));
+
+ assertThat(tuple.doubleValue(columnName), is(value));
+ assertThat(tuple.doubleValue(0), is(value));
+ }
+
+ // NaN
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.DOUBLE,
columnName, Double.NaN);
+
+ assertThat(Float.isNaN(tuple.floatValue(columnName)), is(true));
+ assertThat(Double.isNaN(tuple.doubleValue(columnName)), is(true));
+
+ assertThat(Float.isNaN(tuple.floatValue(0)), is(true));
+ assertThat(Double.isNaN(tuple.doubleValue(0)), is(true));
+ }
+
+ // Positive infinity
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT,
columnName, Double.POSITIVE_INFINITY);
Review Comment:
The column type should be ColumnType.DOUBLE, not ColumnType.FLOAT, since
this test case is part of testReadAsDouble and the columnName is "DOUBLE".
Using FLOAT here is inconsistent with the test's intent.
##########
modules/core/src/main/java/org/apache/ignite/internal/util/TupleTypeCastUtils.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.ignite.internal.util;
+
+import org.apache.ignite.internal.lang.IgniteStringFormatter;
+import org.apache.ignite.internal.lang.InternalTuple;
+import org.apache.ignite.sql.ColumnType;
+
+/**
+ * Helper methods for reading values from {@link InternalTuple} with allowed
numeric type conversions.
+ *
+ * <p>The following conversions are supported:
+ * <ul>
+ * <li>Any integer type to any other integer type with range checks (e.g.
{@link ColumnType#INT64} to {@link ColumnType#INT8}
+ * may throw {@link ArithmeticException} if the value doesn't fit into
{@link ColumnType#INT8}).</li>
+ * <li>{@link ColumnType#FLOAT} to {@link ColumnType#DOUBLE} are always
allowed.</li>
+ * <li>{@link ColumnType#DOUBLE} to {@link ColumnType#FLOAT} with
precision checks (may throw {@link ArithmeticException}
+ * if the value cannot be represented as FLOAT without precision
loss).</li>
+ * </ul>
+ */
+public class TupleTypeCastUtils {
+ private static final String TYPE_CAST_ERROR_COLUMN_NAME = "Column with
name '{}' has type {} but {} was requested";
+
+ private static final String TYPE_CAST_ERROR_COLUMN_INDEX = "Column with
index {} has type {} but {} was requested";
+
+ private static final int INT_COLUMN_TYPES_BITMASK =
buildIntegerTypesBitMask();
+
+ /** Reads a value from the tuple and converts it to a byte if possible. */
+ public static byte readByteValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT8, actualType, columnIndex);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex);
+
+ return castToByte(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and converts it to a byte if possible. */
+ public static byte readByteValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT8, actualType, columnName);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName);
+
+ return castToByte(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and converts it to a short if possible. */
+ public static short readShortValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT16, actualType, columnIndex);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex);
+
+ return castToShort(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and converts it to a short if possible. */
+ public static short readShortValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT16, actualType, columnName);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName);
+
+ return castToShort(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and converts it to an int if possible. */
+ public static int readIntValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT32, actualType, columnIndex);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex);
+
+ return castToInt(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and converts it to an int if possible. */
+ public static int readIntValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT32, actualType, columnName);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName);
+
+ return castToInt(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and returns it as a long. Only integer
column types are allowed. */
+ public static long readLongValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT64, actualType, columnIndex);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex);
+
+ return binaryTuple.longValue(binaryTupleIndex);
+ }
+
+ /** Reads a value from the tuple and returns it as a long. Only integer
column types are allowed. */
+ public static long readLongValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT64, actualType, columnName);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName);
+
+ return binaryTuple.longValue(binaryTupleIndex);
+ }
+
+ /** Reads a value from the tuple and converts it to a float if possible. */
+ public static float readFloatValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (actualType == ColumnType.FLOAT || actualType == ColumnType.DOUBLE)
{
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex,
columnIndex);
+
+ return castToFloat(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ throw newClassCastException(ColumnType.FLOAT, actualType, columnIndex);
+ }
+
+ /** Reads a value from the tuple and converts it to a float if possible. */
+ public static float readFloatValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (actualType == ColumnType.FLOAT || actualType == ColumnType.DOUBLE)
{
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex,
columnName);
+
+ return castToFloat(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ throw newClassCastException(ColumnType.FLOAT, actualType, columnName);
+ }
+
+ /** Reads a value from the tuple and returns it as a double. */
+ public static double readDoubleValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (actualType == ColumnType.DOUBLE || actualType == ColumnType.FLOAT)
{
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex,
columnIndex);
+
+ return binaryTuple.doubleValue(binaryTupleIndex);
+ }
+
+ throw newClassCastException(ColumnType.FLOAT, actualType, columnIndex);
Review Comment:
The exception message incorrectly references ColumnType.FLOAT when it should
reference ColumnType.DOUBLE, since this is the readDoubleValue method. This
will produce misleading error messages when type conversion fails.
##########
modules/core/src/main/java/org/apache/ignite/internal/util/TupleTypeCastUtils.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.ignite.internal.util;
+
+import org.apache.ignite.internal.lang.IgniteStringFormatter;
+import org.apache.ignite.internal.lang.InternalTuple;
+import org.apache.ignite.sql.ColumnType;
+
+/**
+ * Helper methods for reading values from {@link InternalTuple} with allowed
numeric type conversions.
+ *
+ * <p>The following conversions are supported:
+ * <ul>
+ * <li>Any integer type to any other integer type with range checks (e.g.
{@link ColumnType#INT64} to {@link ColumnType#INT8}
+ * may throw {@link ArithmeticException} if the value doesn't fit into
{@link ColumnType#INT8}).</li>
+ * <li>{@link ColumnType#FLOAT} to {@link ColumnType#DOUBLE} are always
allowed.</li>
+ * <li>{@link ColumnType#DOUBLE} to {@link ColumnType#FLOAT} with
precision checks (may throw {@link ArithmeticException}
+ * if the value cannot be represented as FLOAT without precision
loss).</li>
+ * </ul>
+ */
+public class TupleTypeCastUtils {
+ private static final String TYPE_CAST_ERROR_COLUMN_NAME = "Column with
name '{}' has type {} but {} was requested";
+
+ private static final String TYPE_CAST_ERROR_COLUMN_INDEX = "Column with
index {} has type {} but {} was requested";
+
+ private static final int INT_COLUMN_TYPES_BITMASK =
buildIntegerTypesBitMask();
+
+ /** Reads a value from the tuple and converts it to a byte if possible. */
+ public static byte readByteValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT8, actualType, columnIndex);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex);
+
+ return castToByte(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and converts it to a byte if possible. */
+ public static byte readByteValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT8, actualType, columnName);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName);
+
+ return castToByte(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and converts it to a short if possible. */
+ public static short readShortValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT16, actualType, columnIndex);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex);
+
+ return castToShort(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and converts it to a short if possible. */
+ public static short readShortValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT16, actualType, columnName);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName);
+
+ return castToShort(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and converts it to an int if possible. */
+ public static int readIntValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT32, actualType, columnIndex);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex);
+
+ return castToInt(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and converts it to an int if possible. */
+ public static int readIntValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT32, actualType, columnName);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName);
+
+ return castToInt(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ /** Reads a value from the tuple and returns it as a long. Only integer
column types are allowed. */
+ public static long readLongValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT64, actualType, columnIndex);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex);
+
+ return binaryTuple.longValue(binaryTupleIndex);
+ }
+
+ /** Reads a value from the tuple and returns it as a long. Only integer
column types are allowed. */
+ public static long readLongValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (!integerType(actualType)) {
+ throwClassCastException(ColumnType.INT64, actualType, columnName);
+ }
+
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName);
+
+ return binaryTuple.longValue(binaryTupleIndex);
+ }
+
+ /** Reads a value from the tuple and converts it to a float if possible. */
+ public static float readFloatValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (actualType == ColumnType.FLOAT || actualType == ColumnType.DOUBLE)
{
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex,
columnIndex);
+
+ return castToFloat(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ throw newClassCastException(ColumnType.FLOAT, actualType, columnIndex);
+ }
+
+ /** Reads a value from the tuple and converts it to a float if possible. */
+ public static float readFloatValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (actualType == ColumnType.FLOAT || actualType == ColumnType.DOUBLE)
{
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex,
columnName);
+
+ return castToFloat(binaryTuple, binaryTupleIndex, actualType);
+ }
+
+ throw newClassCastException(ColumnType.FLOAT, actualType, columnName);
+ }
+
+ /** Reads a value from the tuple and returns it as a double. */
+ public static double readDoubleValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, int columnIndex) {
+ if (actualType == ColumnType.DOUBLE || actualType == ColumnType.FLOAT)
{
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex,
columnIndex);
+
+ return binaryTuple.doubleValue(binaryTupleIndex);
+ }
+
+ throw newClassCastException(ColumnType.FLOAT, actualType, columnIndex);
+ }
+
+ /** Reads a value from the tuple and returns it as a double. */
+ public static double readDoubleValue(InternalTuple binaryTuple, int
binaryTupleIndex, ColumnType actualType, String columnName) {
+ if (actualType == ColumnType.DOUBLE || actualType == ColumnType.FLOAT)
{
+ IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex,
columnName);
+
+ return binaryTuple.doubleValue(binaryTupleIndex);
+ }
+
+ throw newClassCastException(ColumnType.FLOAT, actualType, columnName);
Review Comment:
The exception message incorrectly references ColumnType.FLOAT when it should
reference ColumnType.DOUBLE, since this is the readDoubleValue method. This
will produce misleading error messages when type conversion fails.
##########
modules/api/src/main/java/org/apache/ignite/table/TupleImpl.java:
##########
@@ -459,4 +483,89 @@ private <T> T valueNotNull(String columnName) {
return value;
}
+
+ private static byte castToByte(Number number) {
+ Class<? extends Number> cls = number.getClass();
+
+ if (cls == Long.class || cls == Integer.class || cls == Short.class) {
+ long longVal = number.longValue();
+ byte byteVal = number.byteValue();
+
+ if (longVal == byteVal) {
+ return byteVal;
+ }
+
+ throw new ArithmeticException("Byte value overflow");
+ }
+
+ return (byte) number;
+ }
+
+ private static short castToShort(Number number) {
+ Class<? extends Number> cls = number.getClass();
+
+ if (cls == Long.class || cls == Integer.class || cls == Byte.class) {
+ long longVal = number.longValue();
+ short shortVal = number.shortValue();
+
+ if (longVal == shortVal) {
+ return shortVal;
+ }
+
+ throw new ArithmeticException("Short value overflow");
+ }
+
+ return (short) number;
+ }
+
+ private static int castToInt(Number number) {
+ Class<? extends Number> cls = number.getClass();
+
+ if (cls == Long.class || cls == Short.class || cls == Byte.class) {
+ long longVal = number.longValue();
+ int intVal = number.intValue();
+
+ if (longVal == intVal) {
+ return intVal;
+ }
+
+ throw new ArithmeticException("Int value overflow");
+ }
+
+ return (int) number;
+ }
+
+ private static long castToLong(Number number) {
+ Class<? extends Number> cls = number.getClass();
+
+ if (cls == Integer.class || cls == Short.class || cls == Byte.class) {
+ return number.longValue();
+ }
+
+ return (long) number;
+ }
+
+ private static float castToFloat(Number number) {
+ if (number.getClass() == Double.class) {
+ double doubleVal = number.doubleValue();
+ float floatVal = number.floatValue();
+
+ //noinspection FloatingPointEquality
+ if (doubleVal == floatVal || Double.isNaN(doubleVal)) {
+ return floatVal;
+ }
+
+ throw new ArithmeticException("Float value overflow");
+ }
+
+ return (float) number;
+ }
+
+ private static double castToDouble(Number number) {
+ if (number.getClass() == Float.class) {
+ return number.doubleValue();
+ }
+
+ return (double) number;
+ }
Review Comment:
The casting methods (castToByte, castToShort, castToInt, castToLong,
castToFloat, castToDouble) are duplicated across AsyncResultSetImpl.SqlRowImpl
and TupleImpl with identical implementations. Consider extracting these methods
into a shared utility class to avoid code duplication and ensure consistent
behavior across implementations.
##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -436,24 +464,109 @@ public String toString() {
return S.tupleToString(this);
}
- private Object getValueNotNull(int columnIndex) {
+ private <T> T getValueNotNull(int columnIndex) {
Object value = row.get(columnIndex);
if (value == null) {
throw new
NullPointerException(format(IgniteUtils.NULL_TO_PRIMITIVE_ERROR_MESSAGE,
columnIndex));
}
- return value;
+ return (T) value;
}
- private Object getValueNotNull(String columnName) {
+ private <T> T getValueNotNull(String columnName) {
Object value = row.get(columnIndexChecked(columnName));
if (value == null) {
throw new
NullPointerException(format(IgniteUtils.NULL_TO_PRIMITIVE_NAMED_ERROR_MESSAGE,
columnName));
}
- return value;
+ return (T) value;
+ }
+
+ private static byte castToByte(Number number) {
+ Class<? extends Number> cls = number.getClass();
+
+ if (cls == Long.class || cls == Integer.class || cls ==
Short.class) {
+ long longVal = number.longValue();
+ byte byteVal = number.byteValue();
+
+ if (longVal == byteVal) {
+ return byteVal;
+ }
+
+ throw new ArithmeticException("Byte value overflow");
+ }
+
+ return (byte) number;
+ }
+
+ private static short castToShort(Number number) {
+ Class<? extends Number> cls = number.getClass();
+
+ if (cls == Long.class || cls == Integer.class || cls ==
Byte.class) {
+ long longVal = number.longValue();
+ short shortVal = number.shortValue();
+
+ if (longVal == shortVal) {
+ return shortVal;
+ }
+
+ throw new ArithmeticException("Short value overflow");
+ }
+
+ return (short) number;
+ }
+
+ private static int castToInt(Number number) {
+ Class<? extends Number> cls = number.getClass();
+
+ if (cls == Long.class || cls == Short.class || cls == Byte.class) {
+ long longVal = number.longValue();
+ int intVal = number.intValue();
+
+ if (longVal == intVal) {
+ return intVal;
+ }
+
+ throw new ArithmeticException("Int value overflow");
+ }
+
+ return (int) number;
+ }
+
+ private static long castToLong(Number number) {
+ Class<? extends Number> cls = number.getClass();
+
+ if (cls == Integer.class || cls == Short.class || cls ==
Byte.class) {
+ return number.longValue();
+ }
+
+ return (long) number;
+ }
+
+ private static float castToFloat(Number number) {
+ if (number.getClass() == Double.class) {
+ double doubleVal = number.doubleValue();
+ float floatVal = number.floatValue();
+
+ //noinspection FloatingPointEquality
+ if (doubleVal == floatVal || Double.isNaN(doubleVal)) {
+ return floatVal;
+ }
+
+ throw new ArithmeticException("Float value overflow");
+ }
+
+ return (float) number;
+ }
+
+ private static double castToDouble(Number number) {
+ if (number.getClass() == Float.class) {
+ return number.doubleValue();
+ }
+
+ return (double) number;
}
Review Comment:
The casting methods (castToByte, castToShort, castToInt, castToLong,
castToFloat, castToDouble) are duplicated across AsyncResultSetImpl.SqlRowImpl
and TupleImpl with identical implementations. Consider extracting these methods
into a shared utility class to avoid code duplication and ensure consistent
behavior across implementations.
##########
modules/api/src/testFixtures/java/org/apache/ignite/table/AbstractImmutableTupleTest.java:
##########
@@ -334,6 +340,375 @@ public void nullPointerWhenReadingNullByNameAsPrimitive(
assertEquals(String.format(NULL_TO_PRIMITIVE_NAMED_ERROR_MESSAGE,
"VAL"), err.getMessage());
}
+ @Test
+ void testReadAsByte() {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT8, "INT8",
Byte.MAX_VALUE);
+
+ assertThat(tuple.byteValue("INT8"), is(Byte.MAX_VALUE));
+ assertThat(tuple.shortValue("INT8"), is((short) Byte.MAX_VALUE));
+ assertThat(tuple.intValue("INT8"), is((int) Byte.MAX_VALUE));
+ assertThat(tuple.longValue("INT8"), is((long) Byte.MAX_VALUE));
+
+ assertThat(tuple.byteValue(0), is(Byte.MAX_VALUE));
+ assertThat(tuple.shortValue(0), is((short) Byte.MAX_VALUE));
+ assertThat(tuple.intValue(0), is((int) Byte.MAX_VALUE));
+ assertThat(tuple.longValue(0), is((long) Byte.MAX_VALUE));
+ }
+
+ @Test
+ void testReadAsShort() {
+ // The field value is within the byte range
+ {
+ String columnName = "INT16";
+ short value = Byte.MAX_VALUE;
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT16,
columnName, value);
+
+ assertThat(tuple.byteValue(columnName), is((byte) value));
+ assertThat(tuple.shortValue(columnName), is(value));
+ assertThat(tuple.intValue(columnName), is((int) value));
+ assertThat(tuple.longValue(columnName), is((long) value));
+
+ assertThat(tuple.byteValue(0), is((byte) value));
+ assertThat(tuple.shortValue(0), is(value));
+ assertThat(tuple.intValue(0), is((int) value));
+ assertThat(tuple.longValue(0), is((long) value));
+ }
+
+ // The field value is out of the byte range.
+ {
+ String columnName = "INT16";
+ short value = Byte.MAX_VALUE + 1;
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT16,
columnName, value);
+
+ ArithmeticException ex0 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(columnName));
+ assertThat(ex0.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(columnName), is(value));
+ assertThat(tuple.intValue(columnName), is((int) value));
+ assertThat(tuple.longValue(columnName), is((long) value));
+
+ ArithmeticException ex1 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(0));
+ assertThat(ex1.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(0), is(value));
+ assertThat(tuple.intValue(0), is((int) value));
+ assertThat(tuple.longValue(0), is((long) value));
+ }
+ }
+
+ @Test
+ void testReadAsInt() {
+ {
+ int value = Byte.MAX_VALUE;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT32,
columnName, value);
+
+ assertThat(tuple.byteValue(columnName), is((byte) value));
+ assertThat(tuple.shortValue(columnName), is((short) value));
+ assertThat(tuple.intValue(columnName), is(value));
+ assertThat(tuple.longValue(columnName), is((long) value));
+
+ assertThat(tuple.byteValue(0), is((byte) value));
+ assertThat(tuple.shortValue(0), is((short) value));
+ assertThat(tuple.intValue(0), is(value));
+ assertThat(tuple.longValue(0), is((long) value));
+ }
+
+ {
+ int value = Byte.MAX_VALUE + 1;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT32,
columnName, value);
+
+ ArithmeticException ex0 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(columnName));
+ assertThat(ex0.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(columnName), is((short) value));
+ assertThat(tuple.intValue(columnName), is(value));
+ assertThat(tuple.longValue(columnName), is((long) value));
+
+ ArithmeticException ex1 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(0));
+ assertThat(ex1.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(0), is((short) value));
+ assertThat(tuple.intValue(0), is(value));
+ assertThat(tuple.longValue(0), is((long) value));
+ }
+
+ {
+ int value = Short.MAX_VALUE + 1;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT32,
columnName, value);
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ assertThat(tuple.intValue(columnName), is(value));
+ assertThat(tuple.longValue(columnName), is((long) value));
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(0));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(0));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ assertThat(tuple.intValue(0), is(value));
+ assertThat(tuple.longValue(0), is((long) value));
+ }
+ }
+
+ @Test
+ void testReadAsLong() {
+ {
+ long value = Byte.MAX_VALUE;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT64,
columnName, value);
+
+ assertThat(tuple.byteValue(columnName), is((byte) value));
+ assertThat(tuple.shortValue(columnName), is((short) value));
+ assertThat(tuple.intValue(columnName), is((int) value));
+ assertThat(tuple.longValue(columnName), is(value));
+
+ assertThat(tuple.byteValue(0), is((byte) value));
+ assertThat(tuple.shortValue(0), is((short) value));
+ assertThat(tuple.intValue(0), is((int) value));
+ assertThat(tuple.longValue(0), is(value));
+ }
+
+ {
+ long value = Byte.MAX_VALUE + 1;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT64,
columnName, value);
+
+ ArithmeticException ex0 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(columnName));
+ assertThat(ex0.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(columnName), is((short) value));
+ assertThat(tuple.intValue(columnName), is((int) value));
+ assertThat(tuple.longValue(columnName), is(value));
+
+ ArithmeticException ex1 = assertThrows(ArithmeticException.class,
() -> tuple.byteValue(0));
+ assertThat(ex1.getMessage(), equalTo("Byte value overflow"));
+
+ assertThat(tuple.shortValue(0), is((short) value));
+ assertThat(tuple.intValue(0), is((int) value));
+ assertThat(tuple.longValue(0), is(value));
+ }
+
+ {
+ long value = Short.MAX_VALUE + 1;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT64,
columnName, value);
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ assertThat(tuple.intValue(columnName), is((int) value));
+ assertThat(tuple.longValue(columnName), is(value));
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(0));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(0));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ assertThat(tuple.intValue(0), is((int) value));
+ assertThat(tuple.longValue(0), is(value));
+ }
+
+ {
+ long value = Integer.MAX_VALUE + 1L;
+ String columnName = "VALUE";
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.INT64,
columnName, value);
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.intValue(columnName));
+ assertThat(ex.getMessage(), equalTo("Int value overflow"));
+ }
+
+ assertThat(tuple.longValue(columnName), is(value));
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.byteValue(0));
+ assertThat(ex.getMessage(), equalTo("Byte value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.shortValue(0));
+ assertThat(ex.getMessage(), equalTo("Short value overflow"));
+ }
+
+ {
+ ArithmeticException ex =
assertThrows(ArithmeticException.class, () -> tuple.intValue(0));
+ assertThat(ex.getMessage(), equalTo("Int value overflow"));
+ }
+
+ assertThat(tuple.longValue(0), is(value));
+ }
+ }
+
+ @Test
+ void testReadAsFloat() {
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT, "FLOAT",
Float.MAX_VALUE);
+
+ assertThat(tuple.floatValue("FLOAT"), is(Float.MAX_VALUE));
+ assertThat(tuple.doubleValue("FLOAT"), is((double)
Float.MAX_VALUE));
+
+ assertThat(tuple.floatValue(0), is(Float.MAX_VALUE));
+ assertThat(tuple.doubleValue(0), is((double) Float.MAX_VALUE));
+ }
+
+ // NaN
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT, "FLOAT",
Float.NaN);
+
+ assertThat(Float.isNaN(tuple.floatValue("FLOAT")), is(true));
+ assertThat(Double.isNaN(tuple.doubleValue("FLOAT")), is(true));
+
+ assertThat(Float.isNaN(tuple.floatValue(0)), is(true));
+ assertThat(Double.isNaN(tuple.doubleValue(0)), is(true));
+ }
+
+ // Positive infinity
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT, "FLOAT",
Float.POSITIVE_INFINITY);
+
+ assertThat(tuple.floatValue("FLOAT"), is(Float.POSITIVE_INFINITY));
+ assertThat(tuple.doubleValue("FLOAT"),
is(Double.POSITIVE_INFINITY));
+
+ assertThat(tuple.floatValue(0), is(Float.POSITIVE_INFINITY));
+ assertThat(tuple.doubleValue(0), is(Double.POSITIVE_INFINITY));
+ }
+
+ // Negative infinity
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT, "FLOAT",
Float.NEGATIVE_INFINITY);
+
+ assertThat(tuple.floatValue("FLOAT"), is(Float.NEGATIVE_INFINITY));
+ assertThat(tuple.doubleValue("FLOAT"),
is(Double.NEGATIVE_INFINITY));
+
+ assertThat(tuple.floatValue(0), is(Float.NEGATIVE_INFINITY));
+ assertThat(tuple.doubleValue(0), is(Double.NEGATIVE_INFINITY));
+ }
+ }
+
+ @Test
+ void testReadAsDouble() {
+ String columnName = "DOUBLE";
+
+ // The field value can be represented as float.
+ {
+ double value = Float.MAX_VALUE;
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.DOUBLE,
columnName, value);
+
+ assertThat(tuple.floatValue(columnName), is((float) value));
+ assertThat(tuple.floatValue(0), is((float) value));
+
+ assertThat(tuple.doubleValue(columnName), is(value));
+ assertThat(tuple.doubleValue(0), is(value));
+ }
+
+ // The field value cannot be represented as float.
+ {
+ double value = Double.MAX_VALUE;
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.DOUBLE,
columnName, value);
+
+ ArithmeticException ex0 = assertThrows(ArithmeticException.class,
() -> tuple.floatValue(columnName));
+ assertThat(ex0.getMessage(), equalTo("Float value overflow"));
+
+ ArithmeticException ex1 = assertThrows(ArithmeticException.class,
() -> tuple.floatValue(0));
+ assertThat(ex1.getMessage(), equalTo("Float value overflow"));
+
+ assertThat(tuple.doubleValue(columnName), is(value));
+ assertThat(tuple.doubleValue(0), is(value));
+ }
+
+ // NaN
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.DOUBLE,
columnName, Double.NaN);
+
+ assertThat(Float.isNaN(tuple.floatValue(columnName)), is(true));
+ assertThat(Double.isNaN(tuple.doubleValue(columnName)), is(true));
+
+ assertThat(Float.isNaN(tuple.floatValue(0)), is(true));
+ assertThat(Double.isNaN(tuple.doubleValue(0)), is(true));
+ }
+
+ // Positive infinity
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT,
columnName, Double.POSITIVE_INFINITY);
+
+ assertThat(tuple.floatValue(columnName),
is(Float.POSITIVE_INFINITY));
+ assertThat(tuple.doubleValue(columnName),
is(Double.POSITIVE_INFINITY));
+
+ assertThat(tuple.floatValue(0), is(Float.POSITIVE_INFINITY));
+ assertThat(tuple.doubleValue(0), is(Double.POSITIVE_INFINITY));
+ }
+
+ // Negative infinity
+ {
+ Tuple tuple = createTupleOfSingleColumn(ColumnType.FLOAT,
columnName, Double.NEGATIVE_INFINITY);
Review Comment:
The column type should be ColumnType.DOUBLE, not ColumnType.FLOAT, since
this test case is part of testReadAsDouble and the columnName is "DOUBLE".
Using FLOAT here is inconsistent with the test's intent.
--
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]