Skip to content

Commit

Permalink
DbgPrintValue for Pg types (#4847)
Browse files Browse the repository at this point in the history
  • Loading branch information
azevaykin authored May 27, 2024
1 parent cb83f09 commit 3c24073
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ydb/core/scheme/scheme_tablecell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,14 @@ void DbgPrintValue(TString &res, const TCell &r, NScheme::TTypeInfo typeInfo) {
case NScheme::NTypeIds::ActorId:
res += ToString(r.AsValue<NActors::TActorId>());
break;
case NScheme::NTypeIds::Pg:
// TODO: support pg types
case NScheme::NTypeIds::Pg: {
auto convert = NPg::PgNativeTextFromNativeBinary(r.AsBuf(), typeInfo.GetTypeDesc());
if (!convert.Error)
res += convert.Str;
else
res += *convert.Error;
break;
}
default:
res += EscapeC(r.Data(), r.Size());
}
Expand Down
70 changes: 70 additions & 0 deletions ydb/core/scheme/ut_pg/scheme_tablecell_pg_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <library/cpp/testing/unittest/registar.h>

#include <ydb/core/scheme/scheme_tablecell.h>
#include <ydb/core/scheme/scheme_type_registry.h>

extern "C" {
#include "postgres.h"
#include "catalog/pg_type_d.h"
}

namespace NKikimr {
namespace NTable {

Y_UNIT_TEST_SUITE(PgTest) {

Y_UNIT_TEST(DumpIntCells) {
NScheme::TTypeRegistry typeRegistry;

i64 intVal = 55555;

{
TCell cell((const char *)&intVal, sizeof(i64));
NScheme::TTypeInfo typeInfo(NScheme::TInt64::TypeId);

TString printRes = DbgPrintCell(cell, typeInfo, typeRegistry);
UNIT_ASSERT_STRINGS_EQUAL(printRes, "Int64 : 55555");
}

{
NScheme::TTypeInfo pgTypeInfo(NScheme::NTypeIds::Pg, NPg::TypeDescFromPgTypeId(INT8OID));
auto desc = pgTypeInfo.GetTypeDesc();
auto res = NPg::PgNativeBinaryFromNativeText(Sprintf("%u", intVal), desc);
UNIT_ASSERT_C(!res.Error, *res.Error);
TString binary = res.Str;
TCell pgCell((const char*)binary.data(), binary.size());

TString printRes = DbgPrintCell(pgCell, pgTypeInfo, typeRegistry);
UNIT_ASSERT_STRINGS_EQUAL(printRes, "pgint8 : 55555");
}
}

Y_UNIT_TEST(DumpStringCells) {
NScheme::TTypeRegistry typeRegistry;

char strVal[] = "This is the value";

{
TCell cell((const char*)&strVal, sizeof(strVal) - 1);
NScheme::TTypeInfo typeInfo(NScheme::TString::TypeId);

TString printRes = DbgPrintCell(cell, typeInfo, typeRegistry);
UNIT_ASSERT_STRINGS_EQUAL(printRes, TStringBuilder() << "String : " << strVal);
}

{
NScheme::TTypeInfo pgTypeInfo(NScheme::NTypeIds::Pg, NPg::TypeDescFromPgTypeId(TEXTOID));
auto desc = pgTypeInfo.GetTypeDesc();
auto res = NPg::PgNativeBinaryFromNativeText(strVal, desc);
UNIT_ASSERT_C(!res.Error, *res.Error);
TString binary = res.Str;
TCell pgCell((const char*)binary.data(), binary.size());

TString printRes = DbgPrintCell(pgCell, pgTypeInfo, typeRegistry);
UNIT_ASSERT_STRINGS_EQUAL(printRes, TStringBuilder() << "pgtext : " << strVal);
}
}
}

}
}
31 changes: 31 additions & 0 deletions ydb/core/scheme/ut_pg/ya.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
UNITTEST_FOR(ydb/core/scheme)

FORK_SUBTESTS()

SRCS(
scheme_tablecell_pg_ut.cpp
)

PEERDIR(
ydb/core/scheme
ydb/library/yql/public/udf/service/exception_policy
ydb/library/yql/parser/pg_wrapper
)

ADDINCL(
ydb/library/yql/parser/pg_wrapper/postgresql/src/include
)

IF (OS_WINDOWS)
CFLAGS(
"-D__thread=__declspec(thread)"
-Dfstat=microsoft_native_fstat
-Dstat=microsoft_native_stat
)
ENDIF()

NO_COMPILER_WARNINGS()

YQL_LAST_ABI_VERSION()

END()

0 comments on commit 3c24073

Please sign in to comment.