Search in sources :

Example 16 with Statement

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

the class Session method analyze.

public void analyze(String statementName, Statement statement, List<DataType> paramTypes, @Nullable String query) {
    AnalyzedStatement analyzedStatement;
    DataType[] parameterTypes;
    try {
        analyzedStatement = analyzer.analyze(statement, sessionContext, new ParamTypeHints(paramTypes));
        parameterTypes = parameterTypeExtractor.getParameterTypes(x -> Relations.traverseDeepSymbols(analyzedStatement, x));
    } catch (Throwable t) {
        jobsLogs.logPreExecutionFailure(UUIDs.dirtyUUID(), query == null ? statementName : query, SQLExceptions.messageOf(t), sessionContext.sessionUser());
        throw t;
    }
    preparedStatements.put(statementName, new PreparedStmt(statement, analyzedStatement, query, parameterTypes));
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) RetryOnFailureResultReceiver(io.crate.protocols.postgres.RetryOnFailureResultReceiver) Analyzer(io.crate.analyze.Analyzer) DependencyCarrier(io.crate.planner.DependencyCarrier) ClusterState(org.elasticsearch.cluster.ClusterState) Relations(io.crate.analyze.Relations) RowN(io.crate.data.RowN) TransactionState(io.crate.protocols.postgres.TransactionState) Map(java.util.Map) JobsLogsUpdateListener(io.crate.protocols.postgres.JobsLogsUpdateListener) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) AnalyzedStatement(io.crate.analyze.AnalyzedStatement) UUIDs(org.elasticsearch.common.UUIDs) UUID(java.util.UUID) Lists2(io.crate.common.collections.Lists2) List(java.util.List) Logger(org.apache.logging.log4j.Logger) AnalyzedDiscard(io.crate.analyze.AnalyzedDiscard) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) AnalyzedBegin(io.crate.analyze.AnalyzedBegin) SubQueryResults(io.crate.planner.operators.SubQueryResults) Statement(io.crate.sql.tree.Statement) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) AnalyzedCommit(io.crate.analyze.AnalyzedCommit) AccessControl(io.crate.auth.AccessControl) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) JobsLogs(io.crate.execution.engine.collect.stats.JobsLogs) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Portal(io.crate.protocols.postgres.Portal) Symbols(io.crate.expression.symbol.Symbols) FormatCodes(io.crate.protocols.postgres.FormatCodes) SqlParser(io.crate.sql.parser.SqlParser) Nullable(javax.annotation.Nullable) Iterator(java.util.Iterator) RelationInfo(io.crate.metadata.RelationInfo) Target(io.crate.sql.tree.DiscardStatement.Target) DataType(io.crate.types.DataType) Planner(io.crate.planner.Planner) RoutingProvider(io.crate.metadata.RoutingProvider) RowConsumer(io.crate.data.RowConsumer) StatementClassifier(io.crate.planner.operators.StatementClassifier) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) ReadOnlyException(io.crate.exceptions.ReadOnlyException) SQLExceptions(io.crate.exceptions.SQLExceptions) LogManager(org.apache.logging.log4j.LogManager) Randomness(org.elasticsearch.common.Randomness) AnalyzedDeallocate(io.crate.analyze.AnalyzedDeallocate) DataType(io.crate.types.DataType) AnalyzedStatement(io.crate.analyze.AnalyzedStatement) ParamTypeHints(io.crate.analyze.ParamTypeHints)

Example 17 with Statement

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

the class ExplainStatementAnalyzer method analyze.

public ExplainAnalyzedStatement analyze(Explain node, Analysis analysis) {
    Statement statement = node.getStatement();
    statement.accept(CHECK_VISITOR, null);
    final AnalyzedStatement subStatement;
    ProfilingContext profilingContext;
    if (node.isAnalyze()) {
        profilingContext = new ProfilingContext(List.of());
        Timer timer = profilingContext.createAndStartTimer(ExplainPlan.Phase.Analyze.name());
        subStatement = analyzer.analyzedStatement(statement, analysis);
        profilingContext.stopTimerAndStoreDuration(timer);
    } else {
        profilingContext = null;
        subStatement = analyzer.analyzedStatement(statement, analysis);
    }
    String columnName = SqlFormatter.formatSql(node);
    return new ExplainAnalyzedStatement(columnName, subStatement, profilingContext);
}
Also used : Timer(io.crate.profile.Timer) Statement(io.crate.sql.tree.Statement) ProfilingContext(io.crate.profile.ProfilingContext)

Example 18 with Statement

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

the class DocIndexMetadataTest method getDocIndexMetadataFromStatement.

private DocIndexMetadata getDocIndexMetadataFromStatement(String stmt) throws IOException {
    Statement statement = SqlParser.createStatement(stmt);
    DocTableInfoFactory docTableInfoFactory = new InternalDocTableInfoFactory(nodeCtx, new IndexNameExpressionResolver());
    ViewInfoFactory viewInfoFactory = (ident, state) -> null;
    DocSchemaInfo docSchemaInfo = new DocSchemaInfo(Schemas.DOC_SCHEMA_NAME, clusterService, nodeCtx, udfService, viewInfoFactory, docTableInfoFactory);
    Path homeDir = createTempDir();
    Schemas schemas = new Schemas(Map.of("doc", docSchemaInfo), clusterService, new DocSchemaInfoFactory(docTableInfoFactory, viewInfoFactory, nodeCtx, udfService));
    FulltextAnalyzerResolver fulltextAnalyzerResolver = new FulltextAnalyzerResolver(clusterService, new AnalysisRegistry(new Environment(Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), homeDir.toString()).build(), homeDir.resolve("config")), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()));
    CreateTableStatementAnalyzer analyzer = new CreateTableStatementAnalyzer(nodeCtx);
    Analysis analysis = new Analysis(new CoordinatorTxnCtx(SessionContext.systemSessionContext()), ParamTypeHints.EMPTY);
    CoordinatorTxnCtx txnCtx = new CoordinatorTxnCtx(SessionContext.systemSessionContext());
    AnalyzedCreateTable analyzedCreateTable = analyzer.analyze((CreateTable<Expression>) statement, analysis.paramTypeHints(), analysis.transactionContext());
    BoundCreateTable analyzedStatement = CreateTablePlan.bind(analyzedCreateTable, txnCtx, nodeCtx, Row.EMPTY, SubQueryResults.EMPTY, new NumberOfShards(clusterService), schemas, fulltextAnalyzerResolver);
    Settings.Builder settingsBuilder = Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).put("index.version.created", org.elasticsearch.Version.CURRENT).put(analyzedStatement.tableParameter().settings());
    IndexMetadata indexMetadata = IndexMetadata.builder(analyzedStatement.tableIdent().name()).settings(settingsBuilder).putMapping(new MappingMetadata(Constants.DEFAULT_MAPPING_TYPE, analyzedStatement.mapping())).build();
    return newMeta(indexMetadata, analyzedStatement.tableIdent().name());
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) Arrays(java.util.Arrays) SessionContext(io.crate.action.sql.SessionContext) RelationName(io.crate.metadata.RelationName) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Environment(org.elasticsearch.env.Environment) Matchers.not(org.hamcrest.Matchers.not) UserDefinedFunctionService(io.crate.expression.udf.UserDefinedFunctionService) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Matchers.hasItems(org.hamcrest.Matchers.hasItems) Matchers.hasKey(org.hamcrest.Matchers.hasKey) ArrayType(io.crate.types.ArrayType) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) CreateTableStatementAnalyzer(io.crate.analyze.CreateTableStatementAnalyzer) Path(java.nio.file.Path) TestingHelpers.createNodeContext(io.crate.testing.TestingHelpers.createNodeContext) BoundCreateTable(io.crate.analyze.BoundCreateTable) FulltextAnalyzerResolver(io.crate.metadata.FulltextAnalyzerResolver) AnalyzedCreateTable(io.crate.analyze.AnalyzedCreateTable) NodeContext(io.crate.metadata.NodeContext) GeneratedReference(io.crate.metadata.GeneratedReference) SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) AnalysisRegistry(org.elasticsearch.index.analysis.AnalysisRegistry) BytesReference(org.elasticsearch.common.bytes.BytesReference) Collectors(java.util.stream.Collectors) Lists2(io.crate.common.collections.Lists2) CheckConstraint(io.crate.sql.tree.CheckConstraint) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Row(io.crate.data.Row) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) DataTypes(io.crate.types.DataTypes) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SubQueryResults(io.crate.planner.operators.SubQueryResults) Statement(io.crate.sql.tree.Statement) Matchers.is(org.hamcrest.Matchers.is) Expression(io.crate.sql.tree.Expression) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) XContentType(org.elasticsearch.common.xcontent.XContentType) Analysis(io.crate.analyze.Analysis) ViewInfoFactory(io.crate.metadata.view.ViewInfoFactory) HashMap(java.util.HashMap) ObjectType(io.crate.types.ObjectType) IndexReference(io.crate.metadata.IndexReference) ArrayList(java.util.ArrayList) XContentHelper(org.elasticsearch.common.xcontent.XContentHelper) Constants(io.crate.Constants) CreateTablePlan(io.crate.planner.node.ddl.CreateTablePlan) ColumnPolicy(io.crate.sql.tree.ColumnPolicy) SqlParser(io.crate.sql.parser.SqlParser) Before(org.junit.Before) CreateTable(io.crate.sql.tree.CreateTable) Collections.emptyMap(java.util.Collections.emptyMap) StringType(io.crate.types.StringType) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) DataType(io.crate.types.DataType) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) IOException(java.io.IOException) NumberOfShards(io.crate.analyze.NumberOfShards) TreeMap(java.util.TreeMap) Schemas(io.crate.metadata.Schemas) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) SymbolMatchers.isLiteral(io.crate.testing.SymbolMatchers.isLiteral) Path(java.nio.file.Path) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) Statement(io.crate.sql.tree.Statement) AnalyzedCreateTable(io.crate.analyze.AnalyzedCreateTable) Schemas(io.crate.metadata.Schemas) NumberOfShards(io.crate.analyze.NumberOfShards) AnalysisRegistry(org.elasticsearch.index.analysis.AnalysisRegistry) CreateTableStatementAnalyzer(io.crate.analyze.CreateTableStatementAnalyzer) BoundCreateTable(io.crate.analyze.BoundCreateTable) ViewInfoFactory(io.crate.metadata.view.ViewInfoFactory) FulltextAnalyzerResolver(io.crate.metadata.FulltextAnalyzerResolver) Expression(io.crate.sql.tree.Expression) Analysis(io.crate.analyze.Analysis) Environment(org.elasticsearch.env.Environment) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

Statement (io.crate.sql.tree.Statement)18 BeginStatement (io.crate.sql.tree.BeginStatement)8 CommitStatement (io.crate.sql.tree.CommitStatement)8 DeallocateStatement (io.crate.sql.tree.DeallocateStatement)8 KillStatement (io.crate.sql.tree.KillStatement)8 SetSessionAuthorizationStatement (io.crate.sql.tree.SetSessionAuthorizationStatement)8 SetStatement (io.crate.sql.tree.SetStatement)8 Test (org.junit.Test)8 Lists2 (io.crate.common.collections.Lists2)6 AnalyzedStatement (io.crate.analyze.AnalyzedStatement)5 ParamTypeHints (io.crate.analyze.ParamTypeHints)5 AccessControl (io.crate.auth.AccessControl)5 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)5 Row (io.crate.data.Row)5 Symbol (io.crate.expression.symbol.Symbol)5 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)5 NodeContext (io.crate.metadata.NodeContext)5 SqlParser (io.crate.sql.parser.SqlParser)5 DataType (io.crate.types.DataType)5 ArrayList (java.util.ArrayList)5