Search in sources :

Example 31 with PartitionName

use of io.crate.metadata.PartitionName in project crate by crate.

the class TransportExecutorDDLTest method testDeletePartitionTask.

@Test
public void testDeletePartitionTask() throws Exception {
    execute("create table t (id integer primary key, name string) partitioned by (id)");
    ensureYellow();
    execute("insert into t (id, name) values (1, 'Ford')");
    assertThat(response.rowCount(), is(1L));
    ensureYellow();
    execute("select * from information_schema.table_partitions where table_name = 't'");
    assertThat(response.rowCount(), is(1L));
    String partitionName = new PartitionName("t", ImmutableList.of(new BytesRef("1"))).asIndexName();
    ESDeletePartition plan = new ESDeletePartition(UUID.randomUUID(), partitionName);
    TestingBatchConsumer consumer = new TestingBatchConsumer();
    executor.execute(plan, consumer, Row.EMPTY);
    Bucket objects = consumer.getBucket();
    assertThat(objects, contains(isRow(-1L)));
    execute("select * from information_schema.table_partitions where table_name = 't'");
    assertThat(response.rowCount(), is(0L));
}
Also used : PartitionName(io.crate.metadata.PartitionName) Bucket(io.crate.data.Bucket) TestingBatchConsumer(io.crate.testing.TestingBatchConsumer) BytesRef(org.apache.lucene.util.BytesRef) ESDeletePartition(io.crate.planner.node.ddl.ESDeletePartition) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest) Test(org.junit.Test)

Example 32 with PartitionName

use of io.crate.metadata.PartitionName in project crate by crate.

the class TransportExecutorDDLTest method testDeletePartitionTaskClosed.

/**
     * this case should not happen as closed indices aren't listed as TableInfo
     * but if it does maybe because of stale cluster state - validate behaviour here
     * <p>
     * cannot prevent this task from deleting closed indices.
     */
@Test
public void testDeletePartitionTaskClosed() throws Exception {
    execute("create table t (id integer primary key, name string) partitioned by (id)");
    ensureYellow();
    execute("insert into t (id, name) values (1, 'Ford')");
    assertThat(response.rowCount(), is(1L));
    ensureYellow();
    String partitionName = new PartitionName("t", ImmutableList.of(new BytesRef("1"))).asIndexName();
    assertTrue(client().admin().indices().prepareClose(partitionName).execute().actionGet().isAcknowledged());
    ESDeletePartition plan = new ESDeletePartition(UUID.randomUUID(), partitionName);
    TestingBatchConsumer consumer = new TestingBatchConsumer();
    executor.execute(plan, consumer, Row.EMPTY);
    Bucket bucket = consumer.getBucket();
    assertThat(bucket, contains(isRow(-1L)));
    execute("select * from information_schema.table_partitions where table_name = 't'");
    assertThat(response.rowCount(), is(0L));
}
Also used : PartitionName(io.crate.metadata.PartitionName) Bucket(io.crate.data.Bucket) TestingBatchConsumer(io.crate.testing.TestingBatchConsumer) BytesRef(org.apache.lucene.util.BytesRef) ESDeletePartition(io.crate.planner.node.ddl.ESDeletePartition) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest) Test(org.junit.Test)

Example 33 with PartitionName

use of io.crate.metadata.PartitionName in project crate by crate.

the class ColumnPolicyIntegrationTest method testResetColumnPolicyPartitioned.

@Test
public void testResetColumnPolicyPartitioned() throws Exception {
    execute("create table dynamic_table (" + "  id integer, " + "  score double" + ") partitioned by (score) with (number_of_replicas=0)");
    ensureYellow();
    execute("insert into dynamic_table (id, score) values (1, 10)");
    execute("refresh table dynamic_table");
    execute("alter table dynamic_table set (column_policy = 'strict')");
    waitNoPendingTasksOnAll();
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("dynamic_table", Arrays.asList(new BytesRef("10.0"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is(ColumnPolicy.STRICT.value()));
    execute("alter table dynamic_table reset (column_policy)");
    waitNoPendingTasksOnAll();
    partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("dynamic_table", Arrays.asList(new BytesRef("10.0"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is(String.valueOf(ColumnPolicy.DYNAMIC.mappingValue())));
}
Also used : PartitionName(io.crate.metadata.PartitionName) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 34 with PartitionName

use of io.crate.metadata.PartitionName in project crate by crate.

the class ColumnPolicyIntegrationTest method testStrictPartitionedTableInsert.

@Test
public void testStrictPartitionedTableInsert() throws Exception {
    execute("create table numbers (" + "  num int, " + "  odd boolean," + "  prime boolean" + ") partitioned by (odd) with (column_policy='strict', number_of_replicas=0)");
    ensureYellow();
    GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates(PartitionName.templateName(null, "numbers")).execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetaData template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false);
    @SuppressWarnings("unchecked") Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(mapping.get("dynamic")), is(ColumnPolicy.STRICT.value()));
    execute("insert into numbers (num, odd, prime) values (?, ?, ?)", new Object[] { 6, true, false });
    execute("refresh table numbers");
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("numbers", Arrays.asList(new BytesRef("true"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is(ColumnPolicy.STRICT.value()));
    expectedException.expect(SQLActionException.class);
    expectedException.expectMessage("Column perfect unknown");
    execute("insert into numbers (num, odd, prime, perfect) values (?, ?, ?, ?)", new Object[] { 28, true, false, true });
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) PartitionName(io.crate.metadata.PartitionName) XContentType(org.elasticsearch.common.xcontent.XContentType) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) HashMap(java.util.HashMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 35 with PartitionName

use of io.crate.metadata.PartitionName in project crate by crate.

the class TransportExecutorDDLTest method testCreateTableWithOrphanedPartitions.

@Test
public void testCreateTableWithOrphanedPartitions() throws Exception {
    String partitionName = new PartitionName("test", Collections.singletonList(new BytesRef("foo"))).asIndexName();
    client().admin().indices().prepareCreate(partitionName).addMapping(Constants.DEFAULT_MAPPING_TYPE, TEST_PARTITIONED_MAPPING).setSettings(TEST_SETTINGS).execute().actionGet();
    ensureGreen();
    execute("create table test (id integer, name string, names string) partitioned by (id)");
    ensureYellow();
    execute("select * from information_schema.tables where table_name = 'test'");
    assertThat(response.rowCount(), is(1L));
    execute("select count(*) from information_schema.columns where table_name = 'test'");
    assertThat((Long) response.rows()[0][0], is(3L));
    // check that orphaned partition has been deleted
    assertThat(client().admin().indices().exists(new IndicesExistsRequest(partitionName)).actionGet().isExists(), is(false));
}
Also used : PartitionName(io.crate.metadata.PartitionName) BytesRef(org.apache.lucene.util.BytesRef) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest) Test(org.junit.Test)

Aggregations

PartitionName (io.crate.metadata.PartitionName)58 BytesRef (org.apache.lucene.util.BytesRef)50 Test (org.junit.Test)44 CrateUnitTest (io.crate.test.integration.CrateUnitTest)20 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)10 WhereClause (io.crate.analyze.WhereClause)8 TableIdent (io.crate.metadata.TableIdent)8 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)7 Settings (org.elasticsearch.common.settings.Settings)7 DocTableInfo (io.crate.metadata.doc.DocTableInfo)6 GetSettingsResponse (org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse)5 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)5 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)4 SQLResponse (io.crate.testing.SQLResponse)4 Map (java.util.Map)4 Table (io.crate.sql.tree.Table)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)3 MetaData (org.elasticsearch.cluster.metadata.MetaData)3