use of io.crate.action.sql.DescribeResult in project crate by crate.
the class PostgresWireProtocol method handleDescribeMessage.
/**
* Describe Message
* Header:
* | 'D' | int32 len
* <p>
* Body:
* | 'S' = prepared statement or 'P' = portal
* | string nameOfPortalOrStatement
*/
private void handleDescribeMessage(ByteBuf buffer, Channel channel) {
byte type = buffer.readByte();
String portalOrStatement = readCString(buffer);
DescribeResult describeResult = session.describe((char) type, portalOrStatement);
Collection<Symbol> fields = describeResult.getFields();
if (type == 'S') {
Messages.sendParameterDescription(channel, describeResult.getParameters());
}
if (fields == null) {
Messages.sendNoData(channel);
} else {
var resultFormatCodes = type == 'P' ? session.getResultFormatCodes(portalOrStatement) : null;
Messages.sendRowDescription(channel, fields, resultFormatCodes, describeResult.relation());
}
}
use of io.crate.action.sql.DescribeResult 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.DescribeResult in project crate by crate.
the class PostgresWireProtocol method handleSingleQuery.
private CompletableFuture<?> handleSingleQuery(Statement statement, DelayableWriteChannel channel) {
CompletableFuture<?> result = new CompletableFuture<>();
String query;
try {
query = SqlFormatter.formatSql(statement);
} catch (Exception e) {
query = statement.toString();
}
AccessControl accessControl = getAccessControl.apply(session.sessionContext());
try {
session.analyze("", statement, Collections.emptyList(), query);
session.bind("", "", Collections.emptyList(), null);
DescribeResult describeResult = session.describe('P', "");
List<Symbol> fields = describeResult.getFields();
CompletableFuture<?> execute;
if (fields == null) {
RowCountReceiver rowCountReceiver = new RowCountReceiver(query, channel.bypassDelay(), accessControl);
execute = session.execute("", 0, rowCountReceiver);
} else {
Messages.sendRowDescription(channel, fields, null, describeResult.relation());
ResultSetReceiver resultSetReceiver = new ResultSetReceiver(query, channel.bypassDelay(), TransactionState.IDLE, accessControl, Lists2.map(fields, x -> PGTypes.get(x.valueType())), null);
execute = session.execute("", 0, resultSetReceiver);
}
if (execute != null) {
channel.delayWritesUntil(execute);
}
return session.sync();
} catch (Throwable t) {
Messages.sendErrorResponse(channel, accessControl, t);
result.completeExceptionally(t);
return result;
}
}
use of io.crate.action.sql.DescribeResult in project crate by crate.
the class SqlHttpHandler method executeBulkRequest.
private CompletableFuture<XContentBuilder> executeBulkRequest(Session session, String stmt, List<List<Object>> bulkArgs) {
final long startTimeInNs = System.nanoTime();
session.parse(UNNAMED, stmt, emptyList());
final RestBulkRowCountReceiver.Result[] results = new RestBulkRowCountReceiver.Result[bulkArgs.size()];
for (int i = 0; i < bulkArgs.size(); i++) {
session.bind(UNNAMED, UNNAMED, bulkArgs.get(i), null);
ResultReceiver resultReceiver = new RestBulkRowCountReceiver(results, i);
session.execute(UNNAMED, 0, resultReceiver);
}
if (results.length > 0) {
DescribeResult describeResult = session.describe('P', UNNAMED);
if (describeResult.getFields() != null) {
return CompletableFuture.failedFuture(new UnsupportedOperationException("Bulk operations for statements that return result sets is not supported"));
}
}
return session.sync().thenApply(ignored -> {
try {
return ResultToXContentBuilder.builder(JsonXContent.contentBuilder()).cols(emptyList()).duration(startTimeInNs).bulkRows(results).build();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
use of io.crate.action.sql.DescribeResult in project crate by crate.
the class SqlHttpHandler method executeSimpleRequest.
private CompletableFuture<XContentBuilder> executeSimpleRequest(Session session, String stmt, List<Object> args, boolean includeTypes) throws IOException {
long startTimeInNs = System.nanoTime();
session.parse(UNNAMED, stmt, emptyList());
session.bind(UNNAMED, UNNAMED, args == null ? emptyList() : args, null);
DescribeResult description = session.describe('P', UNNAMED);
List<Symbol> resultFields = description.getFields();
ResultReceiver<XContentBuilder> resultReceiver;
if (resultFields == null) {
resultReceiver = new RestRowCountReceiver(JsonXContent.contentBuilder(), startTimeInNs, includeTypes);
} else {
CircuitBreaker breaker = circuitBreakerProvider.apply(HierarchyCircuitBreakerService.QUERY);
RamAccounting ramAccounting = new BlockBasedRamAccounting(b -> breaker.addEstimateBytesAndMaybeBreak(b, "http-result"), MAX_BLOCK_SIZE_IN_BYTES);
resultReceiver = new RestResultSetReceiver(JsonXContent.contentBuilder(), resultFields, startTimeInNs, new RowAccountingWithEstimators(Symbols.typeView(resultFields), ramAccounting), includeTypes);
resultReceiver.completionFuture().whenComplete((result, error) -> ramAccounting.close());
}
session.execute(UNNAMED, 0, resultReceiver);
return session.sync().thenCompose(ignored -> resultReceiver.completionFuture());
}
Aggregations