Search in sources :

Example 1 with Layout

use of io.tiledb.java.api.Layout in project TileDB-Spark by TileDB-Inc.

the class TileDBDataSourceOptionsTest method testInvalidLayoutOptions.

@Test(expected = IllegalArgumentException.class)
public void testInvalidLayoutOptions() throws Exception {
    HashMap<String, String> optionMap = new HashMap<>();
    optionMap.put("order", "bad-layout-option");
    TileDBDataSourceOptions options = new TileDBDataSourceOptions(new DataSourceOptions(optionMap));
    Optional<Layout> layout = options.getArrayLayout();
}
Also used : HashMap(java.util.HashMap) Layout(io.tiledb.java.api.Layout) DataSourceOptions(org.apache.spark.sql.sources.v2.DataSourceOptions) Test(org.junit.Test)

Example 2 with Layout

use of io.tiledb.java.api.Layout in project TileDB-Spark by TileDB-Inc.

the class TileDBBatchWrite method writeArraySchema.

private static void writeArraySchema(Context ctx, URI uri, StructType sparkSchema, TileDBDataSourceOptions options) throws TileDBError {
    ArrayType type = ArrayType.TILEDB_SPARSE;
    try (ArraySchema arraySchema = new ArraySchema(ctx, type);
        Domain domain = new Domain(ctx)) {
        String[] dimNames = TileDBWriteSchema.getSchemaDimensionOptions(sparkSchema, options);
        StructField[] sparkFields = sparkSchema.fields();
        for (int dimIdx = 0; dimIdx < dimNames.length; dimIdx++) {
            String dimName = dimNames[dimIdx];
            int idx = sparkSchema.fieldIndex(dimName);
            try (Dimension dim = TileDBWriteSchema.toDimension(ctx, dimName, dimIdx, sparkFields[idx].dataType(), options)) {
                domain.addDimension(dim);
            }
        }
        // set domain
        arraySchema.setDomain(domain);
        // add attributes
        for (StructField field : sparkFields) {
            // skip over dims
            if (Arrays.stream(dimNames).anyMatch(field.name()::equals)) {
                continue;
            }
            try (Attribute attr = TileDBWriteSchema.toAttribute(ctx, field, options)) {
                arraySchema.addAttribute(attr);
            }
        }
        // set schema tile / layouts and remaining attributes
        Optional<Layout> schemaCellOrder = options.getSchemaCellOrder();
        if (schemaCellOrder.isPresent()) {
            arraySchema.setCellOrder(schemaCellOrder.get());
        }
        Optional<Layout> schemaTileOrder = options.getSchemaTileOrder();
        if (schemaTileOrder.isPresent()) {
            arraySchema.setTileOrder(schemaTileOrder.get());
        }
        // schema filters
        Optional<List<Pair<String, Integer>>> coordsFilters = options.getSchemaCoordsFilterList();
        if (coordsFilters.isPresent()) {
            try (FilterList filterList = TileDBWriteSchema.createTileDBFilterList(ctx, coordsFilters.get())) {
                arraySchema.setCoodsFilterList(filterList);
            }
        }
        Optional<List<Pair<String, Integer>>> offsetsFilters = options.getSchemaOffsetsFilterList();
        if (coordsFilters.isPresent()) {
            try (FilterList filterList = TileDBWriteSchema.createTileDBFilterList(ctx, offsetsFilters.get())) {
                arraySchema.setOffsetsFilterList(filterList);
            }
        }
        // set capacity
        Optional<Long> schemaCapacity = options.getSchemaCapacity();
        if (schemaCapacity.isPresent()) {
            arraySchema.setCapacity(schemaCapacity.get());
        }
        // set allows dups
        if (options.getSchemaAllowDups().isPresent() && options.getSchemaAllowDups().get())
            arraySchema.setAllowDups(1);
        arraySchema.check();
        Array.create(uri.toString(), arraySchema);
    }
}
Also used : Attribute(io.tiledb.java.api.Attribute) FilterList(io.tiledb.java.api.FilterList) Dimension(io.tiledb.java.api.Dimension) ArrayType(io.tiledb.java.api.ArrayType) ArraySchema(io.tiledb.java.api.ArraySchema) StructField(org.apache.spark.sql.types.StructField) Layout(io.tiledb.java.api.Layout) FilterList(io.tiledb.java.api.FilterList) List(java.util.List) Domain(io.tiledb.java.api.Domain)

Example 3 with Layout

use of io.tiledb.java.api.Layout in project TileDB-Spark by TileDB-Inc.

the class TileDBDataSourceOptionsTest method testValidArrayLayoutOptions.

@Test
public void testValidArrayLayoutOptions() throws Exception {
    String[] validLayoutStrings = new String[] { "row-major", "TILEDB_ROW_MAJOR", "col-major", "TILEDB_COL_MAJOR", "unordered", "TILEDB_UNORDERED" };
    Layout[] expectedLayouts = new Layout[] { Layout.TILEDB_ROW_MAJOR, Layout.TILEDB_ROW_MAJOR, Layout.TILEDB_COL_MAJOR, Layout.TILEDB_COL_MAJOR, Layout.TILEDB_UNORDERED, Layout.TILEDB_UNORDERED };
    for (int i = 0; i < validLayoutStrings.length; i++) {
        HashMap<String, String> optionMap = new HashMap<>();
        optionMap.put("order", validLayoutStrings[i]);
        TileDBDataSourceOptions options = new TileDBDataSourceOptions(new DataSourceOptions(optionMap));
        Optional<Layout> layout = options.getArrayLayout();
        Assert.assertTrue(layout.isPresent());
        Assert.assertEquals(expectedLayouts[i], layout.get());
    }
}
Also used : Layout(io.tiledb.java.api.Layout) HashMap(java.util.HashMap) DataSourceOptions(org.apache.spark.sql.sources.v2.DataSourceOptions) Test(org.junit.Test)

Aggregations

Layout (io.tiledb.java.api.Layout)3 HashMap (java.util.HashMap)2 DataSourceOptions (org.apache.spark.sql.sources.v2.DataSourceOptions)2 Test (org.junit.Test)2 ArraySchema (io.tiledb.java.api.ArraySchema)1 ArrayType (io.tiledb.java.api.ArrayType)1 Attribute (io.tiledb.java.api.Attribute)1 Dimension (io.tiledb.java.api.Dimension)1 Domain (io.tiledb.java.api.Domain)1 FilterList (io.tiledb.java.api.FilterList)1 List (java.util.List)1 StructField (org.apache.spark.sql.types.StructField)1