@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((def == null) ? 0 : def.hashCode()); return result; }
public MaterializedField getOtherNullableVersion() { MajorType mt = def.getMajorType(); DataMode newDataMode = null; switch (mt.getMode()) { case OPTIONAL: newDataMode = DataMode.REQUIRED; break; case REQUIRED: newDataMode = DataMode.OPTIONAL; break; default: throw new UnsupportedOperationException(); } return new MaterializedField( def.toBuilder().setMajorType(mt.toBuilder().setMode(newDataMode).build()).build()); }
@Test public void testFixedType() { // Build a required uint field definition MajorType.Builder typeBuilder = MajorType.newBuilder(); FieldDef.Builder defBuilder = FieldDef.newBuilder(); typeBuilder.setMinorType(MinorType.UINT4).setMode(DataMode.REQUIRED).setWidth(4); defBuilder.setMajorType(typeBuilder.build()); MaterializedField field = MaterializedField.create(defBuilder.build()); // Create a new value vector for 1024 integers UInt4Vector v = new UInt4Vector(field, allocator); UInt4Vector.Mutator m = v.getMutator(); v.allocateNew(1024); // Put and set a few values m.set(0, 100); m.set(1, 101); m.set(100, 102); m.set(1022, 103); m.set(1023, 104); assertEquals(100, v.getAccessor().get(0)); assertEquals(101, v.getAccessor().get(1)); assertEquals(102, v.getAccessor().get(100)); assertEquals(103, v.getAccessor().get(1022)); assertEquals(104, v.getAccessor().get(1023)); // Ensure unallocated space returns 0 assertEquals(0, v.getAccessor().get(3)); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; MaterializedField other = (MaterializedField) obj; if (def == null) { if (other.def != null) return false; } else if (!def.equals(other.def)) return false; return true; }
@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; } } } }
public String getName() { StringBuilder sb = new StringBuilder(); boolean first = true; for (NamePart np : def.getNameList()) { if (np.getType() == Type.ARRAY) { sb.append("[]"); } else { if (first) { first = false; } else { sb.append("."); } sb.append(np.getName()); } } return sb.toString(); }
public boolean matches(SchemaPath path) { Iterator<NamePart> iter = def.getNameList().iterator(); for (PathSegment p = path.getRootSegment(); ; p = p.getChild()) { if (p == null) break; if (!iter.hasNext()) return false; NamePart n = iter.next(); if (p.isArray()) { if (n.getType() == Type.ARRAY) continue; return false; } else { if (p.getNameSegment().getPath().equals(n.getName())) continue; return false; } } // we've reviewed all path segments. confirm that we don't have any extra name parts. return !iter.hasNext(); }
@Test public void testBitVector() { // Build a required boolean field definition MajorType.Builder typeBuilder = MajorType.newBuilder(); FieldDef.Builder defBuilder = FieldDef.newBuilder(); typeBuilder.setMinorType(MinorType.BIT).setMode(DataMode.REQUIRED).setWidth(4); defBuilder.setMajorType(typeBuilder.build()); MaterializedField field = MaterializedField.create(defBuilder.build()); // Create a new value vector for 1024 integers BitVector v = new BitVector(field, allocator); BitVector.Mutator m = v.getMutator(); v.allocateNew(1024); // Put and set a few values m.set(0, 1); m.set(1, 0); m.set(100, 0); m.set(1022, 1); assertEquals(1, v.getAccessor().get(0)); assertEquals(0, v.getAccessor().get(1)); assertEquals(0, v.getAccessor().get(100)); assertEquals(1, v.getAccessor().get(1022)); // test setting the same value twice m.set(0, 1); m.set(0, 1); m.set(1, 0); m.set(1, 0); assertEquals(1, v.getAccessor().get(0)); assertEquals(0, v.getAccessor().get(1)); // test toggling the values m.set(0, 0); m.set(1, 1); assertEquals(0, v.getAccessor().get(0)); assertEquals(1, v.getAccessor().get(1)); // Ensure unallocated space returns 0 assertEquals(0, v.getAccessor().get(3)); }
@Test public void testNullableVarLen2() { // Build an optional varchar field definition MajorType.Builder typeBuilder = MajorType.newBuilder(); FieldDef.Builder defBuilder = FieldDef.newBuilder(); typeBuilder.setMinorType(MinorType.VARCHAR).setMode(DataMode.OPTIONAL).setWidth(2); defBuilder.setMajorType(typeBuilder.build()); MaterializedField field = MaterializedField.create(defBuilder.build()); // Create a new value vector for 1024 integers NullableVarCharVector v = new NullableVarCharVector(field, allocator); NullableVarCharVector.Mutator m = v.getMutator(); v.allocateNew(1024 * 10, 1024); // Create and set 3 sample strings String str1 = new String("AAAAA1"); String str2 = new String("BBBBBBBBB2"); String str3 = new String("CCCC3"); m.set(0, str1.getBytes(Charset.forName("UTF-8"))); m.set(1, str2.getBytes(Charset.forName("UTF-8"))); m.set(2, str3.getBytes(Charset.forName("UTF-8"))); // Check the sample strings assertEquals(str1, new String(v.getAccessor().get(0), Charset.forName("UTF-8"))); assertEquals(str2, new String(v.getAccessor().get(1), Charset.forName("UTF-8"))); assertEquals(str3, new String(v.getAccessor().get(2), Charset.forName("UTF-8"))); // Ensure null value throws boolean b = false; try { v.getAccessor().get(3); } catch (AssertionError e) { b = true; } finally { if (!b) { assert false; } } }
public DataMode getDataMode() { return def.getMajorType().getMode(); }
public boolean isNullable() { return def.getMajorType().getMode() == DataMode.OPTIONAL; }
public MajorType getType() { return def.getMajorType(); }
public int getWidth() { return def.getMajorType().getWidth(); }
public static MaterializedField create(SchemaPath path, MajorType type) { FieldDef.Builder b = FieldDef.newBuilder(); b.setMajorType(type); addSchemaPathToFieldDef(path, b); return create(b.build()); }
public MaterializedField clone(FieldReference ref) { return create(ref, def.getMajorType()); }
@Override public String toString() { return "MaterializedField [" + def.toString() + "]"; }
@Test public void testNullableFixedType() { // Build an optional uint field definition MajorType.Builder typeBuilder = MajorType.newBuilder(); FieldDef.Builder defBuilder = FieldDef.newBuilder(); typeBuilder.setMinorType(MinorType.UINT4).setMode(DataMode.OPTIONAL).setWidth(4); defBuilder.setMajorType(typeBuilder.build()); MaterializedField field = MaterializedField.create(defBuilder.build()); // Create a new value vector for 1024 integers NullableUInt4Vector v = new NullableUInt4Vector(field, allocator); NullableUInt4Vector.Mutator m = v.getMutator(); v.allocateNew(1024); // Put and set a few values m.set(0, 100); m.set(1, 101); m.set(100, 102); m.set(1022, 103); m.set(1023, 104); assertEquals(100, v.getAccessor().get(0)); assertEquals(101, v.getAccessor().get(1)); assertEquals(102, v.getAccessor().get(100)); assertEquals(103, v.getAccessor().get(1022)); assertEquals(104, v.getAccessor().get(1023)); // 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; } } } m.set(0, 100); m.set(1, 101); m.set(100, 102); m.set(1022, 103); m.set(1023, 104); assertEquals(100, v.getAccessor().get(0)); assertEquals(101, v.getAccessor().get(1)); assertEquals(102, v.getAccessor().get(100)); assertEquals(103, v.getAccessor().get(1022)); assertEquals(104, v.getAccessor().get(1023)); // Ensure null values throw { boolean b = false; try { v.getAccessor().get(3); } catch (AssertionError e) { b = true; } finally { if (!b) { assert false; } } } }