Search in sources :

Example 1 with GeneratedReference

use of io.crate.metadata.GeneratedReference in project crate by crate.

the class DocTableRelation method ensureColumnCanBeUpdated.

/**
 * @throws io.crate.exceptions.ColumnValidationException if the column cannot be updated
 */
@VisibleForTesting
void ensureColumnCanBeUpdated(ColumnIdent ci) {
    if (ci.isSystemColumn()) {
        throw new ColumnValidationException(ci.toString(), tableInfo.ident(), "Updating a system column is not supported");
    }
    for (ColumnIdent pkIdent : tableInfo.primaryKey()) {
        ensureNotUpdated(ci, pkIdent, "Updating a primary key is not supported");
    }
    if (tableInfo.clusteredBy() != null) {
        ensureNotUpdated(ci, tableInfo.clusteredBy(), "Updating a clustered-by column is not supported");
    }
    List<GeneratedReference> generatedReferences = tableInfo.generatedColumns();
    for (Reference partitionRef : tableInfo.partitionedByColumns()) {
        ensureNotUpdated(ci, partitionRef.column(), "Updating a partitioned-by column is not supported");
        if (!(partitionRef instanceof GeneratedReference)) {
            continue;
        }
        int idx = generatedReferences.indexOf(partitionRef);
        if (idx >= 0) {
            GeneratedReference generatedReference = generatedReferences.get(idx);
            for (Reference reference : generatedReference.referencedReferences()) {
                ensureNotUpdated(ci, reference.column(), "Updating a column which is referenced in a partitioned by generated column expression is not supported");
            }
        }
    }
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) GeneratedReference(io.crate.metadata.GeneratedReference) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) ColumnValidationException(io.crate.exceptions.ColumnValidationException) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 2 with GeneratedReference

use of io.crate.metadata.GeneratedReference in project crate by crate.

the class CopyToPlan method bind.

@VisibleForTesting
public static BoundCopyTo bind(AnalyzedCopyTo copyTo, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row parameters, SubQueryResults subQueryResults) {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, subQueryResults);
    DocTableInfo table = (DocTableInfo) copyTo.tableInfo();
    List<String> partitions = resolvePartitions(Lists2.map(copyTo.table().partitionProperties(), x -> x.map(eval)), table);
    List<Symbol> outputs = new ArrayList<>();
    Map<ColumnIdent, Symbol> overwrites = null;
    boolean columnsDefined = false;
    final List<String> outputNames = new ArrayList<>(copyTo.columns().size());
    if (!copyTo.columns().isEmpty()) {
        // TODO: remove outputNames?
        for (Symbol symbol : copyTo.columns()) {
            assert symbol instanceof Reference : "Only references are expected here";
            RefVisitor.visitRefs(symbol, r -> outputNames.add(r.column().sqlFqn()));
            outputs.add(DocReferences.toSourceLookup(symbol));
        }
        columnsDefined = true;
    } else {
        Reference sourceRef;
        if (table.isPartitioned() && partitions.isEmpty()) {
            // table is partitioned, insert partitioned columns into the output
            overwrites = new HashMap<>();
            for (Reference reference : table.partitionedByColumns()) {
                if (!(reference instanceof GeneratedReference)) {
                    overwrites.put(reference.column(), reference);
                }
            }
            if (overwrites.size() > 0) {
                sourceRef = table.getReference(DocSysColumns.DOC);
            } else {
                sourceRef = table.getReference(DocSysColumns.RAW);
            }
        } else {
            sourceRef = table.getReference(DocSysColumns.RAW);
        }
        outputs = List.of(sourceRef);
    }
    Settings settings = genericPropertiesToSettings(copyTo.properties().map(eval));
    WriterProjection.CompressionType compressionType = settingAsEnum(WriterProjection.CompressionType.class, COMPRESSION_SETTING.get(settings));
    WriterProjection.OutputFormat outputFormat = settingAsEnum(WriterProjection.OutputFormat.class, OUTPUT_FORMAT_SETTING.get(settings));
    if (!columnsDefined && outputFormat == WriterProjection.OutputFormat.JSON_ARRAY) {
        throw new UnsupportedFeatureException("Output format not supported without specifying columns.");
    }
    WhereClause whereClause = new WhereClause(copyTo.whereClause(), partitions, Collections.emptySet());
    return new BoundCopyTo(outputs, table, whereClause, Literal.of(DataTypes.STRING.sanitizeValue(eval.apply(copyTo.uri()))), compressionType, outputFormat, outputNames.isEmpty() ? null : outputNames, columnsDefined, overwrites, settings);
}
Also used : UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) GenericPropertiesConverter.genericPropertiesToSettings(io.crate.analyze.GenericPropertiesConverter.genericPropertiesToSettings) Match(io.crate.planner.optimizer.matcher.Match) BoundCopyTo(io.crate.analyze.BoundCopyTo) DependencyCarrier(io.crate.planner.DependencyCarrier) RefVisitor(io.crate.expression.symbol.RefVisitor) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) TableStats(io.crate.statistics.TableStats) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) OUTPUT_FORMAT_SETTING(io.crate.analyze.CopyStatementSettings.OUTPUT_FORMAT_SETTING) DocSysColumns(io.crate.metadata.doc.DocSysColumns) DocTableInfo(io.crate.metadata.doc.DocTableInfo) NodeContext(io.crate.metadata.NodeContext) LogicalPlan(io.crate.planner.operators.LogicalPlan) Captures(io.crate.planner.optimizer.matcher.Captures) GeneratedReference(io.crate.metadata.GeneratedReference) Collect(io.crate.planner.operators.Collect) Set(java.util.Set) Lists2(io.crate.common.collections.Lists2) ExecutionPlan(io.crate.planner.ExecutionPlan) OptimizeCollectWhereClauseAccess(io.crate.planner.optimizer.rule.OptimizeCollectWhereClauseAccess) DocReferences(io.crate.metadata.DocReferences) List(java.util.List) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) DataTypes(io.crate.types.DataTypes) SubQueryResults(io.crate.planner.operators.SubQueryResults) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) PartitionPropertiesAnalyzer(io.crate.analyze.PartitionPropertiesAnalyzer) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Assignment(io.crate.sql.tree.Assignment) COMPRESSION_SETTING(io.crate.analyze.CopyStatementSettings.COMPRESSION_SETTING) AnalyzedCopyTo(io.crate.analyze.AnalyzedCopyTo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) MergeCountProjection(io.crate.execution.dsl.projection.MergeCountProjection) WhereClause(io.crate.analyze.WhereClause) NodeOperationTree(io.crate.execution.dsl.phases.NodeOperationTree) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) NodeOperationTreeGenerator(io.crate.execution.engine.NodeOperationTreeGenerator) Merge(io.crate.planner.Merge) RowConsumer(io.crate.data.RowConsumer) WriterProjection(io.crate.execution.dsl.projection.WriterProjection) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Literal(io.crate.expression.symbol.Literal) PlannerContext(io.crate.planner.PlannerContext) CopyStatementSettings.settingAsEnum(io.crate.analyze.CopyStatementSettings.settingAsEnum) Plan(io.crate.planner.Plan) Collections(java.util.Collections) DocTableInfo(io.crate.metadata.doc.DocTableInfo) GeneratedReference(io.crate.metadata.GeneratedReference) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) BoundCopyTo(io.crate.analyze.BoundCopyTo) Symbol(io.crate.expression.symbol.Symbol) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) ArrayList(java.util.ArrayList) WhereClause(io.crate.analyze.WhereClause) WriterProjection(io.crate.execution.dsl.projection.WriterProjection) ColumnIdent(io.crate.metadata.ColumnIdent) GenericPropertiesConverter.genericPropertiesToSettings(io.crate.analyze.GenericPropertiesConverter.genericPropertiesToSettings) Settings(org.elasticsearch.common.settings.Settings) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 3 with GeneratedReference

use of io.crate.metadata.GeneratedReference in project crate by crate.

the class CopyFromPlan method rewriteToCollectToUsePartitionValues.

private static void rewriteToCollectToUsePartitionValues(List<Reference> partitionedByColumns, List<String> partitionValues, List<Symbol> toCollect) {
    for (int i = 0; i < partitionValues.size(); i++) {
        Reference partitionedByColumn = partitionedByColumns.get(i);
        int idx;
        if (partitionedByColumn instanceof GeneratedReference) {
            idx = toCollect.indexOf(((GeneratedReference) partitionedByColumn).generatedExpression());
        } else {
            idx = toCollect.indexOf(partitionedByColumn);
        }
        if (idx > -1) {
            toCollect.set(idx, Literal.of(partitionValues.get(i)));
        }
    }
}
Also used : GeneratedReference(io.crate.metadata.GeneratedReference) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference)

Example 4 with GeneratedReference

use of io.crate.metadata.GeneratedReference in project crate by crate.

the class GeneratedColumns method validateValues.

void validateValues(HashMap<String, Object> source) {
    for (var entry : toValidate.entrySet()) {
        Reference ref = entry.getKey();
        Object providedValue = ValueExtractors.fromMap(source, ref.column());
        if (providedValue == null && !ref.column().isTopLevel()) {
            // If they're null here we can assume that they'll be generated after the validation
            continue;
        }
        Object generatedValue = entry.getValue().value();
        // noinspection unchecked
        DataType<Object> dataType = (DataType<Object>) ref.valueType();
        if (Comparator.nullsFirst(dataType).compare(dataType.sanitizeValue(generatedValue), dataType.sanitizeValue(providedValue)) != 0) {
            throw new IllegalArgumentException("Given value " + providedValue + " for generated column " + ref.column() + " does not match calculation " + ((GeneratedReference) ref).formattedGeneratedExpression() + " = " + generatedValue);
        }
    }
}
Also used : GeneratedReference(io.crate.metadata.GeneratedReference) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) DataType(io.crate.types.DataType)

Example 5 with GeneratedReference

use of io.crate.metadata.GeneratedReference in project crate by crate.

the class AnalyzedTableElements method buildReference.

private static <T> void buildReference(RelationName relationName, AnalyzedColumnDefinition<T> columnDefinition, List<Reference> references) {
    Reference reference;
    DataType<?> type = columnDefinition.dataType() == null ? DataTypes.UNDEFINED : columnDefinition.dataType();
    DataType<?> realType = ArrayType.NAME.equals(columnDefinition.collectionType()) ? new ArrayType<>(type) : type;
    if (columnDefinition.isGenerated() == false) {
        reference = new Reference(new ReferenceIdent(relationName, columnDefinition.ident()), RowGranularity.DOC, realType, columnDefinition.position, // not required in this context
        null);
    } else {
        reference = new GeneratedReference(columnDefinition.position, new ReferenceIdent(relationName, columnDefinition.ident()), RowGranularity.DOC, realType, "dummy expression, real one not needed here");
    }
    references.add(reference);
    for (AnalyzedColumnDefinition<T> childDefinition : columnDefinition.children()) {
        buildReference(relationName, childDefinition, references);
    }
}
Also used : GeneratedReference(io.crate.metadata.GeneratedReference) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) ReferenceIdent(io.crate.metadata.ReferenceIdent)

Aggregations

GeneratedReference (io.crate.metadata.GeneratedReference)16 Reference (io.crate.metadata.Reference)14 ColumnIdent (io.crate.metadata.ColumnIdent)9 IndexReference (io.crate.metadata.IndexReference)4 ArrayList (java.util.ArrayList)4 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)3 Symbol (io.crate.expression.symbol.Symbol)3 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)3 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)2 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)2 TableReferenceResolver (io.crate.analyze.expressions.TableReferenceResolver)2 Lists2 (io.crate.common.collections.Lists2)2 GeoReference (io.crate.metadata.GeoReference)2 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 Test (org.junit.Test)2 AnalyzedCopyTo (io.crate.analyze.AnalyzedCopyTo)1 BoundCopyTo (io.crate.analyze.BoundCopyTo)1