use of io.crate.action.sql.SessionContext in project crate by crate.
the class PostgresWireProtocolTest method submitQueriesThroughSimpleQueryMode.
private void submitQueriesThroughSimpleQueryMode(String statements, @Nullable Throwable failure) {
SQLOperations sqlOperations = Mockito.mock(SQLOperations.class);
Session session = mock(Session.class);
SessionContext sessionContext = new SessionContext(User.CRATE_USER);
when(session.sessionContext()).thenReturn(sessionContext);
when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
DescribeResult describeResult = mock(DescribeResult.class);
when(describeResult.getFields()).thenReturn(null);
when(session.describe(anyChar(), anyString())).thenReturn(describeResult);
when(session.transactionState()).thenReturn(TransactionState.IDLE);
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionCtx -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
if (failure != null) {
when(session.sync()).thenAnswer(invocationOnMock -> {
Messages.sendErrorResponse(channel, AccessControl.DISABLED, failure);
return CompletableFuture.failedFuture(failure);
});
} else {
when(session.sync()).thenReturn(CompletableFuture.completedFuture(null));
}
sendStartupMessage(channel);
readAuthenticationOK(channel);
skipParameterMessages(channel);
readReadyForQueryMessage(channel);
ByteBuf query = Unpooled.buffer();
try {
// the actual statements don't have to be valid as they are not executed
Messages.writeCString(query, statements.getBytes(StandardCharsets.UTF_8));
ctx.handleSimpleQuery(query, new DelayableWriteChannel(channel));
} finally {
query.release();
}
}
use of io.crate.action.sql.SessionContext in project crate by crate.
the class PostgresWireProtocolTest method testBindMessageCanBeReadIfTypeForParamsIsUnknown.
@Test
public void testBindMessageCanBeReadIfTypeForParamsIsUnknown() throws Exception {
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
// no type hints for parameters
ClientMessages.sendParseMessage(buffer, "S1", "select ?, ?", new int[0]);
List<Object> params = Arrays.asList(10, 20);
ClientMessages.sendBindMessage(buffer, "P1", "S1", params);
channel.writeInbound(buffer);
channel.releaseInbound();
Session session = sessions.get(0);
// If the query can be retrieved via portalName it means bind worked
assertThat(session.getQuery("P1"), is("select ?, ?"));
}
use of io.crate.action.sql.SessionContext in project crate by crate.
the class PostgresWireProtocolTest method testDescribeStatementMessage.
@Test
public void testDescribeStatementMessage() throws Exception {
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
{
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
ClientMessages.sendParseMessage(buffer, "S1", "select ? in (1, 2, 3)", new int[0]);
channel.writeInbound(buffer);
channel.releaseInbound();
// we're not interested in the startup, parse, or bind replies
channel.flushOutbound();
channel.releaseOutbound();
channel.outboundMessages().clear();
}
{
// try the describe statement variant
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.STATEMENT, "S1");
channel.writeInbound(buffer);
channel.releaseInbound();
// we should get back a ParameterDescription message
channel.flushOutbound();
ByteBuf response = channel.readOutbound();
try {
assertThat(response.readByte(), is((byte) 't'));
assertThat(response.readInt(), is(10));
assertThat(response.readShort(), is((short) 1));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.INTEGER).oid()));
} finally {
response.release();
}
// we should get back a RowDescription message
response = channel.readOutbound();
try {
assertThat(response.readByte(), is((byte) 'T'));
assertThat(response.readInt(), is(46));
assertThat(response.readShort(), is((short) 1));
assertThat(PostgresWireProtocol.readCString(response), is("($1 = ANY([1, 2, 3]))"));
assertThat(response.readInt(), is(0));
assertThat(response.readShort(), is((short) 0));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.BOOLEAN).oid()));
assertThat(response.readShort(), is(PGTypes.get(DataTypes.BOOLEAN).typeLen()));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).typeMod()));
assertThat(response.readShort(), is((short) 0));
} finally {
response.release();
}
}
}
use of io.crate.action.sql.SessionContext in project crate by crate.
the class PostgresWireProtocolTest method testDescribePortalMessage.
@Test
public void testDescribePortalMessage() throws Exception {
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
{
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
ClientMessages.sendParseMessage(buffer, "S1", "select ? in (1, 2, 3)", new int[] { PGTypes.get(DataTypes.INTEGER).oid() });
ClientMessages.sendBindMessage(buffer, "P1", "S1", Collections.singletonList(1));
channel.writeInbound(buffer);
channel.releaseInbound();
// we're not interested in the startup, parse, or bind replies
channel.flushOutbound();
channel.releaseOutbound();
channel.outboundMessages().clear();
}
{
// try portal describe message
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.PORTAL, "P1");
channel.writeInbound(buffer);
channel.releaseInbound();
// we should get back a RowDescription message
channel.flushOutbound();
ByteBuf response = channel.readOutbound();
try {
assertThat(response.readByte(), is((byte) 'T'));
assertThat(response.readInt(), is(46));
assertThat(response.readShort(), is((short) 1));
assertThat(PostgresWireProtocol.readCString(response), is("($1 = ANY([1, 2, 3]))"));
assertThat(response.readInt(), is(0));
assertThat(response.readShort(), is((short) 0));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.BOOLEAN).oid()));
assertThat(response.readShort(), is(PGTypes.get(DataTypes.BOOLEAN).typeLen()));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).typeMod()));
assertThat(response.readShort(), is((short) 0));
} finally {
response.release();
}
}
}
use of io.crate.action.sql.SessionContext in project crate by crate.
the class SQLIntegrationTestCase method plan.
public PlanForNode plan(String stmt) {
String[] nodeNames = internalCluster().getNodeNames();
String nodeName = nodeNames[randomIntBetween(1, nodeNames.length) - 1];
Analyzer analyzer = internalCluster().getInstance(Analyzer.class, nodeName);
Planner planner = internalCluster().getInstance(Planner.class, nodeName);
NodeContext nodeCtx = internalCluster().getInstance(NodeContext.class, nodeName);
SessionContext sessionContext = new SessionContext(User.CRATE_USER, sqlExecutor.getCurrentSchema());
CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
RoutingProvider routingProvider = new RoutingProvider(Randomness.get().nextInt(), planner.getAwarenessAttributes());
PlannerContext plannerContext = new PlannerContext(planner.currentClusterState(), routingProvider, UUID.randomUUID(), coordinatorTxnCtx, nodeCtx, 0, null);
Plan plan = planner.plan(analyzer.analyze(SqlParser.createStatement(stmt), coordinatorTxnCtx.sessionContext(), ParamTypeHints.EMPTY), plannerContext);
return new PlanForNode(plan, nodeName, plannerContext);
}
Aggregations