@Override public Type getSerializedType() { // Types are: firstNull, secondNull, firstField, secondField return RowType.anonymous(ImmutableList.of(BOOLEAN, BOOLEAN, firstType, secondType)); }
public Type getIntermediateType() { if (intermediateType.size() == 1) { return getOnlyElement(intermediateType); } else { return RowType.anonymous(intermediateType); } }
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)); }
public MultimapAggregationStateSerializer(Type keyType, Type valueType) { this.keyType = requireNonNull(keyType); this.valueType = requireNonNull(valueType); this.arrayType = new ArrayType(RowType.anonymous(ImmutableList.of(valueType, keyType))); }
public static Type getSerializedType(Type keyType, Type valueType) { return RowType.anonymous(ImmutableList.of(BIGINT, new ArrayType(keyType), new ArrayType(valueType))); }
@Override protected Type visitRow(Row node, StackableAstVisitorContext<Context> context) { List<Type> types = node.getItems().stream() .map((child) -> process(child, context)) .collect(toImmutableList()); Type type = RowType.anonymous(types); return setExpressionType(node, type); }
private static Type entryType(Type keyType, Type valueType) { return new ArrayType(RowType.anonymous(ImmutableList.of(keyType, valueType))); } }
public static Block rowBlockOf(List<Type> parameterTypes, Object... values) { RowType rowType = RowType.anonymous(parameterTypes); BlockBuilder blockBuilder = rowType.createBlockBuilder(null, 1); BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry(); for (int i = 0; i < values.length; i++) { appendToBlockBuilder(parameterTypes.get(i), values[i], singleRowBlockWriter); } blockBuilder.closeEntry(); return rowType.getObject(blockBuilder, 0); }
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(); }
private static void generateGetSerializedType(ClassDefinition definition, List<StateField> fields, CallSiteBinder callSiteBinder) { BytecodeBlock body = definition.declareMethod(a(PUBLIC), "getSerializedType", type(Type.class)).getBody(); Type type; if (fields.size() > 1) { List<Type> types = fields.stream().map(StateField::getSqlType).collect(toImmutableList()); type = RowType.anonymous(types); } else if (fields.size() == 1) { type = getOnlyElement(fields).getSqlType(); } else { type = UNKNOWN; } body.comment("return %s", type.getTypeSignature()) .append(constantType(callSiteBinder, type)) .retObject(); }
private static Type zipReturnType(Type... types) { return new ArrayType(RowType.anonymous(list(types))); }
public static Block toRow(List<Type> parameterTypes, Object... values) { checkArgument(parameterTypes.size() == values.length, "parameterTypes.size(" + parameterTypes.size() + ") does not equal to values.length(" + values.length + ")"); RowType rowType = RowType.anonymous(parameterTypes); BlockBuilder blockBuilder = new RowBlockBuilder(parameterTypes, null, 1); BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry(); for (int i = 0; i < values.length; i++) { appendToBlockBuilder(parameterTypes.get(i), values[i], singleRowBlockWriter); } blockBuilder.closeEntry(); return rowType.getObject(blockBuilder, 0); }
@Test public void testDifferentLength() { assertFunction( "zip_with(ARRAY[1], ARRAY['a', 'bc'], (x, y) -> (y, x))", new ArrayType(RowType.anonymous(ImmutableList.of(createVarcharType(2), INTEGER))), ImmutableList.of(ImmutableList.of("a", 1), asList("bc", null))); assertFunction( "zip_with(ARRAY[NULL, 2], ARRAY['a'], (x, y) -> (y, x))", new ArrayType(RowType.anonymous(ImmutableList.of(createVarcharType(1), INTEGER))), ImmutableList.of(asList("a", null), asList(null, 2))); assertFunction( "zip_with(ARRAY[NULL, NULL], ARRAY[NULL, 2, 1], (x, y) -> x + y)", new ArrayType(INTEGER), asList(null, null, null)); }
private void assertRowHashOperator(String inputString, List<Type> types, List<Object> elements) { checkArgument(types.size() == elements.size(), "types and elements must have the same size"); RowType rowType = RowType.anonymous(types); BlockBuilder blockBuilder = rowType.createBlockBuilder(null, 1); BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry(); for (int i = 0; i < types.size(); i++) { appendToBlockBuilder(types.get(i), elements.get(i), singleRowBlockWriter); } blockBuilder.closeEntry(); assertOperator(HASH_CODE, inputString, BIGINT, rowType.hash(blockBuilder.build(), 0)); }
@Setup public void setup() { Signature signature = new Signature("$operator$CAST", SCALAR, RowType.anonymous(fromFieldTypes).getTypeSignature(), RowType.anonymous(toFieldTypes).getTypeSignature()); List<RowExpression> projections = ImmutableList.of( new CallExpression(signature, RowType.anonymous(fromFieldTypes), ImmutableList.of(field(0, RowType.anonymous(toFieldTypes))))); MetadataManager metadata = MetadataManager.createTestMetadataManager(); pageProcessor = new ExpressionCompiler(metadata, new PageFunctionCompiler(metadata, 0)) .compilePageProcessor(Optional.empty(), projections) .get(); Block[] fieldBlocks = fromFieldTypes.stream() .map(type -> createBlock(POSITION_COUNT, type)) .toArray(Block[]::new); Block rowBlock = fromFieldBlocks(POSITION_COUNT, Optional.empty(), fieldBlocks); page = new Page(rowBlock); }
private static MethodDefinition generateGetIntermediateType(ClassDefinition definition, CallSiteBinder callSiteBinder, List<Type> type) { MethodDefinition methodDefinition = definition.declareMethod(a(PUBLIC), "getIntermediateType", type(Type.class)); if (type.size() == 1) { methodDefinition.getBody() .append(constantType(callSiteBinder, getOnlyElement(type))) .retObject(); } else { methodDefinition.getBody() .append(constantType(callSiteBinder, RowType.anonymous(type))) .retObject(); } return methodDefinition; }
@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 = RowType.anonymous(ImmutableList.of(INTEGER, BIGINT)); com.facebook.presto.spi.type.Type arrayOfRowType = RowType.anonymous(ImmutableList.of(new ArrayType(rowType))); Block actual = toBinaryBlock(arrayOfRowType, listHolder, getInspector(ListHolder.class)); BlockBuilder blockBuilder = rowType.createBlockBuilder(null, 1024); rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 8, 9L)); rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, 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)); RowType rowType = RowType.anonymous(ImmutableList.of(INTEGER, BIGINT)); RowType rowOfMapOfVarcharRowType = RowType.anonymous(ImmutableList.of(mapType(VARCHAR, rowType))); Block actual = toBinaryBlock(rowOfMapOfVarcharRowType, holder, getInspector(MapHolder.class)); Block mapBlock = mapBlockOf( VARCHAR, rowType, new Object[] {utf8Slice("fifteen"), utf8Slice("twelve")}, new Object[] {rowBlockOf(rowType.getTypeParameters(), 16, 17L), rowBlockOf(rowType.getTypeParameters(), 13, 14L)}); Block expected = rowBlockOf(ImmutableList.of(mapType(VARCHAR, rowType)), mapBlock); assertBlockEquals(actual, expected); }
@Test public void testRowDisplayNoColumnNames() { List<Type> types = asList( BOOLEAN, DOUBLE, new ArrayType(VARCHAR), new MapType( BOOLEAN, DOUBLE, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"))); RowType row = RowType.anonymous(types); assertEquals( row.getDisplayName(), "row(boolean, double, array(varchar), map(boolean, double))"); }
@Test public void testVarianceStateSerialization() { AccumulatorStateFactory<VarianceState> factory = StateCompiler.generateStateFactory(VarianceState.class); AccumulatorStateSerializer<VarianceState> serializer = StateCompiler.generateStateSerializer(VarianceState.class); VarianceState singleState = factory.createSingleState(); VarianceState deserializedState = factory.createSingleState(); singleState.setMean(1); singleState.setCount(2); singleState.setM2(3); BlockBuilder builder = RowType.anonymous(ImmutableList.of(BIGINT, DOUBLE, DOUBLE)).createBlockBuilder(null, 1); serializer.serialize(singleState, builder); Block block = builder.build(); serializer.deserialize(block, 0, deserializedState); assertEquals(deserializedState.getCount(), singleState.getCount()); assertEquals(deserializedState.getMean(), singleState.getMean()); assertEquals(deserializedState.getM2(), singleState.getM2()); }