public static SqlDecimal of(long unscaledValue, int precision, int scale) { return new SqlDecimal(BigInteger.valueOf(unscaledValue), precision, scale); }
public static Object getJsonObjectValue(Type valueType, ConnectorSession session, Block block, int position) { Object objectValue = valueType.getObjectValue(session, block, position); if (objectValue instanceof SqlDecimal) { objectValue = ((SqlDecimal) objectValue).toBigDecimal(); } return objectValue; } }
protected void assertDecimalFunction(String statement, SqlDecimal expectedResult) { assertFunction( statement, createDecimalType(expectedResult.getPrecision(), expectedResult.getScale()), expectedResult); }
@Test public void testToString() { assertEquals(new SqlDecimal(new BigInteger("0"), 2, 1).toString(), "0.0"); assertEquals(new SqlDecimal(new BigInteger("0"), 3, 2).toString(), "0.00"); assertEquals(new SqlDecimal(new BigInteger("0"), 6, 5).toString(), "0.00000"); assertEquals(new SqlDecimal(new BigInteger("0"), 10, 5).toString(), "0.00000"); assertEquals(new SqlDecimal(new BigInteger("1"), 2, 1).toString(), "0.1"); assertEquals(new SqlDecimal(new BigInteger("0"), 3, 3).toString(), "0.000"); assertEquals(new SqlDecimal(new BigInteger("1"), 1, 0).toString(), "1"); assertEquals(new SqlDecimal(new BigInteger("1000"), 4, 3).toString(), "1.000"); assertEquals(new SqlDecimal(new BigInteger("12345678901234567890123456789012345678"), 38, 20) .toString(), "123456789012345678.90123456789012345678"); assertEquals(new SqlDecimal(new BigInteger("-10"), 2, 1).toString(), "-1.0"); assertEquals(new SqlDecimal(new BigInteger("-100"), 3, 2).toString(), "-1.00"); assertEquals(new SqlDecimal(new BigInteger("-100000"), 6, 5).toString(), "-1.00000"); assertEquals(new SqlDecimal(new BigInteger("-100000"), 10, 5).toString(), "-1.00000"); assertEquals(new SqlDecimal(new BigInteger("-1"), 2, 1).toString(), "-0.1"); assertEquals(new SqlDecimal(new BigInteger("-1"), 3, 3).toString(), "-0.001"); assertEquals(new SqlDecimal(new BigInteger("-1"), 1, 0).toString(), "-1"); assertEquals(new SqlDecimal(new BigInteger("-1000"), 4, 3).toString(), "-1.000"); assertEquals(new SqlDecimal(new BigInteger("-12345678901234567890123456789012345678"), 38, 20) .toString(), "-123456789012345678.90123456789012345678"); } }
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start, 10, 5); }
type.writeLong(blockBuilder, ((SqlDecimal) element).getUnscaledValue().longValue()); type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) element).getUnscaledValue()));
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start + length - 1, 10, 5); }
type.writeLong(blockBuilder, ((SqlDecimal) element).getUnscaledValue().longValue()); type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) element).getUnscaledValue()));
public static SqlDecimal of(String unscaledValue, int precision, int scale) { return new SqlDecimal(new BigInteger(unscaledValue), precision, scale); }
private static Optional<BigDecimal> getDecimalValue(ConnectorSession session, Type type, Block block) { return block.isNull(0) ? Optional.empty() : Optional.of(((SqlDecimal) type.getObjectValue(session, block, 0)).toBigDecimal()); }
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start + length - 1, 30, 5); }
public static SqlDecimal of(String decimalValue) { BigDecimal bigDecimal = new BigDecimal(decimalValue); return new SqlDecimal(bigDecimal.unscaledValue(), bigDecimal.precision(), bigDecimal.scale()); }
convertedValue = ((SqlDecimal) prestoValue).toBigDecimal();
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start, 30, 5); }
private static List<SqlDecimal> decimalSequence(String start, String step, int items, int precision, int scale) { BigInteger decimalStep = new BigInteger(step); List<SqlDecimal> values = new ArrayList<>(); BigInteger nextValue = new BigInteger(start); for (int i = 0; i < items; i++) { values.add(new SqlDecimal(nextValue, precision, scale)); nextValue = nextValue.add(decimalStep); } return values; }
return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
@Test public void testMapToMapCast() { assertFunction("CAST(MAP(ARRAY['1', '100'], ARRAY[true, false]) AS MAP<varchar,bigint>)", mapType(VARCHAR, BIGINT), ImmutableMap.of("1", 1L, "100", 0L)); assertFunction("CAST(MAP(ARRAY[1,2], ARRAY[1,2]) AS MAP<bigint, boolean>)", mapType(BIGINT, BOOLEAN), ImmutableMap.of(1L, true, 2L, true)); assertFunction("CAST(MAP(ARRAY[1,2], ARRAY[array[1],array[2]]) AS MAP<bigint, array<boolean>>)", mapType(BIGINT, new ArrayType(BOOLEAN)), ImmutableMap.of(1L, ImmutableList.of(true), 2L, ImmutableList.of(true))); assertFunction("CAST(MAP(ARRAY[1], ARRAY[MAP(ARRAY[1.0E0], ARRAY[false])]) AS MAP<varchar, MAP(bigint,bigint)>)", mapType(VARCHAR, mapType(BIGINT, BIGINT)), ImmutableMap.of("1", ImmutableMap.of(1L, 0L))); assertFunction("CAST(MAP(ARRAY[1,2], ARRAY[DATE '2016-01-02', DATE '2016-02-03']) AS MAP(bigint, varchar))", mapType(BIGINT, VARCHAR), ImmutableMap.of(1L, "2016-01-02", 2L, "2016-02-03")); assertFunction("CAST(MAP(ARRAY[1,2], ARRAY[TIMESTAMP '2016-01-02 01:02:03', TIMESTAMP '2016-02-03 03:04:05']) AS MAP(bigint, varchar))", mapType(BIGINT, VARCHAR), ImmutableMap.of(1L, "2016-01-02 01:02:03.000", 2L, "2016-02-03 03:04:05.000")); assertFunction("CAST(MAP(ARRAY['123', '456'], ARRAY[1.23456E0, 2.34567E0]) AS MAP(integer, real))", mapType(INTEGER, REAL), ImmutableMap.of(123, 1.23456F, 456, 2.34567F)); assertFunction("CAST(MAP(ARRAY['123', '456'], ARRAY[1.23456E0, 2.34567E0]) AS MAP(smallint, decimal(6,5)))", mapType(SMALLINT, createDecimalType(6, 5)), ImmutableMap.of((short) 123, SqlDecimal.of("1.23456"), (short) 456, SqlDecimal.of("2.34567"))); assertFunction("CAST(MAP(ARRAY[json '1'], ARRAY[1]) AS MAP(bigint, bigint))", mapType(BIGINT, BIGINT), ImmutableMap.of(1L, 1L)); assertFunction("CAST(MAP(ARRAY['1'], ARRAY[json '1']) AS MAP(bigint, bigint))", mapType(BIGINT, BIGINT), ImmutableMap.of(1L, 1L)); // null values Map<Long, Double> expected = new HashMap<>(); expected.put(0L, 1.0); expected.put(1L, null); expected.put(2L, null); expected.put(3L, 2.0); assertFunction("CAST(MAP(ARRAY[0, 1, 2, 3], ARRAY[1,NULL, NULL, 2]) AS MAP<BIGINT, DOUBLE>)", mapType(BIGINT, DOUBLE), expected); assertInvalidCast("CAST(MAP(ARRAY[1, 2], ARRAY[6, 9]) AS MAP<boolean, bigint>)", "duplicate keys"); assertInvalidCast("CAST(MAP(ARRAY[json 'null'], ARRAY[1]) AS MAP<bigint, bigint>)", "map key is null"); }
private List<SqlDecimal> createDecimalValuesNoNull() { Random random = new Random(); List<SqlDecimal> values = new ArrayList<>(); for (int i = 0; i < ROWS; ++i) { values.add(new SqlDecimal(BigInteger.valueOf(random.nextLong() % 10000000000L), 10, 5)); } return values; } }
return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
assertFunction("sign(DECIMAL '0')", expectedDecimalReturnType, SqlDecimal.of("0")); assertFunction("sign(DECIMAL '123')", expectedDecimalReturnType, SqlDecimal.of("1")); assertFunction("sign(DECIMAL '-123')", expectedDecimalReturnType, SqlDecimal.of("-1")); assertFunction("sign(DECIMAL '123.000000000000000')", expectedDecimalReturnType, SqlDecimal.of("1")); assertFunction("sign(DECIMAL '-123.000000000000000')", expectedDecimalReturnType, SqlDecimal.of("-1")); assertFunction("sign(DECIMAL '0.000000000000000000')", expectedDecimalReturnType, SqlDecimal.of("0")); assertFunction("sign(DECIMAL '1230.000000000000000')", expectedDecimalReturnType, SqlDecimal.of("1")); assertFunction("sign(DECIMAL '-1230.000000000000000')", expectedDecimalReturnType, SqlDecimal.of("-1"));