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));
}
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")));
}
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)));
}
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;
}
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;
}
}
Aggregations