Skip to content
Merged
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
60 changes: 4 additions & 56 deletions query/src/main/java/tech/ydb/query/impl/TableClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import tech.ydb.query.QueryStream;
import tech.ydb.query.result.QueryInfo;
import tech.ydb.query.result.QueryResultPart;
import tech.ydb.query.result.QueryStats;
import tech.ydb.query.settings.ExecuteQuerySettings;
import tech.ydb.query.settings.QueryStatsMode;
import tech.ydb.table.Session;
Expand All @@ -30,17 +31,10 @@
import tech.ydb.table.impl.BaseSession;
import tech.ydb.table.query.DataQueryResult;
import tech.ydb.table.query.Params;
import tech.ydb.table.query.stats.CompilationStats;
import tech.ydb.table.query.stats.OperationStats;
import tech.ydb.table.query.stats.QueryPhaseStats;
import tech.ydb.table.query.stats.QueryStats;
import tech.ydb.table.query.stats.TableAccessStats;
import tech.ydb.table.rpc.TableRpc;
import tech.ydb.table.rpc.grpc.GrpcTableRpc;
import tech.ydb.table.settings.ExecuteDataQuerySettings;

import static java.util.stream.Collectors.toList;

/**
*
* @author Aleksandr Gorshenin
Expand Down Expand Up @@ -100,53 +94,6 @@ private YdbQuery.TransactionControl mapTxControl(YdbTable.TransactionControl tc)
return TxControl.txModeCtrl(TxMode.NONE, tc.getCommitTx());
}

private static QueryStats queryStats(tech.ydb.query.result.QueryStats stats) {
if (stats == null) {
return null;
}
return new QueryStats(
stats.getPhases().stream().map(qp -> queryPhaseStats(qp)).collect(toList()),
compilationStats(stats.getCompilationStats()),
stats.getProcessCpuTimeUs(),
stats.getQueryPlan(),
stats.getQueryAst(),
stats.getTotalDurationUs(),
stats.getTotalCpuTimeUs()
);
}

private static QueryPhaseStats queryPhaseStats(tech.ydb.query.result.QueryStats.QueryPhase queryPhase) {
return new QueryPhaseStats(
queryPhase.getDurationUs(),
queryPhase.getTableAccesses().stream().map(ta -> tableAccessStats(ta)).collect(toList()),
queryPhase.getCpuTimeUs(),
queryPhase.getAffectedShards(),
queryPhase.isLiteralPhase()
);
}

private static TableAccessStats tableAccessStats(tech.ydb.query.result.QueryStats.TableAccess tableAccess) {
return new TableAccessStats(
tableAccess.getTableName(),
operationStats(tableAccess.getReads()),
operationStats(tableAccess.getUpdates()),
operationStats(tableAccess.getDeletes()),
tableAccess.getPartitionsCount()
);
}

private static OperationStats operationStats(tech.ydb.query.result.QueryStats.Operation operation) {
return new OperationStats(operation.getRows(), operation.getBytes());
}

private static CompilationStats compilationStats(tech.ydb.query.result.QueryStats.Compilation compilation) {
return new CompilationStats(
compilation.isFromCache(),
compilation.getDurationUs(),
compilation.getCpuTimeUs()
);
}

private class TableSession extends BaseSession {
private final SessionImpl querySession;

Expand Down Expand Up @@ -204,10 +151,11 @@ public void onNextRawPart(long index, ValueProtos.ResultSet rs) {
if (!res.isSuccess()) {
return res.map(v -> null);
}
QueryStats info = queryStats(res.getValue().getStats());
QueryStats stats = res.getValue().getStats();
String txId = txRef.get();
Status status = res.getStatus().withIssues(issues.toArray(new Issue[0]));
return Result.success(new DataQueryResult(txId, results, info), status);
DataQueryResult value = new DataQueryResult(txId, results, stats != null ? stats.toProtobuf() : null);
return Result.success(value, status);
});
}

Expand Down
164 changes: 163 additions & 1 deletion query/src/main/java/tech/ydb/query/result/QueryStats.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.ydb.query.result;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import tech.ydb.proto.YdbQueryStats;
Expand Down Expand Up @@ -30,11 +31,23 @@ public QueryStats(YdbQueryStats.QueryStats stats) {
this.totalCpuTimeUs = stats.getTotalCpuTimeUs();
}

public YdbQueryStats.QueryStats toProtobuf() {
return YdbQueryStats.QueryStats.newBuilder()
.setQueryAst(queryAst)
.setQueryPlan(queryPlan)
.setTotalCpuTimeUs(totalCpuTimeUs)
.setTotalDurationUs(totalDurationUs)
.setProcessCpuTimeUs(processCpuTimeUs)
.setCompilation(compilationStats.toProtobuf())
.addAllQueryPhases(queryPhases.stream().map(QueryPhase::toProtobuf).collect(Collectors.toList()))
.build();
}

public List<QueryPhase> getPhases() {
return this.queryPhases;
}

/**
/*
* @deprecated Use {{@link #getCompilationStats()}} instead
*/
@Deprecated
Expand Down Expand Up @@ -66,6 +79,34 @@ public long getProcessCpuTimeUs() {
return this.processCpuTimeUs;
}

@Override
public int hashCode() {
int hash = Objects.hash(queryPlan, queryAst, compilationStats, queryPhases);
hash = 31 * hash + (int) (processCpuTimeUs ^ (processCpuTimeUs >>> 32));
hash = 31 * hash + (int) (totalDurationUs ^ (totalDurationUs >>> 32));
hash = 31 * hash + (int) (totalCpuTimeUs ^ (totalCpuTimeUs >>> 32));
return hash;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}

QueryStats o = (QueryStats) other;
return Objects.equals(queryPlan, o.queryPlan)
&& Objects.equals(queryAst, o.queryAst)
&& Objects.equals(compilationStats, o.compilationStats)
&& Objects.equals(queryPhases, o.queryPhases)
&& processCpuTimeUs == o.processCpuTimeUs
&& totalDurationUs == o.totalDurationUs
&& totalCpuTimeUs == o.totalCpuTimeUs;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("QueryStats{");
Expand Down Expand Up @@ -104,10 +145,39 @@ public boolean isFromCache() {
return this.isFromCache;
}

public YdbQueryStats.CompilationStats toProtobuf() {
return YdbQueryStats.CompilationStats.newBuilder()
.setCpuTimeUs(cpuTimeUs)
.setDurationUs(durationUs)
.setFromCache(isFromCache)
.build();
}

@Override
public String toString() {
return "Compilation{durationUs=" + durationUs + ", cpuTimeUs=" + cpuTimeUs + ", cache=" + isFromCache + "}";
}

@Override
public int hashCode() {
int hash = Boolean.hashCode(isFromCache);
hash = 31 * hash + (int) (durationUs ^ (durationUs >>> 32));
hash = 31 * hash + (int) (cpuTimeUs ^ (cpuTimeUs >>> 32));
return hash;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}

Compilation o = (Compilation) other;
return isFromCache == o.isFromCache && durationUs == o.durationUs && cpuTimeUs == o.cpuTimeUs;
}
}

public static class QueryPhase {
Expand Down Expand Up @@ -157,6 +227,40 @@ public String toString() {
sb.append("]}");
return sb.toString();
}

public YdbQueryStats.QueryPhaseStats toProtobuf() {
return YdbQueryStats.QueryPhaseStats.newBuilder()
.setAffectedShards(affectedShards)
.setCpuTimeUs(cpuTimeUs)
.setDurationUs(durationUs)
.setLiteralPhase(isLiteralPhase)
.addAllTableAccess(tableAccesses.stream().map(TableAccess::toProtobuf).collect(Collectors.toList()))
.build();
}

@Override
public int hashCode() {
int hash = Boolean.hashCode(isLiteralPhase);
hash = 31 * hash + (int) (durationUs ^ (durationUs >>> 32));
hash = 31 * hash + (int) (cpuTimeUs ^ (cpuTimeUs >>> 32));
hash = 31 * hash + (int) (affectedShards ^ (affectedShards >>> 32));
hash = 31 * hash + tableAccesses.hashCode();
return hash;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}

QueryPhase o = (QueryPhase) other;
return durationUs == o.durationUs && cpuTimeUs == o.cpuTimeUs && affectedShards == o.affectedShards
&& isLiteralPhase == o.isLiteralPhase && Objects.equals(tableAccesses, o.tableAccesses);
}
}

public static class TableAccess {
Expand Down Expand Up @@ -203,6 +307,40 @@ public String toString() {
+ ", deletes={rows=" + deletes.rows + ", byte=" + deletes.bytes + "}"
+ "}";
}

public YdbQueryStats.TableAccessStats toProtobuf() {
return YdbQueryStats.TableAccessStats.newBuilder()
.setName(name)
.setPartitionsCount(partitionsCount)
.setReads(reads.toProtobuf())
.setDeletes(deletes.toProtobuf())
.setUpdates(updates.toProtobuf())
.build();
}

@Override
public int hashCode() {
int hash = Objects.hash(name, reads, updates, deletes);
hash = 31 * hash + (int) (partitionsCount ^ (partitionsCount >>> 32));
return hash;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}

TableAccess o = (TableAccess) other;
return Objects.equals(name, o.name)
&& Objects.equals(reads, o.reads)
&& Objects.equals(updates, o.updates)
&& Objects.equals(deletes, o.deletes)
&& partitionsCount == o.partitionsCount;
}
}

public static class Operation {
Expand All @@ -226,5 +364,29 @@ public long getBytes() {
public String toString() {
return "OperationStats{rows=" + rows + ", bytes=" + bytes + "}";
}

public YdbQueryStats.OperationStats toProtobuf() {
return YdbQueryStats.OperationStats.newBuilder().setRows(rows).setBytes(bytes).build();
}

@Override
public int hashCode() {
int hash = 31 + (int) (rows ^ (rows >>> 32));
hash = 31 * hash + (int) (bytes ^ (bytes >>> 32));
return hash;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}

Operation o = (Operation) other;
return rows == o.rows && bytes == o.bytes;
}
}
}
Loading