Skip to content
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
14 changes: 13 additions & 1 deletion ydb/library/yql/parser/pg_wrapper/arrow.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct TPgKernelState : arrow::compute::KernelState {
bool IsFixedResult;
i32 TypeLen;
std::shared_ptr<void> FmgrDataHolder;
const NPg::TProcDesc* ProcDesc;
};

template <PGFunction PgFunc>
Expand Down Expand Up @@ -356,7 +357,7 @@ struct TGenericExec {
fcinfo->context = state.context;
fcinfo->resultinfo = state.resultinfo;
fcinfo->fncollation = state.fncollation;
fcinfo->nargs = batch.values.size();
fcinfo->nargs = batch.values.size();

TInputArgsAccessor<TArgsPolicy> inputArgsAccessor;
inputArgsAccessor.Bind(batch.values);
Expand All @@ -371,6 +372,7 @@ struct TGenericExec {

for (size_t i = 0; i < length; ++i) {
Datum ret;
bool needToFree = false;
if constexpr (!TArgsPolicy::VarArgs) {
if (!constexpr_for_tuple([&](auto const& j, auto const& v) {
NullableDatum d;
Expand Down Expand Up @@ -438,7 +440,17 @@ struct TGenericExec {
}

fcinfo->isnull = false;
if constexpr (TArgsPolicy::VarArgs) {
needToFree = PrepareVariadicArray(*fcinfo, *state.ProcDesc);
}

ret = Func(fcinfo);
if constexpr (TArgsPolicy::VarArgs) {
if (needToFree) {
FreeVariadicArray(*fcinfo, batch.values.size());
}
}

if constexpr (IsFixedResult) {
fixedResultData[i] = ui64(ret);
fixedResultValidMask[i] = !fcinfo->isnull;
Expand Down
13 changes: 10 additions & 3 deletions ydb/library/yql/parser/pg_wrapper/comp_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,14 +1117,20 @@ class TPgResolvedCall : public TPgResolvedCallBase<TPgResolvedCall<UseContext>>
callInfo.args[i] = argDatum;
}

const bool needToFree = PrepareVariadicArray(callInfo, this->ProcDesc);
NUdf::TUnboxedValuePod res;
if constexpr (!UseContext) {
TPAllocScope call;
return this->DoCall(callInfo);
res = this->DoCall(callInfo);
} else {
res = this->DoCall(callInfo);
}

if constexpr (UseContext) {
return this->DoCall(callInfo);
if (needToFree) {
FreeVariadicArray(callInfo, this->ArgNodes.size());
}

return res;
}

private:
Expand Down Expand Up @@ -2618,6 +2624,7 @@ std::shared_ptr<arrow::compute::ScalarKernel> MakePgKernel(TVector<TType*> argTy

state->flinfo.fn_expr = fmgrDataHolder->Build(procDesc);
state->FmgrDataHolder = fmgrDataHolder;
state->ProcDesc = &procDesc;

return arrow::Result(std::move(state));
};
Expand Down
8 changes: 2 additions & 6 deletions ydb/library/yql/parser/pg_wrapper/ut/codegen_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,8 @@ Y_UNIT_TEST_SUITE(TPgCodegen) {
kernelCtx.SetState(&state);
FmgrInfo finfo;
Zero(state.flinfo);
if (fixed) {
fmgr_info(NPg::LookupProc("date_eq", { 0, 0}).ProcId, &state.flinfo);
} else {
fmgr_info(NPg::LookupProc("textout", { 0} ).ProcId, &state.flinfo);
}

state.ProcDesc = fixed ? &NPg::LookupProc("date_eq", { 0, 0 }) : &NPg::LookupProc("textout", { 0 });
fmgr_info(state.ProcDesc->ProcId, &state.flinfo);
state.context = nullptr;
state.resultinfo = nullptr;
state.fncollation = DEFAULT_COLLATION_OID;
Expand Down
47 changes: 47 additions & 0 deletions ydb/library/yql/parser/pg_wrapper/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <util/system/compiler.h>
#include <ydb/library/yql/parser/pg_catalog/catalog.h>

extern "C" {
#include "postgres.h"
#include "fmgr.h"
#include "utils/array.h"
}

#undef Max

#include "utils.h"

namespace NYql {

void PrepareVariadicArraySlow(FunctionCallInfoBaseData& callInfo, const NPg::TProcDesc& desc) {
const auto& elemDesc = NPg::LookupType(desc.VariadicType);

Datum varArgs[FUNC_MAX_ARGS];
bool varArgsNulls[FUNC_MAX_ARGS];
const ui32 nelems = callInfo.nargs - desc.ArgTypes.size();
Y_ENSURE(nelems >= 1);
for (ui32 i = desc.ArgTypes.size(); i < callInfo.nargs; ++i) {
varArgs[i - desc.ArgTypes.size()] = callInfo.args[i].value;
varArgsNulls[i - desc.ArgTypes.size()] = callInfo.args[i].isnull;
}

callInfo.nargs = desc.ArgTypes.size() + 1;
int dims[MAXDIM];
int lbs[MAXDIM];
dims[0] = nelems;
lbs[0] = 1;

auto array = construct_md_array(varArgs, varArgsNulls, 1, dims, lbs,
desc.VariadicType, elemDesc.TypeLen, elemDesc.PassByValue, elemDesc.TypeAlign);

auto& argDatum = callInfo.args[callInfo.nargs - 1];
argDatum.value = PointerGetDatum(array);
argDatum.isnull = false;
}

void FreeVariadicArray(FunctionCallInfoBaseData& callInfo, ui32 originalArgs) {
pfree(DatumGetPointer(callInfo.args[callInfo.nargs - 1].value));
callInfo.nargs = originalArgs;
}

}
12 changes: 12 additions & 0 deletions ydb/library/yql/parser/pg_wrapper/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,16 @@ inline ui32 MakeTypeIOParam(const NPg::TTypeDesc& desc) {
return desc.ElementTypeId ? desc.ElementTypeId : desc.TypeId;
}

void PrepareVariadicArraySlow(FunctionCallInfoBaseData& callInfo, const NPg::TProcDesc& desc);
void FreeVariadicArray(FunctionCallInfoBaseData& callInfo, ui32 originalArgs);

inline bool PrepareVariadicArray(FunctionCallInfoBaseData& callInfo, const NPg::TProcDesc& desc) {
if (!desc.VariadicArgType || desc.VariadicArgType == desc.VariadicType) {
return false;
}

PrepareVariadicArraySlow(callInfo, desc);
return true;
}

}
1 change: 1 addition & 0 deletions ydb/library/yql/parser/pg_wrapper/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SRCS(
cost_mocks.cpp
syscache.cpp
pg_utils_wrappers.cpp
utils.cpp
)

IF (ARCH_X86_64)
Expand Down
22 changes: 22 additions & 0 deletions ydb/library/yql/tests/sql/dq_file/part12/canondata/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,28 @@
}
],
"test.test[pg-tpch-q16-default.txt-Results]": [],
"test.test[pg-variadic_array_arg-default.txt-Analyze]": [
{
"checksum": "b4dd508a329723c74293d80f0278c705",
"size": 505,
"uri": "https://{canondata_backend}/1942671/2b244e8340f9afab99b1136c03a3466d54265dea/resource.tar.gz#test.test_pg-variadic_array_arg-default.txt-Analyze_/plan.txt"
}
],
"test.test[pg-variadic_array_arg-default.txt-Debug]": [
{
"checksum": "22b71fe5b1ac2e9a705d2153308b4a05",
"size": 567,
"uri": "https://{canondata_backend}/1942671/2b244e8340f9afab99b1136c03a3466d54265dea/resource.tar.gz#test.test_pg-variadic_array_arg-default.txt-Debug_/opt.yql_patched"
}
],
"test.test[pg-variadic_array_arg-default.txt-Plan]": [
{
"checksum": "b4dd508a329723c74293d80f0278c705",
"size": 505,
"uri": "https://{canondata_backend}/1942671/2b244e8340f9afab99b1136c03a3466d54265dea/resource.tar.gz#test.test_pg-variadic_array_arg-default.txt-Plan_/plan.txt"
}
],
"test.test[pg-variadic_array_arg-default.txt-Results]": [],
"test.test[pg_catalog-pg_class-default.txt-Analyze]": [
{
"checksum": "c1f2d837c3623c81dd596a9877913fb8",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,20 @@
"uri": "https://{canondata_backend}/1936842/5461a7f0f4d722c81cba2eff5dd1d41bf3a77f80/resource.tar.gz#test.test_pg-tpch-q18-default.txt-Plan_/plan.txt"
}
],
"test.test[pg-variadic_array_arg-default.txt-Debug]": [
{
"checksum": "ffb1482578a6230fb1086e0c71c19735",
"size": 566,
"uri": "https://{canondata_backend}/1923547/be7e687ea36299e4a042c1495c58b793c69141af/resource.tar.gz#test.test_pg-variadic_array_arg-default.txt-Debug_/opt.yql_patched"
}
],
"test.test[pg-variadic_array_arg-default.txt-Plan]": [
{
"checksum": "b4dd508a329723c74293d80f0278c705",
"size": 505,
"uri": "https://{canondata_backend}/1923547/be7e687ea36299e4a042c1495c58b793c69141af/resource.tar.gz#test.test_pg-variadic_array_arg-default.txt-Plan_/plan.txt"
}
],
"test.test[pg_catalog-pg_description_pg_syntax-default.txt-Debug]": [
{
"checksum": "b7b94c65971f853c86abf32804ff3370",
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 @@ -13474,6 +13474,13 @@
"uri": "https://{canondata_backend}/937458/e965a077f6aeb511184e419b0de866e505778b68/resource.tar.gz#test_sql2yql.test_pg-variadic_/sql.yql"
}
],
"test_sql2yql.test[pg-variadic_array_arg]": [
{
"checksum": "1815b2cb1ec493aefbcf189ebd533e2a",
"size": 608,
"uri": "https://{canondata_backend}/1936273/81aad5938509f97b7812a831a7a4698b6f82ebcc/resource.tar.gz#test_sql2yql.test_pg-variadic_array_arg_/sql.yql"
}
],
"test_sql2yql.test[pg-wide_sort]": [
{
"checksum": "0a312c05be5c91545de12d451a697d00",
Expand Down
3 changes: 3 additions & 0 deletions ydb/library/yql/tests/sql/suites/pg/variadic_array_arg.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--!syntax_pg
select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}', 'f4', 'f6')

Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,27 @@
"uri": "https://{canondata_backend}/1942415/14c2050022adcb6379d90c4a2a09abf0847e7643/resource.tar.gz#test.test_pg-tpch-q16-default.txt-Results_/results.txt"
}
],
"test.test[pg-variadic_array_arg-default.txt-Debug]": [
{
"checksum": "8ca7d22107f6a514104e3669b8015871",
"size": 511,
"uri": "https://{canondata_backend}/1937150/18f6be1226f90a509c838f8303749f0e053d2e45/resource.tar.gz#test.test_pg-variadic_array_arg-default.txt-Debug_/opt.yql"
}
],
"test.test[pg-variadic_array_arg-default.txt-Plan]": [
{
"checksum": "b4dd508a329723c74293d80f0278c705",
"size": 505,
"uri": "https://{canondata_backend}/1937150/18f6be1226f90a509c838f8303749f0e053d2e45/resource.tar.gz#test.test_pg-variadic_array_arg-default.txt-Plan_/plan.txt"
}
],
"test.test[pg-variadic_array_arg-default.txt-Results]": [
{
"checksum": "64d4e5a10ce4702fb690e678b070597f",
"size": 678,
"uri": "https://{canondata_backend}/1937150/18f6be1226f90a509c838f8303749f0e053d2e45/resource.tar.gz#test.test_pg-variadic_array_arg-default.txt-Results_/results.txt"
}
],
"test.test[pg_catalog-pg_class-default.txt-Debug]": [
{
"checksum": "99f6276f6794cfca07f55964603dacfe",
Expand Down