Search in sources :

Example 1 with AnalyzedTableElements

use of io.crate.analyze.AnalyzedTableElements 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 AnalyzedTableElements

use of io.crate.analyze.AnalyzedTableElements 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)

Example 3 with AnalyzedTableElements

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

the class CreateTablePlan method bind.

@VisibleForTesting
public static BoundCreateTable bind(AnalyzedCreateTable createTable, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row params, SubQueryResults subQueryResults, NumberOfShards numberOfShards, Schemas schemas, FulltextAnalyzerResolver fulltextAnalyzerResolver) {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, params, subQueryResults);
    CreateTable<Symbol> table = createTable.createTable();
    RelationName relationName = createTable.relationName();
    GenericProperties<Object> properties = table.properties().map(eval);
    AnalyzedTableElements<Object> tableElements = createTable.analyzedTableElements().map(eval);
    TableParameter tableParameter = new TableParameter();
    Optional<ClusteredBy<Object>> mappedClusteredBy = table.clusteredBy().map(x -> x.map(eval));
    Integer numShards = mappedClusteredBy.flatMap(ClusteredBy::numberOfShards).map(numberOfShards::fromNumberOfShards).orElseGet(numberOfShards::defaultNumberOfShards);
    tableParameter.settingsBuilder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numShards);
    // apply default in case it is not specified in the properties,
    // if it is it will get overwritten afterwards.
    TablePropertiesAnalyzer.analyzeWithBoundValues(tableParameter, TableParameters.TABLE_CREATE_PARAMETER_INFO, properties, true);
    AnalyzedTableElements<Symbol> tableElementsWithExpressions = createTable.analyzedTableElementsWithExpressions().map(x -> SubQueryAndParamBinder.convert(x, params, subQueryResults));
    // validate table elements
    AnalyzedTableElements.finalizeAndValidate(relationName, tableElementsWithExpressions, tableElements);
    // update table settings
    Settings tableSettings = AnalyzedTableElements.validateAndBuildSettings(tableElements, fulltextAnalyzerResolver);
    tableParameter.settingsBuilder().put(tableSettings);
    ColumnIdent routingColumn = mappedClusteredBy.map(clusteredBy -> resolveRoutingFromClusteredBy(clusteredBy, tableElements)).orElse(null);
    Optional<PartitionedBy<Object>> partitionedByOptional = table.partitionedBy().map(x -> x.map(eval));
    partitionedByOptional.ifPresent(partitionedBy -> processPartitionedBy(partitionedByOptional.get(), tableElements, relationName, routingColumn));
    return new BoundCreateTable(relationName, tableElements, tableParameter, routingColumn, table.ifNotExists(), schemas);
}
Also used : AnalyzedColumnDefinition(io.crate.analyze.AnalyzedColumnDefinition) RelationName(io.crate.metadata.RelationName) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) SENTINEL(io.crate.data.SentinelRow.SENTINEL) GenericProperties(io.crate.sql.tree.GenericProperties) Function(java.util.function.Function) DependencyCarrier(io.crate.planner.DependencyCarrier) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Settings(org.elasticsearch.common.settings.Settings) Locale(java.util.Locale) OneRowActionListener(io.crate.execution.support.OneRowActionListener) BoundCreateTable(io.crate.analyze.BoundCreateTable) FulltextAnalyzerResolver(io.crate.metadata.FulltextAnalyzerResolver) Nullable(javax.annotation.Nullable) TableParameter(io.crate.analyze.TableParameter) CreateTable(io.crate.sql.tree.CreateTable) AnalyzedCreateTable(io.crate.analyze.AnalyzedCreateTable) NodeContext(io.crate.metadata.NodeContext) PartitionedBy(io.crate.sql.tree.PartitionedBy) TableCreator(io.crate.execution.ddl.tables.TableCreator) TablePropertiesAnalyzer(io.crate.analyze.TablePropertiesAnalyzer) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) ColumnIdent(io.crate.metadata.ColumnIdent) SubQueryAndParamBinder(io.crate.planner.operators.SubQueryAndParamBinder) TableParameters(io.crate.analyze.TableParameters) RowConsumer(io.crate.data.RowConsumer) AnalyzedTableElements(io.crate.analyze.AnalyzedTableElements) NumberOfShards(io.crate.analyze.NumberOfShards) ClusteredBy(io.crate.sql.tree.ClusteredBy) 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) Optional(java.util.Optional) Schemas(io.crate.metadata.Schemas) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) Symbol(io.crate.expression.symbol.Symbol) TableParameter(io.crate.analyze.TableParameter) PartitionedBy(io.crate.sql.tree.PartitionedBy) ColumnIdent(io.crate.metadata.ColumnIdent) BoundCreateTable(io.crate.analyze.BoundCreateTable) ClusteredBy(io.crate.sql.tree.ClusteredBy) RelationName(io.crate.metadata.RelationName) Settings(org.elasticsearch.common.settings.Settings) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 4 with AnalyzedTableElements

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

the class AlterTableDropCheckConstraintPlan method bind.

@VisibleForTesting
public static BoundAddColumn bind(AnalyzedAlterTableDropCheckConstraint dropCheckConstraint) {
    DocTableInfo tableInfo = dropCheckConstraint.tableInfo();
    AnalyzedTableElements<Object> tableElementsBound = new AnalyzedTableElements<>();
    AlterTableAddColumnPlan.addExistingPrimaryKeys(tableInfo, tableElementsBound);
    tableInfo.checkConstraints().stream().filter(c -> !dropCheckConstraint.name().equals(c.name())).forEach(c -> tableElementsBound.addCheckConstraint(tableInfo.ident(), c));
    return new BoundAddColumn(tableInfo, tableElementsBound, Settings.builder().build(), AnalyzedTableElements.finalizeAndValidate(tableInfo.ident(), new AnalyzedTableElements<>(), tableElementsBound), false, false);
}
Also used : AnalyzedTableElements(io.crate.analyze.AnalyzedTableElements) DocTableInfo(io.crate.metadata.doc.DocTableInfo) BoundAddColumn(io.crate.analyze.BoundAddColumn) AnalyzedAlterTableDropCheckConstraint(io.crate.analyze.AnalyzedAlterTableDropCheckConstraint) RowConsumer(io.crate.data.RowConsumer) DependencyCarrier(io.crate.planner.DependencyCarrier) AnalyzedTableElements(io.crate.analyze.AnalyzedTableElements) Settings(org.elasticsearch.common.settings.Settings) Row(io.crate.data.Row) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) OneRowActionListener(io.crate.execution.support.OneRowActionListener) Row1(io.crate.data.Row1) DocTableInfo(io.crate.metadata.doc.DocTableInfo) BoundAddColumn(io.crate.analyze.BoundAddColumn) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Aggregations

AnalyzedTableElements (io.crate.analyze.AnalyzedTableElements)4 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)4 Row (io.crate.data.Row)4 Row1 (io.crate.data.Row1)4 RowConsumer (io.crate.data.RowConsumer)4 OneRowActionListener (io.crate.execution.support.OneRowActionListener)4 DependencyCarrier (io.crate.planner.DependencyCarrier)4 Plan (io.crate.planner.Plan)4 PlannerContext (io.crate.planner.PlannerContext)4 SubQueryResults (io.crate.planner.operators.SubQueryResults)4 Settings (org.elasticsearch.common.settings.Settings)4 AnalyzedColumnDefinition (io.crate.analyze.AnalyzedColumnDefinition)3 BoundAddColumn (io.crate.analyze.BoundAddColumn)3 SymbolEvaluator (io.crate.analyze.SymbolEvaluator)3 Symbol (io.crate.expression.symbol.Symbol)3 ColumnIdent (io.crate.metadata.ColumnIdent)3 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)3 FulltextAnalyzerResolver (io.crate.metadata.FulltextAnalyzerResolver)3 NodeContext (io.crate.metadata.NodeContext)3 DocTableInfo (io.crate.metadata.doc.DocTableInfo)3