Search in sources :

Example 16 with SessionContext

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

the class ExpressionAnalyzerTest method testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation.

@Test
public void testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation() throws Exception {
    TableInfo t1 = executor.resolveTableInfo("t1");
    TableRelation relation = new TableRelation(t1);
    RelationName a1 = new RelationName(null, "a1");
    RelationName a2 = new RelationName(null, "a2");
    Map<RelationName, AnalyzedRelation> sources = Map.of(a1, new AliasedAnalyzedRelation(relation, a1), a2, new AliasedAnalyzedRelation(relation, a2));
    SessionContext sessionContext = SessionContext.systemSessionContext();
    CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, expressions.nodeCtx, paramTypeHints, new FullQualifiedNameFieldProvider(sources, ParentRelations.NO_PARENTS, sessionContext.searchPath().currentSchema()), null);
    Function andFunction = (Function) expressionAnalyzer.convert(SqlParser.createExpression("not a1.x = 1 and not a2.x = 1"), context);
    ScopedSymbol t1Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(0)).arguments().get(0)).arguments().get(0));
    ScopedSymbol t2Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(1)).arguments().get(0)).arguments().get(0));
    assertThat(t1Id.relation(), is(not(t2Id.relation())));
}
Also used : CoalesceFunction(io.crate.expression.scalar.conditional.CoalesceFunction) SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) ArraySliceFunction(io.crate.expression.scalar.ArraySliceFunction) ImplicitCastFunction(io.crate.expression.scalar.cast.ImplicitCastFunction) Function(io.crate.expression.symbol.Function) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) RelationName(io.crate.metadata.RelationName) SessionContext(io.crate.action.sql.SessionContext) TableInfo(io.crate.metadata.table.TableInfo) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) TableRelation(io.crate.analyze.relations.TableRelation) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 17 with SessionContext

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

the class SqlHttpHandlerTest method testSessionSettingsArePreservedAcrossRequests.

@Test
public void testSessionSettingsArePreservedAcrossRequests() {
    User dummyUser = User.of("dummy");
    var sessionConext = new SessionContext(dummyUser);
    var mockedSession = mock(Session.class);
    when(mockedSession.sessionContext()).thenReturn(sessionConext);
    var mockedSqlOperations = mock(SQLOperations.class);
    when(mockedSqlOperations.createSession(null, dummyUser)).thenReturn(mockedSession);
    var mockedRequest = mock(FullHttpRequest.class);
    when(mockedRequest.headers()).thenReturn(new DefaultHttpHeaders());
    SqlHttpHandler handler = new SqlHttpHandler(Settings.EMPTY, mockedSqlOperations, (s) -> new NoopCircuitBreaker("dummy"), userName -> dummyUser, sessionContext -> AccessControl.DISABLED, Netty4CorsConfigBuilder.forAnyOrigin().build());
    // 1st call to ensureSession creates a session instance bound to 'dummyUser'
    var session = handler.ensureSession(mockedRequest);
    verify(mockedRequest, atLeast(1)).headers();
    assertThat(session.sessionContext().authenticatedUser(), is(dummyUser));
    assertThat(session.sessionContext().searchPath().currentSchema(), containsString("doc"));
    assertTrue(session.sessionContext().isHashJoinEnabled());
    // modify the session settings
    session.sessionContext().setSearchPath("dummy_path");
    session.sessionContext().setHashJoinEnabled(false);
    // test that the 2nd call to ensureSession will retrieve the session settings modified previously
    session = handler.ensureSession(mockedRequest);
    assertFalse(session.sessionContext().isHashJoinEnabled());
    assertThat(session.sessionContext().searchPath().currentSchema(), containsString("dummy_path"));
}
Also used : User(io.crate.user.User) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) SessionContext(io.crate.action.sql.SessionContext) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) Test(org.junit.Test)

Example 18 with SessionContext

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

the class SchemasTest method testResolveTableInfoForValidFQN.

@Test
public void testResolveTableInfoForValidFQN() throws IOException {
    RelationName tableIdent = new RelationName("schema", "t");
    SQLExecutor sqlExecutor = getSqlExecutorBuilderForTable(tableIdent, "doc", "schema").build();
    QualifiedName fqn = QualifiedName.of("schema", "t");
    SessionContext sessionContext = sqlExecutor.getSessionContext();
    TableInfo tableInfo = sqlExecutor.schemas().resolveTableInfo(fqn, Operation.READ, sessionContext.sessionUser(), sessionContext.searchPath());
    RelationName relation = tableInfo.ident();
    assertThat(relation.schema(), is("schema"));
    assertThat(relation.name(), is("t"));
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) QualifiedName(io.crate.sql.tree.QualifiedName) SessionContext(io.crate.action.sql.SessionContext) TableInfo(io.crate.metadata.table.TableInfo) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) ViewsMetadataTest(io.crate.metadata.view.ViewsMetadataTest)

Example 19 with SessionContext

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

the class ExpressionAnalyzerTest method testAnalyzeSubscriptFunctionCall.

@Test
public void testAnalyzeSubscriptFunctionCall() throws Exception {
    // Test when use subscript function is used explicitly then it's handled (and validated)
    // the same way it's handled when the subscript operator `[]` is used
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, new SessionContext(0, EnumSet.of(Option.ALLOW_QUOTED_SUBSCRIPT), null), paramTypeHints, new FullQualifedNameFieldProvider(dummySources), null);
    ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
    FunctionCall subscriptFunctionCall = new FunctionCall(new QualifiedName("subscript"), ImmutableList.of(new ArrayLiteral(ImmutableList.of(new StringLiteral("obj"))), new LongLiteral("1")));
    Function function = (Function) expressionAnalyzer.convert(subscriptFunctionCall, expressionAnalysisContext);
    assertEquals("subscript(_array(Literal{obj, type=string}),Literal{1, type=integer})", function.toString());
}
Also used : Function(io.crate.analyze.symbol.Function) SessionContext(io.crate.action.sql.SessionContext) FullQualifedNameFieldProvider(io.crate.analyze.relations.FullQualifedNameFieldProvider) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 20 with SessionContext

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

the class CreateAlterTableStatementAnalyzerTest method testDefaultSchemaIsAddedToTableIdentIfNoEplicitSchemaExistsInTheStatement.

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

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