Search in sources :

Example 1 with AlterTableAddColumn

use of io.crate.sql.tree.AlterTableAddColumn in project crate by crate.

the class AlterTableAddColumnAnalyzer method analyze.

public AnalyzedAlterTableAddColumn analyze(AlterTableAddColumn<Expression> alterTable, ParamTypeHints paramTypeHints, CoordinatorTxnCtx txnCtx) {
    if (!alterTable.table().partitionProperties().isEmpty()) {
        throw new UnsupportedOperationException("Adding a column to a single partition is not supported");
    }
    DocTableInfo tableInfo = (DocTableInfo) schemas.resolveTableInfo(alterTable.table().getName(), Operation.ALTER, txnCtx.sessionContext().sessionUser(), txnCtx.sessionContext().searchPath());
    TableReferenceResolver referenceResolver = new TableReferenceResolver(tableInfo.columns(), tableInfo.ident());
    var exprAnalyzerWithReferenceResolver = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, referenceResolver, null);
    var exprAnalyzerWithFieldsAsString = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, FieldProvider.FIELDS_AS_LITERAL, null);
    var exprCtx = new ExpressionAnalysisContext(txnCtx.sessionContext());
    AddColumnDefinition<Expression> tableElement = alterTable.tableElement();
    // convert and validate the column name
    ExpressionToColumnIdentVisitor.convert(tableElement.name());
    // 1st phase, exclude check constraints (their expressions contain column references) and generated expressions
    AddColumnDefinition<Symbol> addColumnDefinition = new AddColumnDefinition<>(exprAnalyzerWithFieldsAsString.convert(tableElement.name(), exprCtx), // expression must be mapped later on using mapExpressions()
    null, tableElement.type() == null ? null : tableElement.type().map(y -> exprAnalyzerWithFieldsAsString.convert(y, exprCtx)), tableElement.constraints().stream().filter(c -> false == c instanceof CheckColumnConstraint).map(x -> x.map(y -> exprAnalyzerWithFieldsAsString.convert(y, exprCtx))).collect(Collectors.toList()), false, tableElement.generatedExpression() != null);
    AnalyzedTableElements<Symbol> analyzedTableElements = TableElementsAnalyzer.analyze(singletonList(addColumnDefinition), tableInfo.ident(), tableInfo);
    // 2nd phase, analyze possible generated expressions
    AddColumnDefinition<Symbol> addColumnDefinitionWithExpression = (AddColumnDefinition<Symbol>) tableElement.mapExpressions(addColumnDefinition, x -> exprAnalyzerWithReferenceResolver.convert(x, exprCtx));
    AnalyzedTableElements<Symbol> analyzedTableElementsWithExpressions = TableElementsAnalyzer.analyze(singletonList(addColumnDefinitionWithExpression), tableInfo.ident(), tableInfo);
    // now analyze possible check expressions
    var checkColumnConstraintsAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, paramTypeHints, new SelfReferenceFieldProvider(tableInfo.ident(), referenceResolver, analyzedTableElements.columns()), null);
    tableElement.constraints().stream().filter(CheckColumnConstraint.class::isInstance).map(x -> x.map(y -> checkColumnConstraintsAnalyzer.convert(y, exprCtx))).forEach(c -> {
        CheckColumnConstraint<Symbol> check = (CheckColumnConstraint<Symbol>) c;
        analyzedTableElements.addCheckColumnConstraint(tableInfo.ident(), check);
        analyzedTableElementsWithExpressions.addCheckColumnConstraint(tableInfo.ident(), check);
    });
    return new AnalyzedAlterTableAddColumn(tableInfo, analyzedTableElements, analyzedTableElementsWithExpressions);
}
Also used : ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) AlterTableAddColumn(io.crate.sql.tree.AlterTableAddColumn) RelationName(io.crate.metadata.RelationName) FieldProvider(io.crate.analyze.relations.FieldProvider) Operation(io.crate.metadata.table.Operation) TableReferenceResolver(io.crate.analyze.expressions.TableReferenceResolver) Collections.singletonList(java.util.Collections.singletonList) Locale(java.util.Locale) ExpressionToColumnIdentVisitor(io.crate.analyze.expressions.ExpressionToColumnIdentVisitor) QualifiedName(io.crate.sql.tree.QualifiedName) Nullable(javax.annotation.Nullable) DocTableInfo(io.crate.metadata.doc.DocTableInfo) NodeContext(io.crate.metadata.NodeContext) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) CheckColumnConstraint(io.crate.sql.tree.CheckColumnConstraint) Collectors(java.util.stream.Collectors) List(java.util.List) RowGranularity(io.crate.metadata.RowGranularity) Symbol(io.crate.expression.symbol.Symbol) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) Schemas(io.crate.metadata.Schemas) ReferenceIdent(io.crate.metadata.ReferenceIdent) AddColumnDefinition(io.crate.sql.tree.AddColumnDefinition) Expression(io.crate.sql.tree.Expression) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) DocTableInfo(io.crate.metadata.doc.DocTableInfo) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.expression.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) AddColumnDefinition(io.crate.sql.tree.AddColumnDefinition) Expression(io.crate.sql.tree.Expression) CheckColumnConstraint(io.crate.sql.tree.CheckColumnConstraint) TableReferenceResolver(io.crate.analyze.expressions.TableReferenceResolver)

Aggregations

ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)1 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)1 ExpressionToColumnIdentVisitor (io.crate.analyze.expressions.ExpressionToColumnIdentVisitor)1 TableReferenceResolver (io.crate.analyze.expressions.TableReferenceResolver)1 FieldProvider (io.crate.analyze.relations.FieldProvider)1 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)1 Symbol (io.crate.expression.symbol.Symbol)1 ColumnIdent (io.crate.metadata.ColumnIdent)1 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)1 NodeContext (io.crate.metadata.NodeContext)1 Reference (io.crate.metadata.Reference)1 ReferenceIdent (io.crate.metadata.ReferenceIdent)1 RelationName (io.crate.metadata.RelationName)1 RowGranularity (io.crate.metadata.RowGranularity)1 Schemas (io.crate.metadata.Schemas)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 Operation (io.crate.metadata.table.Operation)1 AddColumnDefinition (io.crate.sql.tree.AddColumnDefinition)1 AlterTableAddColumn (io.crate.sql.tree.AlterTableAddColumn)1 CheckColumnConstraint (io.crate.sql.tree.CheckColumnConstraint)1