Search in sources :

Example 1 with IndexType

use of com.hazelcast.config.IndexType in project hazelcast by hazelcast.

the class SqlCreateIndex method getIndexType.

/**
 * @return the index type, if specified, the default index type if not.
 */
@Nonnull
private IndexType getIndexType() {
    if (type == null) {
        return IndexConfig.DEFAULT_TYPE;
    }
    String indexType = type.toString().toLowerCase();
    IndexType type;
    switch(indexType) {
        case "sorted":
            type = IndexType.SORTED;
            break;
        case "hash":
            type = IndexType.HASH;
            break;
        case "bitmap":
            type = IndexType.BITMAP;
            break;
        default:
            throw QueryException.error("Can't create index: wrong index type. Only HASH, SORTED and BITMAP types are supported.");
    }
    return type;
}
Also used : IndexType(com.hazelcast.config.IndexType) Nonnull(javax.annotation.Nonnull)

Example 2 with IndexType

use of com.hazelcast.config.IndexType in project hazelcast by hazelcast.

the class SqlCreateIndex method validate.

@Override
public void validate(SqlValidator validator, SqlValidatorScope scope) {
    if (getReplace()) {
        throw validator.newValidationError(this, RESOURCE.notSupported("OR REPLACE", "CREATE INDEX"));
    }
    Set<String> columnNames = new HashSet<>();
    for (SqlNode column : columns.getList()) {
        String name = ((SqlIdentifier) requireNonNull(column)).getSimple();
        if (!columnNames.add(name)) {
            throw validator.newValidationError(column, RESOURCE.duplicateIndexAttribute(name));
        }
    }
    IndexType indexType = getIndexType();
    if (!indexType.equals(IndexType.BITMAP) && !options.getList().isEmpty()) {
        throw validator.newValidationError(options, RESOURCE.unsupportedIndexType(indexType.name(), options().keySet().iterator().next()));
    }
    Set<String> optionNames = new HashSet<>();
    for (SqlNode option : options.getList()) {
        String name = ((SqlOption) option).keyString();
        if (!optionNames.add(name)) {
            throw validator.newValidationError(option, RESOURCE.duplicateOption(name));
        }
    }
}
Also used : SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) IndexType(com.hazelcast.config.IndexType) HashSet(java.util.HashSet) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with IndexType

use of com.hazelcast.config.IndexType in project hazelcast by hazelcast.

the class IndexTest method testIt.

private void testIt(boolean ordered) {
    IndexType type = ordered ? IndexType.SORTED : IndexType.HASH;
    IndexConfig config = IndexUtils.createTestIndexConfig(type, QueryConstants.THIS_ATTRIBUTE_NAME.value());
    IndexImpl index = new IndexImpl(config, ss, newExtractor(), copyBehavior, PerIndexStats.EMPTY, MemberPartitionStateImpl.DEFAULT_PARTITION_COUNT);
    assertEquals(0, index.getRecords(0L).size());
    assertEquals(0, index.getRecords(0L, true, 1000L, true).size());
    CachedQueryEntry<?, ?> record5 = newRecord(5L, 55L);
    index.putEntry(record5, null, record5, Index.OperationSource.USER);
    assertEquals(Collections.<QueryableEntry>singleton(record5), index.getRecords(55L));
    CachedQueryEntry<?, ?> record6 = newRecord(6L, 66L);
    index.putEntry(record6, null, record6, Index.OperationSource.USER);
    assertEquals(Collections.<QueryableEntry>singleton(record6), index.getRecords(66L));
    CachedQueryEntry<?, ?> newRecord5 = newRecord(5L, 555L);
    index.putEntry(newRecord5, record5, newRecord5, Index.OperationSource.USER);
    record5 = newRecord5;
    assertEquals(0, index.getRecords(55L).size());
    assertEquals(Collections.<QueryableEntry>singleton(record5), index.getRecords(555L));
    assertEquals(1, index.getRecords(555L).size());
    assertEquals(2, index.getRecords(55L, true, 555L, true).size());
    assertEquals(2, index.getRecords(66L, true, 555L, true).size());
    assertEquals(1, index.getRecords(555L, true, 555L, true).size());
    CachedQueryEntry<?, ?> record50 = newRecord(50L, 555L);
    index.putEntry(record50, null, record50, Index.OperationSource.USER);
    assertEquals(new HashSet<QueryableEntry>(asList(record5, record50)), index.getRecords(555L));
    Map<Data, QueryableEntry> records = getRecordMap(index, 555L);
    assertNotNull(records);
    assertEquals(2, records.size());
    assertEquals(record5, records.get(record5.getKeyData()));
    assertEquals(record50, records.get(record50.getKeyData()));
    assertEquals(2, index.getRecords(555L).size());
    assertEquals(3, index.getRecords(55L, true, 555L, true).size());
    assertEquals(3, index.getRecords(66L, true, 555L, true).size());
    assertEquals(2, index.getRecords(555L, true, 555L, true).size());
    assertEquals(0, index.getRecords(Comparison.LESS, 66L).size());
    assertEquals(1, index.getRecords(Comparison.LESS_OR_EQUAL, 66L).size());
    assertEquals(1, index.getRecords(Comparison.LESS_OR_EQUAL, 67L).size());
    assertEquals(2, index.getRecords(Comparison.GREATER, 66L).size());
    assertEquals(3, index.getRecords(Comparison.GREATER_OR_EQUAL, 66L).size());
    assertEquals(3, index.getRecords(Comparison.GREATER_OR_EQUAL, 61L).size());
    assertEquals(3, index.getRecords(new Comparable[] { 66L, 555L, 34234L }).size());
    assertEquals(2, index.getRecords(new Comparable[] { 555L, 34234L }).size());
    index.removeEntry(record5, Index.OperationSource.USER);
    assertEquals(Collections.<QueryableEntry>singleton(record50), index.getRecords(555L));
    records = getRecordMap(index, 555L);
    assertNotNull(records);
    assertNull(records.get(5L));
    assertEquals(record50, records.get(toData(50L)));
    assertEquals(1, index.getRecords(555L).size());
    assertEquals(2, index.getRecords(55L, true, 555L, true).size());
    assertEquals(2, index.getRecords(66L, true, 555L, true).size());
    assertEquals(1, index.getRecords(555L, true, 555L, true).size());
    assertEquals(0, index.getRecords(Comparison.LESS, 66L).size());
    assertEquals(1, index.getRecords(Comparison.LESS_OR_EQUAL, 66L).size());
    assertEquals(1, index.getRecords(Comparison.LESS_OR_EQUAL, 67L).size());
    assertEquals(1, index.getRecords(Comparison.GREATER, 66L).size());
    assertEquals(2, index.getRecords(Comparison.GREATER_OR_EQUAL, 66L).size());
    assertEquals(2, index.getRecords(Comparison.GREATER_OR_EQUAL, 61L).size());
    index.removeEntry(record50, Index.OperationSource.USER);
    assertEquals(0, index.getRecords(555L).size());
    records = getRecordMap(index, 555L);
    assertNull(records);
    assertEquals(0, index.getRecords(555L).size());
    assertEquals(1, index.getRecords(55L, true, 555L, true).size());
    assertEquals(1, index.getRecords(66L, true, 555L, true).size());
    assertEquals(0, index.getRecords(555L, true, 555L, true).size());
    index.removeEntry(record6, Index.OperationSource.USER);
    assertEquals(0, index.getRecords(66L).size());
    assertNull(getRecordMap(index, 66L));
    assertEquals(0, index.getRecords(555L).size());
    assertEquals(0, index.getRecords(55L, true, 555L, true).size());
    assertEquals(0, index.getRecords(66L, true, 555L, true).size());
    assertEquals(0, index.getRecords(555L, true, 555L, true).size());
}
Also used : IndexConfig(com.hazelcast.config.IndexConfig) TestUtil.toData(com.hazelcast.instance.impl.TestUtil.toData) Data(com.hazelcast.internal.serialization.Data) IndexType(com.hazelcast.config.IndexType)

Example 4 with IndexType

use of com.hazelcast.config.IndexType in project hazelcast by hazelcast.

the class MapTableIndexTest method testEquals.

@Test
public void testEquals() {
    String name1 = "index1";
    String name2 = "index2";
    IndexType type1 = SORTED;
    IndexType type2 = HASH;
    int components1 = 1;
    int components2 = 2;
    List<Integer> ordinals1 = singletonList(1);
    List<Integer> ordinals2 = singletonList(2);
    List<QueryDataType> types1 = singletonList(INT);
    List<QueryDataType> types2 = singletonList(BIGINT);
    MapTableIndex index = new MapTableIndex(name1, type1, components1, ordinals1, types1);
    checkEquals(index, new MapTableIndex(name1, type1, components1, ordinals1, types1), true);
    checkEquals(index, new MapTableIndex(name2, type1, components1, ordinals1, types1), false);
    checkEquals(index, new MapTableIndex(name1, type2, components1, ordinals1, types1), false);
    checkEquals(index, new MapTableIndex(name1, type1, components2, ordinals1, types1), false);
    checkEquals(index, new MapTableIndex(name1, type1, components1, ordinals2, types1), false);
    checkEquals(index, new MapTableIndex(name1, type1, components1, ordinals1, types2), false);
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) IndexType(com.hazelcast.config.IndexType) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with IndexType

use of com.hazelcast.config.IndexType in project hazelcast by hazelcast.

the class SqlIndexResolutionTest method checkIndex.

private void checkIndex(IMap<?, ?> map, List<QueryDataType> expectedFieldConverterTypes) {
    String mapName = map.getName();
    List<PartitionedMapTable> tables = resolver.getTables().stream().filter(t -> t instanceof PartitionedMapTable).map(t -> (PartitionedMapTable) t).filter(t -> t.getMapName().equals(mapName)).collect(Collectors.toList());
    assertEquals(1, tables.size());
    PartitionedMapTable table = tables.get(0);
    assertEquals(1, table.getIndexes().size());
    MapTableIndex index = table.getIndexes().get(0);
    assertEquals(indexName, index.getName());
    assertEquals(indexType, index.getType());
    // Components count depends on the index attribute count
    assertEquals(composite ? 2 : 1, index.getComponentsCount());
    int field1Ordinal = findFieldOrdinal(table, "field1");
    int field2Ordinal = findFieldOrdinal(table, "field2");
    // Check resolved field converter types. We do not test more than two components.
    assertTrue(expectedFieldConverterTypes.size() <= 2);
    assertEquals(expectedFieldConverterTypes, index.getFieldConverterTypes());
    // Resolved field ordinals depend on the number of resolved converter types
    if (expectedFieldConverterTypes.isEmpty()) {
        assertTrue(index.getFieldOrdinals().isEmpty());
    } else if (expectedFieldConverterTypes.size() == 1) {
        assertEquals(singletonList(field1Ordinal), index.getFieldOrdinals());
    } else {
        assertEquals(Arrays.asList(field1Ordinal, field2Ordinal), index.getFieldOrdinals());
    }
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) QuickTest(com.hazelcast.test.annotation.QuickTest) SqlStatement(com.hazelcast.sql.SqlStatement) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) RunWith(org.junit.runner.RunWith) ExpressionBiValue(com.hazelcast.jet.sql.impl.support.expressions.ExpressionBiValue) SqlConnectorCache(com.hazelcast.jet.sql.impl.connector.SqlConnectorCache) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) ExpressionBiValue.createBiValue(com.hazelcast.jet.sql.impl.support.expressions.ExpressionBiValue.createBiValue) IndexType(com.hazelcast.config.IndexType) OptimizerTestSupport(com.hazelcast.jet.sql.impl.opt.OptimizerTestSupport) Arrays.asList(java.util.Arrays.asList) QueryPath(com.hazelcast.sql.impl.extract.QueryPath) Assert.fail(org.junit.Assert.fail) PartitionedMapTable(com.hazelcast.sql.impl.schema.map.PartitionedMapTable) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) NodeEngine(com.hazelcast.spi.impl.NodeEngine) HazelcastParametrizedRunner(com.hazelcast.test.HazelcastParametrizedRunner) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) TableResolverImpl(com.hazelcast.jet.sql.impl.schema.TableResolverImpl) MapTableUtils.getPartitionedMapIndexes(com.hazelcast.sql.impl.schema.map.MapTableUtils.getPartitionedMapIndexes) Assert.assertTrue(org.junit.Assert.assertTrue) HazelcastParallelParametersRunnerFactory(com.hazelcast.test.HazelcastParallelParametersRunnerFactory) Test(org.junit.Test) ExpressionBiValue.createBiClass(com.hazelcast.jet.sql.impl.support.expressions.ExpressionBiValue.createBiClass) IndexScanMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel) Category(org.junit.experimental.categories.Category) FullScanPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.FullScanPhysicalRel) Collectors(java.util.stream.Collectors) TableField(com.hazelcast.sql.impl.schema.TableField) TableResolver(com.hazelcast.sql.impl.schema.TableResolver) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Util.getNodeEngine(com.hazelcast.jet.impl.util.Util.getNodeEngine) FullScanLogicalRel(com.hazelcast.jet.sql.impl.opt.logical.FullScanLogicalRel) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) MapTableIndex(com.hazelcast.sql.impl.schema.map.MapTableIndex) ExpressionType(com.hazelcast.jet.sql.impl.support.expressions.ExpressionType) TablesStorage(com.hazelcast.jet.sql.impl.schema.TablesStorage) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) MapTableIndex(com.hazelcast.sql.impl.schema.map.MapTableIndex) PartitionedMapTable(com.hazelcast.sql.impl.schema.map.PartitionedMapTable)

Aggregations

IndexType (com.hazelcast.config.IndexType)8 IndexConfig (com.hazelcast.config.IndexConfig)3 UniqueKeyTransformation (com.hazelcast.config.BitmapIndexOptions.UniqueKeyTransformation)2 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 ArrayList (java.util.ArrayList)2 NamedNodeMap (org.w3c.dom.NamedNodeMap)2 Node (org.w3c.dom.Node)2 TestUtil.toData (com.hazelcast.instance.impl.TestUtil.toData)1 Data (com.hazelcast.internal.serialization.Data)1 Util.getNodeEngine (com.hazelcast.jet.impl.util.Util.getNodeEngine)1 SqlConnectorCache (com.hazelcast.jet.sql.impl.connector.SqlConnectorCache)1 OptimizerTestSupport (com.hazelcast.jet.sql.impl.opt.OptimizerTestSupport)1 FullScanLogicalRel (com.hazelcast.jet.sql.impl.opt.logical.FullScanLogicalRel)1 FullScanPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.FullScanPhysicalRel)1 IndexScanMapPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel)1 HazelcastTable (com.hazelcast.jet.sql.impl.schema.HazelcastTable)1 TableResolverImpl (com.hazelcast.jet.sql.impl.schema.TableResolverImpl)1 TablesStorage (com.hazelcast.jet.sql.impl.schema.TablesStorage)1