Skip to content

Commit 47a80ed

Browse files
committed
Intermediate changes
commit_hash:56c7deefce71a95f10a4738b0a51e6ba384dd983
1 parent 6c1c32e commit 47a80ed

File tree

8 files changed

+180
-36
lines changed

8 files changed

+180
-36
lines changed

yql/essentials/sql/v1/complete/check/check_complete.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace NSQLComplete {
4949
}
5050
}
5151

52-
return MakeSchemaNameService(MakeSimpleSchema(MakeStaticSimpleSchema(std::move(fs))));
52+
return MakeSchemaNameService(MakeSimpleSchema(MakeStaticSimpleSchema({.Folders = std::move(fs)})));
5353
}
5454

5555
} // namespace

yql/essentials/sql/v1/complete/name/object/simple/schema_ut.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ Y_UNIT_TEST_SUITE(StaticSchemaTests) {
2020
{"Table", "meta"}}},
2121
{"/test/service/", {{"Table", "example"}}}}},
2222
};
23-
return MakeSimpleSchema(
24-
MakeStaticSimpleSchema(std::move(fs)));
23+
return MakeSimpleSchema(MakeStaticSimpleSchema({.Folders = std::move(fs)}));
2524
}
2625

2726
Y_UNIT_TEST(ListFolderBasic) {

yql/essentials/sql/v1/complete/name/object/simple/static/schema.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ namespace NSQLComplete {
7979

8080
} // namespace
8181

82-
ISimpleSchema::TPtr MakeStaticSimpleSchema(
83-
THashMap<TString, THashMap<TString, TVector<TFolderEntry>>> folders,
84-
THashMap<TString, THashMap<TString, TTableDetails>> tables) {
85-
return new TSimpleSchema(std::move(folders), std::move(tables));
82+
ISimpleSchema::TPtr MakeStaticSimpleSchema(TSchemaData data) {
83+
return new TSimpleSchema(std::move(data.Folders), std::move(data.Tables));
8684
}
8785

8886
} // namespace NSQLComplete

yql/essentials/sql/v1/complete/name/object/simple/static/schema.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

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

5+
#include <util/generic/hash.h>
6+
57
namespace NSQLComplete {
68

7-
ISimpleSchema::TPtr MakeStaticSimpleSchema(
8-
THashMap<TString, THashMap<TString, TVector<TFolderEntry>>> folders,
9-
THashMap<TString, THashMap<TString, TTableDetails>> tables = {});
9+
struct TSchemaData {
10+
THashMap<TString, THashMap<TString, TVector<TFolderEntry>>> Folders;
11+
THashMap<TString, THashMap<TString, TTableDetails>> Tables;
12+
};
13+
14+
ISimpleSchema::TPtr MakeStaticSimpleSchema(TSchemaData data);
1015

1116
} // namespace NSQLComplete
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include "schema_json.h"
2+
3+
#include "schema.h"
4+
5+
#include <util/generic/hash.h>
6+
#include <util/generic/vector.h>
7+
#include <util/generic/scope.h>
8+
#include <util/string/join.h>
9+
10+
namespace NSQLComplete {
11+
12+
namespace {
13+
14+
TString PathString(const TVector<TString>& path) {
15+
return "/" + JoinSeq("/", path);
16+
}
17+
18+
const NJson::TJsonValue& Expect(
19+
const TVector<TString>& path,
20+
const NJson::TJsonValue::TMapType& json,
21+
TStringBuf key) {
22+
if (!json.contains(key)) {
23+
ythrow yexception() << "At " << PathString(path) << " expected " << key;
24+
}
25+
return json.at(key);
26+
}
27+
28+
const NJson::TJsonValue::TMapType& ExpectMap(
29+
const TVector<TString>& path,
30+
const NJson::TJsonValue& json) {
31+
if (!json.IsMap()) {
32+
ythrow yexception() << "At " << PathString(path) << " expected Map";
33+
}
34+
return json.GetMapSafe();
35+
}
36+
37+
const NJson::TJsonValue::TMapType& ExpectMap(
38+
const TVector<TString>& path,
39+
const NJson::TJsonValue::TMapType& json,
40+
TStringBuf key) {
41+
Expect(path, json, key);
42+
return ExpectMap(path, json.at(key));
43+
}
44+
45+
TString Parse(
46+
const TString& cluster,
47+
const NJson::TJsonValue::TMapType& json,
48+
TSchemaData& data,
49+
TVector<TString>& path);
50+
51+
void ParseFolder(
52+
const TString& cluster,
53+
const NJson::TJsonValue::TMapType& json,
54+
TSchemaData& data,
55+
TVector<TString>& path) {
56+
for (const auto& [name, entry] : ExpectMap(path, json, "entries")) {
57+
path.emplace_back(name);
58+
TString type = Parse(cluster, ExpectMap(path, entry), data, path);
59+
path.pop_back();
60+
61+
TStringBuf suffix = (!path.empty()) ? "/" : "";
62+
data.Folders[cluster][PathString(path) + suffix].emplace_back(type, name);
63+
}
64+
}
65+
66+
void ParseTable(
67+
const TString& cluster,
68+
const NJson::TJsonValue::TMapType& json,
69+
TSchemaData& data,
70+
TVector<TString>& path) {
71+
for (const auto& [name, _] : ExpectMap(path, json, "columns")) {
72+
data.Tables[cluster][PathString(path)].Columns.emplace_back(name);
73+
}
74+
}
75+
76+
TString Parse(
77+
const TString& cluster,
78+
const NJson::TJsonValue::TMapType& json,
79+
TSchemaData& data,
80+
TVector<TString>& path) {
81+
auto type = Expect(path, json, "type");
82+
83+
if (type == TFolderEntry::Folder) {
84+
ParseFolder(cluster, json, data, path);
85+
return TFolderEntry::Folder;
86+
}
87+
88+
if (type == TFolderEntry::Table) {
89+
ParseTable(cluster, json, data, path);
90+
return TFolderEntry::Table;
91+
}
92+
93+
ythrow yexception() << "Unexpected type: " << type;
94+
}
95+
96+
} // namespace
97+
98+
ISimpleSchema::TPtr MakeStaticSimpleSchema(const NJson::TJsonMap& json) {
99+
TSchemaData data;
100+
101+
for (const auto& [cluster, tree] : json.GetMap()) {
102+
TVector<TString> path;
103+
ParseFolder(cluster, tree.GetMapSafe(), data, path);
104+
}
105+
106+
return MakeStaticSimpleSchema(std::move(data));
107+
}
108+
109+
} // namespace NSQLComplete
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <library/cpp/json/json_value.h>
4+
5+
#include <yql/essentials/sql/v1/complete/name/object/simple/schema.h>
6+
7+
namespace NSQLComplete {
8+
9+
ISimpleSchema::TPtr MakeStaticSimpleSchema(const NJson::TJsonMap& json);
10+
11+
} // namespace NSQLComplete
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
LIBRARY()
22

33
SRCS(
4+
schema_json.cpp
45
schema.cpp
56
)
67

78
PEERDIR(
89
yql/essentials/sql/v1/complete/name/object/simple
10+
library/cpp/json
911
)
1012

1113
END()

yql/essentials/sql/v1/complete/sql_complete_ut.cpp

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <yql/essentials/sql/v1/complete/name/object/simple/schema.h>
77
#include <yql/essentials/sql/v1/complete/name/object/simple/cached/schema.h>
88
#include <yql/essentials/sql/v1/complete/name/object/simple/static/schema.h>
9+
#include <yql/essentials/sql/v1/complete/name/object/simple/static/schema_json.h>
910
#include <yql/essentials/sql/v1/complete/name/service/ranking/frequency.h>
1011
#include <yql/essentials/sql/v1/complete/name/service/ranking/ranking.h>
1112
#include <yql/essentials/sql/v1/complete/name/service/cluster/name_service.h>
@@ -20,6 +21,8 @@
2021
#include <library/cpp/testing/unittest/registar.h>
2122
#include <library/cpp/iterator/iterate_keys.h>
2223
#include <library/cpp/iterator/functools.h>
24+
#include <library/cpp/json/json_value.h>
25+
#include <library/cpp/json/json_reader.h>
2326

2427
#include <util/charset/utf8.h>
2528

@@ -84,38 +87,51 @@ Y_UNIT_TEST_SUITE(SqlCompleteTests) {
8487
},
8588
};
8689

87-
THashMap<TString, THashMap<TString, TVector<TFolderEntry>>> folders = {
88-
{"", {{"/", {{"Folder", "local"},
89-
{"Folder", "test"},
90-
{"Folder", "prod"},
91-
{"Folder", ".sys"}}},
92-
{"/local/", {{"Table", "example"},
93-
{"Table", "account"},
94-
{"Table", "abacaba"}}},
95-
{"/test/", {{"Folder", "service"},
96-
{"Table", "meta"}}},
97-
{"/test/service/", {{"Table", "example"}}},
98-
{"/.sys/", {{"Table", "status"}}}}},
99-
{"example",
100-
{{"/", {{"Table", "people"},
101-
{"Folder", "yql"}}},
102-
{"/yql/", {{"Table", "tutorial"}}}}},
103-
{"saurus",
104-
{{"/", {{"Table", "maxim"}}}}},
105-
};
106-
107-
THashMap<TString, THashMap<TString, TTableDetails>> tables = {
108-
{"example", {{"/people", {{"name", "age"}}}}}};
90+
TString clustersText = R"({
91+
"": { "type": "Folder", "entries": {
92+
"local": { "type": "Folder", "entries": {
93+
"example": { "type": "Table", "columns": {} },
94+
"account": { "type": "Table", "columns": {} },
95+
"abacaba": { "type": "Table", "columns": {} }
96+
}},
97+
"test": { "type": "Folder", "entries": {
98+
"service": { "type": "Folder", "entries": {
99+
"example": { "type": "Table", "columns": {} }
100+
}},
101+
"meta": { "type": "Table", "columns": {} }
102+
}},
103+
"prod": { "type": "Folder", "entries": {
104+
}},
105+
".sys": { "type": "Folder", "entries": {
106+
"status": { "type": "Table", "columns": {} }
107+
}}
108+
}},
109+
"example": { "type": "Folder", "entries": {
110+
"people": { "type": "Table", "columns": {
111+
"name": {},
112+
"age": {}
113+
}},
114+
"yql": { "type": "Folder", "entries": {
115+
"tutorial": { "type": "Table", "columns": {} }
116+
}}
117+
}},
118+
"saurus": { "type": "Folder", "entries": {
119+
"maxim": { "type": "Table", "columns": {} }
120+
}}
121+
})";
122+
123+
NJson::TJsonMap clustersJson;
124+
Y_ENSURE(NJson::ReadJsonTree(clustersText, &clustersJson));
109125

110126
auto clustersIt = NFuncTools::Filter(
111-
[](const auto& x) { return !x.empty(); }, IterateKeys(folders));
127+
[](const auto& x) { return !x.empty(); }, IterateKeys(clustersJson.GetMapSafe()));
112128
TVector<TString> clusters(begin(clustersIt), end(clustersIt));
113129

114130
TFrequencyData frequency;
115131

116132
TVector<INameService::TPtr> children = {
117133
MakeStaticNameService(std::move(names), frequency),
118-
MakeSchemaNameService(MakeSimpleSchema(MakeStaticSimpleSchema(std::move(folders), std::move(tables)))),
134+
MakeSchemaNameService(MakeSimpleSchema(MakeStaticSimpleSchema(clustersJson))),
119135
MakeClusterNameService(MakeStaticClusterDiscovery(std::move(clusters))),
120136
};
121137
INameService::TPtr service = MakeUnionNameService(
@@ -1369,13 +1385,17 @@ JOIN yt:$cluster_name.test;
13691385
MakeSimpleSchema(
13701386
MakeCachedSimpleSchema(
13711387
cache, "alice",
1372-
MakeStaticSimpleSchema({{"", {{"/", {{"Table", "alice"}}}}}}))));
1388+
MakeStaticSimpleSchema(TSchemaData{
1389+
.Folders = {{"", {{"/", {{"Table", "alice"}}}}}},
1390+
}))));
13731391

13741392
auto petyaService = MakeSchemaNameService(
13751393
MakeSimpleSchema(
13761394
MakeCachedSimpleSchema(
13771395
cache, "petya",
1378-
MakeStaticSimpleSchema({{"", {{"/", {{"Table", "petya"}}}}}}))));
1396+
MakeStaticSimpleSchema(TSchemaData{
1397+
.Folders = {{"", {{"/", {{"Table", "petya"}}}}}},
1398+
}))));
13791399

13801400
auto aliceEngine = MakeSqlCompletionEngine(lexer, std::move(aliceService));
13811401
auto petyaEngine = MakeSqlCompletionEngine(lexer, std::move(petyaService));

0 commit comments

Comments
 (0)