public static Optional<DecimalType> getDecimalType(String hiveTypeName) { Matcher matcher = SUPPORTED_DECIMAL_TYPE.matcher(hiveTypeName); if (matcher.matches()) { int precision = parseInt(matcher.group(DECIMAL_PRECISION_GROUP)); int scale = parseInt(matcher.group(DECIMAL_SCALE_GROUP)); return Optional.of(createDecimalType(precision, scale)); } else { return Optional.empty(); } }
public static DataType<BigDecimal> decimalDataType(int precision, int scale) { String databaseType = format("decimal(%s, %s)", precision, scale); return dataType( databaseType, createDecimalType(precision, scale), bigDecimal -> format("CAST('%s' AS %s)", bigDecimal, databaseType), bigDecimal -> bigDecimal.setScale(scale, UNNECESSARY)); }
private static Type getDecimalType(BigDecimal decimal) { if (decimal == null) { return createDecimalType(1, 0); } return createDecimalType(decimal.precision(), decimal.scale()); }
@Test public void testCoerceTypeBase() { assertEquals(typeRegistry.coerceTypeBase(createDecimalType(21, 1), "decimal"), Optional.of(createDecimalType(21, 1))); assertEquals(typeRegistry.coerceTypeBase(BIGINT, "decimal"), Optional.of(createDecimalType(19, 0))); assertEquals(typeRegistry.coerceTypeBase(INTEGER, "decimal"), Optional.of(createDecimalType(10, 0))); assertEquals(typeRegistry.coerceTypeBase(TINYINT, "decimal"), Optional.of(createDecimalType(3, 0))); assertEquals(typeRegistry.coerceTypeBase(SMALLINT, "decimal"), Optional.of(createDecimalType(5, 0))); }
private void checkParseDecimal(String input, int precision, int scale, BigDecimal expected) { byte[] bytes = input.getBytes(US_ASCII); BigDecimal parsed = HiveDecimalParser.parseHiveDecimal(bytes, 0, bytes.length, DecimalType.createDecimalType(precision, scale)); assertEquals(parsed, expected); } }
private static Type getCommonSuperTypeForDecimal(DecimalType firstType, DecimalType secondType) { int targetScale = Math.max(firstType.getScale(), secondType.getScale()); int targetPrecision = Math.max(firstType.getPrecision() - firstType.getScale(), secondType.getPrecision() - secondType.getScale()) + targetScale; //we allow potential loss of precision here. Overflow checking is done in operators. targetPrecision = Math.min(38, targetPrecision); return createDecimalType(targetPrecision, targetScale); }
public static PrimitiveColumnReader createReader(RichColumnDescriptor descriptor, int precision, int scale) { DecimalType decimalType = DecimalType.createDecimalType(precision, scale); if (decimalType.isShort()) { return new ShortDecimalColumnReader(descriptor); } else { return new LongDecimalColumnReader(descriptor); } } }
private static List<Type> getTypes() { List<Type> simpleTypes = METADATA.getTypeManager().getTypes(); return new ImmutableList.Builder<Type>() .addAll(simpleTypes) .add(VARCHAR) .add(DecimalType.createDecimalType(1)) .add(RowType.anonymous(ImmutableList.of(BIGINT, VARCHAR, DOUBLE))) .build(); }
@Setup public void setup() { addSymbol("l_38_30", createDecimalType(38, 30)); addSymbol("l_26_18", createDecimalType(26, 18)); addSymbol("l_20_12", createDecimalType(20, 12)); addSymbol("l_20_8", createDecimalType(20, 8)); addSymbol("s_17_9", createDecimalType(17, 9)); generateInputPage(10000, 10000, 10000, 10000, 10000); generateProcessor(expression); } }
private void assertParseResult(String value, Object expectedObject, int expectedPrecision, int expectedScale) { assertEquals(Decimals.parse(value), new DecimalParseResult( expectedObject, createDecimalType(expectedPrecision, expectedScale))); }
@Override protected List<String> getFunctionParameterTypes() { return ImmutableList.of(DecimalType.createDecimalType(MAX_SHORT_PRECISION + 1, 2).getTypeSignature().toString()); } }
private void assertParseResultIncludeLeadingZerosInPrecision(String value, Object expectedObject, int expectedPrecision, int expectedScale) { assertEquals(Decimals.parseIncludeLeadingZerosInPrecision(value), new DecimalParseResult( expectedObject, createDecimalType(expectedPrecision, expectedScale))); }
@Override protected List<String> getFunctionParameterTypes() { return ImmutableList.of(DecimalType.createDecimalType(MAX_SHORT_PRECISION + 1, 2).getTypeSignature().toString()); } }
@Test public void testCoalesce() { assertDecimalFunction("coalesce(2.1, null, cast(null as decimal(5,3)))", decimal("02.100")); assertFunction("coalesce(cast(null as decimal(17,3)), null, cast(null as decimal(12,3)))", createDecimalType(17, 3), null); assertDecimalFunction("coalesce(3, 2.1, null, cast(null as decimal(6,3)))", decimal("0000000003.000")); assertFunction("coalesce(cast(null as decimal(17,3)), null, cast(null as decimal(12,3)))", createDecimalType(17, 3), null); }
@Setup public void setup() { addSymbol("v1", createDecimalType(Integer.valueOf(precision), SCALE)); String expression = "CAST(v1 AS DOUBLE)"; generateRandomInputPage(); generateProcessor(expression); } }
@Setup public void setup() { addSymbol("v1", createDecimalType(Integer.valueOf(precision), SCALE)); String expression = "CAST(v1 AS VARCHAR)"; generateRandomInputPage(); generateProcessor(expression); } }
protected void assertDecimalFunction(String statement, SqlDecimal expectedResult) { assertFunction( statement, createDecimalType(expectedResult.getPrecision(), expectedResult.getScale()), expectedResult); }
@Test public void testShortDecimal() { InternalAggregationFunction decimalAgg = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("checksum", AGGREGATE, parseTypeSignature(VARBINARY), parseTypeSignature("decimal(10,2)"))); Block block = createShortDecimalsBlock("11.11", "22.22", null, "33.33", "44.44"); DecimalType shortDecimalType = DecimalType.createDecimalType(1); assertAggregation(decimalAgg, expectedChecksum(shortDecimalType, block), block); }
@Test public void testLongDecimal() { InternalAggregationFunction decimalAgg = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("checksum", AGGREGATE, parseTypeSignature(VARBINARY), parseTypeSignature("decimal(19,2)"))); Block block = createLongDecimalsBlock("11.11", "22.22", null, "33.33", "44.44"); DecimalType longDecimalType = DecimalType.createDecimalType(19); assertAggregation(decimalAgg, expectedChecksum(longDecimalType, block), block); }
@Test public void testBasic() { assertFunction("\"$internal$try\"(() -> 42)", INTEGER, 42); assertFunction("\"$internal$try\"(() -> DOUBLE '4.5')", DOUBLE, 4.5); assertFunction("\"$internal$try\"(() -> DECIMAL '4.5')", createDecimalType(2, 1), SqlDecimal.of("4.5")); assertFunction("\"$internal$try\"(() -> TRUE)", BOOLEAN, true); assertFunction("\"$internal$try\"(() -> 'hello')", createVarcharType(5), "hello"); assertFunction("\"$internal$try\"(() -> JSON '[true, false, 12, 12.7, \"12\", null]')", JSON, "[true,false,12,12.7,\"12\",null]"); assertFunction("\"$internal$try\"(() -> ARRAY [1, 2])", new ArrayType(INTEGER), asList(1, 2)); assertFunction("\"$internal$try\"(() -> NULL)", UNKNOWN, null); }