Files
greptimedb/tests/cases/standalone/common/alter/alter_auto_flush_interval.sql
sribatsha dash 8f5d681e4b feat: support ALTER TABLE SET auto_flush_interval (#8403)
* feat: support ALTER TABLE SET auto_flush_interval

Closes #8394.

Add a new SetRegionOption::AutoFlushInterval variant so that the
per-table auto flush interval can be changed on existing tables
via 'ALTER TABLE t SET ...', following up the CREATE TABLE path
from #8357.

- region_request.rs: parse 'auto_flush_interval' with humantime and
  map it to the new variant.
- metadata.rs: persist the value (or remove it, if None) in
  TableOptions.extra_options using the same humantime string format
  the engine already expects.
- handle_alter.rs: apply the new interval in
  handle_alter_region_options_fast (no memtable flush needed, same
  pattern as Ttl) and group the variant with Ttl/Twsc in
  new_region_options_on_empty_memtable.

Tests:
- Two unit tests in metadata.rs covering set and unset-to-None.
- A new sqlness case alter_auto_flush_interval.sql covering
  create-then-alter, alter-then-alter, invalid duration, and
  alter on a table that already had auto_flush_interval at create
  time.

Signed-off-by: srivtx <crypticcc101@gmail.com>

* fix: validate auto_flush_interval > 0 in ALTER SET path

Gemini code assist flagged that the request parser accepted a zero
duration, leaving the rejection to the downstream RegionOptions
validation which only fires on next flush. Reject it at parse time
so users get the error immediately at the ALTER TABLE statement.

Also add a '0s' error case to the sqlness test.

Signed-off-by: srivtx <crypticcc101@gmail.com>

* fix: handle SET 'auto_flush_interval' = NULL and add checked-in .result

Address the rest of fengjiachun's review on #8403:

1. Empty value in ALTER SET clears the override (parallels Ttl).
   'ALTER TABLE t SET ... = NULL' comes through as value = ''; we
   now return AutoFlushInterval(None) so the override is removed
   from TableOptions.extra_options, matching the Ttl pattern.

2. Add a unit test in region_request.rs covering the four cases
   (valid, empty-clears, zero-rejected, garbage-rejected).

3. Generate and check in alter_auto_flush_interval.result via
   'cargo sqlness-runner bare -t alter_auto_flush_interval'. Both
   the standalone and distributed sqlness jobs now pass locally,
   and the test extension covers the NULL-clears path end to end.

Signed-off-by: srivtx <crypticcc101@gmail.com>

---------

Signed-off-by: srivtx <crypticcc101@gmail.com>
2026-07-07 07:34:05 +00:00

84 lines
2.8 KiB
SQL

-- Test altering auto_flush_interval on a mito table
-- Create a table without auto_flush_interval (uses global default)
CREATE TABLE test_alter_auto_flush_interval(
host STRING,
ts TIMESTAMP TIME INDEX,
cpu DOUBLE,
PRIMARY KEY(host)
) ENGINE=mito;
-- Verify the table was created with the default auto_flush_interval
SHOW CREATE TABLE test_alter_auto_flush_interval;
-- Alter auto_flush_interval to 5 minutes
ALTER TABLE test_alter_auto_flush_interval SET 'auto_flush_interval' = '5m';
-- Verify the change took effect
SHOW CREATE TABLE test_alter_auto_flush_interval;
-- Insert some data after the alter
INSERT INTO test_alter_auto_flush_interval VALUES ('host1', 0, 1.0), ('host2', 1, 2.0);
SELECT * FROM test_alter_auto_flush_interval ORDER BY host, ts;
-- Re-altering to the same value should succeed
ALTER TABLE test_alter_auto_flush_interval SET 'auto_flush_interval' = '5m';
-- Re-altering to a different value should succeed
ALTER TABLE test_alter_auto_flush_interval SET 'auto_flush_interval' = '10m';
-- Verify the new value took effect
SHOW CREATE TABLE test_alter_auto_flush_interval;
-- Trying to set an invalid duration should fail
-- SQLNESS REPLACE \d+\(\d+,\s+\d+\) REDACTED
ALTER TABLE test_alter_auto_flush_interval SET 'auto_flush_interval' = 'not_a_duration';
-- Trying to set a zero duration should fail (engine requires > 0)
-- SQLNESS REPLACE \d+\(\d+,\s+\d+\) REDACTED
ALTER TABLE test_alter_auto_flush_interval SET 'auto_flush_interval' = '0s';
-- Clean up
DROP TABLE test_alter_auto_flush_interval;
-- Test clearing the auto_flush_interval override via SET = NULL
CREATE TABLE test_alter_auto_flush_interval_clear(
host STRING,
ts TIMESTAMP TIME INDEX,
cpu DOUBLE,
PRIMARY KEY(host)
) ENGINE=mito WITH ('auto_flush_interval' = '15m');
-- Verify initial value
SHOW CREATE TABLE test_alter_auto_flush_interval_clear;
-- Clear the override by setting to NULL
ALTER TABLE test_alter_auto_flush_interval_clear SET 'auto_flush_interval' = NULL;
-- Verify the override is cleared (the option should no longer appear in SHOW CREATE)
SHOW CREATE TABLE test_alter_auto_flush_interval_clear;
-- Clean up
DROP TABLE test_alter_auto_flush_interval_clear;
-- Test altering auto_flush_interval on a table that already had it set at create time
CREATE TABLE test_alter_auto_flush_interval_with_default(
host STRING,
ts TIMESTAMP TIME INDEX,
cpu DOUBLE,
PRIMARY KEY(host)
) ENGINE=mito WITH ('auto_flush_interval' = '1h');
-- Verify initial value
SHOW CREATE TABLE test_alter_auto_flush_interval_with_default;
-- Alter it
ALTER TABLE test_alter_auto_flush_interval_with_default SET 'auto_flush_interval' = '30m';
-- Verify the new value
SHOW CREATE TABLE test_alter_auto_flush_interval_with_default;
-- Clean up
DROP TABLE test_alter_auto_flush_interval_with_default;