Skip to content

Filled pg_operator table #3093

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 1 commit into from
Mar 22, 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
87 changes: 83 additions & 4 deletions ydb/library/yql/parser/pg_catalog/catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,21 @@ bool ValidateOperArgs(const TOperDesc& d, const TVector<ui32>& argTypeIds, const
return true;
}

struct TLazyOperInfo {
TString Com;
TString Negate;
};

class TOperatorsParser : public TParser {
public:
TOperatorsParser(TOperators& operators, const THashMap<TString, ui32>& typeByName, const TTypes& types,
const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs)
const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs, THashMap<ui32, TLazyOperInfo>& lazyInfos)
: Operators(operators)
, TypeByName(typeByName)
, Types(types)
, ProcByName(procByName)
, Procs(procs)
, LazyInfos(lazyInfos)
{}

void OnKey(const TString& key, const TString& value) override {
Expand Down Expand Up @@ -346,6 +352,10 @@ class TOperatorsParser : public TParser {
LastOperator.ResultType = *typeIdPtr;
} else if (key == "oprcode") {
LastCode = value;
} else if (key == "oprnegate") {
LastNegate = value;
} else if (key == "oprcom") {
LastCom = value;
}
}

Expand All @@ -372,11 +382,21 @@ class TOperatorsParser : public TParser {
Y_ENSURE(!LastOperator.Name.empty());
Operators[LastOperator.OperId] = LastOperator;
}

if (!LastCom.empty()) {
LazyInfos[LastOperator.OperId].Com = LastCom;
}

if (!LastNegate.empty()) {
LazyInfos[LastOperator.OperId].Negate = LastNegate;
}
}
}

LastOperator = TOperDesc();
LastCode = "";
LastNegate = "";
LastCom = "";
IsSupported = true;
}

Expand All @@ -386,9 +406,12 @@ class TOperatorsParser : public TParser {
const TTypes& Types;
const THashMap<TString, TVector<ui32>>& ProcByName;
const TProcs& Procs;
THashMap<ui32, TLazyOperInfo>& LazyInfos;
TOperDesc LastOperator;
bool IsSupported = true;
TString LastCode;
TString LastNegate;
TString LastCom;
};

class TProcsParser : public TParser {
Expand Down Expand Up @@ -1362,13 +1385,67 @@ class TLanguagesParser : public TParser {
};

TOperators ParseOperators(const TString& dat, const THashMap<TString, ui32>& typeByName,
const TTypes& types, const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs) {
const TTypes& types, const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs, THashMap<ui32, TLazyOperInfo>& lazyInfos) {
TOperators ret;
TOperatorsParser parser(ret, typeByName, types, procByName, procs);
TOperatorsParser parser(ret, typeByName, types, procByName, procs, lazyInfos);
parser.Do(dat);
return ret;
}

ui32 FindOperator(const THashMap<TString, TVector<ui32>>& operatorsByName, const THashMap<TString, ui32>& typeByName, TOperators& operators, const TString& signature) {
auto pos1 = signature.find('(');
auto pos2 = signature.find(')');
Y_ENSURE(pos1 != TString::npos && pos1 > 0);
Y_ENSURE(pos2 != TString::npos && pos2 > pos1);
auto name = signature.substr(0, pos1);
auto operIdsPtr = operatorsByName.FindPtr(name);
Y_ENSURE(operIdsPtr);
TVector<TString> strArgs;
Split(signature.substr(pos1 + 1, pos2 - pos1 - 1), ",", strArgs);
Y_ENSURE(strArgs.size() >= 1 && strArgs.size() <= 2);
TVector<ui32> argTypes;
for (const auto& str : strArgs) {
auto typePtr = typeByName.FindPtr(str);
Y_ENSURE(typePtr);
argTypes.push_back(*typePtr);
}

for (const auto& operId : *operIdsPtr) {
auto operPtr = operators.FindPtr(operId);
Y_ENSURE(operPtr);
if (argTypes.size() == 1) {
if (operPtr->RightType != argTypes[0]) {
continue;
}
} else {
if (operPtr->LeftType != argTypes[0]) {
continue;
}

if (operPtr->RightType != argTypes[1]) {
continue;
}
}

return operId;
}

// for example, some operators are based on SQL system_functions.sql
return 0;
}

void ApplyLazyOperInfos(TOperators& operators, const THashMap<TString, TVector<ui32>>& operatorsByName, const THashMap<TString, ui32>& typeByName, const THashMap<ui32, TLazyOperInfo>& lazyInfos) {
for (const auto& x : lazyInfos) {
if (!x.second.Com.empty()) {
operators[x.first].ComId = FindOperator(operatorsByName, typeByName, operators, x.second.Com);
}

if (!x.second.Negate.empty()) {
operators[x.first].NegateId = FindOperator(operatorsByName, typeByName, operators, x.second.Negate);
}
}
}

TAggregations ParseAggregations(const TString& dat, const THashMap<TString, ui32>& typeByName,
const TTypes& types, const THashMap<TString, TVector<ui32>>& procByName, const TProcs& procs) {
TAggregations ret;
Expand Down Expand Up @@ -1682,11 +1759,13 @@ struct TCatalog {
Y_ENSURE(CastsByDir.insert(std::make_pair(std::make_pair(v.SourceId, v.TargetId), k)).second);
}

Operators = ParseOperators(opData, TypeByName, Types, ProcByName, Procs);
THashMap<ui32, TLazyOperInfo> lazyOperInfos;
Operators = ParseOperators(opData, TypeByName, Types, ProcByName, Procs, lazyOperInfos);
for (const auto&[k, v] : Operators) {
OperatorsByName[v.Name].push_back(k);
}

ApplyLazyOperInfos(Operators, OperatorsByName, TypeByName, lazyOperInfos);
Aggregations = ParseAggregations(aggData, TypeByName, Types, ProcByName, Procs);
for (const auto&[k, v] : Aggregations) {
AggregationsByName[v.Name].push_back(k);
Expand Down
2 changes: 2 additions & 0 deletions ydb/library/yql/parser/pg_catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct TOperDesc {
ui32 RightType = 0;
ui32 ResultType = 0;
ui32 ProcId = 0;
ui32 ComId = 0;
ui32 NegateId = 0;
};

enum class EProcKind : char {
Expand Down
29 changes: 29 additions & 0 deletions ydb/library/yql/parser/pg_wrapper/comp_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,20 @@ class TPgTableContent : public TMutableComputationNode<TPgTableContent> {
};

ApplyFillers(AllPgProcFillers, Y_ARRAY_SIZE(AllPgProcFillers), PgProcFillers_);
} else if (Table_ == "pg_operator") {
static const std::pair<const char*, TPgOperFiller> AllPgOperFillers[] = {
{"oid", [](const NPg::TOperDesc& desc) { return ScalarDatumToPod(ObjectIdGetDatum(desc.OperId)); }},
{"oprcom", [](const NPg::TOperDesc& desc) { return ScalarDatumToPod(ObjectIdGetDatum(desc.ComId)); }},
{"oprleft", [](const NPg::TOperDesc& desc) { return ScalarDatumToPod(ObjectIdGetDatum(desc.LeftType)); }},
{"oprname", [](const NPg::TOperDesc& desc) { return PointerDatumToPod((Datum)MakeFixedString(desc.Name, NAMEDATALEN)); }},
{"oprnamespace", [](const NPg::TOperDesc& desc) { return ScalarDatumToPod(ObjectIdGetDatum(PG_CATALOG_NAMESPACE)); }},
{"oprnegate", [](const NPg::TOperDesc& desc) { return ScalarDatumToPod(ObjectIdGetDatum(desc.NegateId)); }},
{"oprowner", [](const NPg::TOperDesc& desc) { return ScalarDatumToPod(ObjectIdGetDatum(1)); }},
{"oprresult", [](const NPg::TOperDesc& desc) { return ScalarDatumToPod(ObjectIdGetDatum(desc.ResultType)); }},
{"oprright", [](const NPg::TOperDesc& desc) { return ScalarDatumToPod(ObjectIdGetDatum(desc.RightType)); }},
};

ApplyFillers(AllPgOperFillers, Y_ARRAY_SIZE(AllPgOperFillers), PgOperFillers_);
} else if (Table_ == "pg_aggregate") {
static const std::pair<const char*, TPgAggregateFiller> AllPgAggregateFillers[] = {
{"aggfnoid", [](const NPg::TAggregateDesc& desc) { return ScalarDatumToPod(ObjectIdGetDatum(desc.AggId)); }},
Expand Down Expand Up @@ -737,6 +751,18 @@ class TPgTableContent : public TMutableComputationNode<TPgTableContent> {
}
}

rows.emplace_back(row);
});
} else if (Table_ == "pg_operator") {
NPg::EnumOperators([&](const NPg::TOperDesc& desc) {
NUdf::TUnboxedValue* items;
auto row = compCtx.HolderFactory.CreateDirectArrayHolder(PgOperFillers_.size(), items);
for (ui32 i = 0; i < PgOperFillers_.size(); ++i) {
if (PgOperFillers_[i]) {
items[i] = PgOperFillers_[i](desc);
}
}

rows.emplace_back(row);
});
} else if (Table_ == "pg_aggregate") {
Expand Down Expand Up @@ -854,6 +880,9 @@ class TPgTableContent : public TMutableComputationNode<TPgTableContent> {

using TPgLanguageFiller = NUdf::TUnboxedValuePod(*)(const NPg::TLanguageDesc&);
TVector<TPgLanguageFiller> PgLanguageFillers_;

using TPgOperFiller = NUdf::TUnboxedValuePod(*)(const NPg::TOperDesc&);
TVector<TPgOperFiller> PgOperFillers_;
};

class TFunctionCallInfo {
Expand Down
22 changes: 22 additions & 0 deletions ydb/library/yql/tests/sql/dq_file/part19/canondata/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -2376,6 +2376,28 @@
}
],
"test.test[pg_catalog-pg_inherits-default.txt-Results]": [],
"test.test[pg_catalog-pg_operator-default.txt-Analyze]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
"size": 948,
"uri": "https://{canondata_backend}/1899731/bf1552ea5a722ea8bc2de463418359c419c09386/resource.tar.gz#test.test_pg_catalog-pg_operator-default.txt-Analyze_/plan.txt"
}
],
"test.test[pg_catalog-pg_operator-default.txt-Debug]": [
{
"checksum": "7d3dca9c71abaa9e3e6f6c722d880117",
"size": 8718,
"uri": "https://{canondata_backend}/1899731/bf1552ea5a722ea8bc2de463418359c419c09386/resource.tar.gz#test.test_pg_catalog-pg_operator-default.txt-Debug_/opt.yql_patched"
}
],
"test.test[pg_catalog-pg_operator-default.txt-Plan]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
"size": 948,
"uri": "https://{canondata_backend}/1899731/bf1552ea5a722ea8bc2de463418359c419c09386/resource.tar.gz#test.test_pg_catalog-pg_operator-default.txt-Plan_/plan.txt"
}
],
"test.test[pg_catalog-pg_operator-default.txt-Results]": [],
"test.test[pg_catalog-pg_tablespace-default.txt-Analyze]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2407,6 +2407,20 @@
"uri": "https://{canondata_backend}/1936842/11d23d4a39031af80d6dc470ce99f9427771e7d4/resource.tar.gz#test.test_pg-values-default.txt-Plan_/plan.txt"
}
],
"test.test[pg_catalog-pg_operator-default.txt-Debug]": [
{
"checksum": "f3c7a708a71d2ac8029e39c23a264f18",
"size": 8717,
"uri": "https://{canondata_backend}/1942100/c3d647446edbe752077b3f908285ed4a4a032d7f/resource.tar.gz#test.test_pg_catalog-pg_operator-default.txt-Debug_/opt.yql_patched"
}
],
"test.test[pg_catalog-pg_operator-default.txt-Plan]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
"size": 948,
"uri": "https://{canondata_backend}/1942100/c3d647446edbe752077b3f908285ed4a4a032d7f/resource.tar.gz#test.test_pg_catalog-pg_operator-default.txt-Plan_/plan.txt"
}
],
"test.test[pg_catalog-pg_tablespace-default.txt-Debug]": [
{
"checksum": "6b156b324a12c3db5a2086a74c29aa5c",
Expand Down
7 changes: 7 additions & 0 deletions ydb/library/yql/tests/sql/sql2yql/canondata/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -13810,6 +13810,13 @@
"uri": "https://{canondata_backend}/1781765/838f2ea2c0326d84b118c1ad739c96cc6935c327/resource.tar.gz#test_sql2yql.test_pg_catalog-pg_namespace_pg_syntax_/sql.yql"
}
],
"test_sql2yql.test[pg_catalog-pg_operator]": [
{
"checksum": "232c62503b48f3fb9b548a839356dd14",
"size": 2286,
"uri": "https://{canondata_backend}/1942100/716d93bfadbda65b3aa70a8c7060fb888ce65b07/resource.tar.gz#test_sql2yql.test_pg_catalog-pg_operator_/sql.yql"
}
],
"test_sql2yql.test[pg_catalog-pg_proc]": [
{
"checksum": "d6ef15d8a06c97dd58aa4d19d042239f",
Expand Down
23 changes: 23 additions & 0 deletions ydb/library/yql/tests/sql/suites/pg_catalog/pg_operator.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--!syntax_pg
select
min(oid) as min_oid,
min(oprcom) as min_oprcom,
min(oprleft) as min_oprleft,
min(oprname) as min_oprname,
min(oprnamespace) as min_oprnamespace,
min(oprnegate) as min_oprnegate,
min(oprowner) as min_oprowner,
min(oprresult) as min_oprresult,
min(oprright) as min_oprright,

max(oid) as max_oid,
max(oprcom) as max_oprcom,
max(oprleft) as max_oprleft,
max(oprname) as max_oprname,
max(oprnamespace) as max_oprnamespace,
max(oprnegate) as max_oprnegate,
max(oprowner) as max_oprowner,
max(oprresult) as max_oprresult,
max(oprright) as max_oprright
from pg_operator;

Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,27 @@
"uri": "https://{canondata_backend}/1880306/1b2ed13cc159ed80cf548a266f7c691ac03de80a/resource.tar.gz#test.test_pg_catalog-pg_inherits-default.txt-Results_/results.txt"
}
],
"test.test[pg_catalog-pg_operator-default.txt-Debug]": [
{
"checksum": "03cff7950ae384b1ae5ccfb7a6333ba7",
"size": 8644,
"uri": "https://{canondata_backend}/1775319/a6272878eea075471386c8c6e2f0bf0b4ac8a8ce/resource.tar.gz#test.test_pg_catalog-pg_operator-default.txt-Debug_/opt.yql"
}
],
"test.test[pg_catalog-pg_operator-default.txt-Plan]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
"size": 948,
"uri": "https://{canondata_backend}/1775319/a6272878eea075471386c8c6e2f0bf0b4ac8a8ce/resource.tar.gz#test.test_pg_catalog-pg_operator-default.txt-Plan_/plan.txt"
}
],
"test.test[pg_catalog-pg_operator-default.txt-Results]": [
{
"checksum": "4c451ebc3f8bcb00b5518fea893b80aa",
"size": 5686,
"uri": "https://{canondata_backend}/1775319/a6272878eea075471386c8c6e2f0bf0b4ac8a8ce/resource.tar.gz#test.test_pg_catalog-pg_operator-default.txt-Results_/results.txt"
}
],
"test.test[pg_catalog-pg_tablespace-default.txt-Debug]": [
{
"checksum": "25f12ab4fa730b9992ecb3d9caa38919",
Expand Down