Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use named constants to define columns containing IDs and generic values #678

Merged
merged 12 commits into from
Feb 21, 2024
50 changes: 28 additions & 22 deletions mlos_bench/mlos_bench/storage/sql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

_LOG = logging.getLogger(__name__)

# This class is internal to SqlStorage and is mostly a struct
# for all DB tables, so it's ok to disable the warnings.
# pylint: disable=too-many-instance-attributes


class _DDL:
"""
Expand All @@ -46,19 +42,29 @@ class DbSchema:
A class to define and create the DB schema.
"""

# This class is internal to SqlStorage and is mostly a struct
# for all DB tables, so it's ok to disable the warnings.
# pylint: disable=too-many-instance-attributes

# Common string column sizes.
_ID_LEN = 512
_PARAM_VALUE_LEN = 1024
_METRIC_VALUE_LEN = 255
_STATUS_LEN = 16

def __init__(self, engine: Engine):
"""
Declare the SQLAlchemy schema for the database.
"""
_LOG.info("Create the DB schema for: %s", engine)
self._engine = engine
# TODO: bind for automatic schema updates?
# TODO: bind for automatic schema updates? (#649)
self._meta = MetaData()

self.experiment = Table(
"experiment",
self._meta,
Column("exp_id", String(255), nullable=False),
Column("exp_id", String(self._ID_LEN), nullable=False),
Column("description", String(1024)),
Column("root_env_config", String(1024), nullable=False),
Column("git_repo", String(1024), nullable=False),
Expand All @@ -71,7 +77,7 @@ def __init__(self, engine: Engine):
"objectives",
self._meta,
Column("exp_id"),
Column("optimization_target", String(1024), nullable=False),
Column("optimization_target", String(self._ID_LEN), nullable=False),
Column("optimization_direction", String(4), nullable=False),
# TODO: Note: weight is not fully supported yet as currently
# multi-objective is expected to explore each objective equally.
Expand Down Expand Up @@ -103,13 +109,13 @@ def __init__(self, engine: Engine):
self.trial = Table(
"trial",
self._meta,
Column("exp_id", String(255), nullable=False),
Column("exp_id", String(self._ID_LEN), nullable=False),
Column("trial_id", Integer, nullable=False),
Column("config_id", Integer, nullable=False),
Column("ts_start", DateTime, nullable=False),
Column("ts_end", DateTime),
# Should match the text IDs of `mlos_bench.environments.Status` enum:
Column("status", String(16), nullable=False),
Column("status", String(self._STATUS_LEN), nullable=False),

PrimaryKeyConstraint("exp_id", "trial_id"),
ForeignKeyConstraint(["exp_id"], [self.experiment.c.exp_id]),
Expand All @@ -122,8 +128,8 @@ def __init__(self, engine: Engine):
"config_param",
self._meta,
Column("config_id", Integer, nullable=False),
Column("param_id", String(255), nullable=False),
Column("param_value", String(255)),
Column("param_id", String(self._ID_LEN), nullable=False),
Column("param_value", String(self._PARAM_VALUE_LEN)),

PrimaryKeyConstraint("config_id", "param_id"),
ForeignKeyConstraint(["config_id"], [self.config.c.config_id]),
Expand All @@ -134,10 +140,10 @@ def __init__(self, engine: Engine):
self.trial_param = Table(
"trial_param",
self._meta,
Column("exp_id", String(255), nullable=False),
Column("exp_id", String(self._ID_LEN), nullable=False),
Column("trial_id", Integer, nullable=False),
Column("param_id", String(255), nullable=False),
Column("param_value", String(255)),
Column("param_id", String(self._ID_LEN), nullable=False),
Column("param_value", String(self._PARAM_VALUE_LEN)),

PrimaryKeyConstraint("exp_id", "trial_id", "param_id"),
ForeignKeyConstraint(["exp_id", "trial_id"],
Expand All @@ -147,10 +153,10 @@ def __init__(self, engine: Engine):
self.trial_status = Table(
"trial_status",
self._meta,
Column("exp_id", String(255), nullable=False),
Column("exp_id", String(self._ID_LEN), nullable=False),
Column("trial_id", Integer, nullable=False),
Column("ts", DateTime, nullable=False, default="now"),
Column("status", String(16), nullable=False),
Column("status", String(self._STATUS_LEN), nullable=False),

UniqueConstraint("exp_id", "trial_id", "ts"),
ForeignKeyConstraint(["exp_id", "trial_id"],
Expand All @@ -160,10 +166,10 @@ def __init__(self, engine: Engine):
self.trial_result = Table(
"trial_result",
self._meta,
Column("exp_id", String(255), nullable=False),
Column("exp_id", String(self._ID_LEN), nullable=False),
Column("trial_id", Integer, nullable=False),
Column("metric_id", String(255), nullable=False),
Column("metric_value", String(255)),
Column("metric_id", String(self._ID_LEN), nullable=False),
Column("metric_value", String(self._METRIC_VALUE_LEN)),

PrimaryKeyConstraint("exp_id", "trial_id", "metric_id"),
ForeignKeyConstraint(["exp_id", "trial_id"],
Expand All @@ -173,11 +179,11 @@ def __init__(self, engine: Engine):
self.trial_telemetry = Table(
"trial_telemetry",
self._meta,
Column("exp_id", String(255), nullable=False),
Column("exp_id", String(self._ID_LEN), nullable=False),
Column("trial_id", Integer, nullable=False),
Column("ts", DateTime, nullable=False, default="now"),
Column("metric_id", String(255), nullable=False),
Column("metric_value", String(255)),
Column("metric_id", String(self._ID_LEN), nullable=False),
Column("metric_value", String(self._METRIC_VALUE_LEN)),

UniqueConstraint("exp_id", "trial_id", "ts", "metric_id"),
ForeignKeyConstraint(["exp_id", "trial_id"],
Expand Down
Loading