Search in sources :

Example 21 with PartitionName

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

the class PartitionedTableIntegrationTest method testAlterTableResetPartitionedTable.

@Test
public void testAlterTableResetPartitionedTable() throws Exception {
    execute("create table quotes (id integer, quote string, date timestamp) " + "partitioned by(date) clustered into 3 shards with (number_of_replicas='1-all')");
    ensureYellow();
    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();
    execute("alter table quotes reset (number_of_replicas)");
    ensureYellow();
    String templateName = PartitionName.templateName(null, "quotes");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    Settings templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0), is(1));
    assertThat(templateSettings.get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("false"));
    List<String> partitions = ImmutableList.of(new PartitionName("quotes", Collections.singletonList(new BytesRef("1395874800000"))).asIndexName(), new PartitionName("quotes", Collections.singletonList(new BytesRef("1395961200000"))).asIndexName());
    Thread.sleep(1000);
    GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings(partitions.get(0), partitions.get(1)).execute().get();
    for (String index : partitions) {
        assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_NUMBER_OF_REPLICAS), is("1"));
        assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("false"));
    }
}
Also used : PartitionName(io.crate.metadata.PartitionName) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) Settings(org.elasticsearch.common.settings.Settings) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 22 with PartitionName

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

the class WhereClauseAnalyzerTest method testUpdateWherePartitionedByColumn.

@Test
public void testUpdateWherePartitionedByColumn() throws Exception {
    UpdateAnalyzedStatement updateAnalyzedStatement = analyzeUpdate("update parted set id = 2 where date = 1395874800000");
    UpdateAnalyzedStatement.NestedAnalyzedStatement nestedAnalyzedStatement = updateAnalyzedStatement.nestedStatements().get(0);
    assertThat(nestedAnalyzedStatement.whereClause().hasQuery(), is(false));
    assertThat(nestedAnalyzedStatement.whereClause().noMatch(), is(false));
    assertEquals(ImmutableList.of(new PartitionName("parted", Arrays.asList(new BytesRef("1395874800000"))).asIndexName()), nestedAnalyzedStatement.whereClause().partitions());
}
Also used : PartitionName(io.crate.metadata.PartitionName) UpdateAnalyzedStatement(io.crate.analyze.UpdateAnalyzedStatement) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 23 with PartitionName

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

the class WhereClauseAnalyzerTest method testSelectFromPartitionedTable.

@Test
public void testSelectFromPartitionedTable() throws Exception {
    String partition1 = new PartitionName("parted", Arrays.asList(new BytesRef("1395874800000"))).asIndexName();
    String partition2 = new PartitionName("parted", Arrays.asList(new BytesRef("1395961200000"))).asIndexName();
    String partition3 = new PartitionName("parted", new ArrayList<BytesRef>() {

        {
            add(null);
        }
    }).asIndexName();
    WhereClause whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000");
    assertEquals(ImmutableList.of(partition1), whereClause.partitions());
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 " + "and substr(name, 0, 4) = 'this'");
    assertEquals(ImmutableList.of(partition1), whereClause.partitions());
    assertThat(whereClause.hasQuery(), is(true));
    assertThat(whereClause.noMatch(), is(false));
    whereClause = analyzeSelectWhere("select id, name from parted where date >= 1395874800000");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date < 1395874800000");
    assertEquals(ImmutableList.of(), whereClause.partitions());
    assertTrue(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 and date = 1395961200000");
    assertEquals(ImmutableList.of(), whereClause.partitions());
    assertTrue(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 or date = 1395961200000");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date < 1395874800000 or date > 1395874800000");
    assertEquals(ImmutableList.of(partition2), whereClause.partitions());
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date in (1395874800000, 1395961200000)");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date in (1395874800000, 1395961200000) and id = 1");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertTrue(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    /**
         *
         * obj['col'] = 'undefined' => null as col doesn't exist
         *
         *  partition1: not (true  and null) -> not (null)  -> null -> no match
         *  partition2: not (false and null) -> not (false) -> true -> match
         *  partition3: not (null  and null) -> not (null)  -> null -> no match
         */
    whereClause = analyzeSelectWhere("select id, name from parted where not (date = 1395874800000 and obj['col'] = 'undefined')");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition2));
    assertThat(whereClause.hasQuery(), is(false));
    assertThat(whereClause.noMatch(), is(false));
    whereClause = analyzeSelectWhere("select id, name from parted where date in (1395874800000) or date in (1395961200000)");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395961200000 and id = 1");
    assertEquals(ImmutableList.of(partition2), whereClause.partitions());
    assertTrue(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where (date =1395874800000 or date = 1395961200000) and id = 1");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertTrue(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date = 1395874800000 and id is null");
    assertEquals(ImmutableList.of(partition1), whereClause.partitions());
    assertTrue(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where date is null and id = 1");
    assertEquals(ImmutableList.of(partition3), whereClause.partitions());
    assertTrue(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where 1395874700000 < date and date < 1395961200001");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
    whereClause = analyzeSelectWhere("select id, name from parted where '2014-03-16T22:58:20' < date and date < '2014-03-27T23:00:01'");
    assertThat(whereClause.partitions(), containsInAnyOrder(partition1, partition2));
    assertFalse(whereClause.hasQuery());
    assertFalse(whereClause.noMatch());
}
Also used : PartitionName(io.crate.metadata.PartitionName) ArrayList(java.util.ArrayList) WhereClause(io.crate.analyze.WhereClause) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 24 with PartitionName

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

the class WhereClauseAnalyzerTest method testSelectWherePartitionedByColumn.

@Test
public void testSelectWherePartitionedByColumn() throws Exception {
    WhereClause whereClause = analyzeSelectWhere("select id from parted where date = 1395874800000");
    assertThat(whereClause.hasQuery(), is(false));
    assertThat(whereClause.noMatch(), is(false));
    assertThat(whereClause.partitions(), Matchers.contains(new PartitionName("parted", Arrays.asList(new BytesRef("1395874800000"))).asIndexName()));
}
Also used : PartitionName(io.crate.metadata.PartitionName) WhereClause(io.crate.analyze.WhereClause) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 25 with PartitionName

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

the class WhereClauseAnalyzerTest method registerTables.

private void registerTables(SQLExecutor.Builder builder) {
    builder.addDocTable(TestingTableInfo.builder(new TableIdent("doc", "users"), twoNodeRouting).add("id", DataTypes.STRING, null).add("name", DataTypes.STRING, null).add("tags", new ArrayType(DataTypes.STRING), null).addPrimaryKey("id").clusteredBy("id").build());
    builder.addDocTable(TestingTableInfo.builder(new TableIdent("doc", "parted"), twoNodeRouting).add("id", DataTypes.INTEGER, null).add("name", DataTypes.STRING, null).add("date", DataTypes.TIMESTAMP, null, true).add("obj", DataTypes.OBJECT, null, ColumnPolicy.IGNORED).addPartitions(new PartitionName("parted", Arrays.asList(new BytesRef("1395874800000"))).asIndexName(), new PartitionName("parted", Arrays.asList(new BytesRef("1395961200000"))).asIndexName(), new PartitionName("parted", new ArrayList<BytesRef>() {

        {
            add(null);
        }
    }).asIndexName()).build());
    builder.addDocTable(TestingTableInfo.builder(new TableIdent("doc", "parted_pk"), twoNodeRouting).addPrimaryKey("id").addPrimaryKey("date").add("id", DataTypes.INTEGER, null).add("name", DataTypes.STRING, null).add("date", DataTypes.TIMESTAMP, null, true).add("obj", DataTypes.OBJECT, null, ColumnPolicy.IGNORED).addPartitions(new PartitionName("parted_pk", Arrays.asList(new BytesRef("1395874800000"))).asIndexName(), new PartitionName("parted_pk", Arrays.asList(new BytesRef("1395961200000"))).asIndexName(), new PartitionName("parted_pk", new ArrayList<BytesRef>() {

        {
            add(null);
        }
    }).asIndexName()).build());
    builder.addDocTable(TestingTableInfo.builder(new TableIdent("doc", "bystring"), twoNodeRouting).add("name", DataTypes.STRING, null).add("score", DataTypes.DOUBLE, null).addPrimaryKey("name").clusteredBy("name").build());
    builder.addDocTable(TestingTableInfo.builder(new TableIdent("doc", "users_multi_pk"), twoNodeRouting).add("id", DataTypes.LONG, null).add("name", DataTypes.STRING, null).add("details", DataTypes.OBJECT, null).add("awesome", DataTypes.BOOLEAN, null).add("friends", new ArrayType(DataTypes.OBJECT), null, ColumnPolicy.DYNAMIC).addPrimaryKey("id").addPrimaryKey("name").clusteredBy("id").build());
    builder.addDocTable(TestingTableInfo.builder(new TableIdent("doc", "pk4"), twoNodeRouting).add("i1", DataTypes.INTEGER, null).add("i2", DataTypes.INTEGER, null).add("i3", DataTypes.INTEGER, null).add("i4", DataTypes.INTEGER, null).addPrimaryKey("i1").addPrimaryKey("i2").addPrimaryKey("i3").addPrimaryKey("i4").build());
    builder.addDocTable(TestingTableInfo.builder(new TableIdent("doc", "users_clustered_by_only"), twoNodeRouting).add("id", DataTypes.LONG, null).add("name", DataTypes.STRING, null).add("details", DataTypes.OBJECT, null).add("awesome", DataTypes.BOOLEAN, null).add("friends", new ArrayType(DataTypes.OBJECT), null, ColumnPolicy.DYNAMIC).clusteredBy("id").build());
}
Also used : ArrayType(io.crate.types.ArrayType) PartitionName(io.crate.metadata.PartitionName) TableIdent(io.crate.metadata.TableIdent) BytesRef(org.apache.lucene.util.BytesRef)

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