diff --git a/src/main/java/io/tiledb/java/api/Array.java b/src/main/java/io/tiledb/java/api/Array.java index a4c5c9f7..33608673 100644 --- a/src/main/java/io/tiledb/java/api/Array.java +++ b/src/main/java/io/tiledb/java/api/Array.java @@ -482,8 +482,7 @@ public HashMap nonEmptyDomain() throws TileDBError { public Pair getNonEmptyDomainFromIndex(long index) throws TileDBError { checkIsOpen(); try (Domain domain = schema.getDomain(); - NativeArray domainArray = - new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) { + NativeArray domainArray = new NativeArray(ctx, 2, domain.getDimension(index).getType())) { if (domain.getDimension(index).isVar()) return getNonEmptyDomainVarFromIndex(index); @@ -515,8 +514,7 @@ public Pair getNonEmptyDomainFromIndex(long index) throws TileDBError { public Pair getNonEmptyDomainFromName(String name) throws TileDBError { checkIsOpen(); try (Domain domain = schema.getDomain(); - NativeArray domainArray = - new NativeArray(ctx, 2 * (int) domain.getRank(), domain.getType())) { + NativeArray domainArray = new NativeArray(ctx, 2, domain.getDimension(name).getType())) { if (domain.getDimension(name).isVar()) return this.getNonEmptyDomainVarFromName(name); diff --git a/src/main/java/io/tiledb/java/api/Query.java b/src/main/java/io/tiledb/java/api/Query.java index 5a1dc287..a8b293bb 100644 --- a/src/main/java/io/tiledb/java/api/Query.java +++ b/src/main/java/io/tiledb/java/api/Query.java @@ -174,7 +174,7 @@ public synchronized Query addRange(int dimIdx, Object start, Object end) throws Datatype dimType; try (ArraySchema schema = array.getSchema(); Domain domain = schema.getDomain()) { - dimType = domain.getType(); + dimType = domain.getDimension(dimIdx).getType(); } // We use java type check here because we can not tell the difference between unsigned and @@ -215,7 +215,7 @@ public synchronized Query addRangeVar(int dimIdx, String start, String end) thro Datatype dimType; try (ArraySchema schema = array.getSchema(); Domain domain = schema.getDomain()) { - dimType = domain.getType(); + dimType = domain.getDimension(dimIdx).getType(); } Types.javaTypeCheck(start.getClass(), dimType.javaClass()); @@ -276,7 +276,7 @@ public synchronized Pair getRangeVar(int dimIdx, BigInteger rang Datatype dimType; try (ArraySchema schema = array.getSchema(); Domain domain = schema.getDomain()) { - dimType = domain.getType(); + dimType = domain.getDimension(dimIdx).getType(); } Pair size = this.getRangeVarSize(dimIdx, rangeIdx); @@ -325,7 +325,7 @@ public Pair getRange(int dimIdx, long rangeIdx) throws TileDBErr Datatype dimType; try (ArraySchema schema = array.getSchema(); Domain domain = schema.getDomain()) { - dimType = domain.getType(); + dimType = domain.getDimension(dimIdx).getType(); } SWIGTYPE_p_p_void startArrpp = tiledb.new_voidpArray(1); @@ -368,7 +368,7 @@ public synchronized Query setBuffer(String attr, NativeArray buffer) throws Tile try (ArraySchema schema = array.getSchema()) { try (Domain domain = schema.getDomain()) { if (attr.equals(tiledb.tiledb_coords())) { - Types.typeCheck(domain.getType(), buffer.getNativeType()); + Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType()); } else if (domain.hasDimension(attr)) { Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType()); } else { @@ -429,7 +429,7 @@ public synchronized Query setBuffer(String attr, NativeArray buffer, long buffer try (ArraySchema schema = array.getSchema()) { try (Domain domain = schema.getDomain()) { if (attr.equals(tiledb.tiledb_coords())) { - Types.typeCheck(domain.getType(), buffer.getNativeType()); + Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType()); } else if (domain.hasDimension(attr)) { Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType()); } else { @@ -492,7 +492,7 @@ public synchronized Query setBuffer(String attr, NativeArray offsets, NativeArra try (ArraySchema schema = array.getSchema()) { try (Domain domain = schema.getDomain()) { if (attr.equals(tiledb.tiledb_coords())) { - Types.typeCheck(domain.getType(), buffer.getNativeType()); + Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType()); } else if (domain.hasDimension(attr)) { Types.typeCheck(domain.getDimension(attr).getType(), buffer.getNativeType()); } else { @@ -748,6 +748,7 @@ public Query setCoordinates(NativeArray buffer) throws TileDBError { * @return The query result coordinate buffer. * @exception TileDBError A TileDB exception */ + @Deprecated public Object getCoordinates() throws TileDBError { return getBuffer(tiledb.tiledb_coords()); } diff --git a/src/test/java/io/tiledb/java/api/AsyncTest.java b/src/test/java/io/tiledb/java/api/AsyncTest.java index a7870e81..09bee2eb 100644 --- a/src/test/java/io/tiledb/java/api/AsyncTest.java +++ b/src/test/java/io/tiledb/java/api/AsyncTest.java @@ -42,10 +42,8 @@ public void test() throws Exception { public void arrayCreate() throws Exception { // Create getDimensions - Dimension rows = - new Dimension(ctx, "rows", Integer.class, new Pair(1, 4), 2); - Dimension cols = - new Dimension(ctx, "cols", Integer.class, new Pair(1, 4), 2); + Dimension rows = new Dimension(ctx, "rows", Integer.class, new Pair(1, 4), 2); + Dimension cols = new Dimension(ctx, "cols", Integer.class, new Pair(1, 4), 2); // Create and set getDomain Domain domain = new Domain(ctx); @@ -65,18 +63,20 @@ public void arrayCreate() throws Exception { } public void arrayWrite() throws Exception { + NativeArray d1_data = new NativeArray(ctx, new int[] {1, 2, 2, 4}, Integer.class); + NativeArray d2_data = new NativeArray(ctx, new int[] {1, 1, 2, 3}, Integer.class); + // Prepare cell buffers NativeArray data = new NativeArray(ctx, new int[] {1, 2, 3, 4}, Integer.class); - NativeArray coords_buff = - new NativeArray(ctx, new int[] {1, 1, 2, 1, 2, 2, 4, 3}, Integer.class); - // Create query Array array = new Array(ctx, arrayURI, TILEDB_WRITE); Query query = new Query(array); query.setLayout(TILEDB_GLOBAL_ORDER); + query.setBuffer("rows", d1_data); + query.setBuffer("cols", d2_data); query.setBuffer("a", data); - query.setCoordinates(coords_buff); + // Submit query query.submitAsync(); @@ -102,9 +102,13 @@ private void arrayRead() throws Exception { Query query = new Query(array, TILEDB_READ); query.setLayout(TILEDB_ROW_MAJOR); query.setBuffer( - "a", new NativeArray(ctx, max_sizes.get("a").getSecond().intValue(), Integer.class)); - query.setCoordinates( + "rows", new NativeArray(ctx, max_sizes.get(TILEDB_COORDS).getSecond().intValue(), Integer.class)); + query.setBuffer( + "cols", + new NativeArray(ctx, max_sizes.get(TILEDB_COORDS).getSecond().intValue(), Integer.class)); + query.setBuffer( + "a", new NativeArray(ctx, max_sizes.get("a").getSecond().intValue(), Integer.class)); // Submit query with callback query.submitAsync(new ReadCallback()); @@ -119,14 +123,16 @@ private void arrayRead() throws Exception { // Print cell values (assumes all getAttributes are read) HashMap> result_el = query.resultBufferElements(); + int[] rows = (int[]) query.getBuffer("rows"); + int[] cols = (int[]) query.getBuffer("cols"); int[] data = (int[]) query.getBuffer("a"); - int[] coords = (int[]) query.getCoordinates(); query.close(); array.close(); Assert.assertTrue(callbackCalled); - Assert.assertArrayEquals(coords, new int[] {1, 1, 2, 1, 2, 2, 4, 3}); + Assert.assertArrayEquals(rows, new int[] {1, 2, 2, 4}); + Assert.assertArrayEquals(cols, new int[] {1, 1, 2, 3}); Assert.assertArrayEquals(data, new int[] {1, 2, 3, 4}); } diff --git a/src/test/java/io/tiledb/java/api/FragmentsConsolidationTest.java b/src/test/java/io/tiledb/java/api/FragmentsConsolidationTest.java index 14e35dcc..b7df39a8 100644 --- a/src/test/java/io/tiledb/java/api/FragmentsConsolidationTest.java +++ b/src/test/java/io/tiledb/java/api/FragmentsConsolidationTest.java @@ -112,6 +112,8 @@ public void arrayWrite2() throws Exception { public void arrayWrite3() throws Exception { // Prepare cell buffers + NativeArray rows = new NativeArray(ctx, new int[] {1, 3}, Integer.class); + NativeArray cols = new NativeArray(ctx, new int[] {1, 4}, Integer.class); NativeArray data = new NativeArray(ctx, new int[] {201, 202}, Integer.class); NativeArray coords = new NativeArray(ctx, new int[] {1, 1, 3, 4}, Integer.class); @@ -120,8 +122,9 @@ public void arrayWrite3() throws Exception { Array array = new Array(ctx, arrayURI, TILEDB_WRITE); Query query = new Query(array); query.setLayout(TILEDB_UNORDERED); + query.setBuffer("rows", rows); + query.setBuffer("cols", cols); query.setBuffer("a", data); - query.setCoordinates(coords); // Submit query query.submit(); query.close(); @@ -140,24 +143,23 @@ private void arrayRead() throws Exception { query.setLayout(TILEDB_ROW_MAJOR); query.setSubarray(subarray); query.setBuffer("a", new NativeArray(ctx, 16, Integer.class)); - query.setCoordinates(new NativeArray(ctx, 32, Integer.class)); + query.setBuffer("rows", new NativeArray(ctx, 16, Integer.class)); + query.setBuffer("cols", new NativeArray(ctx, 16, Integer.class)); // Submit query query.submit(); // Print cell values (assumes all getAttributes are read) HashMap> result_el = query.resultBufferElements(); + int[] rows = (int[]) query.getBuffer("rows"); + int[] cols = (int[]) query.getBuffer("cols"); int[] data = (int[]) query.getBuffer("a"); - int[] coords = (int[]) query.getCoordinates(); query.close(); array.close(); - Assert.assertArrayEquals( - coords, - new int[] { - 1, 1, 1, 2, 1, 3, 1, 4, 2, 1, 2, 2, 2, 3, 2, 4, 3, 1, 3, 2, 3, 3, 3, 4, 4, 1, 4, 2, 4, 3, - 4, 4 - }); + Assert.assertArrayEquals(rows, new int[] {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4}); + Assert.assertArrayEquals(cols, new int[] {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}); + Assert.assertArrayEquals( data, new int[] { diff --git a/src/test/java/io/tiledb/java/api/HeterogeneousSparseTest.java b/src/test/java/io/tiledb/java/api/HeterogeneousSparseTest.java new file mode 100644 index 00000000..2cb2c12d --- /dev/null +++ b/src/test/java/io/tiledb/java/api/HeterogeneousSparseTest.java @@ -0,0 +1,141 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2020 TileDB, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package io.tiledb.java.api; + +import static io.tiledb.java.api.ArrayType.TILEDB_SPARSE; +import static io.tiledb.java.api.Layout.TILEDB_GLOBAL_ORDER; +import static io.tiledb.java.api.Layout.TILEDB_ROW_MAJOR; +import static io.tiledb.java.api.QueryType.TILEDB_READ; +import static io.tiledb.java.api.QueryType.TILEDB_WRITE; + +import java.nio.file.Files; +import java.nio.file.Paths; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +@SuppressWarnings("ALL") +public class HeterogeneousSparseTest { + + private Context ctx; + private String arrayURI = "my_sparse_array"; + + @Before + public void setup() throws Exception { + ctx = new Context(); + if (Files.exists(Paths.get(arrayURI))) { + TileDBObject.remove(ctx, arrayURI); + } + } + + @After + public void teardown() throws Exception { + if (Files.exists(Paths.get(arrayURI))) { + TileDBObject.remove(ctx, arrayURI); + } + } + + @Test + public void testQuickstartSparse() throws Exception { + arrayCreate(); + arrayWrite(); + arrayRead(); + } + + public void arrayCreate() throws Exception { + Dimension d1 = new Dimension(ctx, "d1", Datatype.TILEDB_STRING_ASCII, null, null); + Dimension d2 = new Dimension(ctx, "d2", Datatype.TILEDB_INT32, new Pair(0, 100), 2); + + // Create and set getDomain + Domain domain = new Domain(ctx); + domain.addDimension(d1).addDimension(d2); + + // Add two attributes "a1" and "a2", so each (i,j) cell can store + // a character on "a1" and a vector of two floats on "a2". + Attribute a1 = new Attribute(ctx, "a1", Integer.class); + + ArraySchema schema = new ArraySchema(ctx, TILEDB_SPARSE); + schema.setTileOrder(TILEDB_ROW_MAJOR); + schema.setCellOrder(TILEDB_ROW_MAJOR); + schema.setDomain(domain); + schema.addAttribute(a1); + + Array.create(arrayURI, schema); + } + + public void arrayWrite() throws Exception { + NativeArray d1_data = new NativeArray(ctx, "aabbccddee", Datatype.TILEDB_STRING_ASCII); + NativeArray d1_off = new NativeArray(ctx, new long[] {0, 2, 4, 6, 8}, Datatype.TILEDB_UINT64); + NativeArray d2_data = new NativeArray(ctx, new int[] {1, 4, 9, 10, 12}, Datatype.TILEDB_INT32); + + // Prepare cell buffers + NativeArray a1 = new NativeArray(ctx, new int[] {1, 2, 3, 4, 5}, Integer.class); + + // Create query + Array array = new Array(ctx, arrayURI, TILEDB_WRITE); + Query query = new Query(array); + query.setLayout(TILEDB_GLOBAL_ORDER); + + query.setBuffer("d1", d1_off, d1_data); + query.setBuffer("d2", d2_data); + query.setBuffer("a1", a1); + + // Submit query + query.submit(); + + query.finalizeQuery(); + query.close(); + array.close(); + } + + private void arrayRead() throws Exception { + NativeArray d_data = new NativeArray(ctx, 20, Datatype.TILEDB_STRING_ASCII); + NativeArray d_off = new NativeArray(ctx, 20, Datatype.TILEDB_UINT64); + + NativeArray d2_data = new NativeArray(ctx, 20, Datatype.TILEDB_INT32); + + Query q = new Query(new Array(ctx, arrayURI), TILEDB_READ); + q.setLayout(TILEDB_GLOBAL_ORDER); + + q.setBuffer("d1", d_off, d_data); + q.setBuffer("d2", d2_data); + + q.addRangeVar(0, "a", "z"); + + q.submit(); + + byte[] data = (byte[]) q.getBuffer("d1"); + long[] offsets = q.getVarBuffer("d1"); + int[] d2 = (int[]) q.getBuffer("d2"); + + String[] results = new String[offsets.length]; + + results = Util.bytesToStrings(offsets, data); + + Assert.assertArrayEquals(results, new String[] {"aa", "bb", "cc", "dd", "ee"}); + Assert.assertArrayEquals(d2, new int[] {1, 4, 9, 10, 12}); + } +} diff --git a/src/test/java/io/tiledb/java/api/QuickstartSparseTest.java b/src/test/java/io/tiledb/java/api/QuickstartSparseTest.java index fc36308e..03ea1716 100644 --- a/src/test/java/io/tiledb/java/api/QuickstartSparseTest.java +++ b/src/test/java/io/tiledb/java/api/QuickstartSparseTest.java @@ -106,6 +106,11 @@ public void arrayCreate() throws Exception { } public void arrayWrite() throws Exception { + NativeArray d1_buffer = + new NativeArray(ctx, new long[] {1, 1, 1, 2, 3, 4, 3, 3}, Datatype.TILEDB_INT64); + NativeArray d2_buffer = + new NativeArray(ctx, new long[] {1, 2, 4, 3, 1, 2, 3, 4}, Datatype.TILEDB_INT64); + // Prepare cell buffers NativeArray a1_data = new NativeArray(ctx, new int[] {0, 1, 2, 3, 4, 5, 6, 7}, Integer.class); NativeArray a2_offsets = @@ -121,18 +126,15 @@ public void arrayWrite() throws Exception { }, Float.class); - NativeArray coords_buff = - new NativeArray( - ctx, new long[] {1, 1, 1, 2, 1, 4, 2, 3, 3, 1, 4, 2, 3, 3, 3, 4}, Long.class); - // Create query Array my_sparse_array = new Array(ctx, arrayURI, TILEDB_WRITE); Query query = new Query(my_sparse_array); query.setLayout(TILEDB_GLOBAL_ORDER); + query.setBuffer("d1", d1_buffer); + query.setBuffer("d2", d2_buffer); query.setBuffer("a1", a1_data); query.setBuffer("a2", a2_offsets, buffer_var_a2); query.setBuffer("a3", buffer_a3); - query.setCoordinates(coords_buff); // Submit query query.submit(); @@ -173,6 +175,14 @@ private void arrayRead() throws Exception { Query query = new Query(my_sparse_array, TILEDB_READ); query.setLayout(TILEDB_GLOBAL_ORDER); query.setSubarray(subarray); + + query.setBuffer( + "d1", + new NativeArray(ctx, max_sizes.get(TILEDB_COORDS).getSecond().intValue(), Long.class)); + + query.setBuffer( + "d2", + new NativeArray(ctx, max_sizes.get(TILEDB_COORDS).getSecond().intValue(), Long.class)); query.setBuffer( "a1", new NativeArray(ctx, max_sizes.get("a1").getSecond().intValue(), Integer.class)); query.setBuffer( @@ -181,8 +191,6 @@ private void arrayRead() throws Exception { new NativeArray(ctx, max_sizes.get("a2").getSecond().intValue(), String.class)); query.setBuffer( "a3", new NativeArray(ctx, max_sizes.get("a3").getSecond().intValue(), Float.class)); - query.setCoordinates( - new NativeArray(ctx, max_sizes.get(TILEDB_COORDS).getSecond().intValue(), Long.class)); // Submit query query.submit(); @@ -190,14 +198,16 @@ private void arrayRead() throws Exception { // Print cell values (assumes all getAttributes are read) HashMap> result_el = query.resultBufferElements(); + long[] d1 = (long[]) query.getBuffer("d1"); + long[] d2 = (long[]) query.getBuffer("d2"); int[] a1_buff = (int[]) query.getBuffer("a1"); long[] a2_offsets = (long[]) query.getVarBuffer("a2"); byte[] a2_data = (byte[]) query.getBuffer("a2"); float[] a3_buff = (float[]) query.getBuffer("a3"); - long[] coords = (long[]) query.getBuffer(TILEDB_COORDS); // check coords - Assert.assertArrayEquals(coords, new long[] {1, 1, 1, 2, 1, 4, 2, 3, 3, 1, 4, 2, 3, 3, 3, 4}); + Assert.assertArrayEquals(d1, new long[] {1, 1, 1, 2, 3, 4, 3, 3}); + Assert.assertArrayEquals(d2, new long[] {1, 2, 4, 3, 1, 2, 3, 4}); // check a1 Assert.assertArrayEquals(a1_buff, new int[] {0, 1, 2, 3, 4, 5, 6, 7});