@@ -11,6 +11,113 @@ using namespace NYdb::NTable;
11
11
12
12
Y_UNIT_TEST_SUITE (KqpReturning) {
13
13
14
+ Y_UNIT_TEST (ReturningTwice) {
15
+ NKikimrConfig::TAppConfig appConfig;
16
+ appConfig.MutableTableServiceConfig ()->SetEnableSequences (true );
17
+ appConfig.MutableTableServiceConfig ()->SetEnableColumnsWithDefault (true );
18
+ auto serverSettings = TKikimrSettings ().SetAppConfig (appConfig);
19
+ TKikimrRunner kikimr (serverSettings);
20
+
21
+ auto client = kikimr.GetTableClient ();
22
+ auto session = client.CreateSession ().GetValueSync ().GetSession ();
23
+
24
+ const auto queryCreate = Q_ (R"(
25
+ CREATE TABLE IF NOT EXISTS tasks (
26
+ hashed_key Uint32,
27
+ queue_name String,
28
+ task_id String,
29
+ worker_id Int32,
30
+ running Bool,
31
+ eta Timestamp,
32
+ lock_timeout Timestamp,
33
+ num_fails Int32,
34
+ num_reschedules Int32,
35
+ body String,
36
+ first_fail Timestamp,
37
+ idempotency_run_id String,
38
+ PRIMARY KEY (hashed_key, queue_name, task_id)
39
+ );
40
+
41
+ CREATE TABLE IF NOT EXISTS tasks_eta_002 (
42
+ eta Timestamp,
43
+ hashed_key Uint32,
44
+ queue_name String,
45
+ task_id String,
46
+ PRIMARY KEY (eta, hashed_key, queue_name, task_id)
47
+ ) WITH (
48
+ AUTO_PARTITIONING_MIN_PARTITIONS_COUNT = 1,
49
+ AUTO_PARTITIONING_MAX_PARTITIONS_COUNT = 1
50
+ );
51
+
52
+ CREATE TABLE IF NOT EXISTS tasks_processing_002 (
53
+ expiration_ts Timestamp,
54
+ hashed_key Uint32,
55
+ queue_name String,
56
+ task_id String,
57
+ PRIMARY KEY (expiration_ts, hashed_key, queue_name, task_id)
58
+ ) WITH (
59
+ AUTO_PARTITIONING_MIN_PARTITIONS_COUNT = 1,
60
+ AUTO_PARTITIONING_MAX_PARTITIONS_COUNT = 1
61
+ );
62
+ )" );
63
+
64
+ auto resultCreate = session.ExecuteSchemeQuery (queryCreate).GetValueSync ();
65
+ UNIT_ASSERT_C (resultCreate.IsSuccess (), resultCreate.GetIssues ().ToString ());
66
+
67
+ {
68
+ const auto query = Q_ (R"(
69
+ --!syntax_v1
70
+ DECLARE $eta AS Timestamp;
71
+ DECLARE $expiration_ts AS Timestamp;
72
+ DECLARE $limit AS Int32;
73
+
74
+ $to_move = (
75
+ SELECT $expiration_ts AS expiration_ts, eta, hashed_key, queue_name, task_id
76
+ FROM tasks_eta_002
77
+ WHERE eta <= $eta
78
+ ORDER BY eta, hashed_key, queue_name, task_id
79
+ LIMIT $limit
80
+ );
81
+
82
+ UPSERT INTO tasks_processing_002 (expiration_ts, hashed_key, queue_name, task_id)
83
+ SELECT expiration_ts, hashed_key, queue_name, task_id FROM $to_move
84
+ RETURNING expiration_ts, hashed_key, queue_name, task_id;
85
+
86
+ UPSERT INTO tasks (hashed_key, queue_name, task_id, running, lock_timeout)
87
+ SELECT hashed_key, queue_name, task_id, True as running, $expiration_ts AS lock_timeout FROM $to_move;
88
+
89
+ DELETE FROM tasks_eta_002 ON
90
+ SELECT eta, hashed_key, queue_name, task_id FROM $to_move;
91
+ )" );
92
+
93
+ auto params = TParamsBuilder ()
94
+ .AddParam (" $eta" ).Timestamp (TInstant::Zero ()).Build ()
95
+ .AddParam (" $expiration_ts" ).Timestamp (TInstant::Zero ()).Build ()
96
+ .AddParam (" $limit" ).Int32 (1 ).Build ()
97
+ .Build ();
98
+
99
+ NYdb::NTable::TExecDataQuerySettings execSettings;
100
+ execSettings.CollectQueryStats (ECollectQueryStatsMode::Full);
101
+
102
+ auto result = session.ExecuteDataQuery (query, TTxControl::BeginTx ().CommitTx (), params, execSettings).GetValueSync ();
103
+ UNIT_ASSERT (result.IsSuccess ());
104
+
105
+ size_t eta_table_access = 0 ;
106
+ auto stats = NYdb::TProtoAccessor::GetProto (*result.GetStats ());
107
+
108
+ for (auto phase : stats.query_phases ()) {
109
+ for (auto table : phase.table_access ()) {
110
+ if (table.name () == " /Root/tasks_eta_002" ) {
111
+ eta_table_access++;
112
+ }
113
+ }
114
+ }
115
+ Cerr << " access count " << eta_table_access << Endl;
116
+ UNIT_ASSERT_EQUAL (eta_table_access, 1 );
117
+ // Cerr << stats.Utf8DebugString() << Endl;
118
+ }
119
+ }
120
+
14
121
Y_UNIT_TEST (ReturningSerial) {
15
122
NKikimrConfig::TAppConfig appConfig;
16
123
appConfig.MutableTableServiceConfig ()->SetEnableSequences (true );
0 commit comments