use of io.crate.metadata.ColumnIdent in project crate by crate.
the class AbstractTableRelation method hasMatchingParent.
/**
* return true if the given {@linkplain com.google.common.base.Predicate}
* returns true for a parent column of this one.
* returns false if info has no parent column.
*/
private boolean hasMatchingParent(Reference info, Predicate<Reference> parentMatchPredicate) {
ColumnIdent parent = info.ident().columnIdent().getParent();
while (parent != null) {
Reference parentInfo = tableInfo.getReference(parent);
if (parentMatchPredicate.apply(parentInfo)) {
return true;
}
parent = parent.getParent();
}
return false;
}
use of io.crate.metadata.ColumnIdent in project crate by crate.
the class FullQualifedNameFieldProvider method resolveField.
public Field resolveField(QualifiedName qualifiedName, @Nullable List<String> path, Operation operation) {
List<String> parts = qualifiedName.getParts();
String columnSchema = null;
String columnTableName = null;
ColumnIdent columnIdent = new ColumnIdent(parts.get(parts.size() - 1), path);
switch(parts.size()) {
case 1:
break;
case 2:
columnTableName = parts.get(0);
break;
case 3:
columnSchema = parts.get(0);
columnTableName = parts.get(1);
break;
default:
throw new IllegalArgumentException("Column reference \"%s\" has too many parts. " + "A column reference can have at most 3 parts and must have one of the following formats: " + "\"<column>\", \"<table>.<column>\" or \"<schema>.<table>.<column>\"");
}
boolean schemaMatched = false;
boolean tableNameMatched = false;
Field lastField = null;
for (Map.Entry<QualifiedName, AnalyzedRelation> entry : sources.entrySet()) {
List<String> sourceParts = entry.getKey().getParts();
String sourceSchema = null;
String sourceTableOrAlias;
if (sourceParts.size() == 1) {
sourceTableOrAlias = sourceParts.get(0);
} else if (sourceParts.size() == 2) {
sourceSchema = sourceParts.get(0);
sourceTableOrAlias = sourceParts.get(1);
} else {
throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "sources key (QualifiedName) must have 1 or 2 parts, not %d", sourceParts.size()));
}
AnalyzedRelation sourceRelation = entry.getValue();
if (columnSchema != null && sourceSchema != null && !columnSchema.equals(sourceSchema)) {
continue;
}
schemaMatched = true;
if (columnTableName != null && !sourceTableOrAlias.equals(columnTableName)) {
continue;
}
tableNameMatched = true;
Field newField = sourceRelation.getField(columnIdent, operation);
if (newField != null) {
if (lastField != null) {
throw new AmbiguousColumnException(columnIdent);
}
lastField = newField;
}
}
if (lastField == null) {
if (!schemaMatched || !tableNameMatched) {
throw RelationUnknownException.of(columnSchema, columnTableName);
}
throw new ColumnUnknownException(columnIdent.sqlFqn());
}
return lastField;
}
use of io.crate.metadata.ColumnIdent in project crate by crate.
the class NameFieldProvider method resolveField.
public Field resolveField(QualifiedName qualifiedName, @Nullable List<String> path, Operation operation) {
List<String> parts = qualifiedName.getParts();
ColumnIdent columnIdent = new ColumnIdent(parts.get(parts.size() - 1), path);
if (parts.size() != 1) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Column reference \"%s\" has too many parts. " + "A column must not have a schema or a table here.", qualifiedName));
}
Field field = relation.getField(columnIdent, operation);
if (field == null) {
throw new ColumnUnknownException(columnIdent.sqlFqn());
}
return field;
}
use of io.crate.metadata.ColumnIdent in project crate by crate.
the class AbstractIndexWriterProjection method writeTo.
@Override
public void writeTo(StreamOutput out) throws IOException {
tableIdent.writeTo(out);
out.writeOptionalString(partitionIdent);
Symbols.toStream(idSymbols, out);
out.writeVInt(primaryKeys.size());
for (ColumnIdent primaryKey : primaryKeys) {
primaryKey.writeTo(out);
}
Symbols.toStream(partitionedBySymbols, out);
if (clusteredBySymbol == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
Symbols.toStream(clusteredBySymbol, out);
}
if (clusteredByColumn == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
clusteredByColumn.writeTo(out);
}
out.writeVInt(bulkActions);
out.writeBoolean(autoCreateIndices);
}
use of io.crate.metadata.ColumnIdent 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));
}
Aggregations