Search in sources :

Example 1 with CreateView

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

the class ViewAnalyzer method analyze.

public CreateViewStmt analyze(CreateView createView, CoordinatorTxnCtx txnCtx) {
    RelationName name = RelationName.of(createView.name(), txnCtx.sessionContext().searchPath().currentSchema());
    name.ensureValidForRelationCreation();
    if (BlobSchemaInfo.NAME.equals(name.schema())) {
        throw new UnsupportedOperationException("Creating a view in the \"blob\" schema is not supported");
    }
    AnalyzedRelation query;
    String formattedQuery;
    try {
        formattedQuery = SqlFormatter.formatSql(createView.query());
    } catch (Exception e) {
        throw new UnsupportedOperationException("Invalid query used in CREATE VIEW. Query: " + createView.query());
    }
    try {
        // Analyze the formatted Query to make sure the formatting didn't mess it up in any way.
        query = relationAnalyzer.analyze((Query) SqlParser.createStatement(formattedQuery), txnCtx, ParamTypeHints.EMPTY);
    } catch (Exception e) {
        throw new UnsupportedOperationException("Invalid query used in CREATE VIEW. " + e.getMessage() + ". Query: " + formattedQuery);
    }
    if (query.outputs().stream().map(f -> Symbols.pathFromSymbol(f).sqlFqn()).distinct().count() != query.outputs().size()) {
        throw new IllegalArgumentException("Query in CREATE VIEW must not have duplicate column names");
    }
    return new CreateViewStmt(name, query, createView.query(), createView.replaceExisting(), txnCtx.sessionContext().sessionUser());
}
Also used : RelationName(io.crate.metadata.RelationName) SqlFormatter(io.crate.sql.SqlFormatter) DropView(io.crate.sql.tree.DropView) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) ArrayList(java.util.ArrayList) CreateView(io.crate.sql.tree.CreateView) RelationsUnknown(io.crate.exceptions.RelationsUnknown) Symbols(io.crate.expression.symbol.Symbols) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) RelationAnalyzer(io.crate.analyze.relations.RelationAnalyzer) SqlParser(io.crate.sql.parser.SqlParser) Schemas(io.crate.metadata.Schemas) Query(io.crate.sql.tree.Query) RelationUnknown(io.crate.exceptions.RelationUnknown) QualifiedName(io.crate.sql.tree.QualifiedName) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) Query(io.crate.sql.tree.Query) RelationName(io.crate.metadata.RelationName) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation)

Aggregations

AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)1 RelationAnalyzer (io.crate.analyze.relations.RelationAnalyzer)1 RelationUnknown (io.crate.exceptions.RelationUnknown)1 RelationsUnknown (io.crate.exceptions.RelationsUnknown)1 Symbols (io.crate.expression.symbol.Symbols)1 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)1 RelationName (io.crate.metadata.RelationName)1 Schemas (io.crate.metadata.Schemas)1 BlobSchemaInfo (io.crate.metadata.blob.BlobSchemaInfo)1 SqlFormatter (io.crate.sql.SqlFormatter)1 SqlParser (io.crate.sql.parser.SqlParser)1 CreateView (io.crate.sql.tree.CreateView)1 DropView (io.crate.sql.tree.DropView)1 QualifiedName (io.crate.sql.tree.QualifiedName)1 Query (io.crate.sql.tree.Query)1 ArrayList (java.util.ArrayList)1