Skip to content

Commit

Permalink
Implemented --explain-ast param for ydb sql command (#11928)
Browse files Browse the repository at this point in the history
  • Loading branch information
dahbka-lis authored Nov 25, 2024
1 parent 0e10881 commit 4d17f1a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ydb/public/lib/ydb_cli/commands/ydb_sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ void TCommandSql::Config(TConfig& config) {
config.Opts->AddLongOption("explain", "Execute explain request for the query. Shows query logical plan. "
"The query is not actually executed, thus does not affect the database.")
.StoreTrue(&ExplainMode);
config.Opts->AddLongOption("explain-ast", "In addition to the query logical plan, you can get an AST (abstract syntax tree). "
"The AST section contains a representation in the internal miniKQL language.")
.StoreTrue(&ExplainAst);
config.Opts->AddLongOption("explain-analyze", "Execute query in explain-analyze mode. Shows query execution plan. "
"Query results are ignored.\n"
"Important note: The query is actually executed, so any changes will be applied to the database.")
Expand Down Expand Up @@ -75,6 +78,14 @@ void TCommandSql::Parse(TConfig& config) {
throw TMisuseException() << "Both mutually exclusive options \"Explain mode\" (\"--explain\") "
<< "and \"Explain-analyze mode\" (\"--explain-analyze\") were provided.";
}
if (ExplainMode && ExplainAst) {
throw TMisuseException() << "Both mutually exclusive options \"Explain mode\" (\"--explain\") "
<< "and \"Explain-AST mode\" (\"--explain-ast\") were provided.";
}
if (ExplainAst && ExplainAnalyzeMode) {
throw TMisuseException() << "Both mutually exclusive options \"Explain-AST mode\" (\"--explain-ast\") "
<< "and \"Explain-analyze mode\" (\"--explain-analyze\") were provided.";
}
if (ExplainAnalyzeMode && !CollectStatsMode.empty()) {
throw TMisuseException() << "Statistics collection mode option \"--stats\" has no effect in explain-analyze mode. "
"Relevant for execution mode only.";
Expand Down Expand Up @@ -118,7 +129,7 @@ int TCommandSql::RunCommand(TConfig& config) {
// Single stream execution
NQuery::TExecuteQuerySettings settings;

if (ExplainMode) {
if (ExplainMode || ExplainAst) {
// Execute explain request for the query
settings.ExecMode(NQuery::EExecMode::Explain);
} else {
Expand Down Expand Up @@ -171,6 +182,7 @@ int TCommandSql::RunCommand(TConfig& config) {
int TCommandSql::PrintResponse(NQuery::TExecuteQueryIterator& result) {
TMaybe<TString> stats;
TMaybe<TString> plan;
TMaybe<TString> ast;
{
TResultSetPrinter printer(OutputFormat, &IsInterrupted);

Expand All @@ -187,6 +199,7 @@ int TCommandSql::PrintResponse(NQuery::TExecuteQueryIterator& result) {
if (!streamPart.GetStats().Empty()) {
const auto& queryStats = *streamPart.GetStats();
stats = queryStats.ToString();
ast = queryStats.GetAst();

if (queryStats.GetPlan()) {
plan = queryStats.GetPlan();
Expand All @@ -195,6 +208,16 @@ int TCommandSql::PrintResponse(NQuery::TExecuteQueryIterator& result) {
}
} // TResultSetPrinter destructor should be called before printing stats

if (ExplainAst) {
Cout << "Query AST:" << Endl << ast << Endl;

if (IsInterrupted()) {
Cerr << "<INTERRUPTED>" << Endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

if (stats && !ExplainMode && !ExplainAnalyzeMode) {
Cout << Endl << "Statistics:" << Endl << *stats;
}
Expand Down
1 change: 1 addition & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class TCommandSql : public TYdbCommand, public TCommandWithOutput, public TComma
TString Syntax;
bool ExplainMode = false;
bool ExplainAnalyzeMode = false;
bool ExplainAst = false;
};

}
Expand Down

0 comments on commit 4d17f1a

Please sign in to comment.