Search in sources :

Example 26 with ColumnIdent

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;
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference)

Example 27 with ColumnIdent

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;
}
Also used : QualifiedName(io.crate.sql.tree.QualifiedName) ColumnIdent(io.crate.metadata.ColumnIdent) Field(io.crate.analyze.symbol.Field) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) AmbiguousColumnException(io.crate.exceptions.AmbiguousColumnException) Map(java.util.Map)

Example 28 with ColumnIdent

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;
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) Field(io.crate.analyze.symbol.Field) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException)

Example 29 with ColumnIdent

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);
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent)

Example 30 with ColumnIdent

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));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) GeneratedReference(io.crate.metadata.GeneratedReference) Collect(io.crate.planner.node.dql.Collect) Symbol(io.crate.analyze.symbol.Symbol) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) SourceIndexWriterProjection(io.crate.planner.projection.SourceIndexWriterProjection) WriterProjection(io.crate.planner.projection.WriterProjection) MergeCountProjection(io.crate.planner.projection.MergeCountProjection) SourceIndexWriterProjection(io.crate.planner.projection.SourceIndexWriterProjection) Projection(io.crate.planner.projection.Projection) FileUriCollectPhase(io.crate.planner.node.dql.FileUriCollectPhase) ColumnIdent(io.crate.metadata.ColumnIdent) BytesRef(org.apache.lucene.util.BytesRef) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Aggregations

ColumnIdent (io.crate.metadata.ColumnIdent)34 Reference (io.crate.metadata.Reference)15 CrateUnitTest (io.crate.test.integration.CrateUnitTest)12 Test (org.junit.Test)12 Map (java.util.Map)5 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)4 GeneratedReference (io.crate.metadata.GeneratedReference)4 NestedObjectExpression (io.crate.operation.reference.NestedObjectExpression)3 TestingHelpers.mapToSortedString (io.crate.testing.TestingHelpers.mapToSortedString)3 ArrayType (io.crate.types.ArrayType)3 HashMap (java.util.HashMap)3 Field (io.crate.analyze.symbol.Field)2 Symbol (io.crate.analyze.symbol.Symbol)2 ColumnValidationException (io.crate.exceptions.ColumnValidationException)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 FileUriCollectPhase (io.crate.planner.node.dql.FileUriCollectPhase)2 DataType (io.crate.types.DataType)2 BytesRef (org.apache.lucene.util.BytesRef)2 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1