# LogicTest: !metamorphic-batch-sizes

# Regression test for UPSERT batching logic not respecting footprint-based
# limiting (#102472).
statement ok
CREATE TABLE src (s STRING);

statement ok
CREATE TABLE dest (s STRING);

statement ok
INSERT INTO src SELECT repeat('a', 100000) FROM generate_series(1, 60)

user host-cluster-root

# Set the memory limit a little higher than the minimum because the batch sizing
# is inexact, tracked in #117070.
statement ok
SET CLUSTER SETTING kv.raft.command.max_size='5MiB';

user root

statement ok
SET CLUSTER SETTING sql.mutations.mutation_batch_byte_size='1MiB';

# This statement produces a raft command of about 6 MB in size, so if the
# batching logic is incorrect, we'll encounter "command is too large" error.
statement ok
UPSERT INTO dest (s) (SELECT s FROM src)

statement ok
RESET CLUSTER SETTING sql.mutations.mutation_batch_byte_size;

user host-cluster-root

statement ok
RESET CLUSTER SETTING kv.raft.command.max_size;

user root

statement ok
DROP TABLE src;

statement ok
DROP TABLE dest

# Regression test for UPSERT batching logic (#51391).
user host-cluster-root

statement ok
SET CLUSTER SETTING kv.raft.command.max_size='4MiB';

user root

statement ok
CREATE TABLE src (s STRING);

statement ok
CREATE TABLE dest (s STRING);

statement ok
INSERT INTO src
SELECT
	'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
FROM
	generate_series(1, 50000)

# This statement produces a raft command of about 6.6 MiB in size, so if the
# batching logic is incorrect, we'll encounter "command is too large" error.
statement ok
UPSERT INTO dest (s) (SELECT s FROM src)

user host-cluster-root

statement ok
RESET CLUSTER SETTING kv.raft.command.max_size;

user root

statement ok
DROP TABLE src;

statement ok
DROP TABLE dest

# Regression test for finishing UPSERT too early (#54456).
statement ok
CREATE TABLE t54456 (c INT PRIMARY KEY);

statement ok
UPSERT INTO t54456 SELECT i FROM generate_series(1, 25000) AS i

query I
SELECT count(*) FROM t54456
----
25000

# Regression test for clearing up upserted rows too early (#54465).
query I
WITH cte(c) AS (UPSERT INTO t54456 SELECT i FROM generate_series(25001, 40000) AS i RETURNING c) SELECT count(*) FROM cte
----
15000
