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())));
}
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"));
}
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"));
}
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());
}
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"));
}
Aggregations