Skip to content

Commit acf497e

Browse files
committed
Fixes
1 parent 17b2f0f commit acf497e

File tree

2 files changed

+72
-56
lines changed

2 files changed

+72
-56
lines changed

ydb/library/yql/sql/pg/pg_sql.cpp

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "util/charset/utf8.h"
22
#include "utils.h"
33
#include "ydb/public/api/protos/ydb_value.pb.h"
4+
#include <memory>
45
#include <ydb/library/yql/sql/settings/partitioning.h>
56
#include <ydb/library/yql/parser/pg_wrapper/interface/config.h>
67
#include <ydb/library/yql/parser/pg_wrapper/interface/parser.h>
78
#include <ydb/library/yql/parser/pg_wrapper/interface/utils.h>
89
#include <ydb/library/yql/parser/pg_wrapper/interface/raw_parser.h>
910
#include <ydb/library/yql/parser/pg_wrapper/postgresql/src/backend/catalog/pg_type_d.h>
11+
//#include <ydb/library/yql/parser/pg_wrapper/postgresql/src/include/commands/defrem.h>
1012
#include <ydb/library/yql/parser/pg_catalog/catalog.h>
1113
#include <ydb/library/yql/providers/common/provider/yql_provider_names.h>
1214
#include <ydb/library/yql/core/issue/yql_issue.h>
@@ -2657,82 +2659,79 @@ class TConverter : public IPGParseEvents {
26572659

26582660
std::vector<TAstNode*> options;
26592661

2660-
if (value->accessMethod) {
2661-
AddError("USING not supported");
2662-
return nullptr;
2663-
}
2664-
2665-
if (value->if_not_exists) {
2666-
TString mode = (ctx.ifNotExists) ? "create_if_not_exists" : "create";
2667-
options.push_back(QL(QA("mode"), QA(mode)));
2668-
}
2662+
TString mode = (value->if_not_exists) ? "create_if_not_exists" : "create";
2663+
options.push_back(QL(QA("mode"), QA(mode)));
26692664

2670-
auto [sink, key] = ParseWriteRangeVar(value->sequence, true);
2665+
auto [sink, key] = ParseQualifiedPgObjectName(
2666+
value->sequence->catalogname,
2667+
value->sequence->schemaname,
2668+
value->sequence->relname,
2669+
"pgSequence"
2670+
);
26712671

26722672
if (!sink || !key) {
26732673
return nullptr;
26742674
}
26752675

2676+
const auto relPersistence = static_cast<NPg::ERelPersistence>(value->sequence->relpersistence);
2677+
switch (relPersistence) {
2678+
case NPg::ERelPersistence::Temp:
2679+
options.push_back(QL(QA("temporary")));
2680+
break;
2681+
case NPg::ERelPersistence::Unlogged:
2682+
AddError("UNLOGGED sequence not supported");
2683+
return nullptr;
2684+
break;
2685+
case NPg::ERelPersistence::Permanent:
2686+
break;
2687+
}
2688+
26762689
for (int i = 0; i < ListLength(value->options); ++i) {
26772690
auto rawNode = ListNodeNth(value->options, i);
26782691

26792692
switch (NodeTag(rawNode)) {
2680-
case T_DefElem:
2681-
auto defElem = CAST_NODE(ColumnDef, rawNode);
2682-
return nullptr;
2693+
case T_DefElem: {
2694+
const auto* defElem = CAST_NODE(DefElem, rawNode);
2695+
TStringBuf nameElem = defElem->defname;
2696+
if (defElem->arg) {
2697+
switch (NodeTag(defElem->arg))
2698+
{
2699+
case T_Integer:
2700+
options.emplace_back(QL(QA(nameElem), QA(ToString(intVal(defElem->arg)))));
2701+
break;
2702+
case T_Float:
2703+
options.emplace_back(QL(QA(nameElem), QA(strVal(defElem->arg))));
2704+
break;
2705+
case T_TypeName: {
2706+
const auto* typeName = reinterpret_cast<PG_TypeName*>(defElem->arg);
2707+
options.emplace_back(QL(QA(nameElem),
2708+
QA(StrVal(ListNodeNth(typeName->names, ListLength(typeName->names) - 1)))));
2709+
break;
2710+
}
2711+
default:
2712+
AddError("storage parameters for index is not supported yet:" + TString(nameElem));
2713+
NodeNotImplemented(defElem->arg);
2714+
return nullptr;
2715+
}
26832716
}
26842717
break;
2685-
2718+
}
26862719
default:
2687-
NodeNotImplemented(value, rawNode);
2720+
NodeNotImplemented(rawNode);
26882721
return nullptr;
26892722
}
26902723
}
26912724

2692-
TString mode = (ctx.ifNotExists) ? "create_if_not_exists" : "create";
2693-
options.push_back(QL(QA("mode"), QA(mode)));
2694-
options.push_back(QL(QA("columns"), BuildColumnsOptions(ctx)));
2695-
if (!ctx.PrimaryKey.empty()) {
2696-
options.push_back(QL(QA("primarykey"), QVL(ctx.PrimaryKey.data(), ctx.PrimaryKey.size())));
2697-
}
2698-
for (auto& uniq : ctx.UniqConstr) {
2699-
auto columns = QVL(uniq.data(), uniq.size());
2700-
options.push_back(QL(QA("index"), QL(
2701-
QL(QA("indexName")),
2702-
QL(QA("indexType"), QA("syncGlobalUnique")),
2703-
QL(QA("dataColumns"), QL()),
2704-
QL(QA("indexColumns"), columns))));
2705-
}
2706-
if (ctx.isTemporary) {
2707-
options.push_back(QL(QA("temporary")));
2725+
if (value->for_identity) {
2726+
options.push_back(QL(QA("for_identity")));
27082727
}
27092728

2710-
for (int i = 0; i < ListLength(value->tableElts); ++i) {
2711-
auto rawNode = ListNodeNth(value->tableElts, i);
2712-
2713-
switch (NodeTag(rawNode)) {
2714-
case T_ColumnDef:
2715-
if (!AddColumn(ctx, CAST_NODE(ColumnDef, rawNode))) {
2716-
return nullptr;
2717-
}
2718-
break;
2719-
2720-
case T_Constraint:
2721-
if (!AddConstraint(ctx, CAST_NODE(Constraint, rawNode))) {
2722-
return nullptr;
2723-
}
2724-
break;
2725-
2726-
default:
2727-
NodeNotImplemented(value, rawNode);
2728-
return nullptr;
2729-
}
2730-
}
2729+
options.push_back(QL(QA("owner_id"), QA(ToString(value->ownerId))));
27312730

27322731
State.Statements.push_back(
27332732
L(A("let"), A("world"),
27342733
L(A("Write!"), A("world"), sink, key, L(A("Void")),
2735-
BuildCreateTableOptions(ctx))));
2734+
QVL(options.data(), options.size()))));
27362735

27372736
return State.Statements.back();
27382737
}

ydb/library/yql/sql/pg/pg_sql_ut.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingOnly) {
166166
)";
167167
const auto expectedAst = NYql::ParseAst(program);
168168
UNIT_ASSERT_STRINGS_EQUAL(res.Root->ToString(), expectedAst.Root->ToString());
169-
}
169+
}
170170

171171
Y_UNIT_TEST(CreateTableStmt_PKAndNotNull) {
172172
auto res = PgSqlToYql("CREATE TABLE t (a int PRIMARY KEY NOT NULL, b text)");
@@ -265,6 +265,23 @@ Y_UNIT_TEST_SUITE(PgSqlParsingOnly) {
265265
UNIT_ASSERT_STRINGS_EQUAL(res.Root->ToString(), expectedAst.Root->ToString());
266266
}
267267

268+
Y_UNIT_TEST(CreateSeqStmt) {
269+
auto res = PgSqlToYql(
270+
"CREATE TEMP SEQUENCE IF NOT EXISTS seq AS integer START WITH 10 INCREMENT BY 2 NO MINVALUE NO MAXVALUE CACHE 3;");
271+
UNIT_ASSERT_C(res.Root, res.Issues.ToString());
272+
273+
TString program = R"(
274+
(
275+
(let world (Configure! world (DataSource 'config) 'OrderedColumns))
276+
(let world (Write! world (DataSink '"kikimr" '"") (Key '('pgObject (String 'seq) (String 'pgSequence))) (Void) '('('mode 'create_if_not_exists) '('temporary) '('as 'int4) '('start '10) '('increment '2) '('cache '3) '('owner_id '0))))
277+
(let world (CommitAll! world))
278+
(return world)
279+
)
280+
)";
281+
const auto expectedAst = NYql::ParseAst(program);
282+
UNIT_ASSERT_STRINGS_EQUAL(res.Root->ToString(), expectedAst.Root->ToString());
283+
}
284+
268285
Y_UNIT_TEST(VariableShowStmt) {
269286
auto res = PgSqlToYql("Show server_version_num");
270287
UNIT_ASSERT(res.Root);
@@ -482,7 +499,7 @@ SELECT COUNT(*) FROM public.t;");
482499
settings);
483500
UNIT_ASSERT_C(res.IsOk(), res.Issues.ToString());
484501
UNIT_ASSERT(res.Root);
485-
502+
486503
res = SqlToYqlWithMode(
487504
R"(select oid,
488505
typinput::int4 as typinput,
@@ -514,7 +531,7 @@ from pg_catalog.pg_type)",
514531
settings);
515532
UNIT_ASSERT(res.IsOk());
516533
UNIT_ASSERT(res.Root);
517-
534+
518535
res = SqlToYqlWithMode(
519536
R"(select set_config('search_path', 'public', false);)",
520537
NSQLTranslation::ESqlMode::QUERY,

0 commit comments

Comments
 (0)