use of io.crate.analyze.BoundAddColumn 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.BoundAddColumn in project crate by crate.
the class TransportSchemaUpdateActionTest method testTemplateMappingUpdateFailsIfTypeIsDifferent.
@Test
public void testTemplateMappingUpdateFailsIfTypeIsDifferent() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addPartitionedTable("create table t (p int) partitioned by (p)").build();
ClusterState currentState = clusterService.state();
PlannerContext plannerContext = e.getPlannerContext(currentState);
BoundAddColumn addXLong = AlterTableAddColumnPlan.bind(e.analyze("alter table t add column x long"), plannerContext.transactionContext(), plannerContext.nodeContext(), Row.EMPTY, SubQueryResults.EMPTY, null);
BoundAddColumn addXString = AlterTableAddColumnPlan.bind(e.analyze("alter table t add column x string"), plannerContext.transactionContext(), plannerContext.nodeContext(), Row.EMPTY, SubQueryResults.EMPTY, null);
String templateName = templateName("doc", "t");
IndexTemplateMetadata template = currentState.metadata().templates().get(templateName);
ClusterState stateWithXLong = ClusterState.builder(currentState).metadata(Metadata.builder(currentState.metadata()).put(IndexTemplateMetadata.builder(templateName).patterns(template.patterns()).putMapping(Constants.DEFAULT_MAPPING_TYPE, Strings.toString(JsonXContent.contentBuilder().map(addXLong.mapping())))).build()).build();
expectedException.expect(IllegalArgumentException.class);
TransportSchemaUpdateAction.updateTemplate(NamedXContentRegistry.EMPTY, stateWithXLong, templateName, addXString.mapping());
}
use of io.crate.analyze.BoundAddColumn 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);
}
use of io.crate.analyze.BoundAddColumn in project crate by crate.
the class AlterTableAddColumnPlan method executeOrFail.
@Override
public void executeOrFail(DependencyCarrier dependencies, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) throws Exception {
BoundAddColumn stmt = bind(alterTable, plannerContext.transactionContext(), dependencies.nodeContext(), params, subQueryResults, dependencies.fulltextAnalyzerResolver());
dependencies.alterTableOperation().executeAlterTableAddColumn(stmt).whenComplete(new OneRowActionListener<>(consumer, rCount -> new Row1(rCount == null ? -1 : rCount)));
}
Aggregations