public ArrayAggregationStateSerializer(Type elementType) { this.elementType = elementType; this.arrayType = new ArrayType(elementType); }
public MinMaxNStateSerializer(BlockComparator blockComparator, Type elementType) { this.blockComparator = blockComparator; this.elementType = elementType; this.arrayType = new ArrayType(elementType); this.serializedType = RowType.anonymous(ImmutableList.of(BIGINT, arrayType)); }
@Override public Type createType(TypeManager typeManager, List<TypeParameter> parameters) { checkArgument(parameters.size() == 1, "Array type expects exactly one type as a parameter, got %s", parameters); checkArgument( parameters.get(0).getKind() == ParameterKind.TYPE, "Array expects type as a parameter, got %s", parameters); return new ArrayType(parameters.get(0).getType()); } }
public static TypedKeyValueHeap deserialize(Block block, Type keyType, Type valueType, BlockComparator blockComparator) { int capacity = toIntExact(BIGINT.getLong(block, 0)); Block keysBlock = new ArrayType(keyType).getObject(block, 1); Block valuesBlock = new ArrayType(valueType).getObject(block, 2); TypedKeyValueHeap heap = new TypedKeyValueHeap(blockComparator, keyType, valueType, capacity); heap.addAll(keysBlock, valuesBlock); return heap; }
@Test public void testDoubleArrayMultimap() { Type arrayType = new ArrayType(VARCHAR); List<Double> expectedKeys = ImmutableList.of(1.0, 2.0, 3.0); List<List<String>> expectedValues = ImmutableList.of(ImmutableList.of("a", "b"), ImmutableList.of("c"), ImmutableList.of("d", "e", "f")); testMultimapAgg(DOUBLE, expectedKeys, arrayType, expectedValues); }
private static Type entryType(Type keyType, Type valueType) { return new ArrayType(RowType.anonymous(ImmutableList.of(keyType, valueType))); } }
@Test public void testEmpty() { assertFunction("filter(ARRAY [], x -> true)", new ArrayType(UNKNOWN), ImmutableList.of()); assertFunction("filter(ARRAY [], x -> false)", new ArrayType(UNKNOWN), ImmutableList.of()); assertFunction("filter(ARRAY [], x -> CAST (null AS BOOLEAN))", new ArrayType(UNKNOWN), ImmutableList.of()); assertFunction("filter(CAST (ARRAY [] AS ARRAY(INTEGER)), x -> true)", new ArrayType(INTEGER), ImmutableList.of()); }
@Test public void testBasic() { assertFunction("array_except(ARRAY[1, 5, 3], ARRAY[3])", new ArrayType(INTEGER), ImmutableList.of(1, 5)); assertFunction("array_except(ARRAY[CAST(1 as BIGINT), 5, 3], ARRAY[5])", new ArrayType(BIGINT), ImmutableList.of(1L, 3L)); assertFunction("array_except(ARRAY[CAST('x' as VARCHAR), 'y', 'z'], ARRAY['x'])", new ArrayType(VARCHAR), ImmutableList.of("y", "z")); assertFunction("array_except(ARRAY[true, false, null], ARRAY[true])", new ArrayType(BOOLEAN), asList(false, null)); assertFunction("array_except(ARRAY[1.1E0, 5.4E0, 3.9E0], ARRAY[5, 5.4E0])", new ArrayType(DOUBLE), ImmutableList.of(1.1, 3.9)); }
@Test public void testDuplicates() { assertFunction("array_except(ARRAY[1, 5, 3, 5, 1], ARRAY[3])", new ArrayType(INTEGER), ImmutableList.of(1, 5)); assertFunction("array_except(ARRAY[CAST(1 as BIGINT), 5, 5, 3, 3, 3, 1], ARRAY[3, 5])", new ArrayType(BIGINT), ImmutableList.of(1L)); assertFunction("array_except(ARRAY[CAST('x' as VARCHAR), 'x', 'y', 'z'], ARRAY['x', 'y', 'x'])", new ArrayType(VARCHAR), ImmutableList.of("z")); assertFunction("array_except(ARRAY[true, false, null, true, false, null], ARRAY[true, true, true])", new ArrayType(BOOLEAN), asList(false, null)); } }
@Test public void testReverse() { assertFunction("REVERSE(ARRAY[1])", new ArrayType(INTEGER), ImmutableList.of(1)); assertFunction("REVERSE(ARRAY[1, 2, 3, 4])", new ArrayType(INTEGER), ImmutableList.of(4, 3, 2, 1)); assertFunction("REVERSE(ARRAY_SORT(ARRAY[2, 3, 4, 1]))", new ArrayType(INTEGER), ImmutableList.of(4, 3, 2, 1)); assertFunction("REVERSE(ARRAY[2, BIGINT '3', 4, 1])", new ArrayType(BIGINT), ImmutableList.of(1L, 4L, 3L, 2L)); assertFunction("REVERSE(ARRAY['a', 'b', 'c', 'd'])", new ArrayType(createVarcharType(1)), ImmutableList.of("d", "c", "b", "a")); assertFunction("REVERSE(ARRAY[TRUE, FALSE])", new ArrayType(BOOLEAN), ImmutableList.of(false, true)); assertFunction("REVERSE(ARRAY[1.1E0, 2.2E0, 3.3E0, 4.4E0])", new ArrayType(DOUBLE), ImmutableList.of(4.4, 3.3, 2.2, 1.1)); assertCachedInstanceHasBoundedRetainedSize("REVERSE(ARRAY[1.1E0, 2.2E0, 3.3E0, 4.4E0])"); }
@Test public void testArrayConcat() { assertFunction("CONCAT(" + Joiner.on(", ").join(nCopies(253, "array[1]")) + ")", new ArrayType(INTEGER), nCopies(253, 1)); assertNotSupported( "CONCAT(" + Joiner.on(", ").join(nCopies(254, "array[1]")) + ")", "Too many arguments for vararg function"); } }
@ScalarFunction(hidden = true) @SqlType("array(unknown)") public static Block arrayConstructor() { BlockBuilder blockBuilder = new ArrayType(UNKNOWN).createBlockBuilder(null, 0); return blockBuilder.build(); } }
@Test public void testArrayHistograms() { ArrayType arrayType = new ArrayType(VARCHAR); MapType mapType = mapType(arrayType, BIGINT); InternalAggregationFunction aggregationFunction = getAggregation(mapType.getTypeSignature(), arrayType.getTypeSignature()); assertAggregation( aggregationFunction, ImmutableMap.of(ImmutableList.of("a", "b", "c"), 1L, ImmutableList.of("d", "e", "f"), 1L, ImmutableList.of("c", "b", "a"), 1L), createStringArraysBlock(ImmutableList.of(ImmutableList.of("a", "b", "c"), ImmutableList.of("d", "e", "f"), ImmutableList.of("c", "b", "a")))); }
@Override public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { Type keyType = boundVariables.getTypeVariable("K"); Type valueType = boundVariables.getTypeVariable("V"); Type outputType = typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of( TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(new ArrayType(valueType).getTypeSignature()))); return generateAggregation(keyType, valueType, outputType); }
@Test public void testNoCaching() { ImmutableList.Builder<RowExpression> projectionsBuilder = ImmutableList.builder(); ArrayType arrayType = new ArrayType(VARCHAR); Signature signature = new Signature("concat", SCALAR, arrayType.getTypeSignature(), arrayType.getTypeSignature(), arrayType.getTypeSignature()); projectionsBuilder.add(new CallExpression(signature, arrayType, ImmutableList.of(field(0, arrayType), field(1, arrayType)))); ImmutableList<RowExpression> projections = projectionsBuilder.build(); PageProcessor pageProcessor = compiler.compilePageProcessor(Optional.empty(), projections).get(); PageProcessor pageProcessor2 = compiler.compilePageProcessor(Optional.empty(), projections).get(); assertTrue(pageProcessor != pageProcessor2); }
@Test public void testArray() { ArrayType arrayType = new ArrayType(BigintType.BIGINT); InternalAggregationFunction stringAgg = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("checksum", AGGREGATE, VarbinaryType.VARBINARY.getTypeSignature(), arrayType.getTypeSignature())); Block block = createArrayBigintBlock(asList(null, asList(1L, 2L), asList(3L, 4L), asList(5L, 6L))); assertAggregation(stringAgg, expectedChecksum(arrayType, block), block); }
private void assertArrayHashOperator(String inputArray, Type elementType, List<Object> elements) { ArrayType arrayType = new ArrayType(elementType); BlockBuilder arrayArrayBuilder = arrayType.createBlockBuilder(null, 1); BlockBuilder arrayBuilder = elementType.createBlockBuilder(null, elements.size()); for (Object element : elements) { appendToBlockBuilder(elementType, element, arrayBuilder); } arrayType.writeObject(arrayArrayBuilder, arrayBuilder.build()); assertOperator(HASH_CODE, inputArray, BIGINT, arrayType.hash(arrayArrayBuilder.build(), 0)); }
@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); }
private static RunLengthEncodedBlock createRLEBlock(Iterable<Double> percentiles, int positionCount) { BlockBuilder rleBlockBuilder = new ArrayType(DOUBLE).createBlockBuilder(null, 1); BlockBuilder arrayBlockBuilder = rleBlockBuilder.beginBlockEntry(); for (double percentile : percentiles) { DOUBLE.writeDouble(arrayBlockBuilder, percentile); } rleBlockBuilder.closeEntry(); return new RunLengthEncodedBlock(rleBlockBuilder.build(), positionCount); } }