Search in sources :

Example 1 with SessionContext

use of io.crate.action.sql.SessionContext in project crate by crate.

the class CreateTableStatementAnalyzer method analyze.

public CreateTableAnalyzedStatement analyze(CreateTable createTable, ParameterContext parameterContext, SessionContext sessionContext) {
    CreateTableAnalyzedStatement statement = new CreateTableAnalyzedStatement();
    Row parameters = parameterContext.parameters();
    TableIdent tableIdent = getTableIdent(createTable, sessionContext);
    statement.table(tableIdent, createTable.ifNotExists(), schemas);
    // apply default in case it is not specified in the genericProperties,
    // if it is it will get overwritten afterwards.
    TablePropertiesAnalyzer.analyze(statement.tableParameter(), new TableParameterInfo(), createTable.properties(), parameters, true);
    AnalyzedTableElements tableElements = TableElementsAnalyzer.analyze(createTable.tableElements(), parameters, fulltextAnalyzerResolver, null);
    // validate table elements
    tableElements.finalizeAndValidate(tableIdent, Collections.emptyList(), functions, parameterContext, sessionContext);
    // update table settings
    statement.tableParameter().settingsBuilder().put(tableElements.settings());
    statement.tableParameter().settingsBuilder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, numberOfShards.defaultNumberOfShards());
    Context context = new Context(statement, parameterContext);
    statement.analyzedTableElements(tableElements);
    for (CrateTableOption option : createTable.crateTableOptions()) {
        process(option, context);
    }
    return statement;
}
Also used : SessionContext(io.crate.action.sql.SessionContext) Row(io.crate.data.Row)

Example 2 with SessionContext

use of io.crate.action.sql.SessionContext in project crate by crate.

the class CreateAlterTableStatementAnalyzerTest method testExplicitSchemaHasPrecedenceOverDefaultSchema.

@Test
public void testExplicitSchemaHasPrecedenceOverDefaultSchema() throws Exception {
    CreateTableAnalyzedStatement statement = (CreateTableAnalyzedStatement) e.analyzer.boundAnalyze(SqlParser.createStatement("create table foo.bar (x string)"), new SessionContext(0, Option.NONE, "hoschi"), new ParameterContext(Row.EMPTY, Collections.<Row>emptyList())).analyzedStatement();
    // schema from statement must take precedence
    assertThat(statement.tableIdent().schema(), is("foo"));
}
Also used : SessionContext(io.crate.action.sql.SessionContext) Row(io.crate.data.Row) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 3 with SessionContext

use of io.crate.action.sql.SessionContext in project crate by crate.

the class CurrentSchemaFunctionTest method testNormalizeCurrentSchemaCustomSchema.

@Test
public void testNormalizeCurrentSchemaCustomSchema() throws Exception {
    sqlExpressions = new SqlExpressions(tableSources, new SessionContext(0, Option.NONE, "custom_schema"));
    functions = sqlExpressions.getInstance(Functions.class);
    assertNormalize("current_schema()", isLiteral("custom_schema"), false);
}
Also used : SessionContext(io.crate.action.sql.SessionContext) Functions(io.crate.metadata.Functions) SqlExpressions(io.crate.testing.SqlExpressions) AbstractScalarFunctionsTest(io.crate.operation.scalar.AbstractScalarFunctionsTest) Test(org.junit.Test)

Example 4 with SessionContext

use of io.crate.action.sql.SessionContext in project crate by crate.

the class ExpressionAnalyzerTest method testQuotedSubscriptExpression.

@Test
public void testQuotedSubscriptExpression() throws Exception {
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, new SessionContext(0, EnumSet.of(Option.ALLOW_QUOTED_SUBSCRIPT), null), paramTypeHints, new FullQualifedNameFieldProvider(dummySources), null);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    Field field1 = (Field) expressionAnalyzer.convert(SqlParser.createExpression("obj['x']"), expressionAnalysisContext);
    Field field2 = (Field) expressionAnalyzer.convert(SqlParser.createExpression("\"obj['x']\""), expressionAnalysisContext);
    assertEquals(field1, field2);
    Field field3 = (Field) expressionAnalyzer.convert(SqlParser.createExpression("\"myObj['x']\""), expressionAnalysisContext);
    assertEquals("myObj['x']", field3.path().toString());
    Field field4 = (Field) expressionAnalyzer.convert(SqlParser.createExpression("\"myObj['x']['AbC']\""), expressionAnalysisContext);
    assertEquals("myObj['x']['AbC']", field4.path().toString());
}
Also used : SymbolMatchers.isField(io.crate.testing.SymbolMatchers.isField) Field(io.crate.analyze.symbol.Field) SessionContext(io.crate.action.sql.SessionContext) FullQualifedNameFieldProvider(io.crate.analyze.relations.FullQualifedNameFieldProvider) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 5 with SessionContext

use of io.crate.action.sql.SessionContext in project crate by crate.

the class SetSessionPlan method executeOrFail.

@Override
public void executeOrFail(DependencyCarrier executor, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) throws Exception {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(plannerContext.transactionContext(), plannerContext.nodeContext(), x, params, subQueryResults);
    SessionContext sessionContext = plannerContext.transactionContext().sessionContext();
    Assignment<Symbol> assignment = settings.get(0);
    String settingName = eval.apply(assignment.columnName()).toString();
    validateSetting(settingName);
    SessionSetting<?> sessionSetting = sessionSettingRegistry.settings().get(settingName);
    if (sessionSetting == null) {
        LOGGER.info("SET SESSION STATEMENT WILL BE IGNORED: {}", settingName);
    } else {
        sessionSetting.apply(sessionContext, assignment.expressions(), eval);
    }
    consumer.accept(InMemoryBatchIterator.empty(SENTINEL), null);
}
Also used : CrateSettings(io.crate.metadata.settings.CrateSettings) SessionContext(io.crate.action.sql.SessionContext) SENTINEL(io.crate.data.SentinelRow.SENTINEL) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) SessionSetting(io.crate.metadata.settings.session.SessionSetting) Function(java.util.function.Function) RowConsumer(io.crate.data.RowConsumer) DependencyCarrier(io.crate.planner.DependencyCarrier) Assignment(io.crate.sql.tree.Assignment) List(java.util.List) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Logger(org.apache.logging.log4j.Logger) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) Locale(java.util.Locale) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) LogManager(org.apache.logging.log4j.LogManager) SessionSettingRegistry(io.crate.metadata.settings.session.SessionSettingRegistry) Symbol(io.crate.expression.symbol.Symbol) SessionContext(io.crate.action.sql.SessionContext)

Aggregations

SessionContext (io.crate.action.sql.SessionContext)31 Test (org.junit.Test)26 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)18 SQLExecutor (io.crate.testing.SQLExecutor)14 User (io.crate.user.User)13 List (java.util.List)13 ArrayList (java.util.ArrayList)12 DependencyCarrier (io.crate.planner.DependencyCarrier)11 DataTypes (io.crate.types.DataTypes)11 Arrays (java.util.Arrays)11 Map (java.util.Map)11 DescribeResult (io.crate.action.sql.DescribeResult)10 SQLOperations (io.crate.action.sql.SQLOperations)10 Session (io.crate.action.sql.Session)10 AccessControl (io.crate.auth.AccessControl)10 AlwaysOKAuthentication (io.crate.auth.AlwaysOKAuthentication)10 Authentication (io.crate.auth.Authentication)10 AuthenticationMethod (io.crate.auth.AuthenticationMethod)10 JobKilledException (io.crate.exceptions.JobKilledException)10 JobsLogs (io.crate.execution.engine.collect.stats.JobsLogs)10