Skip to content

Commit 1833e21

Browse files
committed
Fixed issues in set fill values for var-sized attributes
1 parent a8a0304 commit 1833e21

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

src/main/java/io/tiledb/java/api/Attribute.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,18 @@ public void setFillValue(NativeArray value, BigInteger size) throws TileDBError
295295
* @throws TileDBError
296296
*/
297297
public void setFillValue(Object value) throws TileDBError {
298-
NativeArray array = new NativeArray(ctx, this.type.getNativeSize(), this.type.javaClass());
298+
int size;
299+
300+
if (value instanceof String) size = ((String) value).length();
301+
else size = this.type.getNativeSize();
302+
303+
NativeArray array = new NativeArray(ctx, size, this.type.javaClass());
299304
array.setItem(0, value);
300305

301306
try {
302307
ctx.handleError(
303308
tiledb.tiledb_attribute_set_fill_value(
304-
ctx.getCtxp(),
305-
attributep,
306-
array.toVoidPointer(),
307-
BigInteger.valueOf(this.type.getNativeSize())));
309+
ctx.getCtxp(), attributep, array.toVoidPointer(), BigInteger.valueOf((long) size)));
308310
} catch (TileDBError err) {
309311
throw err;
310312
}
@@ -328,10 +330,12 @@ public Pair<Object, Long> getFillValue() throws TileDBError {
328330
SWIGTYPE_p_p_void v = tiledb.new_voidpArray(1);
329331

330332
ctx.handleError(tiledb.tiledb_attribute_get_fill_value(ctx.getCtxp(), attributep, v, size));
333+
int extractedSize = tiledb.ullp_value(size).intValue();
331334

332335
Object fillValue;
333-
try (NativeArray fillValueArray = new NativeArray(ctx, getType(), v, 1)) {
334-
fillValue = fillValueArray.getItem(0);
336+
try (NativeArray fillValueArray = new NativeArray(ctx, getType(), v, extractedSize)) {
337+
if (this.isVar()) fillValue = fillValueArray.toJavaArray(extractedSize);
338+
else fillValue = fillValueArray.getItem(0);
335339
}
336340

337341
return new Pair(fillValue, tiledb.ullp_value(size));
@@ -378,8 +382,12 @@ public void setFillValueNullable(NativeArray value, BigInteger size, boolean val
378382
* @throws TileDBError
379383
*/
380384
public void setFillValueNullable(Object value, boolean valid) throws TileDBError {
385+
int size;
386+
387+
if (value instanceof String) size = ((String) value).length();
388+
else size = this.type.getNativeSize();
381389

382-
NativeArray valueArray = new NativeArray(ctx, this.type.getNativeSize(), this.type.javaClass());
390+
NativeArray valueArray = new NativeArray(ctx, size, this.type.javaClass());
383391
valueArray.setItem(0, value);
384392

385393
try {
@@ -388,7 +396,7 @@ public void setFillValueNullable(Object value, boolean valid) throws TileDBError
388396
ctx.getCtxp(),
389397
attributep,
390398
valueArray.toVoidPointer(),
391-
BigInteger.valueOf(this.type.getNativeSize()),
399+
BigInteger.valueOf(size),
392400
valid ? (short) 1 : (short) 0));
393401
} catch (TileDBError err) {
394402
throw err;
@@ -419,9 +427,12 @@ public Pair<Object, Pair<Long, Boolean>> getFillValueNullable() throws TileDBErr
419427
tiledb.tiledb_attribute_get_fill_value_nullable(
420428
ctx.getCtxp(), attributep, v, size, valid));
421429

430+
int extractedSize = tiledb.ullp_value(size).intValue();
431+
422432
Object fillValue;
423-
try (NativeArray fillValueArray = new NativeArray(ctx, getType(), v, 1)) {
424-
fillValue = fillValueArray.getItem(0);
433+
try (NativeArray fillValueArray = new NativeArray(ctx, getType(), v, extractedSize)) {
434+
if (this.isVar()) fillValue = fillValueArray.toJavaArray(extractedSize);
435+
else fillValue = fillValueArray.getItem(0);
425436
}
426437

427438
boolean validBoolean = validArr.getUint8_tArray().getitem(0) == 0 ? false : true;

src/test/java/io/tiledb/java/api/AttributeTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ public void testAttributeSetFillValue() throws Exception {
9292
Assert.assertEquals(
9393
BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValue().getSecond());
9494
}
95+
96+
try (Context ctx = new Context();
97+
Attribute a = new Attribute(ctx, "a2", Datatype.TILEDB_CHAR)) {
98+
99+
a.setFillValue((byte) 'c');
100+
101+
Assert.assertEquals((byte) 'c', a.getFillValue().getFirst());
102+
Assert.assertEquals(
103+
BigInteger.valueOf(a.getType().getNativeSize()), a.getFillValue().getSecond());
104+
}
105+
106+
try (Context ctx = new Context();
107+
Attribute a = new Attribute(ctx, "a2", Datatype.TILEDB_STRING_ASCII)) {
108+
a.setCellVar();
109+
110+
String str = "abcdef";
111+
a.setFillValue(str);
112+
113+
Assert.assertEquals(str, new String((byte[]) a.getFillValue().getFirst()));
114+
Assert.assertEquals(BigInteger.valueOf(str.length()), a.getFillValue().getSecond());
115+
}
95116
}
96117

97118
@Test
@@ -140,6 +161,19 @@ public void testAttributeSetFillValueNullable() throws Exception {
140161

141162
Assert.assertEquals(false, a.getFillValueNullable().getSecond().getSecond());
142163
}
164+
165+
try (Context ctx = new Context();
166+
Attribute a = new Attribute(ctx, "a2", Datatype.TILEDB_STRING_ASCII)) {
167+
a.setNullable(true);
168+
a.setCellVar();
169+
170+
String str = "abcdef";
171+
a.setFillValueNullable(str, true);
172+
173+
Assert.assertEquals(str, new String((byte[]) a.getFillValueNullable().getFirst()));
174+
Assert.assertEquals(
175+
BigInteger.valueOf(str.length()), a.getFillValueNullable().getSecond().getFirst());
176+
}
143177
}
144178

145179
@Test

0 commit comments

Comments
 (0)