Back to changelog
New
2 minute read

MySQL: Partitioned Tables in HCL

Atlas now supports MySQL table partitioning in HCL, including the partitioning types described in the MySQL manual: RANGE, LIST, HASH, KEY, plus the RANGE COLUMNS, LIST COLUMNS, LINEAR HASH, and LINEAR KEY variants.

MySQL supports four main partitioning families: RANGE, LIST, HASH, and KEY. The manual also documents extensions and variants for these families, including RANGE COLUMNS, LIST COLUMNS, LINEAR HASH, and LINEAR KEY. Atlas can now represent those MySQL partitioning schemes directly in HCL.

Defining Partitions in HCL

In Atlas HCL, MySQL partition definitions stay inside the table-level partition block. For partition types that define explicit boundaries, such as RANGE and LIST, the partitions are modeled as nested named blocks:

table "range_orders" {
schema = schema.public
column "id" {
null = false
type = int
}
partition {
type = RANGE
by {
expr = "(`id` + 1)"
}
partition "p0" {
values_less_than = ["101"]
comment = "first range"
}
partition "pmax" {
values_less_than = ["MAXVALUE"]
comment = "catch all range"
}
}
}

MySQL Partitioning Types

The MySQL manual describes the following partitioning types, all of which are supported here:

  • RANGE and RANGE COLUMNS
  • LIST and LIST COLUMNS
  • HASH and LINEAR HASH
  • KEY and LINEAR KEY

Column-Based and Expression-Based Partitioning

MySQL uses expressions for RANGE, LIST, HASH, and LINEAR HASH partitioning, while KEY, LINEAR KEY, RANGE COLUMNS, and LIST COLUMNS rely on one or more column values. In Atlas HCL, that becomes by { expr = "..." } for expression-based partitioning and columns = [...] for column-based partitioning. For example, KEY partitioning uses column references rather than expressions:

table "key_logs" {
schema = schema.public
column "id" {
null = false
type = int
}
column "tenant_id" {
null = false
type = int
}
partition {
type = KEY
columns = [column.id, column.tenant_id]
partition "p0" {
comment = "key 0"
}
partition "p1" {
comment = "key 1"
}
}
}

Nested partitions may define values_less_than, values_in, and comment where those options apply. Atlas uses HCL enum values such as RANGE_COLUMNS and LINEAR_HASH to represent the MySQL partitioning types named in the manual as RANGE COLUMNS and LINEAR HASH.

To learn more, see the partitions documentation and the MySQL schema reference.

featuremysqlpartitioninghclschema