diff options
author | vvvv <vvvv@yandex-team.ru> | 2022-02-23 14:47:59 +0300 |
---|---|---|
committer | vvvv <vvvv@yandex-team.ru> | 2022-02-23 14:47:59 +0300 |
commit | 627718e309e5ac397388c83fd4d287e48b51c78d (patch) | |
tree | 09911de39c5e786f535d0393c67b79493fd95cab | |
parent | cd5c4d9254f66cc8ea5753a83971e35cc32c1526 (diff) | |
download | ydb-627718e309e5ac397388c83fd4d287e48b51c78d.tar.gz |
YQL-13710 initial type-dependent lookup
ref:9bcf96e8745b43b3fc8e03a3b87079175677a0e0
-rw-r--r-- | ydb/library/yql/parser/pg_catalog/catalog.cpp | 30 | ||||
-rw-r--r-- | ydb/library/yql/parser/pg_catalog/catalog.h | 2 | ||||
-rw-r--r-- | ydb/library/yql/parser/pg_catalog/ut/catalog_ut.cpp | 45 |
3 files changed, 35 insertions, 42 deletions
diff --git a/ydb/library/yql/parser/pg_catalog/catalog.cpp b/ydb/library/yql/parser/pg_catalog/catalog.cpp index 6071c93c48..8011878831 100644 --- a/ydb/library/yql/parser/pg_catalog/catalog.cpp +++ b/ydb/library/yql/parser/pg_catalog/catalog.cpp @@ -193,7 +193,6 @@ struct TCatalog { Procs = ParseProcs(procData); for (const auto& [k, v]: Procs) { ProcByName[v.Name].push_back(k); - ProcBySrc[v.Src] = k; } } @@ -204,31 +203,26 @@ struct TCatalog { TOperators Operators; TProcs Procs; THashMap<TString, TVector<ui32>> ProcByName; - THashMap<TString, ui32> ProcBySrc; }; -const TProcDesc* LookupFunctionSignature(const TString& name) { +const TProcDesc& LookupProc(const TString& name, const TVector<TString>& argTypes) { const auto& catalog = TCatalog::Instance(); - auto srcIdPtr = catalog.ProcBySrc.FindPtr(name); - ui32 procId; - if (srcIdPtr) { - procId = *srcIdPtr; - } else { - auto procIdPtr = catalog.ProcByName.FindPtr(name); - if (!procIdPtr) { - return nullptr; - } + auto procIdPtr = catalog.ProcByName.FindPtr(name); + if (!procIdPtr) { + throw yexception() << "No such function: " << name; + } - if (procIdPtr->size() != 1) { - throw yexception() << "Ambiguous name: " << name; + for (const auto& id : *procIdPtr) { + const auto& d = catalog.Procs.FindPtr(id); + Y_ENSURE(d); + if (d->ArgTypes != argTypes) { + continue; } - procId = procIdPtr->at(0); + return *d; } - auto desc = catalog.Procs.FindPtr(procId); - Y_ENSURE(desc); - return desc; + throw yexception() << "Unable to find an overload for function " << name << " with given argument types"; } } diff --git a/ydb/library/yql/parser/pg_catalog/catalog.h b/ydb/library/yql/parser/pg_catalog/catalog.h index fbfaaf727d..cc92b5cc9b 100644 --- a/ydb/library/yql/parser/pg_catalog/catalog.h +++ b/ydb/library/yql/parser/pg_catalog/catalog.h @@ -29,6 +29,6 @@ struct TProcDesc { bool IsStrict = true; }; -const TProcDesc* LookupFunctionSignature(const TString& name); +const TProcDesc& LookupProc(const TString& name, const TVector<TString>& argTypes); } diff --git a/ydb/library/yql/parser/pg_catalog/ut/catalog_ut.cpp b/ydb/library/yql/parser/pg_catalog/ut/catalog_ut.cpp index 89f0adb728..719a90c813 100644 --- a/ydb/library/yql/parser/pg_catalog/ut/catalog_ut.cpp +++ b/ydb/library/yql/parser/pg_catalog/ut/catalog_ut.cpp @@ -6,35 +6,34 @@ using namespace NYql; Y_UNIT_TEST_SUITE(TFunctionsTests) { Y_UNIT_TEST(TestMissing) { - auto ret = LookupFunctionSignature("_foo_bar_"); - UNIT_ASSERT(!ret); + UNIT_ASSERT_EXCEPTION(LookupProc("_foo_bar_", {}), yexception); } - Y_UNIT_TEST(TestAmbiguous) { - UNIT_ASSERT_EXCEPTION(LookupFunctionSignature("substring"), yexception); + Y_UNIT_TEST(TestMismatchArgTypes) { + UNIT_ASSERT_EXCEPTION(LookupProc("int4pl", {}), yexception); } Y_UNIT_TEST(TestOk) { - auto ret = LookupFunctionSignature("int4pl"); - UNIT_ASSERT(ret); - UNIT_ASSERT_VALUES_EQUAL(ret->ResultType, "int4"); - UNIT_ASSERT_VALUES_EQUAL(ret->ArgTypes.size(), 2); - UNIT_ASSERT_VALUES_EQUAL(ret->ArgTypes[0], "int4"); - UNIT_ASSERT_VALUES_EQUAL(ret->ArgTypes[1], "int4"); + auto ret = LookupProc("int4pl", {"int4", "int4"}); + UNIT_ASSERT_VALUES_EQUAL(ret.ResultType, "int4"); + UNIT_ASSERT_VALUES_EQUAL(ret.ArgTypes.size(), 2); + UNIT_ASSERT_VALUES_EQUAL(ret.ArgTypes[0], "int4"); + UNIT_ASSERT_VALUES_EQUAL(ret.ArgTypes[1], "int4"); + UNIT_ASSERT_VALUES_EQUAL(ret.Src, "int4pl"); - ret = LookupFunctionSignature("text_substr"); - UNIT_ASSERT(ret); - UNIT_ASSERT_VALUES_EQUAL(ret->ResultType, "text"); - UNIT_ASSERT_VALUES_EQUAL(ret->ArgTypes.size(), 3); - UNIT_ASSERT_VALUES_EQUAL(ret->ArgTypes[0], "text"); - UNIT_ASSERT_VALUES_EQUAL(ret->ArgTypes[1], "int4"); - UNIT_ASSERT_VALUES_EQUAL(ret->ArgTypes[2], "int4"); + ret = LookupProc("substring", {"text", "int4", "int4"}); + UNIT_ASSERT_VALUES_EQUAL(ret.ResultType, "text"); + UNIT_ASSERT_VALUES_EQUAL(ret.ArgTypes.size(), 3); + UNIT_ASSERT_VALUES_EQUAL(ret.ArgTypes[0], "text"); + UNIT_ASSERT_VALUES_EQUAL(ret.ArgTypes[1], "int4"); + UNIT_ASSERT_VALUES_EQUAL(ret.ArgTypes[2], "int4"); + UNIT_ASSERT_VALUES_EQUAL(ret.Src, "text_substr"); - ret = LookupFunctionSignature("text_substr_no_len"); - UNIT_ASSERT(ret); - UNIT_ASSERT_VALUES_EQUAL(ret->ResultType, "text"); - UNIT_ASSERT_VALUES_EQUAL(ret->ArgTypes.size(), 2); - UNIT_ASSERT_VALUES_EQUAL(ret->ArgTypes[0], "text"); - UNIT_ASSERT_VALUES_EQUAL(ret->ArgTypes[1], "int4"); + ret = LookupProc("substring", {"text", "int4"}); + UNIT_ASSERT_VALUES_EQUAL(ret.ResultType, "text"); + UNIT_ASSERT_VALUES_EQUAL(ret.ArgTypes.size(), 2); + UNIT_ASSERT_VALUES_EQUAL(ret.ArgTypes[0], "text"); + UNIT_ASSERT_VALUES_EQUAL(ret.ArgTypes[1], "int4"); + UNIT_ASSERT_VALUES_EQUAL(ret.Src, "text_substr_no_len"); } } |