use of io.crate.metadata.Reference in project crate by crate.
the class FetchPhase method writeTo.
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(executionPhaseId);
out.writeVInt(executionNodes.size());
for (String executionNode : executionNodes) {
out.writeString(executionNode);
}
out.writeVInt(bases.size());
for (Map.Entry<String, Integer> entry : bases.entrySet()) {
out.writeString(entry.getKey());
out.writeVInt(entry.getValue());
}
out.writeVInt(fetchRefs.size());
for (Reference ref : fetchRefs) {
Reference.toStream(ref, out);
}
Map<TableIdent, Collection<String>> map = tableIndices.asMap();
out.writeVInt(map.size());
for (Map.Entry<TableIdent, Collection<String>> entry : map.entrySet()) {
entry.getKey().writeTo(out);
out.writeVInt(entry.getValue().size());
for (String s : entry.getValue()) {
out.writeString(s);
}
}
}
use of io.crate.metadata.Reference in project crate by crate.
the class ColumnIndexWriterProjection method writeTo.
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (columnSymbols == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
Symbols.toStream(columnSymbols, out);
}
if (columnReferences == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeVInt(columnReferences.size());
for (Reference columnIdent : columnReferences) {
Reference.toStream(columnIdent, out);
}
}
if (onDuplicateKeyAssignments == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeVInt(onDuplicateKeyAssignments.size());
for (Map.Entry<Reference, Symbol> entry : onDuplicateKeyAssignments.entrySet()) {
Reference.toStream(entry.getKey(), out);
Symbols.toStream(entry.getValue(), out);
}
}
}
use of io.crate.metadata.Reference in project crate by crate.
the class CopyStatementPlanner method planCopyFrom.
public Plan planCopyFrom(CopyFromAnalyzedStatement analysis, Planner.Context context) {
/**
* copy from has two "modes":
*
* 1: non-partitioned tables or partitioned tables with partition ident --> import into single es index
* -> collect raw source and import as is
*
* 2: partitioned table without partition ident
* -> collect document and partition by values
* -> exclude partitioned by columns from document
* -> insert into es index (partition determined by partition by value)
*/
DocTableInfo table = analysis.table();
int clusteredByPrimaryKeyIdx = table.primaryKey().indexOf(analysis.table().clusteredBy());
List<String> partitionedByNames;
String partitionIdent = null;
List<BytesRef> partitionValues;
if (analysis.partitionIdent() == null) {
if (table.isPartitioned()) {
partitionedByNames = Lists.newArrayList(Lists.transform(table.partitionedBy(), ColumnIdent::fqn));
} else {
partitionedByNames = Collections.emptyList();
}
partitionValues = ImmutableList.of();
} else {
assert table.isPartitioned() : "table must be partitioned if partitionIdent is set";
// partitionIdent is present -> possible to index raw source into concrete es index
partitionValues = PartitionName.decodeIdent(analysis.partitionIdent());
partitionIdent = analysis.partitionIdent();
partitionedByNames = Collections.emptyList();
}
SourceIndexWriterProjection sourceIndexWriterProjection = new SourceIndexWriterProjection(table.ident(), partitionIdent, table.getReference(DocSysColumns.RAW), table.primaryKey(), table.partitionedBy(), partitionValues, table.clusteredBy(), clusteredByPrimaryKeyIdx, analysis.settings(), null, partitionedByNames.size() > 0 ? partitionedByNames.toArray(new String[partitionedByNames.size()]) : null, // autoCreateIndices
table.isPartitioned());
List<Projection> projections = Collections.<Projection>singletonList(sourceIndexWriterProjection);
partitionedByNames.removeAll(Lists.transform(table.primaryKey(), ColumnIdent::fqn));
int referencesSize = table.primaryKey().size() + partitionedByNames.size() + 1;
referencesSize = clusteredByPrimaryKeyIdx == -1 ? referencesSize + 1 : referencesSize;
List<Symbol> toCollect = new ArrayList<>(referencesSize);
// add primaryKey columns
for (ColumnIdent primaryKey : table.primaryKey()) {
toCollect.add(table.getReference(primaryKey));
}
// add partitioned columns (if not part of primaryKey)
Set<Reference> referencedReferences = new HashSet<>();
for (String partitionedColumn : partitionedByNames) {
Reference reference = table.getReference(ColumnIdent.fromPath(partitionedColumn));
Symbol symbol;
if (reference instanceof GeneratedReference) {
symbol = ((GeneratedReference) reference).generatedExpression();
referencedReferences.addAll(((GeneratedReference) reference).referencedReferences());
} else {
symbol = reference;
}
toCollect.add(symbol);
}
// add clusteredBy column (if not part of primaryKey)
if (clusteredByPrimaryKeyIdx == -1 && table.clusteredBy() != null && !DocSysColumns.ID.equals(table.clusteredBy())) {
toCollect.add(table.getReference(table.clusteredBy()));
}
// add _raw or _doc
if (table.isPartitioned() && analysis.partitionIdent() == null) {
toCollect.add(table.getReference(DocSysColumns.DOC));
} else {
toCollect.add(table.getReference(DocSysColumns.RAW));
}
// add columns referenced by generated columns which are used as partitioned by column
for (Reference reference : referencedReferences) {
if (!toCollect.contains(reference)) {
toCollect.add(reference);
}
}
DiscoveryNodes allNodes = clusterService.state().nodes();
FileUriCollectPhase collectPhase = new FileUriCollectPhase(context.jobId(), context.nextExecutionPhaseId(), "copyFrom", getExecutionNodes(allNodes, analysis.settings().getAsInt("num_readers", allNodes.getSize()), analysis.nodePredicate()), analysis.uri(), toCollect, projections, analysis.settings().get("compression", null), analysis.settings().getAsBoolean("shared", null));
Collect collect = new Collect(collectPhase, TopN.NO_LIMIT, 0, 1, 1, null);
return Merge.ensureOnHandler(collect, context, Collections.singletonList(MergeCountProjection.INSTANCE));
}
use of io.crate.metadata.Reference in project crate by crate.
the class FetchContextTest method testGetIndexServiceForInvalidReaderId.
@Test
public void testGetIndexServiceForInvalidReaderId() throws Exception {
final FetchContext context = new FetchContext(new FetchPhase(1, null, new TreeMap<String, Integer>(), HashMultimap.<TableIdent, String>create(), ImmutableList.<Reference>of()), "dummy", new SharedShardContexts(mock(IndicesService.class)), Collections.<Routing>emptyList());
expectedException.expect(IllegalArgumentException.class);
context.indexService(10);
}
Aggregations