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