/** * Load a record batch from a single buffer. * * @param def The definition for the record batch. * @param buf The buffer that holds the data associated with the record batch * @return Whether or not the schema changed since the previous load. * @throws SchemaChangeException */ public boolean load(RecordBatchDef def, ByteBuf buf) throws SchemaChangeException { // logger.debug("Loading record batch with def {} and data {}", def, buf); this.valueCount = def.getRecordCount(); boolean schemaChanged = schema == null; Map<MaterializedField, ValueVector> oldFields = Maps.newHashMap(); for (VectorWrapper<?> w : container) { ValueVector v = w.getValueVector(); oldFields.put(v.getField(), v); } VectorContainer newVectors = new VectorContainer(); List<FieldMetadata> fields = def.getFieldList(); int bufOffset = 0; for (FieldMetadata fmd : fields) { FieldDef fieldDef = fmd.getDef(); ValueVector v = oldFields.remove(fieldDef); if (v != null) { container.add(v); continue; } // if we arrive here, we didn't have a matching vector. schemaChanged = true; MaterializedField m = new MaterializedField(fieldDef); v = TypeHelper.getNewVector(m, allocator); if (fmd.getValueCount() == 0) { v.clear(); } else { v.load(fmd, buf.slice(bufOffset, fmd.getBufferLength())); } bufOffset += fmd.getBufferLength(); newVectors.add(v); } if (!oldFields.isEmpty()) { schemaChanged = true; for (ValueVector v : oldFields.values()) { v.close(); } } // rebuild the schema. SchemaBuilder b = BatchSchema.newBuilder(); for (VectorWrapper<?> v : newVectors) { b.addField(v.getField()); } b.setSelectionVectorMode(BatchSchema.SelectionVectorMode.NONE); this.schema = b.build(); container = newVectors; return schemaChanged; }
@Test public void testNullableFloat() { // Build an optional float field definition MajorType.Builder typeBuilder = MajorType.newBuilder(); FieldDef.Builder defBuilder = FieldDef.newBuilder(); typeBuilder.setMinorType(MinorType.FLOAT4).setMode(DataMode.OPTIONAL).setWidth(4); defBuilder.setMajorType(typeBuilder.build()); MaterializedField field = MaterializedField.create(defBuilder.build()); // Create a new value vector for 1024 integers NullableFloat4Vector v = (NullableFloat4Vector) TypeHelper.getNewVector(field, allocator); NullableFloat4Vector.Mutator m = v.getMutator(); v.allocateNew(1024); // Put and set a few values m.set(0, 100.1f); m.set(1, 101.2f); m.set(100, 102.3f); m.set(1022, 103.4f); m.set(1023, 104.5f); assertEquals(100.1f, v.getAccessor().get(0), 0); assertEquals(101.2f, v.getAccessor().get(1), 0); assertEquals(102.3f, v.getAccessor().get(100), 0); assertEquals(103.4f, v.getAccessor().get(1022), 0); assertEquals(104.5f, v.getAccessor().get(1023), 0); // Ensure null values throw { boolean b = false; try { v.getAccessor().get(3); } catch (AssertionError e) { b = true; } finally { if (!b) { assert false; } } } v.allocateNew(2048); { boolean b = false; try { v.getAccessor().get(0); } catch (AssertionError e) { b = true; } finally { if (!b) { assert false; } } } }