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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.apache.calcite.rel.RelNode;
import org.opensearch.analytics.exec.profile.QueryProfile;
import org.opensearch.sql.ast.statement.ExplainMode;
import org.opensearch.sql.calcite.CalcitePlanContext;
import org.opensearch.sql.common.response.ResponseListener;
Expand Down Expand Up @@ -134,7 +135,8 @@ public static ExplainResponse normalizeLf(ExplainResponse response) {
new ExecutionEngine.ExplainResponseNodeV2(
normalizeLf(calcite.getLogical()),
normalizeLf(calcite.getPhysical()),
normalizeLf(calcite.getExtended())));
normalizeLf(calcite.getExtended()),
calcite.getProfile()));
}
return response;
}
Expand All @@ -160,5 +162,10 @@ class ExplainResponseNodeV2 {
private final String logical;
private final String physical;
private final String extended;
private final QueryProfile profile;

public ExplainResponseNodeV2(String logical, String physical, String extended) {
this(logical, physical, extended, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeField;
import org.opensearch.analytics.exec.QueryPlanExecutor;
import org.opensearch.analytics.exec.profile.ProfiledResult;
import org.opensearch.analytics.exec.profile.QueryProfile;
import org.opensearch.core.action.ActionListener;
import org.opensearch.sql.ast.statement.ExplainMode;
import org.opensearch.sql.calcite.CalcitePlanContext;
Expand Down Expand Up @@ -108,14 +110,36 @@ public void explain(
ExplainMode mode,
CalcitePlanContext context,
ResponseListener<ExplainResponse> listener) {
try {
String logical = RelOptUtil.toString(plan, mode.toExplainLevel());
ExplainResponse response =
new ExplainResponse(new ExplainResponseNodeV2(logical, null, null));
listener.onResponse(ExplainResponse.normalizeLf(response));
} catch (Exception e) {
listener.onFailure(e);
}
String logical = RelOptUtil.toString(plan, mode.toExplainLevel());

planExecutor.executeWithProfile(
plan,
null,
new ActionListener<>() {
@Override
public void onResponse(ProfiledResult result) {
try {
QueryProfile profile = result.profile();
ExplainResponse response =
new ExplainResponse(new ExplainResponseNodeV2(logical, null, null, profile));
listener.onResponse(ExplainResponse.normalizeLf(response));
} catch (Exception e) {
listener.onFailure(e);
}
}

@Override
public void onFailure(Exception e) {
// Fall back to plan-only explain if profiling fails
try {
ExplainResponse response =
new ExplainResponse(new ExplainResponseNodeV2(logical, null, null, null));
listener.onResponse(ExplainResponse.normalizeLf(response));
} catch (Exception ex) {
listener.onFailure(ex);
}
}
});
}

private List<ExprValue> convertRows(Iterable<Object[]> rows, List<RelDataTypeField> fields) {
Expand Down
Loading