Skip to content

Commit 26e438e

Browse files
authored
#9056 Complete column names in YDB CLI (#19918)
1 parent b62fed9 commit 26e438e

File tree

5 files changed

+71
-19
lines changed

5 files changed

+71
-19
lines changed

ydb/apps/ydb/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Added trivial columns completion in interactive mode.
12
* Added the "ydb tools infer csv" command to generate a `CREATE TABLE` SQL query from a CSV file with data.
23
* Fix inline hints
34
* Added named expressions completion in interactive mode, cache schema responses.

ydb/public/lib/ydb_cli/commands/interactive/complete/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ PEERDIR(
1212
yql/essentials/sql/v1/complete/name/object
1313
yql/essentials/sql/v1/complete/name/object/simple
1414
yql/essentials/sql/v1/complete/name/object/simple/cached
15+
yql/essentials/sql/v1/complete/name/service/impatient
1516
yql/essentials/sql/v1/complete/name/service/schema
1617
yql/essentials/sql/v1/complete/name/service/static
1718
yql/essentials/sql/v1/complete/name/service/union

ydb/public/lib/ydb_cli/commands/interactive/complete/ydb_schema.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ydb_schema.h"
22

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

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

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

36+
NThreading::TFuture<TMaybe<NSQLComplete::TTableDetails>>
37+
DescribeTable(const TString& /* cluster */, const TString& path) const override {
38+
auto promise = NThreading::NewPromise<TMaybe<NSQLComplete::TTableDetails>>();
39+
NTable::TTableClient(Driver_)
40+
.GetSession(NTable::TCreateSessionSettings())
41+
.Apply([this, path, promise](auto f) mutable {
42+
static_cast<NTable::TCreateSessionResult>(f.ExtractValue())
43+
.GetSession()
44+
.DescribeTable(Qualified(path))
45+
.Apply([this, path, promise](auto f) mutable {
46+
NTable::TDescribeTableResult result = f.ExtractValue();
47+
promise.SetValue(Convert(path, std::move(result)));
48+
});
49+
});
50+
return promise;
51+
}
52+
3553
private:
3654
TString Qualified(TString folder) const {
3755
if (!folder.StartsWith('/')) {
@@ -45,7 +63,7 @@ namespace NYdb::NConsoleClient {
4563
if (!result.IsSuccess()) {
4664
if (IsVerbose_) {
4765
Cerr << "ListDirectory('" << folder << "') failed: "
48-
<< result.GetIssues().ToOneLineString();
66+
<< result.GetIssues().ToOneLineString() << Endl;
4967
}
5068
return {};
5169
}
@@ -111,6 +129,22 @@ namespace NYdb::NConsoleClient {
111129
}
112130
}
113131

132+
TMaybe<NSQLComplete::TTableDetails> Convert(TString path, NTable::TDescribeTableResult result) const {
133+
if (!result.IsSuccess()) {
134+
if (IsVerbose_) {
135+
Cerr << "DescribeTable('" << path << "') failed: "
136+
<< result.GetIssues().ToOneLineString() << Endl;
137+
}
138+
return Nothing();
139+
}
140+
141+
NSQLComplete::TTableDetails details;
142+
for (TColumn column : result.GetTableDescription().GetColumns()) {
143+
details.Columns.emplace_back(std::move(column.Name));
144+
}
145+
return details;
146+
}
147+
114148
TDriver Driver_;
115149
TString Database_;
116150
bool IsVerbose_;

ydb/public/lib/ydb_cli/commands/interactive/complete/yql_completer.cpp

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <yql/essentials/sql/v1/complete/sql_complete.h>
88
#include <yql/essentials/sql/v1/complete/name/cache/local/cache.h>
99
#include <yql/essentials/sql/v1/complete/name/object/simple/cached/schema.h>
10+
#include <yql/essentials/sql/v1/complete/name/service/impatient/name_service.h>
1011
#include <yql/essentials/sql/v1/complete/name/service/schema/name_service.h>
1112
#include <yql/essentials/sql/v1/complete/name/service/static/name_service.h>
1213
#include <yql/essentials/sql/v1/complete/name/service/union/name_service.h>
@@ -116,6 +117,7 @@ namespace NYdb::NConsoleClient {
116117
case NSQLComplete::ECandidateKind::TableName:
117118
return Color.identifier.quoted;
118119
case NSQLComplete::ECandidateKind::BindingName:
120+
case NSQLComplete::ECandidateKind::ColumnName:
119121
return Color.identifier.variable;
120122
default:
121123
return replxx::Replxx::Color::DEFAULT;
@@ -138,16 +140,22 @@ namespace NYdb::NConsoleClient {
138140
};
139141
}
140142

141-
NSQLComplete::ISchemaListCache::TPtr MakeSchemaCache() {
143+
NSQLComplete::TSchemaCaches MakeSchemaCaches() {
142144
using TKey = NSQLComplete::TSchemaDescribeCacheKey;
143-
using TValue = TVector<NSQLComplete::TFolderEntry>;
144145

145-
return NSQLComplete::MakeLocalCache<TKey, TValue>(
146-
NMonotonic::CreateDefaultMonotonicTimeProvider(),
147-
{
148-
.ByteCapacity = 1 * 1024 * 1024,
149-
.TTL = TDuration::Seconds(8),
150-
});
146+
auto time = NMonotonic::CreateDefaultMonotonicTimeProvider();
147+
148+
NSQLComplete::TLocalCacheConfig config = {
149+
.ByteCapacity = 1 * 1024 * 1024,
150+
.TTL = TDuration::Seconds(8),
151+
};
152+
153+
return {
154+
.List = NSQLComplete::MakeLocalCache<
155+
TKey, TVector<NSQLComplete::TFolderEntry>>(time, config),
156+
.DescribeTable = NSQLComplete::MakeLocalCache<
157+
TKey, TMaybe<NSQLComplete::TTableDetails>>(time, config),
158+
};
151159
}
152160

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

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

159-
TVector<NSQLComplete::INameService::TPtr> services = {
160-
NSQLComplete::MakeStaticNameService(
161-
NSQLComplete::LoadDefaultNameSet(), ranking),
167+
auto statics = NSQLComplete::MakeStaticNameService(NSQLComplete::LoadDefaultNameSet(), ranking);
162168

169+
auto schema =
163170
NSQLComplete::MakeSchemaNameService(
164171
NSQLComplete::MakeSimpleSchema(
165172
NSQLComplete::MakeCachedSimpleSchema(
166-
MakeSchemaCache(),
173+
MakeSchemaCaches(),
167174
/* zone = */ "",
168-
MakeYDBSchema(std::move(driver), std::move(database), isVerbose)))),
169-
};
175+
MakeYDBSchema(std::move(driver), std::move(database), isVerbose))));
176+
177+
auto heavy = NSQLComplete::MakeUnionNameService(
178+
{
179+
statics,
180+
schema,
181+
}, ranking);
170182

171-
auto service = NSQLComplete::MakeUnionNameService(std::move(services), std::move(ranking));
183+
auto light = NSQLComplete::MakeUnionNameService(
184+
{
185+
statics,
186+
NSQLComplete::MakeImpatientNameService(schema),
187+
}, ranking);
172188

173189
auto config = NSQLComplete::MakeYDBConfiguration();
174190

175191
return IYQLCompleter::TPtr(new TYQLCompleter(
176-
/* heavyEngine = */ NSQLComplete::MakeSqlCompletionEngine(lexer, service, config),
177-
/* lightEngine = */ NSQLComplete::MakeSqlCompletionEngine(lexer, service, config),
192+
/* heavyEngine = */ NSQLComplete::MakeSqlCompletionEngine(lexer, heavy, config),
193+
/* lightEngine = */ NSQLComplete::MakeSqlCompletionEngine(lexer, light, config),
178194
std::move(color)));
179195
}
180196

ydb/public/lib/ydb_cli/commands/interactive/line_reader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ TLineReader::TLineReader(std::string prompt, std::string historyFilePath, TClien
7373
return YQLCompleter->ApplyHeavy(Rx.get_state().text(), prefix, contextLen);
7474
});
7575

76-
Rx.set_hint_delay(500);
76+
Rx.set_hint_delay(100);
7777
Rx.set_hint_callback([this](const std::string& prefix, int& contextLen, TColor&) {
7878
return YQLCompleter->ApplyLight(Rx.get_state().text(), prefix, contextLen);
7979
});

0 commit comments

Comments
 (0)