Skip to content

#9056 Complete column names in YDB CLI #19918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 27, 2025
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
1 change: 1 addition & 0 deletions ydb/apps/ydb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Added trivial columns completion in interactive mode.
* Added the "ydb tools infer csv" command to generate a `CREATE TABLE` SQL query from a CSV file with data.
* Fix inline hints
* Added named expressions completion in interactive mode, cache schema responses.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ PEERDIR(
yql/essentials/sql/v1/complete/name/object
yql/essentials/sql/v1/complete/name/object/simple
yql/essentials/sql/v1/complete/name/object/simple/cached
yql/essentials/sql/v1/complete/name/service/impatient
yql/essentials/sql/v1/complete/name/service/schema
yql/essentials/sql/v1/complete/name/service/static
yql/essentials/sql/v1/complete/name/service/union
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ydb_schema.h"

#include <ydb/public/lib/ydb_cli/commands/ydb_command.h>
#include <ydb/public/sdk/cpp/include/ydb-cpp-sdk/client/table/table.h>

#include <yql/essentials/sql/v1/complete/name/object/simple/schema.h>

Expand Down Expand Up @@ -32,6 +33,23 @@ namespace NYdb::NConsoleClient {
.Apply([this, folder](auto f) { return this->Convert(folder, f.ExtractValue()); });
}

NThreading::TFuture<TMaybe<NSQLComplete::TTableDetails>>
DescribeTable(const TString& /* cluster */, const TString& path) const override {
auto promise = NThreading::NewPromise<TMaybe<NSQLComplete::TTableDetails>>();
NTable::TTableClient(Driver_)
.GetSession(NTable::TCreateSessionSettings())
.Apply([this, path, promise](auto f) mutable {
static_cast<NTable::TCreateSessionResult>(f.ExtractValue())
.GetSession()
.DescribeTable(Qualified(path))
.Apply([this, path, promise](auto f) mutable {
NTable::TDescribeTableResult result = f.ExtractValue();
promise.SetValue(Convert(path, std::move(result)));
});
});
return promise;
}

private:
TString Qualified(TString folder) const {
if (!folder.StartsWith('/')) {
Expand All @@ -45,7 +63,7 @@ namespace NYdb::NConsoleClient {
if (!result.IsSuccess()) {
if (IsVerbose_) {
Cerr << "ListDirectory('" << folder << "') failed: "
<< result.GetIssues().ToOneLineString();
<< result.GetIssues().ToOneLineString() << Endl;
}
return {};
}
Expand Down Expand Up @@ -111,6 +129,22 @@ namespace NYdb::NConsoleClient {
}
}

TMaybe<NSQLComplete::TTableDetails> Convert(TString path, NTable::TDescribeTableResult result) const {
if (!result.IsSuccess()) {
if (IsVerbose_) {
Cerr << "DescribeTable('" << path << "') failed: "
<< result.GetIssues().ToOneLineString() << Endl;
}
return Nothing();
}

NSQLComplete::TTableDetails details;
for (TColumn column : result.GetTableDescription().GetColumns()) {
details.Columns.emplace_back(std::move(column.Name));
}
return details;
}

TDriver Driver_;
TString Database_;
bool IsVerbose_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <yql/essentials/sql/v1/complete/sql_complete.h>
#include <yql/essentials/sql/v1/complete/name/cache/local/cache.h>
#include <yql/essentials/sql/v1/complete/name/object/simple/cached/schema.h>
#include <yql/essentials/sql/v1/complete/name/service/impatient/name_service.h>
#include <yql/essentials/sql/v1/complete/name/service/schema/name_service.h>
#include <yql/essentials/sql/v1/complete/name/service/static/name_service.h>
#include <yql/essentials/sql/v1/complete/name/service/union/name_service.h>
Expand Down Expand Up @@ -116,6 +117,7 @@ namespace NYdb::NConsoleClient {
case NSQLComplete::ECandidateKind::TableName:
return Color.identifier.quoted;
case NSQLComplete::ECandidateKind::BindingName:
case NSQLComplete::ECandidateKind::ColumnName:
return Color.identifier.variable;
default:
return replxx::Replxx::Color::DEFAULT;
Expand All @@ -138,16 +140,22 @@ namespace NYdb::NConsoleClient {
};
}

NSQLComplete::ISchemaListCache::TPtr MakeSchemaCache() {
NSQLComplete::TSchemaCaches MakeSchemaCaches() {
using TKey = NSQLComplete::TSchemaDescribeCacheKey;
using TValue = TVector<NSQLComplete::TFolderEntry>;

return NSQLComplete::MakeLocalCache<TKey, TValue>(
NMonotonic::CreateDefaultMonotonicTimeProvider(),
{
.ByteCapacity = 1 * 1024 * 1024,
.TTL = TDuration::Seconds(8),
});
auto time = NMonotonic::CreateDefaultMonotonicTimeProvider();

NSQLComplete::TLocalCacheConfig config = {
.ByteCapacity = 1 * 1024 * 1024,
.TTL = TDuration::Seconds(8),
};

return {
.List = NSQLComplete::MakeLocalCache<
TKey, TVector<NSQLComplete::TFolderEntry>>(time, config),
.DescribeTable = NSQLComplete::MakeLocalCache<
TKey, TMaybe<NSQLComplete::TTableDetails>>(time, config),
};
}

IYQLCompleter::TPtr MakeYQLCompleter(
Expand All @@ -156,25 +164,33 @@ namespace NYdb::NConsoleClient {

auto ranking = NSQLComplete::MakeDefaultRanking(NSQLComplete::LoadFrequencyData());

TVector<NSQLComplete::INameService::TPtr> services = {
NSQLComplete::MakeStaticNameService(
NSQLComplete::LoadDefaultNameSet(), ranking),
auto statics = NSQLComplete::MakeStaticNameService(NSQLComplete::LoadDefaultNameSet(), ranking);

auto schema =
NSQLComplete::MakeSchemaNameService(
NSQLComplete::MakeSimpleSchema(
NSQLComplete::MakeCachedSimpleSchema(
MakeSchemaCache(),
MakeSchemaCaches(),
/* zone = */ "",
MakeYDBSchema(std::move(driver), std::move(database), isVerbose)))),
};
MakeYDBSchema(std::move(driver), std::move(database), isVerbose))));

auto heavy = NSQLComplete::MakeUnionNameService(
{
statics,
schema,
}, ranking);

auto service = NSQLComplete::MakeUnionNameService(std::move(services), std::move(ranking));
auto light = NSQLComplete::MakeUnionNameService(
{
statics,
NSQLComplete::MakeImpatientNameService(schema),
}, ranking);

auto config = NSQLComplete::MakeYDBConfiguration();

return IYQLCompleter::TPtr(new TYQLCompleter(
/* heavyEngine = */ NSQLComplete::MakeSqlCompletionEngine(lexer, service, config),
/* lightEngine = */ NSQLComplete::MakeSqlCompletionEngine(lexer, service, config),
/* heavyEngine = */ NSQLComplete::MakeSqlCompletionEngine(lexer, heavy, config),
/* lightEngine = */ NSQLComplete::MakeSqlCompletionEngine(lexer, light, config),
std::move(color)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ TLineReader::TLineReader(std::string prompt, std::string historyFilePath, TClien
return YQLCompleter->ApplyHeavy(Rx.get_state().text(), prefix, contextLen);
});

Rx.set_hint_delay(500);
Rx.set_hint_delay(100);
Rx.set_hint_callback([this](const std::string& prefix, int& contextLen, TColor&) {
return YQLCompleter->ApplyLight(Rx.get_state().text(), prefix, contextLen);
});
Expand Down
Loading