Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,25 @@ try (Query q = db.borrowQuery()) {
### Page Through a Result

`QwpQueryOptions` asks the server for one page of the result set, and for what the `/exec` HTTP endpoint reports
besides the rows: the row count of the whole result set, execution statistics, and the position of an error in the SQL
text. The handler receives them through the detailed overloads of its callbacks, and a failed `await()` throws a
`QueryException` whose `getPosition()` carries the position. The options need a QuestDB server that supports them; an
older server fails a query that sets `skipRows` or `maxRows`, and runs the others without the details.
besides the rows: the row count of the whole result set, execution statistics, the designated timestamp column, and the
position of an error in the SQL text. The handler receives them through the detailed overloads of its callbacks, and a
failed `await()` throws a `QueryException` whose `getPosition()` carries the position. `omitBinaryValues` asks the
server to leave the bytes of BINARY values out, as `/exec` does: each non-NULL BINARY value then arrives empty, and a NULL
stays NULL. The options need a QuestDB server that supports them; an older server fails a query that sets `skipRows` or
`maxRows`, and runs the others without the details and with the BINARY values.

```java
import io.questdb.client.cutlass.qwp.client.QwpQueryDetails;
import io.questdb.client.cutlass.qwp.client.QwpQueryOptions;

QwpQueryOptions page = new QwpQueryOptions()
.skipRows(100) // leave out the first 100 rows
.maxRows(50) // stream at most 50 rows
.countRows(true) // count the rows of the whole result set
.stats(true) // report compile and execution times
.errorPosition(true); // report where in the SQL text an error is
.skipRows(100) // leave out the first 100 rows
.maxRows(50) // stream at most 50 rows
.countRows(true) // count the rows of the whole result set
.stats(true) // report compile and execution times
.designatedTimestamp(true) // report which column is the designated timestamp
.omitBinaryValues(true) // send BINARY values empty, without their bytes
.errorPosition(true); // report where in the SQL text an error is

QwpColumnBatchHandler pageHandler = new QwpColumnBatchHandler() {
@Override
Expand All @@ -273,6 +277,8 @@ QwpColumnBatchHandler pageHandler = new QwpColumnBatchHandler() {
public void onEnd(long requestId, long totalRows, QwpQueryDetails details) {
System.out.println(totalRows + " of " + details.getRowCount() + " rows in "
+ details.getExecuteNanos() / 1_000 + " us");
// the column index, or -1 when the result has no designated timestamp
System.out.println("designated timestamp: column " + details.getDesignatedTimestampIndex());
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/io/questdb/client/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ public interface Query extends Closeable {

/**
* Sets the per-query options: a row window that pages through the result
* set, and what the server reports besides the rows -- the row count of the
* whole result set, execution statistics and the error position. See
* {@link QwpQueryOptions}. The handle copies the values, so the instance can
* be reused right away; they apply to every {@link #submit()} until changed.
* set, what the server reports besides the rows -- the row count of the
* whole result set, execution statistics, the designated timestamp column
* and the error position -- and whether it leaves the bytes of BINARY values
* out. See {@link QwpQueryOptions}. The handle copies the values, so the
* instance can be reused right away; they apply to every {@link #submit()}
* until changed.
* Each {@link #submit()} runs with the options set when it is called, so
* changing them while a query is in flight affects only the next one. Pass
* null to drop them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public class QueryEvent {
*/
public static final int KIND_TRANSPORT_ERROR = 4;

// valid for KIND_END and KIND_EXEC_DONE: the row count and statistics trailers
// valid for KIND_END and KIND_EXEC_DONE: the row count, statistics and
// designated timestamp trailers
public final QwpQueryDetails details = new QwpQueryDetails();
public QwpBatchBuffer buffer; // valid for KIND_BATCH (must be released to pool by consumer)
public String errorMessage; // valid for KIND_ERROR
Expand Down Expand Up @@ -122,6 +123,15 @@ public void reset() {
this.details.clear();
}

/**
* Records the {@code designated_timestamp} trailer of a {@code RESULT_END}
* on {@link #details}. Call after {@link #asEnd(long)}.
*/
public QueryEvent withDesignatedTimestampIndex(int designatedTimestampIndex) {
this.details.setDesignatedTimestampIndex(designatedTimestampIndex);
return this;
}

/**
* Records the {@code row_count} trailer of a {@code RESULT_END} on
* {@link #details}. Call after {@link #asEnd(long)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ default void onEnd(long requestId, long totalRows) {
/**
* Details-aware overload of {@link #onEnd(long, long)}. Receives what the
* query reported besides its rows on the request of {@link QwpQueryOptions}:
* the row count of the whole result set and execution statistics. The
* default delegates to {@link #onEnd(long, long)} for backwards
* compatibility; override this method when you need the details.
* the row count of the whole result set, execution statistics and the
* designated timestamp column. The default delegates to
* {@link #onEnd(long, long)} for backwards compatibility; override this
* method when you need the details.
*
* @param requestId client-assigned request id (matches {@link QwpColumnBatch#requestId()})
* @param totalRows server-reported number of rows streamed, which a row window
* of {@link QwpQueryOptions} bounds
* @param details the row count and statistics the server reported; never null,
* and valid only for the duration of this call
* @param details the row count, statistics and designated timestamp the server
* reported; never null, and valid only for the duration of this call
*/
default void onEnd(long requestId, long totalRows, QwpQueryDetails details) {
onEnd(requestId, totalRows);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,14 @@ public static QueryEvent decodeExecDone(QueryEvent ev, long payload, int payload
* {@code msg_kind(1) + request_id(8) + final_seq(varint) + total_rows(varint)},
* then {@code row_count(varint)} when {@code queryFlags} has
* {@link QwpEgressMsgKind#QUERY_FLAG_COUNT_ROWS}, then the statistics block
* when it has {@link QwpEgressMsgKind#QUERY_FLAG_STATS}. Bytes after the
* when it has {@link QwpEgressMsgKind#QUERY_FLAG_STATS}, then
* {@code designated_timestamp(i32)} when it has
* {@link QwpEgressMsgKind#QUERY_FLAG_DESIGNATED_TIMESTAMP}. Bytes after the
* known fields are ignored.
*
* @throws QwpDecodeException if the frame ends inside a field the flags call
* for, or a varint does not fit in 64 bits
* for, a varint does not fit in 64 bits, or the
* designated timestamp names no result column
*/
public static QueryEvent decodeResultEnd(QueryEvent ev, long payload, int payloadLen, long queryFlags) throws QwpDecodeException {
TerminatorReader rd = new TerminatorReader("RESULT_END", payload + QwpConstants.HEADER_SIZE + 1 + 8, payload + payloadLen);
Expand All @@ -268,6 +271,15 @@ public static QueryEvent decodeResultEnd(QueryEvent ev, long payload, int payloa
if ((queryFlags & QwpEgressMsgKind.QUERY_FLAG_STATS) != 0) {
readStats(rd, ev);
}
if ((queryFlags & QwpEgressMsgKind.QUERY_FLAG_DESIGNATED_TIMESTAMP) != 0) {
int index = rd.int32("designated_timestamp");
// a result has at most MAX_COLUMNS_PER_TABLE columns, as the batch
// decoder checks: any other value names no column of it
if (index < QwpEgressMsgKind.NO_DESIGNATED_TIMESTAMP || index >= QwpConstants.MAX_COLUMNS_PER_TABLE) {
throw new QwpDecodeException("RESULT_END designated_timestamp out of range: " + index);
}
ev.withDesignatedTimestampIndex(index);
}
return ev;
}

Expand Down Expand Up @@ -854,6 +866,19 @@ private static final class TerminatorReader {
this.limit = limit;
}

/**
* Reads a little-endian int32, as the server writes the fixed-width
* trailers.
*/
int int32(String field) throws QwpDecodeException {
if (p + Integer.BYTES > limit) {
throw new QwpDecodeException(frame + " frame truncated " + (p < limit ? "mid " : "before ") + field);
}
int value = Unsafe.getUnsafe().getInt(p);
p += Integer.BYTES;
return value;
}

byte int8(String field) throws QwpDecodeException {
if (p >= limit) {
throw new QwpDecodeException(frame + " frame truncated before " + field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ public final class QwpEgressMsgKind {
* {@code SERVER_INFO.capabilities} bit: the server honours the query-details
* flags of {@code QUERY_REQUEST.query_flags} -- {@link #QUERY_FLAG_ERROR_POSITION},
* {@link #QUERY_FLAG_STATS}, {@link #QUERY_FLAG_COUNT_ROWS},
* {@link #QUERY_FLAG_SKIP_ROWS} and {@link #QUERY_FLAG_MAX_ROWS}. The client
* sends those bits only when this bit is set: a server without it ignores
* them and answers with the baseline frames. Mirrors the server-side constant
* {@link #QUERY_FLAG_SKIP_ROWS}, {@link #QUERY_FLAG_MAX_ROWS},
* {@link #QUERY_FLAG_DESIGNATED_TIMESTAMP} and
* {@link #QUERY_FLAG_OMIT_BINARY_VALUES}. The client sends those bits only
* when this bit is set: a server without it ignores them and answers with
* the baseline frames. Mirrors the server-side constant
* {@code io.questdb.cutlass.qwp.codec.QwpEgressMsgKind#CAP_QUERY_DETAILS}.
*/
public static final int CAP_QUERY_DETAILS = 0x00000010;
Expand Down Expand Up @@ -79,6 +81,14 @@ public final class QwpEgressMsgKind {
* {@code request_id:u64, op_type:u8, rows_affected:varint}.
*/
public static final byte EXEC_DONE = 0x16;
/**
* Designated timestamp index of a result whose columns include no
* designated timestamp, as the {@code timestamp} of the {@code /exec} HTTP
* endpoint reports it, and of every result when the index was not requested
* or not reported. Mirrors the server-side constant
* {@code io.questdb.cutlass.qwp.codec.QwpEgressMsgKind#NO_DESIGNATED_TIMESTAMP}.
*/
public static final int NO_DESIGNATED_TIMESTAMP = -1;
/**
* Error position that points nowhere in the SQL text: the position of an
* error that does not come from the SQL text, such as a {@code CANCEL} the
Expand All @@ -98,6 +108,16 @@ public final class QwpEgressMsgKind {
* {@link #CAP_QUERY_DETAILS}.
*/
public static final int QUERY_FLAG_COUNT_ROWS = 0x10;
/**
* {@code QUERY_REQUEST.query_flags} bit: report which column of the result
* is its designated timestamp. {@code RESULT_END} then ends with
* {@code designated_timestamp:i32}, after the statistics block when
* {@link #QUERY_FLAG_STATS} is set too: the index of that column in the
* schema of the result's batches, or {@link #NO_DESIGNATED_TIMESTAMP}.
* Statements that do not produce rows ignore it. Sent only when the server
* advertised {@link #CAP_QUERY_DETAILS}.
*/
public static final int QUERY_FLAG_DESIGNATED_TIMESTAMP = 0x80;
/**
* {@code QUERY_REQUEST.query_flags} bit: report the error position. A
* {@code QUERY_ERROR} then carries the bare message, without the
Expand All @@ -117,6 +137,16 @@ public final class QwpEgressMsgKind {
* Sent only when the server advertised {@link #CAP_QUERY_DETAILS}.
*/
public static final int QUERY_FLAG_MAX_ROWS = 0x40;
/**
* {@code QUERY_REQUEST.query_flags} bit: leave the bytes of BINARY values
* out of the result, as the {@code /exec} HTTP endpoint does, which sends
* every BINARY value as {@code []}. Every {@code RESULT_BATCH} of the query
* then carries each non-NULL BINARY value as an empty one, zero bytes long,
* and a NULL as NULL, in the usual layout of the column. Statements that do
* not produce rows ignore it. Sent only when the server advertised
* {@link #CAP_QUERY_DETAILS}; a server without it sends the values.
*/
public static final int QUERY_FLAG_OMIT_BINARY_VALUES = 0x100;
/**
* {@code QUERY_REQUEST.query_flags} bit: reset the connection-scoped SYMBOL
* dict before this query, scoping it to the query. Sent only when the server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@
* <p>
* Per-query options:
* {@link #execute(CharSequence, QwpBindSetter, QwpColumnBatchHandler, QwpQueryOptions)}
* pages through a result set with a row window, and asks the server for what
* the {@code /exec} HTTP endpoint reports besides the rows: the row count of
* the whole result set, execution statistics and the error position. See
* {@link QwpQueryOptions}.
* pages through a result set with a row window, asks the server for what the
* {@code /exec} HTTP endpoint reports besides the rows: the row count of the
* whole result set, execution statistics, the designated timestamp column and
* the error position, and can ask it to leave the bytes of BINARY values out.
* See {@link QwpQueryOptions}.
*/
public class QwpQueryClient implements QuietCloseable {

Expand Down Expand Up @@ -168,7 +169,9 @@ public class QwpQueryClient implements QuietCloseable {
| QwpEgressMsgKind.QUERY_FLAG_STATS
| QwpEgressMsgKind.QUERY_FLAG_COUNT_ROWS
| QwpEgressMsgKind.QUERY_FLAG_SKIP_ROWS
| QwpEgressMsgKind.QUERY_FLAG_MAX_ROWS;
| QwpEgressMsgKind.QUERY_FLAG_MAX_ROWS
| QwpEgressMsgKind.QUERY_FLAG_DESIGNATED_TIMESTAMP
| QwpEgressMsgKind.QUERY_FLAG_OMIT_BINARY_VALUES;
// query_flags bits of the row window, which change what the server streams
private static final int ROW_WINDOW_QUERY_FLAGS = QwpEgressMsgKind.QUERY_FLAG_SKIP_ROWS
| QwpEgressMsgKind.QUERY_FLAG_MAX_ROWS;
Expand Down Expand Up @@ -820,12 +823,14 @@ public void execute(CharSequence sql, QwpBindSetter binds, QwpColumnBatchHandler
/**
* As {@link #execute(CharSequence, QwpBindSetter, QwpColumnBatchHandler)},
* with per-query {@code options}: a row window that pages through the
* result set, and what the server reports besides the rows -- the row count
* of the whole result set and execution statistics, which reach
* result set, what the server reports besides the rows -- the row count of
* the whole result set, execution statistics and the designated timestamp
* column, which reach
* {@link QwpColumnBatchHandler#onEnd(long, long, QwpQueryDetails)} and
* {@link QwpColumnBatchHandler#onExecDone(long, short, long, QwpQueryDetails)},
* and the error position, which reaches
* {@link QwpColumnBatchHandler#onError(long, byte, String, int)}. The client
* {@link QwpColumnBatchHandler#onError(long, byte, String, int)} -- and
* whether the server leaves the bytes of BINARY values out. The client
* reads {@code options} when the call starts; null runs the query without
* options.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

/**
* What a successful query reported besides its rows, on the request of
* {@link QwpQueryOptions}: the row count of the whole result set and execution
* statistics. Passed to
* {@link QwpQueryOptions}: the row count of the whole result set, execution
* statistics and the designated timestamp column. Passed to
* {@link QwpColumnBatchHandler#onEnd(long, long, QwpQueryDetails)} and
* {@link QwpColumnBatchHandler#onExecDone(long, short, long, QwpQueryDetails)},
* never null.
Expand All @@ -40,7 +40,9 @@ public final class QwpQueryDetails {
private long clientWaitNanos = -1;
private long compileNanos = -1;
private long countNanos = -1;
private int designatedTimestampIndex = QwpEgressMsgKind.NO_DESIGNATED_TIMESTAMP;
private long executeNanos = -1;
private boolean hasDesignatedTimestampIndex;
private boolean hasRowCount;
private boolean hasStats;
private boolean isJitCompiled;
Expand Down Expand Up @@ -74,6 +76,17 @@ public long getCountNanos() {
return countNanos;
}

/**
* Returns the index of the result column that is the designated timestamp,
* in the schema of the result's batches, as the {@code timestamp} of the
* {@code /exec} HTTP endpoint reports it.
* {@link QwpEgressMsgKind#NO_DESIGNATED_TIMESTAMP} when the result has no
* designated timestamp, or {@link #hasDesignatedTimestampIndex()} is false.
*/
public int getDesignatedTimestampIndex() {
return designatedTimestampIndex;
}

/**
* Returns how long, in nanoseconds, the query ran on the server, from the
* arrival of the request to its last frame, or -1 when {@link #hasStats()}
Expand All @@ -92,6 +105,18 @@ public long getRowCount() {
return rowCount;
}

/**
* Returns true when the server reported which column of the result is its
* designated timestamp: the query asked for it with
* {@link QwpQueryOptions#designatedTimestamp(boolean)}, the server advertises
* {@link QwpEgressMsgKind#CAP_QUERY_DETAILS}, and the statement produced
* rows. {@link #getDesignatedTimestampIndex()} may still be
* {@link QwpEgressMsgKind#NO_DESIGNATED_TIMESTAMP}: the result has none.
*/
public boolean hasDesignatedTimestampIndex() {
return hasDesignatedTimestampIndex;
}

/**
* Returns true when the server reported the row count of the whole result
* set: the query asked for it with {@link QwpQueryOptions#countRows(boolean)},
Expand Down Expand Up @@ -135,20 +160,33 @@ public String toString() {
.append(", clientWaitNanos=").append(clientWaitNanos)
.append(", jitCompiled=").append(isJitCompiled);
}
if (hasDesignatedTimestampIndex) {
if (hasRowCount || hasStats) {
sb.append(", ");
}
sb.append("designatedTimestampIndex=").append(designatedTimestampIndex);
}
return sb.append('}').toString();
}

void clear() {
clientWaitNanos = -1;
compileNanos = -1;
countNanos = -1;
designatedTimestampIndex = QwpEgressMsgKind.NO_DESIGNATED_TIMESTAMP;
executeNanos = -1;
hasDesignatedTimestampIndex = false;
hasRowCount = false;
hasStats = false;
isJitCompiled = false;
rowCount = -1;
}

void setDesignatedTimestampIndex(int designatedTimestampIndex) {
this.designatedTimestampIndex = designatedTimestampIndex;
this.hasDesignatedTimestampIndex = true;
}

void setRowCount(long rowCount) {
this.rowCount = rowCount;
this.hasRowCount = true;
Expand Down
Loading
Loading