@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start + length - 1, 30, 5); }
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start, 30, 5); }
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start, 30, 5); }
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start + length - 1, 10, 5); }
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start + length - 1, 10, 5); }
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start, 10, 5); }
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start + length - 1, 30, 5); }
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } return SqlDecimal.of(start, 10, 5); }
@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"); }
@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"); }
@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); }
@Test public void testMaxShortDecimalDecimal() { InternalAggregationFunction function = METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("max_by", AGGREGATE, parseTypeSignature("decimal(10,1)"), parseTypeSignature("decimal(10,1)"), parseTypeSignature("decimal(10,1)"))); assertAggregation( function, SqlDecimal.of("3.3"), createShortDecimalsBlock("1.1", "2.2", "3.3", "4.4"), createShortDecimalsBlock("1.2", "1.0", "2.0", "1.5")); }
@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); }
@Test public void testMinShortDecimalDecimal() { InternalAggregationFunction function = METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("min_by", AGGREGATE, parseTypeSignature("decimal(10,1)"), parseTypeSignature("decimal(10,1)"), parseTypeSignature("decimal(10,1)"))); assertAggregation( function, SqlDecimal.of("2.2"), createShortDecimalsBlock("1.1", "2.2", "3.3"), createShortDecimalsBlock("1.2", "1.0", "2.0")); }
@Test public void testMinLongDecimalDecimal() { InternalAggregationFunction function = METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("min_by", AGGREGATE, parseTypeSignature("decimal(19,1)"), parseTypeSignature("decimal(19,1)"), parseTypeSignature("decimal(19,1)"))); assertAggregation( function, SqlDecimal.of("2.2"), createLongDecimalsBlock("1.1", "2.2", "3.3"), createLongDecimalsBlock("1.2", "1.0", "2.0")); }
@Test public void testMaxLongDecimalDecimal() { InternalAggregationFunction function = METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("max_by", AGGREGATE, parseTypeSignature("decimal(19,1)"), parseTypeSignature("decimal(19,1)"), parseTypeSignature("decimal(19,1)"))); assertAggregation( function, SqlDecimal.of("3.3"), createLongDecimalsBlock("1.1", "2.2", "3.3", "4.4"), createLongDecimalsBlock("1.2", "1.0", "2.0", "1.5")); }
@Test public void testMinLongDecimalDecimal() { InternalAggregationFunction function = METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("min_by", AGGREGATE, parseTypeSignature("decimal(19,1)"), parseTypeSignature("decimal(19,1)"), parseTypeSignature("decimal(19,1)"))); assertAggregation( function, SqlDecimal.of("2.2"), createLongDecimalsBlock("1.1", "2.2", "3.3"), createLongDecimalsBlock("1.2", "1.0", "2.0")); }
@Test public void testMaxLongDecimalDecimal() { InternalAggregationFunction function = METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("max_by", AGGREGATE, parseTypeSignature("decimal(19,1)"), parseTypeSignature("decimal(19,1)"), parseTypeSignature("decimal(19,1)"))); assertAggregation( function, SqlDecimal.of("3.3"), createLongDecimalsBlock("1.1", "2.2", "3.3", "4.4"), createLongDecimalsBlock("1.2", "1.0", "2.0", "1.5")); }
@Test public void testMaxShortDecimalDecimal() { InternalAggregationFunction function = METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("max_by", AGGREGATE, parseTypeSignature("decimal(10,1)"), parseTypeSignature("decimal(10,1)"), parseTypeSignature("decimal(10,1)"))); assertAggregation( function, SqlDecimal.of("3.3"), createShortDecimalsBlock("1.1", "2.2", "3.3", "4.4"), createShortDecimalsBlock("1.2", "1.0", "2.0", "1.5")); }
@Test public void testMinShortDecimalDecimal() { InternalAggregationFunction function = METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("min_by", AGGREGATE, parseTypeSignature("decimal(10,1)"), parseTypeSignature("decimal(10,1)"), parseTypeSignature("decimal(10,1)"))); assertAggregation( function, SqlDecimal.of("2.2"), createShortDecimalsBlock("1.1", "2.2", "3.3"), createShortDecimalsBlock("1.2", "1.0", "2.0")); }