Search in sources :

Example 76 with RelationName

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

the class PartitionedTableIntegrationTest method testAlterPartitionedTableKeepsMetadata.

@Test
public void testAlterPartitionedTableKeepsMetadata() throws Exception {
    execute("create table dynamic_table (" + "  id integer, " + "  score double" + ") partitioned by (score) with (number_of_replicas=0, column_policy='dynamic')");
    ensureGreen();
    execute("insert into dynamic_table (id, score) values (1, 10)");
    execute("refresh table dynamic_table");
    ensureGreen();
    MappingMetadata partitionMetadata = clusterService().state().metadata().indices().get(new PartitionName(new RelationName(sqlExecutor.getCurrentSchema(), "dynamic_table"), Collections.singletonList("10.0")).asIndexName()).mapping();
    Map<String, Object> metaMap = (Map) partitionMetadata.getSourceAsMap().get("_meta");
    assertThat(String.valueOf(metaMap.get("partitioned_by")), Matchers.is("[[score, double]]"));
    execute("alter table dynamic_table set (column_policy= 'dynamic')");
    waitNoPendingTasksOnAll();
    partitionMetadata = clusterService().state().metadata().indices().get(new PartitionName(new RelationName(sqlExecutor.getCurrentSchema(), "dynamic_table"), Collections.singletonList("10.0")).asIndexName()).mapping();
    metaMap = (Map) partitionMetadata.getSourceAsMap().get("_meta");
    assertThat(String.valueOf(metaMap.get("partitioned_by")), Matchers.is("[[score, double]]"));
}
Also used : PartitionName(io.crate.metadata.PartitionName) RelationName(io.crate.metadata.RelationName) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 77 with RelationName

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

the class PartitionedTableIntegrationTest method testAlterNumberOfReplicas.

@Test
public void testAlterNumberOfReplicas() {
    String defaultSchema = sqlExecutor.getCurrentSchema();
    execute("create table quotes (" + "   id integer," + "   quote string," + "   date timestamp with time zone" + ") partitioned by(date) " + "clustered into 3 shards with (number_of_replicas='0-all')");
    ensureYellow();
    String templateName = PartitionName.templateName(defaultSchema, "quotes");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    Settings templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.get(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS), is("0-all"));
    execute("alter table quotes set (number_of_replicas=0)");
    ensureYellow();
    templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1), is(0));
    assertThat(templateSettings.getAsBoolean(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, true), is(false));
    execute("insert into quotes (id, quote, date) values (?, ?, ?), (?, ?, ?)", new Object[] { 1, "Don't panic", 1395874800000L, 2, "Now panic", 1395961200000L });
    assertThat(response.rowCount(), is(2L));
    ensureYellow();
    refresh();
    assertTrue(clusterService().state().metadata().hasAlias(getFqn("quotes")));
    List<String> partitions = List.of(new PartitionName(new RelationName(defaultSchema, "quotes"), Collections.singletonList("1395874800000")).asIndexName(), new PartitionName(new RelationName(defaultSchema, "quotes"), Collections.singletonList("1395961200000")).asIndexName());
    execute("select number_of_replicas from information_schema.table_partitions");
    assertThat(printedTable(response.rows()), is("0\n" + "0\n"));
    execute("select number_of_replicas, number_of_shards from information_schema.tables where table_name = 'quotes'");
    assertEquals("0", response.rows()[0][0]);
    assertEquals(3, response.rows()[0][1]);
    execute("alter table quotes set (number_of_replicas='1-all')");
    ensureYellow();
    execute("select number_of_replicas from information_schema.tables where table_name = 'quotes'");
    assertEquals("1-all", response.rows()[0][0]);
    templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.get(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS), is("1-all"));
    execute("select number_of_replicas from information_schema.table_partitions");
    assertThat(printedTable(response.rows()), is("1-all\n" + "1-all\n"));
}
Also used : PartitionName(io.crate.metadata.PartitionName) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) RelationName(io.crate.metadata.RelationName) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Example 78 with RelationName

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

the class InformationSchemaTest method testOpenClosePartitionInformation.

@Test
public void testOpenClosePartitionInformation() {
    execute("create table t (i int) partitioned by (i)");
    ensureYellow();
    execute("insert into t values (1)");
    String partitionIdent = new PartitionName(new RelationName(sqlExecutor.getCurrentSchema(), "t"), Collections.singletonList("1")).ident();
    execute("insert into t values (2), (3)");
    ensureYellow();
    execute("select closed from information_schema.table_partitions where table_name = 't'");
    assertEquals(3, response.rowCount());
    assertEquals(false, response.rows()[0][0]);
    assertEquals(false, response.rows()[1][0]);
    assertEquals(false, response.rows()[2][0]);
    execute("alter table t partition (i = 1) close");
    execute("select partition_ident, values from information_schema.table_partitions" + " where table_name = 't' and closed = true");
    assertEquals(1, response.rowCount());
    HashMap values = (HashMap) response.rows()[0][1];
    assertEquals(1, values.get("i"));
    assertTrue(partitionIdent.endsWith((String) response.rows()[0][0]));
    execute("alter table t partition (i = 1) open");
    execute("select closed from information_schema.table_partitions");
    assertEquals(3, response.rowCount());
    assertEquals(false, response.rows()[0][0]);
    assertEquals(false, response.rows()[1][0]);
    assertEquals(false, response.rows()[2][0]);
}
Also used : PartitionName(io.crate.metadata.PartitionName) HashMap(java.util.HashMap) RelationName(io.crate.metadata.RelationName) Test(org.junit.Test)

Example 79 with RelationName

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

the class PartitionedTableConcurrentIntegrationTest method deletePartitionWhileInsertingData.

private void deletePartitionWhileInsertingData(final boolean useBulk) throws Exception {
    execute("create table parted (id int, name string) " + "clustered into 1 shards " + "partitioned by (id) " + "with (number_of_replicas = 0)");
    int numberOfDocs = 100;
    final Object[][] bulkArgs = new Object[numberOfDocs][];
    for (int i = 0; i < numberOfDocs; i++) {
        bulkArgs[i] = new Object[] { i % 2, randomAsciiLettersOfLength(10) };
    }
    // partition to delete
    final int idToDelete = 1;
    final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
    final CountDownLatch insertLatch = new CountDownLatch(1);
    final String insertStmt = "insert into parted (id, name) values (?, ?)";
    Thread insertThread = new Thread(() -> {
        try {
            if (useBulk) {
                execute(insertStmt, bulkArgs);
            } else {
                for (Object[] args : bulkArgs) {
                    execute(insertStmt, args);
                }
            }
        } catch (Exception t) {
            exceptionRef.set(t);
        } finally {
            insertLatch.countDown();
        }
    });
    final CountDownLatch deleteLatch = new CountDownLatch(1);
    final String partitionName = new PartitionName(new RelationName(sqlExecutor.getCurrentSchema(), "parted"), Collections.singletonList(String.valueOf(idToDelete))).asIndexName();
    final Object[] deleteArgs = new Object[] { idToDelete };
    Thread deleteThread = new Thread(() -> {
        boolean deleted = false;
        while (!deleted) {
            try {
                Metadata metadata = client().admin().cluster().prepareState().execute().actionGet().getState().metadata();
                if (metadata.indices().get(partitionName) != null) {
                    execute("delete from parted where id = ?", deleteArgs);
                    deleted = true;
                }
            } catch (Throwable t) {
            // ignore (mostly partition index does not exists yet)
            }
        }
        deleteLatch.countDown();
    });
    insertThread.start();
    deleteThread.start();
    deleteLatch.await(SQLTransportExecutor.REQUEST_TIMEOUT.getSeconds() + 1, TimeUnit.SECONDS);
    insertLatch.await(SQLTransportExecutor.REQUEST_TIMEOUT.getSeconds() + 1, TimeUnit.SECONDS);
    Exception exception = exceptionRef.get();
    if (exception != null) {
        throw exception;
    }
    insertThread.join();
    deleteThread.join();
}
Also used : Metadata(org.elasticsearch.cluster.metadata.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) PartitionName(io.crate.metadata.PartitionName) RelationName(io.crate.metadata.RelationName)

Example 80 with RelationName

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

the class SelectFromViewAnalyzerTest method testSelectFromViewIsResolvedToViewQueryDefinition.

@Test
public void testSelectFromViewIsResolvedToViewQueryDefinition() {
    QueriedSelectRelation query = e.analyze("select * from doc.v1");
    assertThat(query.outputs(), contains(isField("name"), isField("count(*)")));
    assertThat(query.groupBy(), Matchers.empty());
    assertThat(query.from(), contains(instanceOf(AnalyzedView.class)));
    QueriedSelectRelation queriedDocTable = (QueriedSelectRelation) ((AnalyzedView) query.from().get(0)).relation();
    assertThat(queriedDocTable.groupBy(), contains(isReference("name")));
    assertThat(queriedDocTable.from(), contains(isDocTable(new RelationName("doc", "t1"))));
}
Also used : RelationName(io.crate.metadata.RelationName) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

RelationName (io.crate.metadata.RelationName)180 Test (org.junit.Test)100 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)55 PartitionName (io.crate.metadata.PartitionName)47 Symbol (io.crate.expression.symbol.Symbol)42 Reference (io.crate.metadata.Reference)37 ColumnIdent (io.crate.metadata.ColumnIdent)31 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)21 ReferenceIdent (io.crate.metadata.ReferenceIdent)21 DocTableInfo (io.crate.metadata.doc.DocTableInfo)21 Map (java.util.Map)20 HashMap (java.util.HashMap)19 SqlExpressions (io.crate.testing.SqlExpressions)18 ArrayList (java.util.ArrayList)18 List (java.util.List)17 Before (org.junit.Before)17 DocTableRelation (io.crate.analyze.relations.DocTableRelation)13 SQLExecutor (io.crate.testing.SQLExecutor)11 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)10 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)10