use of io.crate.metadata.Reference in project crate by crate.
the class LogFunctionTest method testNormalizeReference.
@Test
public void testNormalizeReference() throws Exception {
Reference dB = createReference("dB", DataTypes.DOUBLE);
LogFunction log10 = getFunction(LogFunction.NAME, DataTypes.DOUBLE);
Function function = new Function(log10.info(), Arrays.<Symbol>asList(dB));
Function normalized = (Function) log10.normalizeSymbol(function, transactionContext);
assertThat(normalized, Matchers.sameInstance(function));
LogFunction ln = getFunction(LogFunction.LnFunction.NAME, DataTypes.DOUBLE);
function = new Function(ln.info(), Arrays.<Symbol>asList(dB));
normalized = (Function) ln.normalizeSymbol(function, transactionContext);
assertThat(normalized, Matchers.sameInstance(function));
LogFunction logBase = getFunction(LogFunction.NAME, DataTypes.DOUBLE, DataTypes.LONG);
function = new Function(logBase.info(), Arrays.<Symbol>asList(dB, Literal.of(10L)));
normalized = (Function) logBase.normalizeSymbol(function, transactionContext);
assertThat(normalized, Matchers.sameInstance(function));
Reference base = createReference("base", DataTypes.INTEGER);
function = new Function(logBase.info(), Arrays.<Symbol>asList(dB, base));
normalized = (Function) logBase.normalizeSymbol(function, transactionContext);
assertThat(normalized, Matchers.sameInstance(function));
}
use of io.crate.metadata.Reference in project crate by crate.
the class FetchContext method innerPrepare.
@Override
public void innerPrepare() {
HashMap<String, TableIdent> index2TableIdent = new HashMap<>();
for (Map.Entry<TableIdent, Collection<String>> entry : phase.tableIndices().asMap().entrySet()) {
for (String indexName : entry.getValue()) {
index2TableIdent.put(indexName, entry.getKey());
}
}
Set<TableIdent> tablesWithFetchRefs = new HashSet<>();
for (Reference reference : phase.fetchRefs()) {
tablesWithFetchRefs.add(reference.ident().tableIdent());
}
for (Routing routing : routingIterable) {
Map<String, Map<String, List<Integer>>> locations = routing.locations();
Map<String, List<Integer>> indexShards = locations.get(localNodeId);
for (Map.Entry<String, List<Integer>> indexShardsEntry : indexShards.entrySet()) {
String index = indexShardsEntry.getKey();
Integer base = phase.bases().get(index);
if (base == null) {
continue;
}
TableIdent ident = index2TableIdent.get(index);
assert ident != null : "no tableIdent found for index " + index;
tableIdents.put(base, ident);
toFetch.put(ident, new ArrayList<Reference>());
for (Integer shard : indexShardsEntry.getValue()) {
ShardId shardId = new ShardId(index, shard);
int readerId = base + shardId.id();
SharedShardContext shardContext = shardContexts.get(readerId);
if (shardContext == null) {
shardContext = sharedShardContexts.createContext(shardId, readerId);
shardContexts.put(readerId, shardContext);
if (tablesWithFetchRefs.contains(ident)) {
try {
searchers.put(readerId, shardContext.acquireSearcher());
} catch (IndexNotFoundException e) {
if (!PartitionName.isPartition(index)) {
throw e;
}
}
}
}
}
}
}
for (Reference reference : phase.fetchRefs()) {
Collection<Reference> references = toFetch.get(reference.ident().tableIdent());
if (references != null) {
references.add(reference);
}
}
}
use of io.crate.metadata.Reference in project crate by crate.
the class MultiSourceFetchPushDown method process.
void process() {
remainingOutputs = statement.querySpec().outputs();
statement.querySpec().outputs(new ArrayList<>());
HashMap<Symbol, Symbol> topLevelOutputMap = new HashMap<>(statement.canBeFetched().size());
HashMap<Symbol, Symbol> mssOutputMap = new HashMap<>(statement.querySpec().outputs().size() + 2);
ArrayList<Symbol> mssOutputs = new ArrayList<>(statement.sources().size() + statement.requiredForQuery().size());
for (Map.Entry<QualifiedName, RelationSource> entry : statement.sources().entrySet()) {
RelationSource source = entry.getValue();
if (!(source.relation() instanceof DocTableRelation)) {
int index = 0;
for (Symbol output : source.querySpec().outputs()) {
RelationColumn rc = new RelationColumn(entry.getKey(), index++, output.valueType());
mssOutputs.add(rc);
mssOutputMap.put(output, rc);
topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
}
continue;
}
DocTableRelation rel = (DocTableRelation) source.relation();
HashSet<Field> canBeFetched = filterByRelation(statement.canBeFetched(), rel);
if (!canBeFetched.isEmpty()) {
RelationColumn fetchIdColumn = new RelationColumn(entry.getKey(), 0, DataTypes.LONG);
mssOutputs.add(fetchIdColumn);
InputColumn fetchIdInput = new InputColumn(mssOutputs.size() - 1);
ArrayList<Symbol> qtOutputs = new ArrayList<>(source.querySpec().outputs().size() - canBeFetched.size() + 1);
Reference fetchId = rel.tableInfo().getReference(DocSysColumns.FETCHID);
qtOutputs.add(fetchId);
for (Symbol output : source.querySpec().outputs()) {
if (!canBeFetched.contains(output)) {
qtOutputs.add(output);
RelationColumn rc = new RelationColumn(entry.getKey(), qtOutputs.size() - 1, output.valueType());
mssOutputs.add(rc);
mssOutputMap.put(output, rc);
topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
}
}
for (Field field : canBeFetched) {
FetchReference fr = new FetchReference(fetchIdInput, DocReferenceConverter.toSourceLookup(rel.resolveField(field)));
allocateFetchedReference(fr, rel);
topLevelOutputMap.put(field, fr);
}
source.querySpec().outputs(qtOutputs);
} else {
int index = 0;
for (Symbol output : source.querySpec().outputs()) {
RelationColumn rc = new RelationColumn(entry.getKey(), index++, output.valueType());
mssOutputs.add(rc);
mssOutputMap.put(output, rc);
topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
}
}
}
statement.querySpec().outputs(mssOutputs);
MappingSymbolVisitor.inPlace().processInplace(remainingOutputs, topLevelOutputMap);
if (statement.querySpec().orderBy().isPresent()) {
MappingSymbolVisitor.inPlace().processInplace(statement.querySpec().orderBy().get().orderBySymbols(), mssOutputMap);
}
}
use of io.crate.metadata.Reference in project crate by crate.
the class UpdatePlannerTest method testUpdateByQueryPlan.
@Test
public void testUpdateByQueryPlan() throws Exception {
Upsert plan = e.plan("update users set name='Vogon lyric fan'");
assertThat(plan.nodes().size(), is(1));
Merge merge = (Merge) plan.nodes().get(0);
Collect collect = (Collect) merge.subPlan();
RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
assertThat(collectPhase.routing(), is(TableDefinitions.SHARD_ROUTING));
assertFalse(collectPhase.whereClause().noMatch());
assertFalse(collectPhase.whereClause().hasQuery());
assertThat(collectPhase.projections().size(), is(1));
assertThat(collectPhase.projections().get(0), instanceOf(UpdateProjection.class));
assertThat(collectPhase.toCollect().size(), is(1));
assertThat(collectPhase.toCollect().get(0), instanceOf(Reference.class));
assertThat(((Reference) collectPhase.toCollect().get(0)).ident().columnIdent().fqn(), is("_id"));
UpdateProjection updateProjection = (UpdateProjection) collectPhase.projections().get(0);
assertThat(updateProjection.uidSymbol(), instanceOf(InputColumn.class));
assertThat(updateProjection.assignmentsColumns()[0], is("name"));
Symbol symbol = updateProjection.assignments()[0];
assertThat(symbol, isLiteral("Vogon lyric fan", DataTypes.STRING));
MergePhase mergePhase = merge.mergePhase();
assertThat(mergePhase.projections().size(), is(1));
assertThat(mergePhase.projections().get(0), instanceOf(MergeCountProjection.class));
assertThat(mergePhase.outputTypes().size(), is(1));
}
use of io.crate.metadata.Reference in project crate by crate.
the class FetchPhaseTest method testStreaming.
@Test
public void testStreaming() throws Exception {
TableIdent t1 = new TableIdent(null, "t1");
TreeMap<String, Integer> bases = new TreeMap<String, Integer>();
bases.put(t1.name(), 0);
bases.put("i2", 1);
Multimap<TableIdent, String> tableIndices = HashMultimap.create();
tableIndices.put(t1, t1.name());
tableIndices.put(new TableIdent(null, "i2"), "i2_s1");
tableIndices.put(new TableIdent(null, "i2"), "i2_s2");
ReferenceIdent nameIdent = new ReferenceIdent(t1, "name");
Reference name = new Reference(nameIdent, RowGranularity.DOC, DataTypes.STRING);
FetchPhase orig = new FetchPhase(1, ImmutableSet.<String>of("node1", "node2"), bases, tableIndices, ImmutableList.of(name));
BytesStreamOutput out = new BytesStreamOutput();
ExecutionPhases.toStream(out, orig);
StreamInput in = StreamInput.wrap(out.bytes());
FetchPhase streamed = (FetchPhase) ExecutionPhases.fromStream(in);
assertThat(orig.phaseId(), is(streamed.phaseId()));
assertThat(orig.nodeIds(), is(streamed.nodeIds()));
assertThat(orig.fetchRefs(), is(streamed.fetchRefs()));
assertThat(orig.bases(), is(streamed.bases()));
assertThat(orig.tableIndices(), is(streamed.tableIndices()));
}
Aggregations