Search in sources :

Example 46 with Reference

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

the class InsertPlannerTest method testInsertFromValuesWithOnDuplicateKey.

@Test
public void testInsertFromValuesWithOnDuplicateKey() throws Exception {
    UpsertById node = e.plan("insert into users (id, name) values (1, null) on duplicate key update name = values(name)");
    assertThat(node.updateColumns(), is(new String[] { "name" }));
    assertThat(node.insertColumns().length, is(2));
    Reference idRef = node.insertColumns()[0];
    assertThat(idRef.ident().columnIdent().fqn(), is("id"));
    Reference nameRef = node.insertColumns()[1];
    assertThat(nameRef.ident().columnIdent().fqn(), is("name"));
    assertThat(node.items().size(), is(1));
    UpsertById.Item item = node.items().get(0);
    assertThat(item.index(), is("users"));
    assertThat(item.id(), is("1"));
    assertThat(item.routing(), is("1"));
    assertThat(item.insertValues().length, is(2));
    assertThat((Long) item.insertValues()[0], is(1L));
    assertNull(item.insertValues()[1]);
    assertThat(item.updateAssignments().length, is(1));
    assertThat(item.updateAssignments()[0], isLiteral(null, DataTypes.STRING));
}
Also used : Reference(io.crate.metadata.Reference) UpsertById(io.crate.planner.node.dml.UpsertById) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 47 with Reference

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

the class InsertPlannerTest method testInsertPlan.

@Test
public void testInsertPlan() throws Exception {
    UpsertById upsertById = e.plan("insert into users (id, name) values (42, 'Deep Thought')");
    assertThat(upsertById.insertColumns().length, is(2));
    Reference idRef = upsertById.insertColumns()[0];
    assertThat(idRef.ident().columnIdent().fqn(), is("id"));
    Reference nameRef = upsertById.insertColumns()[1];
    assertThat(nameRef.ident().columnIdent().fqn(), is("name"));
    assertThat(upsertById.items().size(), is(1));
    UpsertById.Item item = upsertById.items().get(0);
    assertThat(item.index(), is("users"));
    assertThat(item.id(), is("42"));
    assertThat(item.routing(), is("42"));
    assertThat(item.insertValues().length, is(2));
    assertThat((Long) item.insertValues()[0], is(42L));
    assertThat((BytesRef) item.insertValues()[1], is(new BytesRef("Deep Thought")));
}
Also used : Reference(io.crate.metadata.Reference) UpsertById(io.crate.planner.node.dml.UpsertById) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 48 with Reference

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

the class InsertPlannerTest method testInsertFromQueryWithPartitionedColumn.

@Test
public void testInsertFromQueryWithPartitionedColumn() throws Exception {
    Merge planNode = e.plan("insert into users (id, date) (select id, date from parted_pks)");
    Collect queryAndFetch = (Collect) planNode.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) queryAndFetch.collectPhase());
    List<Symbol> toCollect = collectPhase.toCollect();
    assertThat(toCollect.size(), is(2));
    assertThat(toCollect.get(0), isFunction("to_long"));
    assertThat(((Function) toCollect.get(0)).arguments().get(0), isReference("_doc['id']"));
    assertThat((Reference) toCollect.get(1), equalTo(new Reference(new ReferenceIdent(TableDefinitions.PARTED_PKS_IDENT, "date"), RowGranularity.PARTITION, DataTypes.TIMESTAMP)));
}
Also used : Function(io.crate.analyze.symbol.Function) Collect(io.crate.planner.node.dql.Collect) Symbol(io.crate.analyze.symbol.Symbol) Reference(io.crate.metadata.Reference) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) ReferenceIdent(io.crate.metadata.ReferenceIdent) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 49 with Reference

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

the class InsertFromValuesAnalyzedStatement method newPartitionMap.

// create and add a new partition map
public Map<String, String> newPartitionMap() {
    Map<String, String> map = new HashMap<>(tableInfo().partitionedByColumns().size());
    for (Reference partInfo : tableInfo().partitionedByColumns()) {
        // initialize with null values for missing partitioned columns
        map.put(partInfo.ident().columnIdent().fqn(), null);
    }
    partitionMaps.add(map);
    return map;
}
Also used : HashMap(java.util.HashMap) Reference(io.crate.metadata.Reference)

Example 50 with Reference

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

the class LuceneOrderedDocCollector method nextPageQuery.

@Nullable
@VisibleForTesting
static Query nextPageQuery(FieldDoc lastCollected, OrderBy orderBy, Object[] missingValues, FieldTypeLookup fieldTypeLookup) {
    BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    for (int i = 0; i < orderBy.orderBySymbols().size(); i++) {
        Symbol order = orderBy.orderBySymbols().get(i);
        Object value = lastCollected.fields[i];
        if (order instanceof Reference) {
            boolean nullsFirst = orderBy.nullsFirst()[i] == null ? false : orderBy.nullsFirst()[i];
            value = value == null || value.equals(missingValues[i]) ? null : value;
            if (nullsFirst && value == null) {
                // no filter needed
                continue;
            }
            String columnName = ((Reference) order).ident().columnIdent().fqn();
            MappedFieldType fieldType = requireNonNull(fieldTypeLookup.get(columnName), "Column must exist: " + columnName);
            Query orderQuery;
            // nulls already gone, so they should be excluded
            if (nullsFirst) {
                BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
                booleanQuery.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
                if (orderBy.reverseFlags()[i]) {
                    booleanQuery.add(fieldType.rangeQuery(null, value, false, true), BooleanClause.Occur.MUST_NOT);
                } else {
                    booleanQuery.add(fieldType.rangeQuery(value, null, true, false), BooleanClause.Occur.MUST_NOT);
                }
                orderQuery = booleanQuery.build();
            } else {
                if (orderBy.reverseFlags()[i]) {
                    orderQuery = fieldType.rangeQuery(value, null, false, false);
                } else {
                    orderQuery = fieldType.rangeQuery(null, value, false, false);
                }
            }
            queryBuilder.add(orderQuery, BooleanClause.Occur.MUST);
        }
    }
    BooleanQuery query = queryBuilder.build();
    if (query.clauses().size() > 0) {
        return query;
    } else {
        return null;
    }
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) Reference(io.crate.metadata.Reference) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Aggregations

Reference (io.crate.metadata.Reference)84 Test (org.junit.Test)57 CrateUnitTest (io.crate.test.integration.CrateUnitTest)55 TestingHelpers.mapToSortedString (io.crate.testing.TestingHelpers.mapToSortedString)27 HashMap (java.util.HashMap)20 Map (java.util.Map)20 NestedObjectExpression (io.crate.operation.reference.NestedObjectExpression)17 ColumnIdent (io.crate.metadata.ColumnIdent)15 Symbol (io.crate.analyze.symbol.Symbol)14 BytesRef (org.apache.lucene.util.BytesRef)11 TableIdent (io.crate.metadata.TableIdent)9 ReferenceIdent (io.crate.metadata.ReferenceIdent)8 InputColumn (io.crate.analyze.symbol.InputColumn)6 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)6 ShardId (org.elasticsearch.index.shard.ShardId)5 Routing (io.crate.metadata.Routing)4 UUID (java.util.UUID)4 OrderBy (io.crate.analyze.OrderBy)3 ShardResponse (io.crate.executor.transport.ShardResponse)3 ShardUpsertRequest (io.crate.executor.transport.ShardUpsertRequest)3