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);
}
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());
}
}
Aggregations