Skip to content
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

Fix column order #7839

Merged
merged 3 commits into from
Aug 16, 2024
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
2 changes: 1 addition & 1 deletion ydb/library/yql/core/type_ann/type_ann_pg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,7 @@ bool ScanColumns(TExprNode::TPtr root, TInputs& inputs, const THashSet<TString>&
}
}

if (x.Order && x.Order->IsDuplicated(TString(node->Tail().Content()))) {
if (x.Order && x.Order->IsDuplicatedIgnoreCase(TString(node->Tail().Content()))) {
ctx.Expr.AddError(TIssue(ctx.Expr.GetPosition(node->Pos()),
TStringBuilder() << "Column reference is ambiguous: " << node->Tail().Content()));
isError = true;
Expand Down
13 changes: 7 additions & 6 deletions ydb/library/yql/core/ut/yql_column_order_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ using namespace NYql;

Y_UNIT_TEST_SUITE(TYqlColumnOrder) {

Y_UNIT_TEST(ColumnOrderIgnoreCase) {
TColumnOrder order;
UNIT_ASSERT_EQUAL(order.AddColumn("a"), "a");
UNIT_ASSERT_EQUAL(order.AddColumn("A"), "A_generated_2");
}

Y_UNIT_TEST(ColumnOrderShrink) {
TColumnOrder order;
UNIT_ASSERT_EQUAL(order.AddColumn("a"), "a");
Expand All @@ -32,6 +26,13 @@ Y_UNIT_TEST_SUITE(TYqlColumnOrder) {
UNIT_ASSERT_EQUAL(order.AddColumn("a_generated_2_generated_2"), "a_generated_2_generated_2_generated_2");
}

Y_UNIT_TEST(ColumnOrderCaseSensetive) {
TColumnOrder order;
UNIT_ASSERT_EQUAL(order.AddColumn("a"), "a");
UNIT_ASSERT_EQUAL(order.AddColumn("A"), "A");
UNIT_ASSERT_EQUAL(order.IsDuplicatedIgnoreCase("a"), true);
}

Y_UNIT_TEST(ColumnOrderGeneratedMatchOverVectorCtor) {
TColumnOrder order(TVector<TString>{"a", "a", "a_generated_2", "a_generated_2_generated_2"});
TVector<TColumnOrder::TOrderedItem> got(order.begin(), order.end());
Expand Down
24 changes: 13 additions & 11 deletions ydb/library/yql/core/yql_type_annotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ TString TColumnOrder::Find(const TString& name) const {
TColumnOrder& TColumnOrder::operator=(const TColumnOrder& rhs) {
GeneratedToOriginal_ = rhs.GeneratedToOriginal_;
Order_ = rhs.Order_;
UseCountLcase_ = rhs.UseCountLcase_;
UseCount_ = rhs.UseCount_;
return *this;
}
Expand All @@ -137,32 +138,32 @@ TColumnOrder::TColumnOrder(const TVector<TString>& order) {

TString TColumnOrder::AddColumn(const TString& name) {
auto lcase = to_lower(name);
if (uint64_t count = ++UseCount_[lcase]; count > 1) {
++UseCountLcase_[lcase];
if (uint64_t count = ++UseCount_[name]; count > 1) {
TString generated = name + "_generated_" + ToString(count);
GeneratedToOriginal_[generated] = name;
Order_.emplace_back(name, generated);
++UseCount_[to_lower(generated)];
++UseCount_[generated];
return generated;
}
Order_.emplace_back(name, name);
GeneratedToOriginal_[name] = name;
return name;
}

bool TColumnOrder::IsDuplicated(const TString& name) const {
auto it = UseCount_.find(to_lower(name));
return it != UseCount_.end() && it->second > 1;
bool TColumnOrder::IsDuplicatedIgnoreCase(const TString& name) const {
auto it = UseCountLcase_.find(to_lower(name));
return it != UseCountLcase_.end() && it->second > 1;
}

void TColumnOrder::Shrink(size_t remain) {
for (size_t i = remain; i < Order_.size(); ++i) {
auto logicalLcase = to_lower(Order_[i].LogicalName);
if (!--UseCount_[logicalLcase]) {
UseCount_.erase(logicalLcase);
--UseCountLcase_[to_lower(Order_[i].LogicalName)];
if (!--UseCount_[Order_[i].LogicalName]) {
UseCount_.erase(Order_[i].LogicalName);
}
auto physicalLcase = to_lower(Order_[i].PhysicalName);
if (!--UseCount_[physicalLcase]) {
UseCount_.erase(physicalLcase);
if (!--UseCount_[Order_[i].PhysicalName]) {
UseCount_.erase(Order_[i].PhysicalName);
}
GeneratedToOriginal_.erase(Order_[i].PhysicalName);
}
Expand All @@ -179,6 +180,7 @@ void TColumnOrder::Clear() {
Order_.clear();
GeneratedToOriginal_.clear();
UseCount_.clear();
UseCountLcase_.clear();
}

void TColumnOrder::EraseIf(const std::function<bool(const TString&)>& fn) {
Expand Down
3 changes: 2 additions & 1 deletion ydb/library/yql/core/yql_type_annotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class TColumnOrder {
explicit TColumnOrder(const TVector<TString>& order);
TString AddColumn(const TString& name);

bool IsDuplicated(const TString& name) const;
bool IsDuplicatedIgnoreCase(const TString& name) const;

void Shrink(size_t remain);

Expand Down Expand Up @@ -213,6 +213,7 @@ class TColumnOrder {
private:
THashMap<TString, TString> GeneratedToOriginal_;
THashMap<TString, uint64_t> UseCount_;
THashMap<TString, uint64_t> UseCountLcase_;
// (name, generated_name)
TVector<TOrderedItem> Order_;
};
Expand Down
Loading