use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class ColumnPolicyIntegrationTest method testStrictPartitionedTableUpdate.
@Test
public void testStrictPartitionedTableUpdate() 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(sqlExecutor.getCurrentSchema(), "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(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.STRICT));
execute("insert into numbers (num, odd, prime) values (?, ?, ?)", new Object[] { 6, true, false });
execute("refresh table numbers");
Map<String, Object> sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "numbers"), Arrays.asList("true")).asIndexName());
assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.STRICT));
assertThrowsMatches(() -> execute("update numbers set num=?, perfect=? where num=6", new Object[] { 28, true }), isSQLError(is("Column perfect unknown"), UNDEFINED_COLUMN, NOT_FOUND, 4043));
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class ColumnPolicyIntegrationTest method testAlterColumnPolicyOnPartitionedTableWithExistingPartitions.
@Test
public void testAlterColumnPolicyOnPartitionedTableWithExistingPartitions() throws Exception {
execute("create table dynamic_table (" + " id integer, " + " score double" + ") partitioned by (score) with (number_of_replicas=0, column_policy='strict')");
ensureYellow();
// create at least 2 partitions to test real multi partition logic (1 partition would behave similar to 1 normal table)
execute("insert into dynamic_table (id, score) values (1, 10)");
execute("insert into dynamic_table (id, score) values (1, 20)");
execute("refresh table dynamic_table");
ensureYellow();
execute("alter table dynamic_table set (column_policy = 'dynamic')");
waitNoPendingTasksOnAll();
// After changing the column_policy it's possible to add new columns to existing and new
// partitions
execute("insert into dynamic_table (id, score, comment) values (2, 10, 'this is a new column')");
execute("insert into dynamic_table (id, score, new_comment) values (2, 5, 'this is a new column on a new partition')");
execute("refresh table dynamic_table");
ensureYellow();
GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "dynamic_table")).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, XContentType.JSON);
@SuppressWarnings("unchecked") Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
assertThat(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.DYNAMIC));
execute("insert into dynamic_table (id, score, new_col) values (?, ?, ?)", new Object[] { 6, 3, "hello" });
execute("refresh table dynamic_table");
ensureYellow();
Map<String, Object> sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "dynamic_table"), Arrays.asList("10.0")).asIndexName());
assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.DYNAMIC));
sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "dynamic_table"), Arrays.asList("5.0")).asIndexName());
assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.DYNAMIC));
sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "dynamic_table"), Arrays.asList("3.0")).asIndexName());
assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.DYNAMIC));
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class PartitionedTableIntegrationTest method testAlterTableAddColumnOnPartitionedTable.
@Test
@UseRandomizedSchema(random = false)
public void testAlterTableAddColumnOnPartitionedTable() throws Exception {
execute("create table t (id int primary key, date timestamp with time zone primary key) " + "partitioned by (date) " + "clustered into 1 shards " + "with (number_of_replicas=0)");
execute("insert into t (id, date) values (1, '2014-01-01')");
execute("insert into t (id, date) values (10, '2015-01-01')");
ensureYellow();
refresh();
execute("alter table t add name string");
execute("select * from t");
assertThat(Arrays.asList(response.cols()), Matchers.containsInAnyOrder("date", "id", "name"));
GetIndexTemplatesResponse templatesResponse = client().admin().indices().getTemplates(new GetIndexTemplatesRequest(".partitioned.t.")).actionGet();
IndexTemplateMetadata metadata = templatesResponse.getIndexTemplates().get(0);
String mappingSource = metadata.mappings().get(DEFAULT_MAPPING_TYPE).toString();
Map mapping = (Map) XContentFactory.xContent(mappingSource).createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappingSource).map().get(DEFAULT_MAPPING_TYPE);
assertNotNull(((Map) mapping.get("properties")).get("name"));
// template order must not be touched
assertThat(metadata.order(), is(100));
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class PartitionedTableIntegrationTest method testAlterTableAddColumnOnPartitionedTableWithoutPartitions.
@Test
@UseRandomizedSchema(random = false)
public void testAlterTableAddColumnOnPartitionedTableWithoutPartitions() throws Exception {
execute("create table t (id int primary key, date timestamp with time zone primary key) " + "partitioned by (date) " + "clustered into 1 shards " + "with (number_of_replicas=0)");
ensureYellow();
execute("alter table t add column name string");
execute("alter table t add column ft_name string index using fulltext");
ensureYellow();
execute("select * from t");
assertThat(Arrays.asList(response.cols()), Matchers.containsInAnyOrder("date", "ft_name", "id", "name"));
GetIndexTemplatesResponse templatesResponse = client().admin().indices().getTemplates(new GetIndexTemplatesRequest(".partitioned.t.")).actionGet();
IndexTemplateMetadata metadata = templatesResponse.getIndexTemplates().get(0);
String mappingSource = metadata.mappings().get(DEFAULT_MAPPING_TYPE).toString();
Map mapping = (Map) XContentFactory.xContent(mappingSource).createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappingSource).map().get(DEFAULT_MAPPING_TYPE);
assertNotNull(((Map) mapping.get("properties")).get("name"));
assertNotNull(((Map) mapping.get("properties")).get("ft_name"));
}
use of org.elasticsearch.cluster.metadata.IndexTemplateMetaData in project crate by crate.
the class IndexTemplateUpgraderTest method testInvalidSettingIsRemovedForTemplateInCustomSchema.
@Test
public void testInvalidSettingIsRemovedForTemplateInCustomSchema() {
Settings settings = Settings.builder().put("index.recovery.initial_shards", "quorum").build();
String templateName = PartitionName.templateName("foobar", "t1");
IndexTemplateMetadata template = IndexTemplateMetadata.builder(templateName).settings(settings).patterns(Collections.singletonList("*")).build();
IndexTemplateUpgrader indexTemplateUpgrader = new IndexTemplateUpgrader();
Map<String, IndexTemplateMetadata> result = indexTemplateUpgrader.apply(Collections.singletonMap(templateName, template));
assertThat("Outdated setting `index.recovery.initial_shards` must be removed", result.get(templateName).settings().hasValue("index.recovery.initial_shards"), is(false));
}
Aggregations