public MaxOrMinByStateSerializer(Type valueType, Type keyType) { this.valueType = valueType; this.keyType = keyType; this.serializedType = new RowType(ImmutableList.of(keyType, valueType), Optional.empty()); }
public MinMaxNStateSerializer(BlockComparator blockComparator, Type elementType) { this.blockComparator = blockComparator; this.elementType = elementType; this.arrayType = new ArrayType(elementType); this.serializedType = new RowType(ImmutableList.of(BIGINT, arrayType), Optional.empty()); }
private Type toPrestoRowType(final LogicalSchema.LogicalFieldSchema field) { final List<Type> fieldTypes = Lists.newArrayList(); final List<String> fieldNames = Lists.newArrayList(); field.schema.getFields().stream().forEach(logicalFieldSchema -> { fieldTypes.add(toPrestoType(logicalFieldSchema)); fieldNames.add(logicalFieldSchema.alias); }); return new RowType(fieldTypes, Optional.of(fieldNames)); }
@Override public Type createType(List<TypeParameter> parameters) { checkArgument(!parameters.isEmpty(), "Row type must have at least one parameter"); checkArgument( parameters.stream().allMatch(parameter -> parameter.getKind() == ParameterKind.NAMED_TYPE), "Expected only named types as a parameters, got %s", parameters); List<NamedType> namedTypes = parameters.stream().map(TypeParameter::getNamedType).collect(toList()); return new RowType( namedTypes.stream().map(NamedType::getType).collect(toList()), Optional.of(namedTypes.stream().map(NamedType::getName).collect(toList()))); }
@ScalarFunction("test_row") @SqlType("row<double,array(row<bigint,double>('col0','col1')),row<bigint,double>('col0','col1')>('col0','col1','col2')") public static Block testNestedRowWithArray( @Nullable @SqlType(StandardTypes.DOUBLE) Double arg1, @Nullable @SqlType("array(row<bigint,double>('col0','col1'))") Block arg2, @Nullable @SqlType("row<bigint,double>('col0','col1')") Block arg3) { List<Type> parameterTypes = ImmutableList.of( DOUBLE, new ArrayType(new RowType(ImmutableList.of(BIGINT, DOUBLE), Optional.of(ImmutableList.of("col0", "col1")))), new RowType(ImmutableList.of(BIGINT, DOUBLE), Optional.of(ImmutableList.of("col0", "col1")))); return toStackRepresentation(parameterTypes, arg1, arg2, arg3); }
@Override protected Type visitRow(Row node, StackableAstVisitorContext<AnalysisContext> context) { List<Type> types = node.getItems().stream() .map((child) -> process(child, context)) .collect(toImmutableList()); Type type = new RowType(types, Optional.empty()); expressionTypes.put(node, type); return type; }
@ScalarFunction("test_row") @SqlType("row<double,row<timestamp with time zone,timestamp with time zone>('col0','col1')>('col2','col3')") public static Block testNestedRowsWithTimestampsWithTimeZones(@Nullable @SqlType(StandardTypes.DOUBLE) Double arg1, @Nullable @SqlType("row<timestamp with time zone,timestamp with time zone>('col0','col1')") Block arg2) { List<Type> parameterTypes = ImmutableList.of( DOUBLE, new RowType(ImmutableList.of(TIMESTAMP_WITH_TIME_ZONE, TIMESTAMP_WITH_TIME_ZONE), Optional.of(ImmutableList.of("col0", "col1")))); return toStackRepresentation(parameterTypes, arg1, arg2); }
@ScalarFunction("test_row") @SqlType("row<double,array(bigint),row<bigint,double>('col0','col1')>('col0','col1','col2')") public static Block testNestedRow(@Nullable @SqlType(StandardTypes.DOUBLE) Double arg1, @Nullable @SqlType("array(bigint)") Block arg2, @Nullable @SqlType("row<bigint,double>('col0','col1')") Block arg3) { List<Type> parameterTypes = ImmutableList.of( DOUBLE, new ArrayType(BIGINT), new RowType(ImmutableList.of(BIGINT, DOUBLE), Optional.of(ImmutableList.of("col0", "col1")))); return toStackRepresentation(parameterTypes, arg1, arg2, arg3); }
public static Type getSerializedType(Type keyType, Type valueType) { return new RowType(ImmutableList.of(BIGINT, new ArrayType(keyType), new ArrayType(valueType)), Optional.empty()); }
@Test public void testDoubleRowMap() throws Exception { RowType innerRowType = new RowType(ImmutableList.of(BIGINT, DOUBLE), Optional.of(ImmutableList.of("f1", "f2"))); testMultimapAgg(DOUBLE, ImmutableList.of(1.0, 2.0, 3.0), innerRowType, ImmutableList.of(ImmutableList.of(1L, 1.0), ImmutableList.of(2L, 2.0), ImmutableList.of(3L, 3.0))); }
@Test public void testRowDisplayName() { List<Type> types = asList(BOOLEAN, DOUBLE, new ArrayType(VARCHAR), new MapType(BOOLEAN, DOUBLE)); Optional<List<String>> names = Optional.of(asList("bool_col", "double_col", "array_col", "map_col")); RowType row = new RowType(types, names); assertEquals( row.getDisplayName(), "row(bool_col boolean, double_col double, array_col array(varchar), map_col map(boolean, double))"); }
@Test public void testRowDisplayNoColumnNames() { List<Type> types = asList(BOOLEAN, DOUBLE, new ArrayType(VARCHAR), new MapType(BOOLEAN, DOUBLE)); RowType row = new RowType(types, Optional.empty()); assertEquals( row.getDisplayName(), "row(boolean, double, array(varchar), map(boolean, double))"); } }
throws Exception RowType nameType = new RowType(ImmutableList.of(VARCHAR, VARCHAR), Optional.empty()); RowType phoneType = new RowType(ImmutableList.of(VARCHAR, VARCHAR), Optional.empty()); RowType personType = new RowType(ImmutableList.of(nameType, BIGINT, VARCHAR, new ArrayType(phoneType)), Optional.empty());
@Test public void testListBlock() { List<InnerStruct> array = new ArrayList<>(2); array.add(new InnerStruct(8, 9L)); array.add(new InnerStruct(10, 11L)); ListHolder listHolder = new ListHolder(); listHolder.array = array; com.facebook.presto.spi.type.Type rowType = new RowType(ImmutableList.of(BIGINT, BIGINT), Optional.empty()); com.facebook.presto.spi.type.Type arrayOfRowType = new RowType(ImmutableList.of(new ArrayType(rowType)), Optional.empty()); Block actual = toBinaryBlock(arrayOfRowType, listHolder, getInspector(ListHolder.class)); BlockBuilder blockBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), 1024); rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(BIGINT, BIGINT), 8, 9L)); rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(BIGINT, BIGINT), 10, 11L)); Block expected = rowBlockOf(ImmutableList.of(new ArrayType(rowType)), blockBuilder.build()); assertBlockEquals(actual, expected); }
@Test public void testMapBlock() { MapHolder holder = new MapHolder(); holder.map = new TreeMap<>(); holder.map.put("twelve", new InnerStruct(13, 14L)); holder.map.put("fifteen", new InnerStruct(16, 17L)); com.facebook.presto.spi.type.Type rowType = new RowType(ImmutableList.of(BIGINT, BIGINT), Optional.empty()); com.facebook.presto.spi.type.Type mapOfVarcharRowType = new RowType(ImmutableList.of(new MapType(VARCHAR, rowType)), Optional.empty()); Block actual = toBinaryBlock(mapOfVarcharRowType, holder, getInspector(MapHolder.class)); BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(VARCHAR, rowType), new BlockBuilderStatus(), 1024); VARCHAR.writeString(blockBuilder, "fifteen"); rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(BIGINT, BIGINT), 16, 17L)); VARCHAR.writeString(blockBuilder, "twelve"); rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(BIGINT, BIGINT), 13, 14L)); Block expected = rowBlockOf(ImmutableList.of(new MapType(VARCHAR, rowType)), blockBuilder); assertBlockEquals(actual, expected); }
com.facebook.presto.spi.type.Type rowType = new RowType(ImmutableList.of(BIGINT, BIGINT), Optional.empty()); Block actual = toBinaryBlock(rowType, innerStruct, getInspector(InnerStruct.class)); outerStruct.innerStruct = new InnerStruct(18, 19L); com.facebook.presto.spi.type.Type innerRowType = new RowType(ImmutableList.of(BIGINT, BIGINT), Optional.empty()); com.facebook.presto.spi.type.Type arrayOfInnerRowType = new ArrayType(innerRowType); com.facebook.presto.spi.type.Type mapOfInnerRowType = new MapType(VARCHAR, innerRowType); List<com.facebook.presto.spi.type.Type> outerRowParameterTypes = ImmutableList.of(BIGINT, BIGINT, BIGINT, BIGINT, DOUBLE, DOUBLE, VARCHAR, VARCHAR, arrayOfInnerRowType, mapOfInnerRowType, innerRowType); com.facebook.presto.spi.type.Type outerRowType = new RowType(outerRowParameterTypes, Optional.empty());
@Test public void testRowHistograms() throws Exception { RowType innerRowType = new RowType(ImmutableList.of(BIGINT, DOUBLE), Optional.of(ImmutableList.of("f1", "f2"))); MapType mapType = new MapType(innerRowType, BIGINT); InternalAggregationFunction aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), innerRowType.getTypeSignature().toString())); BlockBuilder builder = innerRowType.createBlockBuilder(new BlockBuilderStatus(), 3); innerRowType.writeObject(builder, testRowBigintBigint(1L, 1.0)); innerRowType.writeObject(builder, testRowBigintBigint(2L, 2.0)); innerRowType.writeObject(builder, testRowBigintBigint(3L, 3.0)); assertAggregation( aggregationFunction, 1.0, ImmutableMap.of(ImmutableList.of(1L, 1.0), 1L, ImmutableList.of(2L, 2.0), 1L, ImmutableList.of(3L, 3.0), 1L), builder.build()); }
@Test public void testStateDeserializer() throws Exception { String[] keys = {"loooooong string", "short string"}; double[] values = {3.14, 2.71}; MaxOrMinByStateSerializer serializer = new MaxOrMinByStateSerializer(DOUBLE, VARCHAR); BlockBuilder builder = new RowType(ImmutableList.of(VARCHAR, DOUBLE), Optional.empty()).createBlockBuilder(new BlockBuilderStatus(), 2); for (int i = 0; i < keys.length; i++) { serializer.serialize(makeState(keys[i], values[i]), builder); } Block serialized = builder.build(); for (int i = 0; i < keys.length; i++) { MaxOrMinByState deserialized = new MaxOrMinByStateFactory().createSingleState(); serializer.deserialize(serialized, i, deserialized); assertEquals(VARCHAR.getSlice(deserialized.getKey(), 0), Slices.utf8Slice(keys[i])); assertEquals(DOUBLE.getDouble(deserialized.getValue(), 0), values[i]); } }
@Test public void testDoubleRowMap() throws Exception { RowType innerRowType = new RowType(ImmutableList.of(BIGINT, DOUBLE), Optional.of(ImmutableList.of("f1", "f2"))); MapType mapType = new MapType(DOUBLE, innerRowType); InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.DOUBLE, innerRowType.getTypeSignature().toString())); BlockBuilder builder = innerRowType.createBlockBuilder(new BlockBuilderStatus(), 3); innerRowType.writeObject(builder, testRowBigintBigint(1L, 1.0)); innerRowType.writeObject(builder, testRowBigintBigint(2L, 2.0)); innerRowType.writeObject(builder, testRowBigintBigint(3L, 3.0)); assertAggregation( aggFunc, 1.0, ImmutableMap.of(1.0, ImmutableList.of(1L, 1.0), 2.0, ImmutableList.of(2L, 2.0), 3.0, ImmutableList.of(3L, 3.0)), createDoublesBlock(1.0, 2.0, 3.0), builder.build()); }
if (elementType != null) { if (!(elementType instanceof RowType)) { elementType = new RowType(Lists.newArrayList(elementType), Optional.of(ImmutableList.of(NAME_ARRAY_ELEMENT)));