| 45 | import io.crate.sql.tree.CheckConstraint; |
| 46 | |
| 47 | @IntegTestCase.ClusterScope(numDataNodes = 2, numClientNodes = 0, supportsDedicatedMasters = false) |
| 48 | public class SchemasITest extends IntegTestCase { |
| 49 | |
| 50 | private Schemas schemas; |
| 51 | private RoutingProvider routingProvider = new RoutingProvider(Randomness.get().nextInt(), Collections.emptyList()); |
| 52 | |
| 53 | @Before |
| 54 | public void setUpService() { |
| 55 | schemas = cluster().getInstance(NodeContext.class).schemas(); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testDocTable() { |
| 60 | execute("create table t1 (" + |
| 61 | "id int primary key, " + |
| 62 | "name string, " + |
| 63 | "CONSTRAINT not_miguel CHECK (name != 'miguel'), " + |
| 64 | "details object(dynamic) as (size byte, created timestamp with time zone)" + |
| 65 | ") clustered into 10 shards with (number_of_replicas=1)"); |
| 66 | |
| 67 | DocTableInfo ti = schemas.getTableInfo(new RelationName(sqlExecutor.getCurrentSchema(), "t1")); |
| 68 | assertThat(ti.ident().name()).isEqualTo("t1"); |
| 69 | |
| 70 | assertThat(ti.rootColumns()).hasSize(3); |
| 71 | assertThat(ti.primaryKey()).hasSize(1); |
| 72 | assertThat(ti.primaryKey().get(0)).isEqualTo(ColumnIdent.of("id")); |
| 73 | assertThat(ti.clusteredBy()).isEqualTo(ColumnIdent.of("id")); |
| 74 | List<CheckConstraint<Symbol>> checkConstraints = ti.checkConstraints(); |
| 75 | assertThat(checkConstraints.size()).isEqualTo(1); |
| 76 | assertThat("not_miguel").isEqualTo(checkConstraints.get(0).name()); |
| 77 | assertThat(checkConstraints.get(0).expressionStr()).isEqualTo("\"name\" <> 'miguel'"); |
| 78 | |
| 79 | ClusterService clusterService = clusterService(); |
| 80 | Routing routing = ti.getRouting( |
| 81 | clusterService.state(), |
| 82 | routingProvider, |
| 83 | WhereClause.MATCH_ALL, |
| 84 | RoutingProvider.ShardSelection.ANY, |
| 85 | CoordinatorSessionSettings.systemDefaults() |
| 86 | ); |
| 87 | |
| 88 | Set<String> nodes = routing.nodes(); |
| 89 | String indexUUID = resolveIndex("t1").getUUID(); |
| 90 | |
| 91 | assertThat(nodes.size()).isBetween(1, 2); // for the rare case |
| 92 | // where all shards are on 1 node |
| 93 | int numShards = 0; |
| 94 | for (Map.Entry<String, Map<String, IntIndexedContainer>> nodeEntry : routing.locations().entrySet()) { |
| 95 | for (Map.Entry<String, IntIndexedContainer> indexEntry : nodeEntry.getValue().entrySet()) { |
| 96 | assertThat(indexEntry.getKey()).isEqualTo(indexUUID); |
| 97 | numShards += indexEntry.getValue().size(); |
| 98 | } |
| 99 | } |
| 100 | assertThat(numShards).isEqualTo(10); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testNodesTable() throws Exception { |