Search in sources :

Example 1 with AnalyzedColumnDefinition

use of io.crate.analyze.AnalyzedColumnDefinition in project crate by crate.

the class AlterTableAddColumnPlan method bind.

@VisibleForTesting
public static BoundAddColumn bind(AnalyzedAlterTableAddColumn alterTable, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row params, SubQueryResults subQueryResults, FulltextAnalyzerResolver fulltextAnalyzerResolver) {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, params, subQueryResults);
    DocTableInfo tableInfo = alterTable.tableInfo();
    AnalyzedTableElements<Object> tableElements = alterTable.analyzedTableElements().map(eval);
    for (AnalyzedColumnDefinition<Object> column : tableElements.columns()) {
        ensureColumnLeafsAreNew(column, tableInfo);
    }
    addExistingPrimaryKeys(tableInfo, tableElements);
    ensureNoIndexDefinitions(tableElements.columns());
    addExistingCheckConstraints(tableInfo, tableElements);
    // validate table elements
    AnalyzedTableElements<Symbol> tableElementsUnboundWithExpressions = alterTable.analyzedTableElementsWithExpressions();
    Settings tableSettings = AnalyzedTableElements.validateAndBuildSettings(tableElements, fulltextAnalyzerResolver);
    Map<String, Object> mapping = AnalyzedTableElements.finalizeAndValidate(tableInfo.ident(), tableElementsUnboundWithExpressions, tableElements);
    int numCurrentPks = tableInfo.primaryKey().size();
    if (tableInfo.primaryKey().contains(DocSysColumns.ID)) {
        numCurrentPks -= 1;
    }
    boolean hasNewPrimaryKeys = AnalyzedTableElements.primaryKeys(tableElements).size() > numCurrentPks;
    boolean hasGeneratedColumns = tableElementsUnboundWithExpressions.hasGeneratedColumns();
    return new BoundAddColumn(tableInfo, tableElements, tableSettings, mapping, hasNewPrimaryKeys, hasGeneratedColumns);
}
Also used : AnalyzedColumnDefinition(io.crate.analyze.AnalyzedColumnDefinition) HashMap(java.util.HashMap) ObjectType(io.crate.types.ObjectType) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DependencyCarrier(io.crate.planner.DependencyCarrier) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Settings(org.elasticsearch.common.settings.Settings) Locale(java.util.Locale) Map(java.util.Map) OneRowActionListener(io.crate.execution.support.OneRowActionListener) AnalyzedAlterTableAddColumn(io.crate.analyze.AnalyzedAlterTableAddColumn) FulltextAnalyzerResolver(io.crate.metadata.FulltextAnalyzerResolver) LinkedHashSet(java.util.LinkedHashSet) DocSysColumns(io.crate.metadata.doc.DocSysColumns) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) BoundAddColumn(io.crate.analyze.BoundAddColumn) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) CheckConstraint(io.crate.sql.tree.CheckConstraint) RowConsumer(io.crate.data.RowConsumer) Objects(java.util.Objects) AnalyzedTableElements(io.crate.analyze.AnalyzedTableElements) List(java.util.List) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) DocTableInfo(io.crate.metadata.doc.DocTableInfo) Symbol(io.crate.expression.symbol.Symbol) CheckConstraint(io.crate.sql.tree.CheckConstraint) BoundAddColumn(io.crate.analyze.BoundAddColumn) Settings(org.elasticsearch.common.settings.Settings) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 2 with AnalyzedColumnDefinition

use of io.crate.analyze.AnalyzedColumnDefinition in project crate by crate.

the class AlterTableAddColumnPlan method addExistingPrimaryKeys.

static void addExistingPrimaryKeys(DocTableInfo tableInfo, AnalyzedTableElements<Object> tableElements) {
    LinkedHashSet<ColumnIdent> pkIncludingAncestors = new LinkedHashSet<>();
    for (ColumnIdent pkIdent : tableInfo.primaryKey()) {
        if (pkIdent.name().equals(DocSysColumns.Names.ID)) {
            continue;
        }
        ColumnIdent maybeParent = pkIdent;
        pkIncludingAncestors.add(maybeParent);
        while ((maybeParent = maybeParent.getParent()) != null) {
            pkIncludingAncestors.add(maybeParent);
        }
    }
    ArrayList<ColumnIdent> columnsToBuildHierarchy = new ArrayList<>(pkIncludingAncestors);
    // We want to have the root columns earlier in the list so that the loop below can be sure parent elements are already present in `columns`
    columnsToBuildHierarchy.sort(Comparator.comparingInt(c -> c.path().size()));
    HashMap<ColumnIdent, AnalyzedColumnDefinition<Object>> columns = new HashMap<>();
    for (ColumnIdent column : columnsToBuildHierarchy) {
        ColumnIdent parent = column.getParent();
        // sort of `columnsToBuildHierarchy` ensures parent would already have been processed and must be present in columns
        AnalyzedColumnDefinition<Object> parentDef = columns.get(parent);
        AnalyzedColumnDefinition<Object> columnDef = new AnalyzedColumnDefinition<>(0, parentDef);
        columns.put(column, columnDef);
        columnDef.ident(column);
        if (tableInfo.primaryKey().contains(column)) {
            columnDef.setPrimaryKeyConstraint();
        }
        Reference reference = Objects.requireNonNull(tableInfo.getReference(column), "Must be able to retrieve Reference for any column that is part of `primaryKey()`");
        if (reference.valueType().id() != ObjectType.ID) {
            columnDef.indexConstraint(reference.indexType());
        }
        columnDef.dataType(reference.valueType().getName());
        if (parentDef != null) {
            parentDef.addChild(columnDef);
        }
        if (column.isTopLevel()) {
            tableElements.add(columnDef);
        }
    }
    for (ColumnIdent columnIdent : tableInfo.partitionedBy()) {
        AnalyzedTableElements.changeToPartitionedByColumn(tableElements, columnIdent, true, tableInfo.ident());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnalyzedColumnDefinition(io.crate.analyze.AnalyzedColumnDefinition) HashMap(java.util.HashMap) ObjectType(io.crate.types.ObjectType) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DependencyCarrier(io.crate.planner.DependencyCarrier) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Settings(org.elasticsearch.common.settings.Settings) Locale(java.util.Locale) Map(java.util.Map) OneRowActionListener(io.crate.execution.support.OneRowActionListener) AnalyzedAlterTableAddColumn(io.crate.analyze.AnalyzedAlterTableAddColumn) FulltextAnalyzerResolver(io.crate.metadata.FulltextAnalyzerResolver) LinkedHashSet(java.util.LinkedHashSet) DocSysColumns(io.crate.metadata.doc.DocSysColumns) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) BoundAddColumn(io.crate.analyze.BoundAddColumn) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) CheckConstraint(io.crate.sql.tree.CheckConstraint) RowConsumer(io.crate.data.RowConsumer) Objects(java.util.Objects) AnalyzedTableElements(io.crate.analyze.AnalyzedTableElements) List(java.util.List) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ColumnIdent(io.crate.metadata.ColumnIdent) HashMap(java.util.HashMap) Reference(io.crate.metadata.Reference) ArrayList(java.util.ArrayList) AnalyzedColumnDefinition(io.crate.analyze.AnalyzedColumnDefinition)

Aggregations

AnalyzedAlterTableAddColumn (io.crate.analyze.AnalyzedAlterTableAddColumn)2 AnalyzedColumnDefinition (io.crate.analyze.AnalyzedColumnDefinition)2 AnalyzedTableElements (io.crate.analyze.AnalyzedTableElements)2 BoundAddColumn (io.crate.analyze.BoundAddColumn)2 SymbolEvaluator (io.crate.analyze.SymbolEvaluator)2 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)2 Row (io.crate.data.Row)2 Row1 (io.crate.data.Row1)2 RowConsumer (io.crate.data.RowConsumer)2 OneRowActionListener (io.crate.execution.support.OneRowActionListener)2 Symbol (io.crate.expression.symbol.Symbol)2 ColumnIdent (io.crate.metadata.ColumnIdent)2 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)2 FulltextAnalyzerResolver (io.crate.metadata.FulltextAnalyzerResolver)2 NodeContext (io.crate.metadata.NodeContext)2 Reference (io.crate.metadata.Reference)2 DocSysColumns (io.crate.metadata.doc.DocSysColumns)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 TableInfo (io.crate.metadata.table.TableInfo)2 DependencyCarrier (io.crate.planner.DependencyCarrier)2