Search in sources :

Example 1 with QueryConstraints

use of org.locationtech.geowave.core.store.query.constraints.QueryConstraints in project geowave by locationtech.

the class BasicDataTypeAdapterExample method querySpatial.

private void querySpatial() {
    try {
        // This bounding box represents approximately Europe, so only the European POIs will be
        // queried
        final String queryPolygonDefinition = "POLYGON (( " + "-10.55 35.96, " + "-10.55 71.30, " + "56.16 71.30, " + "56.16 35.96, " + "-10.55 35.96" + "))";
        final Geometry queryPolygon = new WKTReader(JTSFactoryFinder.getGeometryFactory()).read(queryPolygonDefinition);
        final QueryConstraints queryConstraints = new SpatialTemporalConstraintsBuilderImpl().spatialConstraints(queryPolygon).build();
        // Query the POI adapter
        final Query<POI> query = QueryBuilder.newBuilder(POI.class).addTypeName(adapter.getTypeName()).indexName(spatialIndex.getName()).constraints(queryConstraints).build();
        System.out.println("Executing query on POI adapter, expecting to match Roman Colosseum and Eiffel Tower...");
        try (final CloseableIterator<POI> iterator = dataStore.query(query)) {
            while (iterator.hasNext()) {
                System.out.println("Query match: " + iterator.next().getName());
            }
        }
        // Now query the annotated POI adapter
        final Query<AnnotatedPOI> annotatedQuery = QueryBuilder.newBuilder(AnnotatedPOI.class).addTypeName(annotatedAdapter.getTypeName()).indexName(spatialIndex.getName()).constraints(queryConstraints).build();
        System.out.println("Executing query on Annotated POI adapter, expecting to match Roman Colosseum and Eiffel Tower...");
        try (final CloseableIterator<AnnotatedPOI> iterator = dataStore.query(annotatedQuery)) {
            while (iterator.hasNext()) {
                System.out.println("Query match: " + iterator.next().getName());
            }
        }
    } catch (FactoryRegistryException | ParseException e) {
    }
}
Also used : QueryConstraints(org.locationtech.geowave.core.store.query.constraints.QueryConstraints) SpatialTemporalConstraintsBuilderImpl(org.locationtech.geowave.core.geotime.store.query.SpatialTemporalConstraintsBuilderImpl) WKTReader(org.locationtech.jts.io.WKTReader) Geometry(org.locationtech.jts.geom.Geometry) FactoryRegistryException(org.geotools.util.factory.FactoryRegistryException) ParseException(org.locationtech.jts.io.ParseException)

Example 2 with QueryConstraints

use of org.locationtech.geowave.core.store.query.constraints.QueryConstraints in project geowave by locationtech.

the class GeoWaveSparkIngestIT method testBasicSparkIngest.

@Test
public void testBasicSparkIngest() throws Exception {
    // ingest test points
    TestUtils.testSparkIngest(dataStore, DimensionalityType.SPATIAL, S3URL, GDELT_INPUT_FILES, "gdelt");
    final DataStatisticsStore statsStore = dataStore.createDataStatisticsStore();
    final PersistentAdapterStore adapterStore = dataStore.createAdapterStore();
    final InternalDataAdapter<?>[] adapters = adapterStore.getAdapters();
    for (final InternalDataAdapter<?> internalDataAdapter : adapters) {
        final FeatureDataAdapter adapter = (FeatureDataAdapter) internalDataAdapter.getAdapter();
        // query by the full bounding box, make sure there is more than
        // 0 count and make sure the count matches the number of results
        final BoundingBoxValue bboxValue = InternalStatisticsHelper.getFieldStatistic(statsStore, BoundingBoxStatistic.STATS_TYPE, adapter.getTypeName(), adapter.getFeatureType().getGeometryDescriptor().getLocalName());
        final CountValue count = InternalStatisticsHelper.getDataTypeStatistic(statsStore, CountStatistic.STATS_TYPE, adapter.getTypeName());
        // then query it
        final GeometryFactory factory = new GeometryFactory();
        final Envelope env = new Envelope(bboxValue.getMinX(), bboxValue.getMaxX(), bboxValue.getMinY(), bboxValue.getMaxY());
        final Geometry spatialFilter = factory.toGeometry(env);
        final QueryConstraints query = new ExplicitSpatialQuery(spatialFilter);
        final int resultCount = testQuery(adapter, query);
        assertTrue("'" + adapter.getTypeName() + "' adapter must have at least one element in its statistic", count.getValue() > 0);
        assertEquals("'" + adapter.getTypeName() + "' adapter should have the same results from a spatial query of '" + env + "' as its total count statistic", count.getValue().intValue(), resultCount);
        assertEquals("'" + adapter.getTypeName() + "' adapter entries ingested does not match expected count", new Integer(GDELT_COUNT), new Integer(resultCount));
    }
    // Clean up
    TestUtils.deleteAll(dataStore);
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) QueryConstraints(org.locationtech.geowave.core.store.query.constraints.QueryConstraints) BoundingBoxValue(org.locationtech.geowave.core.geotime.store.statistics.BoundingBoxStatistic.BoundingBoxValue) Envelope(org.locationtech.jts.geom.Envelope) DataStatisticsStore(org.locationtech.geowave.core.store.statistics.DataStatisticsStore) Geometry(org.locationtech.jts.geom.Geometry) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) ExplicitSpatialQuery(org.locationtech.geowave.core.geotime.store.query.ExplicitSpatialQuery) CountValue(org.locationtech.geowave.core.store.statistics.adapter.CountStatistic.CountValue) InternalDataAdapter(org.locationtech.geowave.core.store.adapter.InternalDataAdapter) FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter) Test(org.junit.Test)

Example 3 with QueryConstraints

use of org.locationtech.geowave.core.store.query.constraints.QueryConstraints in project geowave by locationtech.

the class BasicKafkaIT method testBasicIngestGpx.

@Test
public void testBasicIngestGpx() throws Exception {
    KafkaTestUtils.testKafkaStage(OSM_GPX_INPUT_DIR);
    KafkaTestUtils.testKafkaIngest(dataStorePluginOptions, false, OSM_GPX_INPUT_DIR);
    final DataStatisticsStore statsStore = dataStorePluginOptions.createDataStatisticsStore();
    final PersistentAdapterStore adapterStore = dataStorePluginOptions.createAdapterStore();
    int adapterCount = 0;
    final InternalDataAdapter<?>[] adapters = adapterStore.getAdapters();
    for (final InternalDataAdapter<?> internalDataAdapter : adapters) {
        final FeatureDataAdapter adapter = (FeatureDataAdapter) internalDataAdapter.getAdapter();
        final BoundingBoxValue bboxValue = InternalStatisticsHelper.getFieldStatistic(statsStore, BoundingBoxStatistic.STATS_TYPE, adapter.getTypeName(), adapter.getFeatureType().getGeometryDescriptor().getLocalName());
        final CountValue count = InternalStatisticsHelper.getDataTypeStatistic(statsStore, CountStatistic.STATS_TYPE, adapter.getTypeName());
        // then query it
        final GeometryFactory factory = new GeometryFactory();
        final Envelope env = new Envelope(bboxValue.getMinX(), bboxValue.getMaxX(), bboxValue.getMinY(), bboxValue.getMaxY());
        final Geometry spatialFilter = factory.toGeometry(env);
        final QueryConstraints query = new ExplicitSpatialQuery(spatialFilter);
        final int resultCount = testQuery(adapter, query);
        assertTrue("'" + adapter.getTypeName() + "' adapter must have at least one element in its statistic", count.getValue() > 0);
        assertEquals("'" + adapter.getTypeName() + "' adapter should have the same results from a spatial query of '" + env + "' as its total count statistic", count.getValue().intValue(), resultCount);
        assertEquals("'" + adapter.getTypeName() + "' adapter entries ingested does not match expected count", EXPECTED_COUNT_PER_ADAPTER_ID.get(adapter.getTypeName()), new Integer(resultCount));
        adapterCount++;
    }
    assertTrue("There should be exactly two adapters", (adapterCount == 2));
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) QueryConstraints(org.locationtech.geowave.core.store.query.constraints.QueryConstraints) BoundingBoxValue(org.locationtech.geowave.core.geotime.store.statistics.BoundingBoxStatistic.BoundingBoxValue) Envelope(org.locationtech.jts.geom.Envelope) DataStatisticsStore(org.locationtech.geowave.core.store.statistics.DataStatisticsStore) Geometry(org.locationtech.jts.geom.Geometry) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) ExplicitSpatialQuery(org.locationtech.geowave.core.geotime.store.query.ExplicitSpatialQuery) CountValue(org.locationtech.geowave.core.store.statistics.adapter.CountStatistic.CountValue) InternalDataAdapter(org.locationtech.geowave.core.store.adapter.InternalDataAdapter) FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter) Test(org.junit.Test)

Example 4 with QueryConstraints

use of org.locationtech.geowave.core.store.query.constraints.QueryConstraints in project geowave by locationtech.

the class ExpressionQueryIT method testTemporalExpressionQueriesTemporalIndex.

@Test
public void testTemporalExpressionQueriesTemporalIndex() {
    final DataStore ds = dataStore.createDataStore();
    final DataTypeAdapter<SimpleFeature> adapter = createDataAdapter();
    final Index spatialIndex = SpatialDimensionalityTypeProvider.createIndexFromOptions(new SpatialOptions());
    final Index temporalIndex = TemporalDimensionalityTypeProvider.createIndexFromOptions(new TemporalOptions());
    ds.addType(adapter, spatialIndex, temporalIndex);
    final PersistentAdapterStore adapterStore = dataStore.createAdapterStore();
    final InternalAdapterStore internalAdapterStore = dataStore.createInternalAdapterStore();
    final AdapterIndexMappingStore aimStore = dataStore.createAdapterIndexMappingStore();
    final IndexStore indexStore = dataStore.createIndexStore();
    final DataStatisticsStore statsStore = dataStore.createDataStatisticsStore();
    final InternalDataAdapter<?> internalAdapter = adapterStore.getAdapter(internalAdapterStore.getAdapterId(TYPE_NAME));
    // Ingest data
    ingestData(ds);
    // ///////////////////////////////////////////////////
    // After
    // ///////////////////////////////////////////////////
    Query<SimpleFeature> query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.isAfter(new Date(ONE_DAY_MILLIS * (TOTAL_FEATURES / 2)))).build();
    QueryConstraints queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    ExplicitFilteredQuery constraints = (ExplicitFilteredQuery) queryConstraints;
    List<QueryFilter> filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    Filter filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.After);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2 - 1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Before
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.isBefore(ONE_DAY_MILLIS * (TOTAL_FEATURES / 2))).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof Before);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // During or After
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.isDuringOrAfter(ONE_DAY_MILLIS * (TOTAL_FEATURES / 2))).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof DuringOrAfter);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Before or During
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.isBeforeOrDuring(ONE_DAY_MILLIS * (TOTAL_FEATURES / 2))).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof BeforeOrDuring);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2 + 1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // During
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.isDuring(Interval.of(Instant.ofEpochMilli(ONE_DAY_MILLIS * 5), Instant.ofEpochMilli(ONE_DAY_MILLIS * 10)))).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof During);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(5, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Between
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.isBetween(Instant.ofEpochMilli(ONE_DAY_MILLIS * 5), Instant.ofEpochMilli(ONE_DAY_MILLIS * 10))).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof TemporalBetween);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(6, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Contains (inverse of During)
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TemporalLiteral.of(Interval.of(Instant.ofEpochMilli(ONE_DAY_MILLIS * 5), Instant.ofEpochMilli(ONE_DAY_MILLIS * 10))).contains(TIMESTAMP)).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof During);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(5, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Overlaps
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.overlaps(Interval.of(Instant.ofEpochMilli(ONE_DAY_MILLIS * 5), Instant.ofEpochMilli(ONE_DAY_MILLIS * 10)))).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof TimeOverlaps);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(5, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Equal To
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.isEqualTo(ONE_DAY_MILLIS * 12)).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof TemporalEqualTo);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Not Equal To
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.isNotEqualTo(ONE_DAY_MILLIS * 12).and(TIMESTAMP.isNotEqualTo(ONE_DAY_MILLIS * 8))).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof And);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES - 2, Iterators.size(iterator));
    }
}
Also used : Before(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.Before) Index(org.locationtech.geowave.core.store.api.Index) CustomQueryConstraints(org.locationtech.geowave.core.store.query.constraints.CustomQueryConstraints) QueryConstraints(org.locationtech.geowave.core.store.query.constraints.QueryConstraints) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) BeforeOrDuring(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.BeforeOrDuring) During(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.During) DataStatisticsStore(org.locationtech.geowave.core.store.statistics.DataStatisticsStore) TimeOverlaps(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.TimeOverlaps) DuringOrAfter(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.DuringOrAfter) DataStore(org.locationtech.geowave.core.store.api.DataStore) TemporalEqualTo(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.TemporalEqualTo) ExplicitFilteredQuery(org.locationtech.geowave.core.store.query.constraints.ExplicitFilteredQuery) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) SpatialOptions(org.locationtech.geowave.core.geotime.index.SpatialOptions) AdapterIndexMappingStore(org.locationtech.geowave.core.store.adapter.AdapterIndexMappingStore) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Date(java.util.Date) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) QueryFilter(org.locationtech.geowave.core.store.query.filter.QueryFilter) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) QueryFilter(org.locationtech.geowave.core.store.query.filter.QueryFilter) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) Filter(org.locationtech.geowave.core.store.query.filter.expression.Filter) And(org.locationtech.geowave.core.store.query.filter.expression.And) SpatialTemporalOptions(org.locationtech.geowave.core.geotime.index.SpatialTemporalOptions) TemporalOptions(org.locationtech.geowave.core.geotime.index.TemporalOptions) BeforeOrDuring(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.BeforeOrDuring) TemporalBetween(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.TemporalBetween) IndexStore(org.locationtech.geowave.core.store.index.IndexStore) Test(org.junit.Test)

Example 5 with QueryConstraints

use of org.locationtech.geowave.core.store.query.constraints.QueryConstraints in project geowave by locationtech.

the class ExpressionQueryIT method testIndexSelection.

@Test
public void testIndexSelection() {
    final DataStore ds = dataStore.createDataStore();
    final DataTypeAdapter<SimpleFeature> adapter = createDataAdapter();
    final Index spatialIndex = SpatialDimensionalityTypeProvider.createIndexFromOptions(new SpatialOptions());
    final Index spatialTemporalIndex = SpatialTemporalDimensionalityTypeProvider.createIndexFromOptions(new SpatialTemporalOptions());
    final Index temporalIndex = TemporalDimensionalityTypeProvider.createIndexFromOptions(new TemporalOptions());
    ds.addType(adapter, spatialIndex, spatialTemporalIndex, temporalIndex);
    final Index altIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, ALTERNATE_GEOMETRY_FIELD));
    final Index integerIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, INTEGER_FIELD));
    final Index commentIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, COMMENT_FIELD));
    final Index idIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, ID_FIELD));
    final Index latitudeIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, LATITUDE_FIELD));
    final Index longitudeIndex = AttributeDimensionalityTypeProvider.createIndexFromOptions(ds, new AttributeIndexOptions(TYPE_NAME, LONGITUDE_FIELD));
    ds.addIndex(TYPE_NAME, altIndex, integerIndex, commentIndex, idIndex, latitudeIndex, longitudeIndex);
    final PersistentAdapterStore adapterStore = dataStore.createAdapterStore();
    final InternalAdapterStore internalAdapterStore = dataStore.createInternalAdapterStore();
    final AdapterIndexMappingStore aimStore = dataStore.createAdapterIndexMappingStore();
    final IndexStore indexStore = dataStore.createIndexStore();
    final DataStatisticsStore statsStore = dataStore.createDataStatisticsStore();
    final InternalDataAdapter<?> internalAdapter = adapterStore.getAdapter(internalAdapterStore.getAdapterId(TYPE_NAME));
    // Ingest data
    ingestData(ds);
    // ///////////////////////////////////////////////////
    // Basic BBOX on Alternate Geometry
    // ///////////////////////////////////////////////////
    Query<SimpleFeature> query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ALT.bbox(-64.5, 0.5, 0.5, 64.5)).build();
    QueryConstraints queryConstraints = assertBestIndex(internalAdapter, altIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    ExplicitFilteredQuery constraints = (ExplicitFilteredQuery) queryConstraints;
    List<QueryFilter> filters = constraints.createFilters(altIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    Filter filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    assertTrue(filter instanceof BBox);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 4, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Both bbox and comment are indexed, but comment
    // should result in fewer rows queried so that
    // should be the selected index
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ALT.bbox(-64.5, -32.5, 32.5, 64.5).and(COMMENT.startsWith("b", true))).build();
    queryConstraints = assertBestIndex(internalAdapter, commentIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof CustomQueryConstraints);
    CustomQueryConstraints<?> customConstraints = (CustomQueryConstraints<?>) queryConstraints;
    filters = customConstraints.createFilters(commentIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // Comment predicate was exact so only the bbox filter should need to be performed
    assertTrue(filter instanceof BBox);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        int count = 0;
        while (iterator.hasNext()) {
            final SimpleFeature feature = iterator.next();
            assertEquals("Bravo", feature.getAttribute(COMMENT_FIELD));
            count++;
        }
        // 1/4 of entries match the comment predicate, but only 3/4 of those match the bounding box
        assertEquals(Math.round(TOTAL_FEATURES / 8 * 1.5), count);
    }
    // ///////////////////////////////////////////////////
    // Both bbox and comment are indexed, but bbox should
    // result in fewer rows queried so that should be the
    // selected index
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ALT.bbox(-64.5, 32.5, -32.5, 64.5).and(COMMENT.startsWith("b", true))).build();
    queryConstraints = assertBestIndex(internalAdapter, altIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(altIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // bbox is not exact, so it will still be part of the filter
    assertTrue(filter instanceof And);
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 16, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Latitude is the most constraining
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(LATITUDE.isGreaterThan(5).and(LATITUDE.isLessThan(10), LONGITUDE.isGreaterThanOrEqualTo(7))).build();
    queryConstraints = assertBestIndex(internalAdapter, latitudeIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(latitudeIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // since the latitude fields are exact, only the longitude needs to be filtered later
    assertTrue(filter instanceof ComparisonOperator);
    Set<String> referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(LONGITUDE_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(3, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Longitude is the most constraining
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(LONGITUDE.isGreaterThanOrEqualTo(5).and(LONGITUDE.isLessThan(10).or(LATITUDE.isLessThanOrEqualTo(15)))).build();
    queryConstraints = assertBestIndex(internalAdapter, longitudeIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(longitudeIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // The second half of the expression cannot be predetermined, so both sides of the Or need to be
    // present
    assertTrue(filter instanceof Or);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(2, referencedFields.size());
    assertTrue(referencedFields.contains(LATITUDE_FIELD));
    assertTrue(referencedFields.contains(LONGITUDE_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(11, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Longitude is an exact range
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(LONGITUDE.isLessThan(-31.5).or(LONGITUDE.isGreaterThan(31.5))).build();
    queryConstraints = assertBestIndex(internalAdapter, longitudeIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(longitudeIndex);
    // The constraints are exact, so there shouldn't be any additional filtering
    assertEquals(0, filters.size());
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(TOTAL_FEATURES / 2 + 1, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Default geom only should select spatial index
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.bbox(0.5, 0.5, 10.5, 10.5)).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(spatialIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // BBox isn't exact, so it should still be filtered
    assertTrue(filter instanceof BBox);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(DEFAULT_GEOMETRY_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(10, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Default geom and time should select spatial-
    // temporal index
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.bbox(0.5, 0.5, 30.5, 30.5).and(TIMESTAMP.isBefore(new Date((long) (66 * ONE_DAY_MILLIS + 1))))).build();
    queryConstraints = assertBestIndex(internalAdapter, spatialTemporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(spatialTemporalIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // BBox isn't exact, and neither is timestamp in a binned temporal index
    assertTrue(filter instanceof And);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(2, referencedFields.size());
    assertTrue(referencedFields.contains(DEFAULT_GEOMETRY_FIELD));
    assertTrue(referencedFields.contains(TIMESTAMP_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(2, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Only timestamp should use temporal index
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(TIMESTAMP.isBefore(new Date((long) (66 * ONE_DAY_MILLIS + 1)))).build();
    queryConstraints = assertBestIndex(internalAdapter, temporalIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(temporalIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // Timestamp is not exact on temporal index because there could be ranges
    assertTrue(filter instanceof Before);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(TIMESTAMP_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(67, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Integer is more constraining, half of the ID
    // values end with 0
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ID.endsWith("0").and(INTEGER.isBetween(10, 20))).build();
    queryConstraints = assertBestIndex(internalAdapter, integerIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(integerIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // Integer is exact, so only the string predicate should remain
    assertTrue(filter instanceof EndsWith);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(ID_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(6, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // ID is more constraining
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(ID.endsWith("a0").and(INTEGER.isBetween(0, 40))).build();
    queryConstraints = assertBestIndex(internalAdapter, idIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof CustomQueryConstraints);
    customConstraints = (CustomQueryConstraints<?>) queryConstraints;
    filters = customConstraints.createFilters(commentIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // ID constraint is exact, so only the integer predicate should remain
    assertTrue(filter instanceof Between);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(INTEGER_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(2, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Alternate geometry is 50% null, so it is more
    // constraining
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(GEOM.bbox(-30.5, -30.5, 30.5, 30.5).and(ALT.bbox(-30.5, -30.5, 30.5, 30.5))).build();
    queryConstraints = assertBestIndex(internalAdapter, altIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(altIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // Neither bbox is exact, so they will both be filtered
    assertTrue(filter instanceof And);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(2, referencedFields.size());
    assertTrue(referencedFields.contains(DEFAULT_GEOMETRY_FIELD));
    assertTrue(referencedFields.contains(ALTERNATE_GEOMETRY_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(30, Iterators.size(iterator));
    }
    // ///////////////////////////////////////////////////
    // Integer is more constraining
    // ///////////////////////////////////////////////////
    query = QueryBuilder.newBuilder(SimpleFeature.class).filter(INTEGER.isLessThan(-60).and(LATITUDE.isLessThan(5))).build();
    queryConstraints = assertBestIndex(internalAdapter, integerIndex, query, adapterStore, internalAdapterStore, aimStore, indexStore, statsStore);
    assertTrue(queryConstraints instanceof ExplicitFilteredQuery);
    constraints = (ExplicitFilteredQuery) queryConstraints;
    filters = constraints.createFilters(integerIndex);
    assertEquals(1, filters.size());
    assertTrue(filters.get(0) instanceof ExpressionQueryFilter);
    filter = ((ExpressionQueryFilter<?>) filters.get(0)).getFilter();
    // Neither bbox is exact, so they will both be filtered
    assertTrue(filter instanceof ComparisonOperator);
    referencedFields = Sets.newHashSet();
    filter.addReferencedFields(referencedFields);
    assertEquals(1, referencedFields.size());
    assertTrue(referencedFields.contains(LATITUDE_FIELD));
    // Query data
    try (CloseableIterator<SimpleFeature> iterator = ds.query(query)) {
        assertTrue(iterator.hasNext());
        assertEquals(4, Iterators.size(iterator));
    }
}
Also used : Before(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.Before) ComparisonOperator(org.locationtech.geowave.core.store.query.filter.expression.ComparisonOperator) Or(org.locationtech.geowave.core.store.query.filter.expression.Or) Index(org.locationtech.geowave.core.store.api.Index) CustomQueryConstraints(org.locationtech.geowave.core.store.query.constraints.CustomQueryConstraints) QueryConstraints(org.locationtech.geowave.core.store.query.constraints.QueryConstraints) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) LineString(org.locationtech.jts.geom.LineString) SpatialTemporalOptions(org.locationtech.geowave.core.geotime.index.SpatialTemporalOptions) DataStatisticsStore(org.locationtech.geowave.core.store.statistics.DataStatisticsStore) DataStore(org.locationtech.geowave.core.store.api.DataStore) ExplicitFilteredQuery(org.locationtech.geowave.core.store.query.constraints.ExplicitFilteredQuery) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) TemporalBetween(org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.TemporalBetween) Between(org.locationtech.geowave.core.store.query.filter.expression.Between) SpatialOptions(org.locationtech.geowave.core.geotime.index.SpatialOptions) AdapterIndexMappingStore(org.locationtech.geowave.core.store.adapter.AdapterIndexMappingStore) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Point(org.locationtech.jts.geom.Point) Disjoint(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.Disjoint) Date(java.util.Date) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) QueryFilter(org.locationtech.geowave.core.store.query.filter.QueryFilter) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) AttributeIndexOptions(org.locationtech.geowave.core.store.index.AttributeIndexOptions) QueryFilter(org.locationtech.geowave.core.store.query.filter.QueryFilter) ExpressionQueryFilter(org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter) Filter(org.locationtech.geowave.core.store.query.filter.expression.Filter) BBox(org.locationtech.geowave.core.geotime.store.query.filter.expression.spatial.BBox) And(org.locationtech.geowave.core.store.query.filter.expression.And) SpatialTemporalOptions(org.locationtech.geowave.core.geotime.index.SpatialTemporalOptions) TemporalOptions(org.locationtech.geowave.core.geotime.index.TemporalOptions) CustomQueryConstraints(org.locationtech.geowave.core.store.query.constraints.CustomQueryConstraints) IndexStore(org.locationtech.geowave.core.store.index.IndexStore) EndsWith(org.locationtech.geowave.core.store.query.filter.expression.text.EndsWith) Test(org.junit.Test)

Aggregations

QueryConstraints (org.locationtech.geowave.core.store.query.constraints.QueryConstraints)31 PersistentAdapterStore (org.locationtech.geowave.core.store.adapter.PersistentAdapterStore)16 Test (org.junit.Test)15 SimpleFeature (org.opengis.feature.simple.SimpleFeature)15 DataStore (org.locationtech.geowave.core.store.api.DataStore)13 Index (org.locationtech.geowave.core.store.api.Index)13 InternalDataAdapter (org.locationtech.geowave.core.store.adapter.InternalDataAdapter)11 DataStatisticsStore (org.locationtech.geowave.core.store.statistics.DataStatisticsStore)11 AdapterIndexMappingStore (org.locationtech.geowave.core.store.adapter.AdapterIndexMappingStore)10 ExplicitSpatialQuery (org.locationtech.geowave.core.geotime.store.query.ExplicitSpatialQuery)9 InternalAdapterStore (org.locationtech.geowave.core.store.adapter.InternalAdapterStore)9 IndexStore (org.locationtech.geowave.core.store.index.IndexStore)9 QueryFilter (org.locationtech.geowave.core.store.query.filter.QueryFilter)9 CustomQueryConstraints (org.locationtech.geowave.core.store.query.constraints.CustomQueryConstraints)8 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)8 SpatialOptions (org.locationtech.geowave.core.geotime.index.SpatialOptions)7 ExplicitFilteredQuery (org.locationtech.geowave.core.store.query.constraints.ExplicitFilteredQuery)7 ExpressionQueryFilter (org.locationtech.geowave.core.store.query.filter.ExpressionQueryFilter)7 Envelope (org.locationtech.jts.geom.Envelope)7 Geometry (org.locationtech.jts.geom.Geometry)7