Search in sources :

Example 31 with ColumnIdent

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

the class AlterTableAddColumnAnalyzerTest method testAddNewNestedObjectColumn.

@Test
public void testAddNewNestedObjectColumn() throws Exception {
    AddColumnAnalyzedStatement analysis = e.analyze("alter table users add column foo['x']['y'] string");
    // id pk column is also added
    assertThat(analysis.analyzedTableElements().columns().size(), is(2));
    AnalyzedColumnDefinition column = analysis.analyzedTableElements().columns().get(0);
    assertThat(column.ident(), Matchers.equalTo(new ColumnIdent("foo")));
    assertThat(column.children().size(), is(1));
    AnalyzedColumnDefinition xColumn = column.children().get(0);
    assertThat(xColumn.ident(), Matchers.equalTo(new ColumnIdent("foo", Arrays.asList("x"))));
    assertThat(xColumn.children().size(), is(1));
    AnalyzedColumnDefinition yColumn = xColumn.children().get(0);
    assertThat(yColumn.ident(), Matchers.equalTo(new ColumnIdent("foo", Arrays.asList("x", "y"))));
    assertThat(yColumn.children().size(), is(0));
    Map<String, Object> mapping = analysis.analyzedTableElements().toMapping();
    Map foo = (Map) StringObjectMaps.getByPath(mapping, "properties.foo");
    assertThat((String) foo.get("type"), is("object"));
    Map x = (Map) StringObjectMaps.getByPath(mapping, "properties.foo.properties.x");
    assertThat((String) x.get("type"), is("object"));
    Map y = (Map) StringObjectMaps.getByPath(mapping, "properties.foo.properties.x.properties.y");
    assertThat((String) y.get("type"), is("string"));
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) TestingHelpers.mapToSortedString(io.crate.testing.TestingHelpers.mapToSortedString) Map(java.util.Map) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 32 with ColumnIdent

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

the class ESFieldExtractorTest method testNullInList.

@Test
public void testNullInList() throws Exception {
    ESFieldExtractor.Source ex = new ESFieldExtractor.Source(new ColumnIdent("top", "child1"), new ArrayType(DataTypes.INTEGER));
    // test null value in list
    HashMap<String, Object> nullMap = new HashMap<String, Object>(1);
    nullMap.put("child1", null);
    ImmutableMap<String, Object> source = ImmutableMap.<String, Object>of("top", ImmutableList.of(nullMap, ImmutableMap.of("child1", 33)));
    Object[] objects = (Object[]) ex.toValue(source);
    assertThat(objects[0], nullValue());
    assertThat(((Integer) objects[1]), is(33));
}
Also used : ArrayType(io.crate.types.ArrayType) ColumnIdent(io.crate.metadata.ColumnIdent) HashMap(java.util.HashMap) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 33 with ColumnIdent

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

the class ESFieldExtractorTest method testPath3.

@Test
public void testPath3() throws Exception {
    ColumnIdent ci = new ColumnIdent("a", ImmutableList.of("b", "c"));
    ESFieldExtractor.Source ex = new ESFieldExtractor.Source(ci, DataTypes.INTEGER);
    Map<String, Object> source;
    source = ImmutableMap.<String, Object>of("a", ImmutableMap.of("b", ImmutableMap.of("c", 1)));
    assertEquals(1, ex.toValue(source));
    source = ImmutableMap.<String, Object>of("a", ImmutableMap.of("b", ImmutableMap.of("d", 1)));
    assertEquals(null, ex.toValue(source));
    source = ImmutableMap.<String, Object>of("a", ImmutableMap.of("b", ImmutableMap.of("c", 1, "d", 2)));
    assertEquals(1, ex.toValue(source));
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 34 with ColumnIdent

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

the class MapSideDataCollectOperationTest method testFileUriCollect.

@Test
public void testFileUriCollect() throws Exception {
    ClusterService clusterService = new NoopClusterService();
    Functions functions = getFunctions();
    CollectSourceResolver collectSourceResolver = mock(CollectSourceResolver.class);
    when(collectSourceResolver.getService(any(RoutedCollectPhase.class))).thenReturn(new FileCollectSource(functions, clusterService, Collections.<String, FileInputFactory>emptyMap()));
    MapSideDataCollectOperation collectOperation = new MapSideDataCollectOperation(collectSourceResolver, threadPool);
    File tmpFile = temporaryFolder.newFile("fileUriCollectOperation.json");
    try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tmpFile), StandardCharsets.UTF_8)) {
        writer.write("{\"name\": \"Arthur\", \"id\": 4, \"details\": {\"age\": 38}}\n");
        writer.write("{\"id\": 5, \"name\": \"Trillian\", \"details\": {\"age\": 33}}\n");
    }
    FileUriCollectPhase collectNode = new FileUriCollectPhase(UUID.randomUUID(), 0, "test", Collections.singletonList("noop_id"), Literal.of(Paths.get(tmpFile.toURI()).toUri().toString()), Arrays.<Symbol>asList(createReference("name", DataTypes.STRING), createReference(new ColumnIdent("details", "age"), DataTypes.INTEGER)), Collections.emptyList(), null, false);
    String threadPoolName = JobCollectContext.threadPoolName(collectNode, "noop_id");
    TestingBatchConsumer consumer = new TestingBatchConsumer();
    JobCollectContext jobCollectContext = mock(JobCollectContext.class);
    CrateCollector collectors = collectOperation.createCollector(collectNode, consumer, jobCollectContext);
    collectOperation.launchCollector(collectors, threadPoolName);
    assertThat(new CollectionBucket(consumer.getResult()), contains(isRow("Arthur", 38), isRow("Trillian", 33)));
}
Also used : CollectSourceResolver(io.crate.operation.collect.sources.CollectSourceResolver) Functions(io.crate.metadata.Functions) FileUriCollectPhase(io.crate.planner.node.dql.FileUriCollectPhase) ColumnIdent(io.crate.metadata.ColumnIdent) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) ClusterService(org.elasticsearch.cluster.ClusterService) FileCollectSource(io.crate.operation.collect.sources.FileCollectSource) FileOutputStream(java.io.FileOutputStream) FileInputFactory(io.crate.operation.collect.files.FileInputFactory) OutputStreamWriter(java.io.OutputStreamWriter) TestingBatchConsumer(io.crate.testing.TestingBatchConsumer) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) File(java.io.File) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) CollectionBucket(io.crate.data.CollectionBucket) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

ColumnIdent (io.crate.metadata.ColumnIdent)34 Reference (io.crate.metadata.Reference)15 CrateUnitTest (io.crate.test.integration.CrateUnitTest)12 Test (org.junit.Test)12 Map (java.util.Map)5 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)4 GeneratedReference (io.crate.metadata.GeneratedReference)4 NestedObjectExpression (io.crate.operation.reference.NestedObjectExpression)3 TestingHelpers.mapToSortedString (io.crate.testing.TestingHelpers.mapToSortedString)3 ArrayType (io.crate.types.ArrayType)3 HashMap (java.util.HashMap)3 Field (io.crate.analyze.symbol.Field)2 Symbol (io.crate.analyze.symbol.Symbol)2 ColumnValidationException (io.crate.exceptions.ColumnValidationException)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 FileUriCollectPhase (io.crate.planner.node.dql.FileUriCollectPhase)2 DataType (io.crate.types.DataType)2 BytesRef (org.apache.lucene.util.BytesRef)2 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1