Skip to content

Commit

Permalink
Merge b5d7351 into 386bc05
Browse files Browse the repository at this point in the history
  • Loading branch information
vitstn authored Mar 29, 2024
2 parents 386bc05 + b5d7351 commit 0224138
Show file tree
Hide file tree
Showing 19 changed files with 246 additions and 51 deletions.
18 changes: 18 additions & 0 deletions ydb/library/yql/ast/yql_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3029,6 +3029,24 @@ bool TItemExprType::Validate(TPositionHandle position, TExprContext& ctx) const
return Validate(ctx.GetPosition(position), ctx);
}

TStringBuf TItemExprType::GetCleanName(bool isVirtual) const {
if (!isVirtual) {
return Name;
}

YQL_ENSURE(Name.StartsWith(YqlVirtualPrefix));
return Name.SubStr(YqlVirtualPrefix.size());
}

const TItemExprType* TItemExprType::GetCleanItem(bool isVirtual, TExprContext& ctx) const {
if (!isVirtual) {
return this;
}

YQL_ENSURE(Name.StartsWith(YqlVirtualPrefix));
return ctx.MakeType<TItemExprType>(Name.SubStr(YqlVirtualPrefix.size()), ItemType);
}

bool TMultiExprType::Validate(TPosition position, TExprContext& ctx) const {
if (Items.size() > Max<ui16>()) {
ctx.AddError(TIssue(position, TStringBuilder() << "Too many elements: " << Items.size()));
Expand Down
40 changes: 28 additions & 12 deletions ydb/library/yql/ast/yql_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class TBlockExprType;
class TScalarExprType;
const size_t DefaultMistypeDistance = 3;
const TString YqlVirtualPrefix = "_yql_virtual_";
extern const TStringBuf ZeroString;
Expand Down Expand Up @@ -464,6 +465,8 @@ class TItemExprType : public TTypeAnnotationNode {
return Name;
}
TStringBuf GetCleanName(bool isVirtual) const;
const TTypeAnnotationNode* GetItemType() const {
return ItemType;
}
Expand All @@ -472,6 +475,8 @@ class TItemExprType : public TTypeAnnotationNode {
return GetName() == other.GetName() && GetItemType() == other.GetItemType();
}
const TItemExprType* GetCleanItem(bool isVirtual, TExprContext& ctx) const;
private:
const TStringBuf Name;
const TTypeAnnotationNode* ItemType;
Expand Down Expand Up @@ -532,24 +537,35 @@ class TStructExprType : public TTypeAnnotationNode {
return it - Items.begin();
}
TMaybe<ui32> FindItemI(const TStringBuf& name) const {
auto strict = FindItem(name);
if (strict) {
return strict;
}
TMaybe<ui32> FindItemI(const TStringBuf& name, bool* isVirtual) const {
for (ui32 v = 0; v < 2; ++v) {
if (isVirtual) {
*isVirtual = v > 0;
}
TMaybe<ui32> ret;
for (ui32 i = 0; i < Items.size(); ++i) {
if (AsciiEqualsIgnoreCase(name, Items[i]->GetName())) {
if (ret) {
return Nothing();
auto nameToSearch = (v ? YqlVirtualPrefix : "") + name;
auto strict = FindItem(nameToSearch);
if (strict) {
return strict;
}
TMaybe<ui32> ret;
for (ui32 i = 0; i < Items.size(); ++i) {
if (AsciiEqualsIgnoreCase(nameToSearch, Items[i]->GetName())) {
if (ret) {
return Nothing();
}
ret = i;
}
}
ret = i;
if (ret) {
return ret;
}
}
return ret;
return Nothing();
}
const TTypeAnnotationNode* FindItemType(const TStringBuf& name) const {
Expand Down
38 changes: 22 additions & 16 deletions ydb/library/yql/core/type_ann/type_ann_pg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ IGraphTransformer::TStatus InferPgCommonType(TPositionHandle pos, const TExprNod
{
size_t j = 0;
for (const auto& col : *childColumnOrder) {
auto itemIdx = structType->FindItemI(col);
auto itemIdx = structType->FindItemI(col, nullptr);
YQL_ENSURE(itemIdx);

const auto* type = structType->GetItems()[*itemIdx]->GetItemType();
Expand Down Expand Up @@ -1725,7 +1725,8 @@ bool ScanColumns(TExprNode::TPtr root, TInputs& inputs, const THashSet<TString>&
}
}

auto pos = x.Type->FindItemI(node->Tail().Content());
bool isVirtual;
auto pos = x.Type->FindItemI(node->Tail().Content(), &isVirtual);
if (pos) {
foundAlias = x.Alias;
++matches;
Expand All @@ -1737,7 +1738,8 @@ bool ScanColumns(TExprNode::TPtr root, TInputs& inputs, const THashSet<TString>&
}

if (x.Priority == TInput::External) {
x.UsedExternalColumns.insert(TString(x.Type->GetItems()[*pos]->GetName()));
auto name = TString(x.Type->GetItems()[*pos]->GetCleanName(isVirtual));
x.UsedExternalColumns.insert(name);
}
}
}
Expand Down Expand Up @@ -1966,17 +1968,18 @@ void AddColumns(const TInputs& inputs, const bool* hasStar, const THashSet<TStri
continue;
}

auto pos = x.Type->FindItemI(ref);
bool isVirtual;
auto pos = x.Type->FindItemI(ref, &isVirtual);
if (pos) {
auto item = x.Type->GetItems()[*pos];
TString lcase = to_lower(TString(item->GetName()));
TString lcase = to_lower(TString(item->GetCleanName(isVirtual)));
if (auto it = usedInUsing.find(lcase); it != usedInUsing.end() && !present.contains(lcase)) {
items.push_back(ctx.MakeType<TItemExprType>(it->second, item->GetItemType()));
present.emplace(lcase);
}
item = AddAlias(x.Alias, item, ctx);
items.push_back(item);
usedRefs.insert(TString(item->GetName()));
usedRefs.insert(TString(item->GetCleanName(isVirtual)));
}
}

Expand All @@ -1986,7 +1989,7 @@ void AddColumns(const TInputs& inputs, const bool* hasStar, const THashSet<TStri
}

for (const auto& ref : qualifiedRefs->find(x.Alias)->second) {
auto pos = x.Type->FindItemI(ref);
auto pos = x.Type->FindItemI(ref, nullptr);
if (pos) {
auto item = x.Type->GetItems()[*pos];
item = AddAlias(x.Alias, item, ctx);
Expand Down Expand Up @@ -2110,7 +2113,7 @@ IGraphTransformer::TStatus RebuildLambdaColumns(const TExprNode::TPtr& root, con
}
}

auto pos = x.Type->FindItemI(node->Tail().Content());
auto pos = x.Type->FindItemI(node->Tail().Content(), nullptr);
if (pos) {
return ctx.Expr.Builder(node->Pos())
.Callable("Member")
Expand Down Expand Up @@ -3119,7 +3122,7 @@ bool GatherExtraSortColumns(const TExprNode& data, const TInputs& inputs, TExprN
continue;
}

auto pos = x.Type->FindItemI(column);
auto pos = x.Type->FindItemI(column, nullptr);
if (pos) {
index = inputIndex;
break;
Expand Down Expand Up @@ -3791,7 +3794,7 @@ IGraphTransformer::TStatus PgSetItemWrapper(const TExprNode::TPtr& input, TExprN
TVector<const TItemExprType*> newStructItems;
TColumnOrder newOrder;
for (ui32 i = 0; i < p->Child(2)->ChildrenSize(); ++i) {
auto pos = inputStructType->FindItemI((*columnOrder)[i]);
auto pos = inputStructType->FindItemI((*columnOrder)[i], nullptr);
YQL_ENSURE(pos);
auto type = inputStructType->GetItems()[*pos]->GetItemType();
newOrder.push_back(TString(p->Child(2)->Child(i)->Content()));
Expand Down Expand Up @@ -4138,11 +4141,12 @@ IGraphTransformer::TStatus PgSetItemWrapper(const TExprNode::TPtr& input, TExprN
} else {
int matchCount = 0;
for (size_t j = 0; j <= groupInputs.size() - 2; ++j) {
auto pos = groupInputs[j].Type->FindItemI(name);
bool isVirtual;
auto pos = groupInputs[j].Type->FindItemI(name, &isVirtual);
if (!pos) {
continue;
}
lrNames[0] = ctx.Expr.NewAtom(inp->Pos(), MakeAliasedColumn(groupInputs[j].Alias, groupInputs[j].Type->GetItems()[*pos]->GetName()));
lrNames[0] = ctx.Expr.NewAtom(inp->Pos(), MakeAliasedColumn(groupInputs[j].Alias, groupInputs[j].Type->GetItems()[*pos]->GetCleanName(isVirtual)));
++matchCount;
}
if (!matchCount) {
Expand All @@ -4154,13 +4158,14 @@ IGraphTransformer::TStatus PgSetItemWrapper(const TExprNode::TPtr& input, TExprN
return IGraphTransformer::TStatus::Error;
}
}
auto pos = rightInput.Type->FindItemI(name);
bool isVirtual;
auto pos = rightInput.Type->FindItemI(name, &isVirtual);
usedInUsingBefore.emplace(lcase);
if (!pos) {
ctx.Expr.AddError(TIssue(ctx.Expr.GetPosition(child->Pos()), TStringBuilder() << "Can't find column: " << name));
return IGraphTransformer::TStatus::Error;
}
lrNames[1] = ctx.Expr.NewAtom(inp->Pos(), MakeAliasedColumn(rightInput.Alias, rightInput.Type->GetItems()[*pos]->GetName()));
lrNames[1] = ctx.Expr.NewAtom(inp->Pos(), MakeAliasedColumn(rightInput.Alias, rightInput.Type->GetItems()[*pos]->GetCleanName(isVirtual)));
nodes[colIdx] = ctx.Expr.NewList(inp->Pos(), std::move(lrNames));
}
TExprNode::TListType newJoin(4);
Expand Down Expand Up @@ -4616,9 +4621,10 @@ IGraphTransformer::TStatus PgSetItemWrapper(const TExprNode::TPtr& input, TExprN
const auto type = data.Child(j)->Tail().GetTypeAnn()->Cast<TTypeExprType>()->
GetType()->Cast<TStructExprType>();
for (const auto& col : x.UsedExternalColumns) {
auto pos = type->FindItemI(col);
bool isVirtual;
auto pos = type->FindItemI(col, &isVirtual);
YQL_ENSURE(pos);
items.push_back(type->GetItems()[*pos]);
items.push_back(type->GetItems()[*pos]->GetCleanItem(isVirtual, ctx.Expr));
}

auto effectiveType = ctx.Expr.MakeType<TStructExprType>(items);
Expand Down
21 changes: 17 additions & 4 deletions ydb/library/yql/parser/pg_catalog/catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1604,17 +1604,18 @@ struct TCatalog {
"lo_close",
"lo_unlink"
}),
StaticTables({
AllStaticTables({
#include "pg_class.generated.h"
}),
AllStaticColumns({
#include "columns.generated.h"
})
{
THashSet<ui32> usedTableOids;
for (const auto& t : StaticTables) {
for (const auto& t : AllStaticTables) {
StaticColumns.insert(std::make_pair(t, TVector<TColumnInfo>()));
Y_ENSURE(usedTableOids.insert(t.Oid).first);
StaticTables.insert(std::make_pair(TTableInfoKey(t), t));
}

for (const auto& c: AllStaticColumns) {
Expand Down Expand Up @@ -1834,8 +1835,9 @@ struct TCatalog {
THashMap<TString, TVector<ui32>> AggregationsByName;
THashSet<TString> ProhibitedProcs;

TVector<TTableInfo> StaticTables;
TVector<TTableInfo> AllStaticTables;
TVector<TColumnInfo> AllStaticColumns;
THashMap<TTableInfoKey, TTableInfo> StaticTables;
THashMap<TTableInfoKey, TVector<TColumnInfo>> StaticColumns;
};

Expand Down Expand Up @@ -3079,12 +3081,23 @@ void EnumLanguages(std::function<void(ui32, const TLanguageDesc&)> f) {

const TVector<TTableInfo>& GetStaticTables() {
const auto& catalog = TCatalog::Instance();
return catalog.StaticTables;
return catalog.AllStaticTables;
}

const THashMap<TTableInfoKey, TVector<TColumnInfo>>& GetStaticColumns() {
const auto& catalog = TCatalog::Instance();
return catalog.StaticColumns;
}

const TTableInfo& LookupStaticTable(const TTableInfoKey& tableKey) {
const auto& catalog = TCatalog::Instance();
auto tablePtr = catalog.StaticTables.FindPtr(tableKey);
if (!tablePtr) {
throw yexception() << "No such table: " << tableKey.Schema << "." << tableKey.Name;
}

return *tablePtr;
}


}
2 changes: 1 addition & 1 deletion ydb/library/yql/parser/pg_catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ struct TColumnInfo {
};

const TVector<TTableInfo>& GetStaticTables();
const TTableInfo& LookupStaticTable(const TTableInfoKey& tableKey);
const THashMap<TTableInfoKey, TVector<TColumnInfo>>& GetStaticColumns();

}

template <>
Expand Down
Loading

0 comments on commit 0224138

Please sign in to comment.