Skip to content

Instantly share code, notes, and snippets.

@filimonov
Last active May 9, 2018 10:09
Show Gist options
  • Select an option

  • Save filimonov/2604a328cddaf3d004b8ef4ddf0617ec to your computer and use it in GitHub Desktop.

Select an option

Save filimonov/2604a328cddaf3d004b8ef4ddf0617ec to your computer and use it in GitHub Desktop.
clickhouse partition key in system.parts : tuple vs simple type
-- adding braces around partition name (even for simple types) allows to do cast safely
:) create table xxx(d Date, a UInt8) Engine = MergeTree() PARTITION by d ORDER BY a;
CREATE TABLE xxx
(
d Date,
a UInt8
)
ENGINE = MergeTree()
PARTITION BY d
ORDER BY a
Ok.
0 rows in set. Elapsed: 0.006 sec.
:) SELECT CAST(concat('(',partition,')') as Tuple(Date)) val, toTypeName(val) val_type FROM system.parts WHERE table = 'yyy' and active = 1;
SELECT
CAST(concat('(', partition, ')') AS Tuple(Date)) AS val,
toTypeName(val) AS val_type
FROM system.parts
WHERE (table = 'xxx') AND (active = 1)
┌─val────────────┬─val_type────┐
│ ('2018-05-08') │ Tuple(Date) │
└────────────────┴─────────────┘
1 rows in set. Elapsed: 0.003 sec.
-- basically clickhouse tolerates well when you give him a tuple with one val inside as partition id
:) insert into xxx values (today(),1),(today()-1,1),(today()-2,1);
INSERT INTO xxx VALUES
Ok.
3 rows in set. Elapsed: 0.003 sec.
:) select count() from xxx;
SELECT count()
FROM xxx
┌─count()─┐
│ 3 │
└─────────┘
1 rows in set. Elapsed: 0.002 sec.
--- alter table xxx drop partition today(); -- works exactly the same
:) alter table xxx drop partition tuple(today());
ALTER TABLE xxx
DROP PARTITION tuple(today())
Ok.
0 rows in set. Elapsed: 0.001 sec.
:) select count() from xxx;
SELECT count()
FROM xxx
┌─count()─┐
│ 2 │
└─────────┘
1 rows in set. Elapsed: 0.002 sec.
--- and it looks like clickhouse uses date when you use tuple(date) as expression.
-- so that behaves exactly the same as prv sample:
-- create table yyy(d Date, a UInt8) Engine = MergeTree() PARTITION by tuple(d) ORDER BY a;
-- but there is some inconsistency here:
:) alter table xxx drop partition CAST(tuple(today()-1) AS Tuple(Date));
ALTER TABLE xxx
DROP PARTITION CAST(tuple(today()) AS Tuple(Date))
Received exception from server (version 1.1.54362):
Code: 53. DB::Exception: Received from localhost:9000, ::1. DB::Exception: Type mismatch in IN or VALUES section. Expected: Date. Got: Tuple.
0 rows in set. Elapsed: 0.036 sec.
-- with 'real' tuples clickhouse already adds braces around partition id in the system tables:
:) create table zzz (a UInt32, b UInt32, c String, d Array(String)) Engine=MergeTree PARTITION BY (a,b,c) ORDER BY (a,b,c)
CREATE TABLE zzz
(
a UInt32,
b UInt32,
c String,
d Array(String)
)
ENGINE = MergeTree
PARTITION BY (a, b, c)
ORDER BY (a, b, c)
Ok.
0 rows in set. Elapsed: 0.007 sec.
:) insert into zzz values (1,1,'1',['1']), (2,2,'22',['2','2']), (3,3,'333',['3','3','3'])
INSERT INTO zzz VALUES
Ok.
3 rows in set. Elapsed: 0.004 sec.
-- it's easy to cast if you know the target type
SELECT CAST(partition AS Tuple(UInt32,UInt32,String)) AS val
FROM system.parts
WHERE (table = 'zzz') AND (active = 1)
┌─val─────────┐
│ (3,3,'333') │
│ (2,2,'22') │
│ (1,1,'1') │
└─────────────┘
3 rows in set. Elapsed: 0.002 sec.
-- but the same inconsistency appears
:) alter table zzz drop partition tuple(2,2,'22');
ALTER TABLE zzz
DROP PARTITION (2, 2, '22')
Ok.
0 rows in set. Elapsed: 0.001 sec.
:) alter table zzz drop partition CAST( tuple(2,2,'22') AS Tuple(UInt32,UInt32,String));
ALTER TABLE zzz
DROP PARTITION CAST((2, 2, '22') AS Tuple(UInt32,UInt32,String))
Received exception from server (version 1.1.54362):
Code: 248. DB::Exception: Received from localhost:9000, ::1. DB::Exception: Wrong number of fields in the partition expression: 1, must be: 3.
0 rows in set. Elapsed: 0.030 sec.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment