Skip to main content

mz_adapter/catalog/open/
builtin_schema_migration.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Support for migrating the schemas of builtin storage collections.
11//!
12//! If a version upgrade changes the schema of a builtin collection that's made durable in persist,
13//! that persist shard's schema must be migrated accordingly. The migration must happen in a way
14//! that's compatible with 0dt upgrades: Read-only environments need to be able to read the
15//! collections with the new schema, without interfering with the leader environment's continued
16//! use of the old schema.
17//!
18//! Two migration mechanisms are provided:
19//!
20//!  * [`Mechanism::Evolution`] uses persist's schema evolution support to evolve the persist
21//!    shard's schema in-place. Only works for backward-compatible changes.
22//!  * [`Mechanism::Replacement`] creates a new shard to serve the builtin collection in the new
23//!    version. Works for all schema changes but discards existing data.
24//!
25//! Which mechanism to use is selected through entries in the `MIGRATIONS` list. In general, the
26//! `Evolution` mechanism should be used when possible, as it avoids data loss.
27//!
28//! For more context and details on the implementation, see
29//! `doc/developer/design/20251015_builtin_schema_migration.md`.
30
31use std::collections::{BTreeMap, BTreeSet};
32use std::sync::{Arc, LazyLock};
33
34use anyhow::bail;
35use futures::FutureExt;
36use futures::future::BoxFuture;
37use mz_build_info::{BuildInfo, DUMMY_BUILD_INFO};
38use mz_catalog::builtin::{
39    BUILTIN_LOOKUP, Builtin, Fingerprint, MZ_CATALOG_RAW, MZ_CATALOG_RAW_DESCRIPTION,
40    MZ_OBJECT_ARRANGEMENT_SIZE_HISTORY_DESCRIPTION, MZ_STORAGE_USAGE_BY_SHARD,
41    MZ_STORAGE_USAGE_BY_SHARD_DESCRIPTION, RUNTIME_ALTERABLE_FINGERPRINT_SENTINEL,
42};
43use mz_catalog::config::BuiltinItemMigrationConfig;
44use mz_catalog::durable::objects::SystemObjectUniqueIdentifier;
45use mz_catalog::durable::{SystemObjectDescription, SystemObjectMapping, Transaction};
46use mz_catalog::memory::error::{Error, ErrorKind};
47use mz_ore::soft_assert_or_log;
48use mz_persist_client::cfg::USE_CRITICAL_SINCE_CATALOG;
49use mz_persist_client::critical::{Opaque, SinceHandle};
50use mz_persist_client::read::ReadHandle;
51use mz_persist_client::schema::CaESchema;
52use mz_persist_client::write::WriteHandle;
53use mz_persist_client::{Diagnostics, PersistClient};
54use mz_persist_types::ShardId;
55use mz_persist_types::codec_impls::{ShardIdSchema, UnitSchema};
56use mz_persist_types::schema::backward_compatible;
57use mz_repr::namespaces::{MZ_CATALOG_SCHEMA, MZ_INTERNAL_SCHEMA};
58use mz_repr::{CatalogItemId, GlobalId, Timestamp};
59use mz_sql::catalog::{CatalogItemType, NameReference};
60use mz_storage_client::controller::StorageTxn;
61use mz_storage_types::StorageDiff;
62use mz_storage_types::sources::SourceData;
63use semver::Version;
64use timely::progress::Antichain;
65use tracing::{debug, info};
66
67use crate::catalog::migrate::get_migration_version;
68
69/// Builtin schema migrations required to upgrade to the current build version.
70///
71/// Migration steps for old versions must be retained around according to the upgrade policy. For
72/// example, if we support upgrading one major version at a time, the release of version `N.0.0`
73/// can delete all migration steps with versions before `(N-1).0.0`.
74///
75/// Exception: when a builtin's `SystemObjectDescription` changes — e.g. a builtin table is
76/// converted to a materialized view (see `migrate_builtin_tables_to_mvs`), or a builtin is
77/// renamed or removed — existing steps naming the old description must be removed regardless
78/// of version, because `validate_migration_steps` panics on steps that don't resolve to a
79/// current builtin. This is safe only if a `Replacement` step for the new description is added
80/// at the conversion version: every environment that needed the removed steps upgrades from an
81/// even older version, so the new replacement subsumes them.
82///
83/// Smallest supported version: 0.147.0
84static MIGRATIONS: LazyLock<Vec<MigrationStep>> = LazyLock::new(|| {
85    vec![
86        MigrationStep::replacement(
87            "0.149.0",
88            CatalogItemType::Source,
89            MZ_INTERNAL_SCHEMA,
90            "mz_sink_statistics_raw",
91        ),
92        MigrationStep::replacement(
93            "0.149.0",
94            CatalogItemType::Source,
95            MZ_INTERNAL_SCHEMA,
96            "mz_source_statistics_raw",
97        ),
98        MigrationStep::evolution(
99            "0.159.0",
100            CatalogItemType::Source,
101            MZ_INTERNAL_SCHEMA,
102            "mz_cluster_replica_metrics_history",
103        ),
104        MigrationStep::replacement(
105            "0.160.0",
106            CatalogItemType::Table,
107            MZ_CATALOG_SCHEMA,
108            "mz_sinks",
109        ),
110        MigrationStep::replacement(
111            "26.18.0-dev.0",
112            CatalogItemType::MaterializedView,
113            MZ_CATALOG_SCHEMA,
114            "mz_databases",
115        ),
116        MigrationStep::replacement(
117            "26.19.0-dev.0",
118            CatalogItemType::MaterializedView,
119            MZ_CATALOG_SCHEMA,
120            "mz_schemas",
121        ),
122        MigrationStep::replacement(
123            "26.19.0-dev.0",
124            CatalogItemType::MaterializedView,
125            MZ_CATALOG_SCHEMA,
126            "mz_role_members",
127        ),
128        MigrationStep::replacement(
129            "26.19.0-dev.0",
130            CatalogItemType::MaterializedView,
131            MZ_INTERNAL_SCHEMA,
132            "mz_network_policies",
133        ),
134        MigrationStep::replacement(
135            "26.19.0-dev.0",
136            CatalogItemType::MaterializedView,
137            MZ_INTERNAL_SCHEMA,
138            "mz_network_policy_rules",
139        ),
140        MigrationStep::replacement(
141            "26.19.0-dev.0",
142            CatalogItemType::MaterializedView,
143            MZ_INTERNAL_SCHEMA,
144            "mz_cluster_workload_classes",
145        ),
146        MigrationStep::replacement(
147            "26.19.0-dev.0",
148            CatalogItemType::MaterializedView,
149            MZ_INTERNAL_SCHEMA,
150            "mz_internal_cluster_replicas",
151        ),
152        MigrationStep::replacement(
153            "26.19.0-dev.0",
154            CatalogItemType::MaterializedView,
155            MZ_INTERNAL_SCHEMA,
156            "mz_pending_cluster_replicas",
157        ),
158        MigrationStep::replacement(
159            "26.20.0-dev.0",
160            CatalogItemType::MaterializedView,
161            MZ_CATALOG_SCHEMA,
162            "mz_materialized_views",
163        ),
164        MigrationStep::replacement(
165            "26.22.0-dev.0",
166            CatalogItemType::MaterializedView,
167            MZ_CATALOG_SCHEMA,
168            "mz_connections",
169        ),
170        MigrationStep::replacement(
171            "26.22.0-dev.0",
172            CatalogItemType::MaterializedView,
173            MZ_CATALOG_SCHEMA,
174            "mz_secrets",
175        ),
176        MigrationStep::replacement(
177            "26.27.0-dev.0",
178            CatalogItemType::MaterializedView,
179            MZ_CATALOG_SCHEMA,
180            "mz_sources",
181        ),
182        MigrationStep::replacement(
183            "26.29.0-dev.0",
184            CatalogItemType::MaterializedView,
185            MZ_CATALOG_SCHEMA,
186            "mz_indexes",
187        ),
188        MigrationStep::replacement(
189            "26.29.0-dev.0",
190            CatalogItemType::MaterializedView,
191            MZ_CATALOG_SCHEMA,
192            "mz_roles",
193        ),
194        MigrationStep::replacement(
195            "26.29.0-dev.0",
196            CatalogItemType::MaterializedView,
197            MZ_CATALOG_SCHEMA,
198            "mz_role_parameters",
199        ),
200        // Required because we added `mz_cluster_replica_size_internal_ind` builtin
201        // index without bumping mz_indexes. make_mz_indexes inlines the builtin-index
202        // set as VALUES, so any add/remove changes its SQL fingerprint and requires
203        // an explicit replacement step.
204        MigrationStep::replacement(
205            "26.30.0-dev.0",
206            CatalogItemType::MaterializedView,
207            MZ_CATALOG_SCHEMA,
208            "mz_indexes",
209        ),
210        MigrationStep::replacement(
211            "26.30.0-dev.0",
212            CatalogItemType::MaterializedView,
213            MZ_CATALOG_SCHEMA,
214            "mz_clusters",
215        ),
216        MigrationStep::replacement(
217            "26.30.0-dev.0",
218            CatalogItemType::MaterializedView,
219            MZ_CATALOG_SCHEMA,
220            "mz_cluster_replicas",
221        ),
222        MigrationStep::replacement(
223            "26.30.0-dev.0",
224            CatalogItemType::MaterializedView,
225            MZ_INTERNAL_SCHEMA,
226            "mz_cluster_schedules",
227        ),
228        MigrationStep::replacement(
229            "26.30.0-dev.0",
230            CatalogItemType::MaterializedView,
231            MZ_CATALOG_SCHEMA,
232            "mz_default_privileges",
233        ),
234        MigrationStep::replacement(
235            "26.30.0-dev.0",
236            CatalogItemType::MaterializedView,
237            MZ_CATALOG_SCHEMA,
238            "mz_system_privileges",
239        ),
240        // The mz_cluster_replicas MV definition changed in 26.31.0-dev (the
241        // `availability_zone` column now aggregates the durable
242        // `availability_zones` list).
243        MigrationStep::replacement(
244            "26.31.0-dev.0",
245            CatalogItemType::MaterializedView,
246            MZ_CATALOG_SCHEMA,
247            "mz_cluster_replicas",
248        ),
249        MigrationStep::replacement(
250            "26.32.0-dev.0",
251            CatalogItemType::MaterializedView,
252            MZ_INTERNAL_SCHEMA,
253            "mz_comments",
254        ),
255        // Required because we added the console cluster-utilization overview builtin
256        // indexes (overview/_3h/_24h). make_mz_indexes inlines the builtin-index set
257        // as VALUES, so any add/remove changes its SQL fingerprint and requires an
258        // explicit replacement step.
259        MigrationStep::replacement(
260            "26.32.0-dev.0",
261            CatalogItemType::MaterializedView,
262            MZ_CATALOG_SCHEMA,
263            "mz_indexes",
264        ),
265        MigrationStep::replacement(
266            "26.33.0-dev.0",
267            CatalogItemType::MaterializedView,
268            MZ_CATALOG_SCHEMA,
269            "mz_audit_events",
270        ),
271        // Required because we added the `mz_cluster_reconfigurations_ind` and
272        // `mz_cluster_auto_scaling_strategies_ind` builtin indexes without
273        // bumping mz_indexes. make_mz_indexes inlines the builtin-index set as
274        // VALUES, so any add/remove changes its SQL fingerprint and requires an
275        // explicit replacement step.
276        //
277        // NOTE: this version must stay at the workspace's current dev version
278        // until this change ships in a release. A dev version orders below its
279        // release, so a step pinned to an older dev version is skipped when
280        // upgrading from that release onward, and the fingerprint check then
281        // panics at catalog open.
282        MigrationStep::replacement(
283            "26.34.0-dev.0",
284            CatalogItemType::MaterializedView,
285            MZ_CATALOG_SCHEMA,
286            "mz_indexes",
287        ),
288        // Converting mz_postgres_sources / mz_kafka_sources from builtin tables
289        // to materialized views changes their catalog fingerprint, so both need
290        // an explicit replacement step. See the NOTE above: this version must
291        // stay at the workspace's current dev version until the change ships.
292        MigrationStep::replacement(
293            "26.34.0-dev.0",
294            CatalogItemType::MaterializedView,
295            MZ_INTERNAL_SCHEMA,
296            "mz_postgres_sources",
297        ),
298        MigrationStep::replacement(
299            "26.34.0-dev.0",
300            CatalogItemType::MaterializedView,
301            MZ_CATALOG_SCHEMA,
302            "mz_kafka_sources",
303        ),
304        // Converting the four mz_*_source_tables from builtin tables to
305        // materialized views changes their catalog fingerprint, so each needs an
306        // explicit replacement step.
307        MigrationStep::replacement(
308            "26.37.0-dev.0",
309            CatalogItemType::MaterializedView,
310            MZ_INTERNAL_SCHEMA,
311            "mz_postgres_source_tables",
312        ),
313        MigrationStep::replacement(
314            "26.37.0-dev.0",
315            CatalogItemType::MaterializedView,
316            MZ_INTERNAL_SCHEMA,
317            "mz_mysql_source_tables",
318        ),
319        MigrationStep::replacement(
320            "26.37.0-dev.0",
321            CatalogItemType::MaterializedView,
322            MZ_INTERNAL_SCHEMA,
323            "mz_sql_server_source_tables",
324        ),
325        MigrationStep::replacement(
326            "26.37.0-dev.0",
327            CatalogItemType::MaterializedView,
328            MZ_INTERNAL_SCHEMA,
329            "mz_kafka_source_tables",
330        ),
331    ]
332});
333
334/// A migration required to upgrade past a specific version.
335#[derive(Clone, Debug)]
336struct MigrationStep {
337    /// The build version that requires this migration.
338    version: Version,
339    /// The object that requires migration.
340    object: SystemObjectDescription,
341    /// The migration mechanism to be used.
342    mechanism: Mechanism,
343}
344
345impl MigrationStep {
346    /// Helper to construct an `Evolution` migration step.
347    fn evolution(version: &str, type_: CatalogItemType, schema: &str, name: &str) -> Self {
348        Self {
349            version: Version::parse(version).expect("valid"),
350            object: SystemObjectDescription {
351                schema_name: schema.into(),
352                object_type: type_,
353                object_name: name.into(),
354            },
355            mechanism: Mechanism::Evolution,
356        }
357    }
358
359    /// Helper to construct a `Replacement` migration step.
360    fn replacement(version: &str, type_: CatalogItemType, schema: &str, name: &str) -> Self {
361        Self {
362            version: Version::parse(version).expect("valid"),
363            object: SystemObjectDescription {
364                schema_name: schema.into(),
365                object_type: type_,
366                object_name: name.into(),
367            },
368            mechanism: Mechanism::Replacement,
369        }
370    }
371}
372
373/// The mechanism to use to migrate the schema of a builtin collection.
374#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
375#[allow(dead_code)]
376enum Mechanism {
377    /// Persist schema evolution.
378    ///
379    /// Keeps existing contents but only works for schema changes that are backward compatible
380    /// according to [`backward_compatible`].
381    Evolution,
382    /// Shard replacement.
383    ///
384    /// Works for arbitrary schema changes but loses existing contents.
385    Replacement,
386}
387
388/// The result of a builtin schema migration.
389pub(super) struct MigrationResult {
390    /// IDs of items whose shards have been replaced using the `Replacement` mechanism.
391    pub replaced_items: BTreeSet<CatalogItemId>,
392    /// A cleanup action to take once the migration has been made durable.
393    pub cleanup_action: BoxFuture<'static, ()>,
394}
395
396impl Default for MigrationResult {
397    fn default() -> Self {
398        Self {
399            replaced_items: Default::default(),
400            cleanup_action: async {}.boxed(),
401        }
402    }
403}
404
405/// Run builtin schema migrations.
406///
407/// This is the entry point used by adapter when opening the catalog. It uses the hardcoded
408/// `BUILTINS` and `MIGRATIONS` lists to initialize the lists of available builtins and required
409/// migrations, respectively.
410pub(super) async fn run(
411    build_info: &BuildInfo,
412    deploy_generation: u64,
413    txn: &mut Transaction<'_>,
414    config: BuiltinItemMigrationConfig,
415) -> Result<MigrationResult, Error> {
416    // Sanity check to ensure we're not touching durable state in read-only mode.
417    assert_eq!(config.read_only, txn.is_savepoint());
418
419    // Tests may provide a dummy build info that confuses the migration step selection logic. Skip
420    // migrations if we observe this build info.
421    if *build_info == DUMMY_BUILD_INFO {
422        return Ok(MigrationResult::default());
423    }
424
425    let Some(durable_version) = get_migration_version(txn) else {
426        // New catalog; nothing to do.
427        return Ok(MigrationResult::default());
428    };
429    let build_version = build_info.semver_version();
430
431    let collection_metadata = txn.get_collection_metadata();
432    let system_objects = txn
433        .get_system_object_mappings()
434        .map(|m| {
435            let object = m.description;
436            let global_id = m.unique_identifier.global_id;
437            let shard_id = collection_metadata.get(&global_id).copied();
438            let Some((_, builtin)) = BUILTIN_LOOKUP.get(&object) else {
439                panic!("missing builtin {object:?}");
440            };
441            let info = ObjectInfo {
442                global_id,
443                shard_id,
444                builtin,
445                fingerprint: m.unique_identifier.fingerprint,
446            };
447            (object, info)
448        })
449        .collect();
450
451    let migration_shard = txn.get_builtin_migration_shard().expect("must exist");
452
453    let migration = Migration {
454        source_version: durable_version.clone(),
455        target_version: build_version.clone(),
456        deploy_generation,
457        system_objects,
458        migration_shard,
459        config,
460    };
461
462    let result = migration.run(&MIGRATIONS).await.map_err(|e| {
463        Error::new(ErrorKind::FailedBuiltinSchemaMigration {
464            last_seen_version: durable_version.to_string(),
465            this_version: build_version.to_string(),
466            cause: e.to_string(),
467        })
468    })?;
469
470    result.apply(txn);
471
472    let replaced_items = txn
473        .get_system_object_mappings()
474        .map(|m| m.unique_identifier)
475        .filter(|ids| result.new_shards.contains_key(&ids.global_id))
476        .map(|ids| ids.catalog_id)
477        .collect();
478
479    Ok(MigrationResult {
480        replaced_items,
481        cleanup_action: result.cleanup_action,
482    })
483}
484
485/// Result produced by `Migration::run`.
486struct MigrationRunResult {
487    new_shards: BTreeMap<GlobalId, ShardId>,
488    new_fingerprints: BTreeMap<SystemObjectDescription, String>,
489    shards_to_finalize: BTreeSet<ShardId>,
490    cleanup_action: BoxFuture<'static, ()>,
491}
492
493impl Default for MigrationRunResult {
494    fn default() -> Self {
495        Self {
496            new_shards: BTreeMap::new(),
497            new_fingerprints: BTreeMap::new(),
498            shards_to_finalize: BTreeSet::new(),
499            cleanup_action: async {}.boxed(),
500        }
501    }
502}
503
504impl MigrationRunResult {
505    /// Apply this migration result to the given transaction.
506    fn apply(&self, txn: &mut Transaction<'_>) {
507        // Update collection metadata.
508        let replaced_ids = self.new_shards.keys().copied().collect();
509        let old_metadata = txn.delete_collection_metadata(replaced_ids);
510        txn.insert_collection_metadata(self.new_shards.clone())
511            .expect("inserting unique shards IDs after deleting existing entries");
512
513        // Register shards for finalization.
514        let mut unfinalized_shards: BTreeSet<_> =
515            old_metadata.into_iter().map(|(_, sid)| sid).collect();
516        unfinalized_shards.extend(self.shards_to_finalize.iter().copied());
517        txn.insert_unfinalized_shards(unfinalized_shards)
518            .expect("cannot fail");
519
520        // Update fingerprints.
521        let mappings = txn
522            .get_system_object_mappings()
523            .filter_map(|m| {
524                let fingerprint = self.new_fingerprints.get(&m.description)?;
525                Some(SystemObjectMapping {
526                    description: m.description,
527                    unique_identifier: SystemObjectUniqueIdentifier {
528                        catalog_id: m.unique_identifier.catalog_id,
529                        global_id: m.unique_identifier.global_id,
530                        fingerprint: fingerprint.clone(),
531                    },
532                })
533            })
534            .collect();
535        txn.set_system_object_mappings(mappings)
536            .expect("filtered existing mappings remain unique");
537    }
538}
539
540/// Information about a system object required to run a `Migration`.
541#[derive(Clone, Debug)]
542struct ObjectInfo {
543    global_id: GlobalId,
544    shard_id: Option<ShardId>,
545    builtin: &'static Builtin<NameReference>,
546    fingerprint: String,
547}
548
549/// Context of a builtin schema migration.
550struct Migration {
551    /// The version we are migrating from.
552    ///
553    /// Same as the build version of the most recent leader process that successfully performed
554    /// migrations.
555    source_version: Version,
556    /// The version we are migration to.
557    ///
558    /// Same as the build version of this process.
559    target_version: Version,
560    /// The deploy generation of this process.
561    deploy_generation: u64,
562    /// Information about all objects in the system.
563    system_objects: BTreeMap<SystemObjectDescription, ObjectInfo>,
564    /// The ID of the migration shard.
565    migration_shard: ShardId,
566    /// Additional configuration.
567    config: BuiltinItemMigrationConfig,
568}
569
570impl Migration {
571    async fn run(self, steps: &[MigrationStep]) -> anyhow::Result<MigrationRunResult> {
572        info!(
573            deploy_generation = %self.deploy_generation,
574            "running builtin schema migration: {} -> {}",
575            self.source_version, self.target_version
576        );
577
578        self.validate_migration_steps(steps);
579
580        // Version-based migration filter fails for dev versions, see for example
581        // https://github.com/MaterializeInc/database-issues/issues/11335
582        let force_migration = if self.source_version != self.target_version
583            && self.source_version.pre.as_str().starts_with("dev")
584            && self.config.force_migration.is_none()
585        {
586            Some("evolution".to_string())
587        } else {
588            self.config.force_migration.clone()
589        };
590
591        let (force, plan) = match force_migration.as_deref() {
592            None => (false, self.plan_migration(steps)),
593            Some("evolution") => (true, self.plan_forced_migration(Mechanism::Evolution)),
594            Some("replacement") => (true, self.plan_forced_migration(Mechanism::Replacement)),
595            Some(other) => panic!("unknown force migration mechanism: {other}"),
596        };
597
598        if self.source_version == self.target_version && !force {
599            info!("skipping migration: already at target version");
600            return Ok(MigrationRunResult::default());
601        } else if self.source_version > self.target_version {
602            bail!("downgrade not supported");
603        }
604
605        // In leader mode, upgrade the version of the migration shard to the target version.
606        // This fences out any readers at lower versions.
607        if !self.config.read_only {
608            self.upgrade_migration_shard_version().await;
609        }
610
611        info!("executing migration plan: {plan:?}");
612
613        self.migrate_evolve(&plan.evolve).await?;
614        let new_shards = self.migrate_replace(&plan.replace).await?;
615
616        let mut migrated_objects = BTreeSet::new();
617        migrated_objects.extend(plan.evolve);
618        migrated_objects.extend(plan.replace);
619
620        let new_fingerprints = self.update_fingerprints(&migrated_objects)?;
621
622        let (shards_to_finalize, cleanup_action) = self.cleanup().await?;
623
624        Ok(MigrationRunResult {
625            new_shards,
626            new_fingerprints,
627            shards_to_finalize,
628            cleanup_action,
629        })
630    }
631
632    /// Sanity check the given migration steps.
633    ///
634    /// If any of these checks fail, that's a bug in Materialize, and we panic immediately.
635    fn validate_migration_steps(&self, steps: &[MigrationStep]) {
636        for step in steps {
637            assert!(
638                step.version <= self.target_version,
639                "migration step version greater than target version: {} > {}",
640                step.version,
641                self.target_version,
642            );
643
644            let object = &step.object;
645
646            // `mz_storage_usage_by_shard` cannot be migrated for multiple reasons. Firstly, it would
647            // cause the table to be truncated because the contents are not also stored in the durable
648            // catalog. Secondly, we prune `mz_storage_usage_by_shard` of old events in the background
649            // on startup. The correctness of that pruning relies on there being no other retractions
650            // to `mz_storage_usage_by_shard`.
651            //
652            // TODO: Confirm the above reasoning, it might be outdated?
653            assert_ne!(
654                &*MZ_STORAGE_USAGE_BY_SHARD_DESCRIPTION, object,
655                "mz_storage_usage_by_shard cannot be migrated or else the table will be truncated"
656            );
657
658            // Same hazard as `mz_storage_usage_by_shard`: the startup pruner
659            // (`Coordinator::prune_arrangement_sizes_history_on_startup`) assumes it is
660            // the only source of retractions, so migration-driven truncation would break it.
661            assert_ne!(
662                &*MZ_OBJECT_ARRANGEMENT_SIZE_HISTORY_DESCRIPTION, object,
663                "mz_object_arrangement_size_history cannot be migrated or else the table will be truncated"
664            );
665
666            // `mz_catalog_raw` cannot be migrated because it contains the durable catalog and it
667            // wouldn't be very durable if we allowed it to be truncated.
668            assert_ne!(
669                &*MZ_CATALOG_RAW_DESCRIPTION, object,
670                "mz_catalog_raw cannot be migrated"
671            );
672
673            let Some(object_info) = self.system_objects.get(object) else {
674                panic!("migration step for non-existent builtin: {object:?}");
675            };
676
677            let builtin = object_info.builtin;
678            use Builtin::*;
679            assert!(
680                matches!(builtin, Table(..) | Source(..) | MaterializedView(..)),
681                "schema migration not supported for builtin: {builtin:?}",
682            );
683        }
684    }
685
686    /// Select for each object to migrate the appropriate migration mechanism.
687    fn plan_migration(&self, steps: &[MigrationStep]) -> Plan {
688        // Ignore any steps at versions before `source_version`.
689        let steps = steps.iter().filter(|s| s.version > self.source_version);
690
691        // Select a mechanism for each object, according to the requested migrations:
692        //  * If any `Replacement` was requested, use `Replacement`.
693        //  * Otherwise, (i.e. only `Evolution` was requested), use `Evolution`.
694        let mut by_object = BTreeMap::new();
695        for step in steps {
696            if let Some(entry) = by_object.get_mut(&step.object) {
697                *entry = match (step.mechanism, *entry) {
698                    (Mechanism::Evolution, Mechanism::Evolution) => Mechanism::Evolution,
699                    (Mechanism::Replacement, _) | (_, Mechanism::Replacement) => {
700                        Mechanism::Replacement
701                    }
702                };
703            } else {
704                by_object.insert(step.object.clone(), step.mechanism);
705            }
706        }
707
708        let mut plan = Plan::default();
709        for (object, mechanism) in by_object {
710            match mechanism {
711                Mechanism::Evolution => plan.evolve.push(object),
712                Mechanism::Replacement => plan.replace.push(object),
713            }
714        }
715
716        plan
717    }
718
719    /// Plan a forced migration of all objects using the given mechanism.
720    fn plan_forced_migration(&self, mechanism: Mechanism) -> Plan {
721        let objects = self
722            .system_objects
723            .iter()
724            // Skip objects that don't yet have a shard registered. These are brand-new builtins
725            // added in this version; the leader will allocate their shards during bootstrap, and
726            // there is nothing to evolve or replace.
727            .filter(|(_, info)| info.shard_id.is_some())
728            .filter(|(_, info)| {
729                use Builtin::*;
730                match info.builtin {
731                    // Filter out the 'mz_storage_usage_by_shard' table since we need to retain
732                    // that info for billing purposes.
733                    Table(table) => **table != *MZ_STORAGE_USAGE_BY_SHARD,
734                    MaterializedView(..) => true,
735                    Source(source) => **source != *MZ_CATALOG_RAW,
736                    Log(..) | View(..) | Type(..) | Func(..) | Index(..) | Connection(..) => false,
737                }
738            })
739            .map(|(object, _)| object.clone())
740            .collect();
741
742        let mut plan = Plan::default();
743        match mechanism {
744            Mechanism::Evolution => plan.evolve = objects,
745            Mechanism::Replacement => plan.replace = objects,
746        }
747
748        plan
749    }
750
751    /// Upgrade the migration shard to the target version.
752    async fn upgrade_migration_shard_version(&self) {
753        let persist = &self.config.persist_client;
754        let diagnostics = Diagnostics {
755            shard_name: "builtin_migration".to_string(),
756            handle_purpose: format!("migration shard upgrade @ {}", self.target_version),
757        };
758
759        persist
760            .upgrade_version::<migration_shard::Key, ShardId, Timestamp, StorageDiff>(
761                self.migration_shard,
762                diagnostics,
763            )
764            .await
765            .expect("valid usage");
766    }
767
768    /// Migrate the given objects using the `Evolution` mechanism.
769    async fn migrate_evolve(&self, objects: &[SystemObjectDescription]) -> anyhow::Result<()> {
770        for object in objects {
771            self.migrate_evolve_one(object).await?;
772        }
773        Ok(())
774    }
775
776    async fn migrate_evolve_one(&self, object: &SystemObjectDescription) -> anyhow::Result<()> {
777        let persist = &self.config.persist_client;
778
779        let Some(object_info) = self.system_objects.get(object) else {
780            bail!("missing builtin {object:?}");
781        };
782        let id = object_info.global_id;
783
784        let Some(shard_id) = object_info.shard_id else {
785            // No shard is registered for this builtin. In leader mode, this is fine, we'll
786            // register the shard during bootstrap. In read-only mode, we might be racing with the
787            // leader to register the shard and it's unclear what sort of confusion can arise from
788            // that -- better to bail out in this case.
789            if self.config.read_only {
790                bail!("missing shard ID for builtin {object:?} ({id})");
791            } else {
792                return Ok(());
793            }
794        };
795
796        let target_desc = match object_info.builtin {
797            Builtin::Table(table) => &table.desc,
798            Builtin::Source(source) => &source.desc,
799            Builtin::MaterializedView(mv) => &mv.desc,
800            _ => bail!("not a storage collection: {object:?}"),
801        };
802
803        let diagnostics = Diagnostics {
804            shard_name: id.to_string(),
805            handle_purpose: format!("builtin schema migration @ {}", self.target_version),
806        };
807        let source_schema = persist
808            .latest_schema::<SourceData, (), Timestamp, StorageDiff>(shard_id, diagnostics.clone())
809            .await
810            .expect("valid usage");
811
812        info!(?object, %id, %shard_id, ?source_schema, ?target_desc, "migrating by evolution");
813
814        if self.config.read_only {
815            // In read-only mode, only check that the new schema is backward compatible.
816            // We'll register it when/if we restart in leader mode.
817            if let Some((_, source_desc, _)) = &source_schema {
818                let old = mz_persist_types::columnar::data_type::<SourceData>(source_desc)?;
819                let new = mz_persist_types::columnar::data_type::<SourceData>(target_desc)?;
820                if backward_compatible(&old, &new).is_none() {
821                    bail!(
822                        "incompatible schema evolution for {object:?}: \
823                         {source_desc:?} -> {target_desc:?}"
824                    );
825                }
826            }
827
828            return Ok(());
829        }
830
831        let (mut schema_id, mut source_desc) = match source_schema {
832            Some((schema_id, source_desc, _)) => (schema_id, source_desc),
833            None => {
834                // If no schema was previously registered, simply try to register the new one. This
835                // might fail due to a concurrent registration, in which case we'll fall back to
836                // `compare_and_evolve_schema`.
837
838                debug!(%id, %shard_id, "no previous schema found; registering initial one");
839                let schema_id = persist
840                    .register_schema::<SourceData, (), Timestamp, StorageDiff>(
841                        shard_id,
842                        target_desc,
843                        &UnitSchema,
844                        diagnostics.clone(),
845                    )
846                    .await
847                    .expect("valid usage");
848                if schema_id.is_some() {
849                    return Ok(());
850                }
851
852                debug!(%id, %shard_id, "schema registration failed; falling back to CaES");
853                let (schema_id, source_desc, _) = persist
854                    .latest_schema::<SourceData, (), Timestamp, StorageDiff>(
855                        shard_id,
856                        diagnostics.clone(),
857                    )
858                    .await
859                    .expect("valid usage")
860                    .expect("known to exist");
861
862                (schema_id, source_desc)
863            }
864        };
865
866        loop {
867            // Evolving the schema might fail if another process evolved the schema concurrently,
868            // in which case we need to retry. Most likely the other process evolved the schema to
869            // our own target schema and the second try is a no-op.
870
871            debug!(%id, %shard_id, %schema_id, ?source_desc, ?target_desc, "attempting CaES");
872            let result = persist
873                .compare_and_evolve_schema::<SourceData, (), Timestamp, StorageDiff>(
874                    shard_id,
875                    schema_id,
876                    target_desc,
877                    &UnitSchema,
878                    diagnostics.clone(),
879                )
880                .await
881                .expect("valid usage");
882
883            match result {
884                CaESchema::Ok(schema_id) => {
885                    debug!(%id, %shard_id, %schema_id, "schema evolved successfully");
886                    break;
887                }
888                CaESchema::Incompatible => bail!(
889                    "incompatible schema evolution for {object:?}: \
890                     {source_desc:?} -> {target_desc:?}"
891                ),
892                CaESchema::ExpectedMismatch {
893                    schema_id: new_id,
894                    key,
895                    val: UnitSchema,
896                } => {
897                    schema_id = new_id;
898                    source_desc = key;
899                }
900            }
901        }
902
903        Ok(())
904    }
905
906    /// Migrate the given objects using the `Replacement` mechanism.
907    async fn migrate_replace(
908        &self,
909        objects: &[SystemObjectDescription],
910    ) -> anyhow::Result<BTreeMap<GlobalId, ShardId>> {
911        if objects.is_empty() {
912            return Ok(Default::default());
913        }
914
915        let diagnostics = Diagnostics {
916            shard_name: "builtin_migration".to_string(),
917            handle_purpose: format!("builtin schema migration @ {}", self.target_version),
918        };
919        let (mut persist_write, mut persist_read) =
920            self.open_migration_shard(diagnostics.clone()).await;
921
922        let mut ids_to_replace = BTreeSet::new();
923        for object in objects {
924            if let Some(info) = self.system_objects.get(object) {
925                ids_to_replace.insert(info.global_id);
926            } else {
927                bail!("missing id for builtin {object:?}");
928            }
929        }
930
931        info!(?objects, ?ids_to_replace, "migrating by replacement");
932
933        // Fetch replacement shard IDs from the migration shard, or insert new ones if none exist.
934        // This can fail due to writes by concurrent processes, so we need to retry.
935        let replaced_shards = loop {
936            if let Some(shards) = self
937                .try_get_or_insert_replacement_shards(
938                    &ids_to_replace,
939                    &mut persist_write,
940                    &mut persist_read,
941                )
942                .await?
943            {
944                break shards;
945            }
946        };
947
948        Ok(replaced_shards)
949    }
950
951    /// Try to get or insert replacement shards for the given IDs into the migration shard, at
952    /// `target_version` and `deploy_generation`.
953    ///
954    /// This method looks for existing entries in the migration shards and returns those if they
955    /// are present. Otherwise it generates new shard IDs and tries to insert them.
956    ///
957    /// The result of this call is `None` if no existing entries were found and inserting new ones
958    /// failed because of a concurrent write to the migration shard. In this case, the caller is
959    /// expected to retry.
960    async fn try_get_or_insert_replacement_shards(
961        &self,
962        ids_to_replace: &BTreeSet<GlobalId>,
963        persist_write: &mut WriteHandle<migration_shard::Key, ShardId, Timestamp, StorageDiff>,
964        persist_read: &mut ReadHandle<migration_shard::Key, ShardId, Timestamp, StorageDiff>,
965    ) -> anyhow::Result<Option<BTreeMap<GlobalId, ShardId>>> {
966        let upper = persist_write.fetch_recent_upper().await;
967        let write_ts = *upper.as_option().expect("migration shard not sealed");
968
969        let mut ids_to_replace = ids_to_replace.clone();
970        let mut replaced_shards = BTreeMap::new();
971
972        // Another process might already have done a shard replacement at our version and
973        // generation, in which case we can directly reuse the replacement shards.
974        //
975        // Note that we can't assume that the previous process had the same `ids_to_replace` as we
976        // do. The set of migrations to run depends on both the source and the target version, and
977        // the migration shard is not keyed by source version. The previous writer might have seen
978        // a different source version, if there was a concurrent migration by a leader process.
979        if let Some(read_ts) = write_ts.step_back() {
980            let pred = |key: &migration_shard::Key| {
981                key.build_version == self.target_version
982                    && key.deploy_generation == Some(self.deploy_generation)
983            };
984            if let Some(entries) = read_migration_shard(persist_read, read_ts, pred).await {
985                for (key, shard_id) in entries {
986                    let id = GlobalId::System(key.global_id);
987                    if ids_to_replace.remove(&id) {
988                        replaced_shards.insert(id, shard_id);
989                    }
990                }
991
992                debug!(
993                    %read_ts, ?replaced_shards, ?ids_to_replace,
994                    "found existing entries in migration shard",
995                );
996            }
997
998            if ids_to_replace.is_empty() {
999                return Ok(Some(replaced_shards));
1000            }
1001        }
1002
1003        // Generate new shard IDs and attempt to insert them into the migration shard. If we get a
1004        // CaA failure at `write_ts` that means a concurrent process has inserted in the meantime
1005        // and we need to re-check the migration shard contents.
1006        let mut updates = Vec::new();
1007        for id in ids_to_replace {
1008            let shard_id = ShardId::new();
1009            replaced_shards.insert(id, shard_id);
1010
1011            let GlobalId::System(global_id) = id else {
1012                bail!("attempt to migrate a non-system collection: {id}");
1013            };
1014            let key = migration_shard::Key {
1015                global_id,
1016                build_version: self.target_version.clone(),
1017                deploy_generation: Some(self.deploy_generation),
1018            };
1019            updates.push(((key, shard_id), write_ts, 1));
1020        }
1021
1022        let upper = Antichain::from_elem(write_ts);
1023        let new_upper = Antichain::from_elem(write_ts.step_forward());
1024        debug!(%write_ts, "attempting insert into migration shard");
1025        let result = persist_write
1026            .compare_and_append(updates, upper, new_upper)
1027            .await
1028            .expect("valid usage");
1029
1030        match result {
1031            Ok(()) => {
1032                debug!(
1033                    %write_ts, ?replaced_shards,
1034                    "successfully inserted into migration shard"
1035                );
1036                Ok(Some(replaced_shards))
1037            }
1038            Err(_mismatch) => Ok(None),
1039        }
1040    }
1041
1042    /// Open writer and reader for the migration shard.
1043    async fn open_migration_shard(
1044        &self,
1045        diagnostics: Diagnostics,
1046    ) -> (
1047        WriteHandle<migration_shard::Key, ShardId, Timestamp, StorageDiff>,
1048        ReadHandle<migration_shard::Key, ShardId, Timestamp, StorageDiff>,
1049    ) {
1050        let persist = &self.config.persist_client;
1051
1052        persist
1053            .open(
1054                self.migration_shard,
1055                Arc::new(migration_shard::KeySchema),
1056                Arc::new(ShardIdSchema),
1057                diagnostics,
1058                USE_CRITICAL_SINCE_CATALOG.get(persist.dyncfgs()),
1059            )
1060            .await
1061            .expect("valid usage")
1062    }
1063
1064    /// Open a [`SinceHandle`] for the migration shard.
1065    async fn open_migration_shard_since(
1066        &self,
1067        diagnostics: Diagnostics,
1068    ) -> SinceHandle<migration_shard::Key, ShardId, Timestamp, StorageDiff> {
1069        self.config
1070            .persist_client
1071            .open_critical_since(
1072                self.migration_shard,
1073                // TODO: We may need to use a different critical reader
1074                // id for this if we want to be able to introspect it via SQL.
1075                PersistClient::CONTROLLER_CRITICAL_SINCE,
1076                Opaque::encode(&i64::MIN),
1077                diagnostics.clone(),
1078            )
1079            .await
1080            .expect("valid usage")
1081    }
1082
1083    /// Update the fingerprints for `migrated_items`.
1084    ///
1085    /// Returns the new fingerprints. Also asserts that the current fingerprints of all other
1086    /// system items match their builtin definitions.
1087    fn update_fingerprints(
1088        &self,
1089        migrated_items: &BTreeSet<SystemObjectDescription>,
1090    ) -> anyhow::Result<BTreeMap<SystemObjectDescription, String>> {
1091        let mut new_fingerprints = BTreeMap::new();
1092        for (object, object_info) in &self.system_objects {
1093            let id = object_info.global_id;
1094            let builtin = object_info.builtin;
1095
1096            let fingerprint = builtin.fingerprint();
1097            if fingerprint == object_info.fingerprint {
1098                continue; // fingerprint unchanged, nothing to do
1099            }
1100
1101            // Fingerprint mismatch is expected for a migrated item.
1102            let migrated = migrated_items.contains(object);
1103            // Some builtin types have schemas but no durable state. No migration needed for those.
1104            let ephemeral = matches!(
1105                builtin,
1106                Builtin::Log(_) | Builtin::View(_) | Builtin::Index(_),
1107            );
1108
1109            if migrated || ephemeral {
1110                new_fingerprints.insert(object.clone(), fingerprint);
1111            } else if builtin.runtime_alterable() {
1112                // Runtime alterable builtins have no meaningful builtin fingerprint, and a
1113                // sentinel value stored in the catalog.
1114                assert_eq!(
1115                    object_info.fingerprint, RUNTIME_ALTERABLE_FINGERPRINT_SENTINEL,
1116                    "fingerprint mismatch for runtime-alterable builtin {object:?} ({id})",
1117                );
1118            } else {
1119                panic!(
1120                    "fingerprint mismatch for builtin {builtin:?} ({id}): {} != {}",
1121                    fingerprint, object_info.fingerprint,
1122                );
1123            }
1124        }
1125
1126        Ok(new_fingerprints)
1127    }
1128
1129    /// Perform cleanup of migration state, i.e. the migration shard.
1130    ///
1131    /// Returns a list of shards to finalize, and a `Future` that must be run after the shard
1132    /// finalization has been durably enqueued. The `Future` is used to remove entries from the
1133    /// migration shard only after we know the respective shards will be finalized. Removing
1134    /// entries immediately would risk leaking the shards.
1135    ///
1136    /// We only perform cleanup in leader mode, to keep the durable state changes made by read-only
1137    /// processes a minimal as possible. Given that Materialize doesn't support version downgrades,
1138    /// it is safe to assume that any state for versions below the `target_version` is not needed
1139    /// anymore and can be cleaned up.
1140    ///
1141    /// Note that it is fine for cleanup to sometimes fail or be skipped. The size of the migration
1142    /// shard should always be pretty small, so keeping migration state around for longer isn't a
1143    /// concern. As a result, we can keep the logic simple here and skip doing cleanup in response
1144    /// to transient failures, instead of retrying.
1145    async fn cleanup(&self) -> anyhow::Result<(BTreeSet<ShardId>, BoxFuture<'static, ()>)> {
1146        let noop_action = async {}.boxed();
1147        let noop_result = (BTreeSet::new(), noop_action);
1148
1149        if self.config.read_only {
1150            return Ok(noop_result);
1151        }
1152
1153        let diagnostics = Diagnostics {
1154            shard_name: "builtin_migration".to_string(),
1155            handle_purpose: "builtin schema migration cleanup".into(),
1156        };
1157        let (mut persist_write, mut persist_read) =
1158            self.open_migration_shard(diagnostics.clone()).await;
1159        let mut persist_since = self.open_migration_shard_since(diagnostics.clone()).await;
1160
1161        let upper = persist_write.fetch_recent_upper().await.clone();
1162        let write_ts = *upper.as_option().expect("migration shard not sealed");
1163        let Some(read_ts) = write_ts.step_back() else {
1164            return Ok(noop_result);
1165        };
1166
1167        // Collect old entries to remove.
1168        let pred = |key: &migration_shard::Key| key.build_version < self.target_version;
1169        let Some(stale_entries) = read_migration_shard(&mut persist_read, read_ts, pred).await
1170        else {
1171            return Ok(noop_result);
1172        };
1173
1174        debug!(
1175            ?stale_entries,
1176            "cleaning migration shard up to version {}", self.target_version,
1177        );
1178
1179        let current_shards: BTreeMap<_, _> = self
1180            .system_objects
1181            .values()
1182            .filter_map(|o| o.shard_id.map(|shard_id| (o.global_id, shard_id)))
1183            .collect();
1184
1185        let mut shards_to_finalize = BTreeSet::new();
1186        let mut retractions = Vec::new();
1187        for (key, shard_id) in stale_entries {
1188            // The migration shard contains both shards created during aborted upgrades and shards
1189            // created during successful upgrades. The latter may still be in use, so we have to
1190            // check and only finalize those that aren't anymore.
1191            let gid = GlobalId::System(key.global_id);
1192            if current_shards.get(&gid) != Some(&shard_id) {
1193                shards_to_finalize.insert(shard_id);
1194            }
1195
1196            retractions.push(((key, shard_id), write_ts, -1));
1197        }
1198
1199        let cleanup_action = async move {
1200            if !retractions.is_empty() {
1201                let new_upper = Antichain::from_elem(write_ts.step_forward());
1202                let result = persist_write
1203                    .compare_and_append(retractions, upper, new_upper)
1204                    .await
1205                    .expect("valid usage");
1206                match result {
1207                    Ok(()) => debug!("cleaned up migration shard"),
1208                    Err(mismatch) => debug!(?mismatch, "migration shard cleanup failed"),
1209                }
1210            }
1211        }
1212        .boxed();
1213
1214        // Downgrade the since, to enable some compaction.
1215        let o = persist_since.opaque().clone();
1216        let new_since = Antichain::from_elem(read_ts);
1217        let result = persist_since
1218            .maybe_compare_and_downgrade_since(&o, (&o, &new_since))
1219            .await;
1220        soft_assert_or_log!(result.is_none_or(|r| r.is_ok()), "opaque mismatch");
1221
1222        Ok((shards_to_finalize, cleanup_action))
1223    }
1224}
1225
1226/// Read the migration shard at the given timestamp, returning all entries that match the given
1227/// predicate.
1228///
1229/// Returns `None` if the migration shard contains no matching entries, or if it isn't readable at
1230/// `read_ts`.
1231async fn read_migration_shard<P>(
1232    persist_read: &mut ReadHandle<migration_shard::Key, ShardId, Timestamp, StorageDiff>,
1233    read_ts: Timestamp,
1234    predicate: P,
1235) -> Option<Vec<(migration_shard::Key, ShardId)>>
1236where
1237    P: for<'a> Fn(&migration_shard::Key) -> bool,
1238{
1239    let as_of = Antichain::from_elem(read_ts);
1240    let updates = persist_read.snapshot_and_fetch(as_of).await.ok()?;
1241
1242    assert!(
1243        updates.iter().all(|(_, _, diff)| *diff == 1),
1244        "migration shard contains invalid diffs: {updates:?}",
1245    );
1246
1247    let entries: Vec<_> = updates
1248        .into_iter()
1249        .map(|(data, _, _)| data)
1250        .filter(move |(key, _)| predicate(key))
1251        .collect();
1252
1253    (!entries.is_empty()).then_some(entries)
1254}
1255
1256/// A plan to migrate between two versions.
1257#[derive(Debug, Default)]
1258struct Plan {
1259    /// Objects to migrate using the `Evolution` mechanism.
1260    evolve: Vec<SystemObjectDescription>,
1261    /// Objects to migrate using the `Replacement` mechanism.
1262    replace: Vec<SystemObjectDescription>,
1263}
1264
1265/// Types and persist codec impls for the migration shard used by the `Replacement` mechanism.
1266mod migration_shard {
1267    use std::fmt;
1268    use std::str::FromStr;
1269
1270    use arrow::array::{StringArray, StringBuilder};
1271    use bytes::{BufMut, Bytes};
1272    use mz_persist_types::Codec;
1273    use mz_persist_types::codec_impls::{
1274        SimpleColumnarData, SimpleColumnarDecoder, SimpleColumnarEncoder,
1275    };
1276    use mz_persist_types::columnar::Schema;
1277    use mz_persist_types::stats::NoneStats;
1278    use semver::Version;
1279    use serde::{Deserialize, Serialize};
1280
1281    #[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
1282    pub(super) struct Key {
1283        pub(super) global_id: u64,
1284        pub(super) build_version: Version,
1285        // Versions < 26.0 didn't include the deploy generation. As long as we still might
1286        // encounter migration shard entries that don't have it, we need to keep this an `Option`
1287        // and keep supporting both key formats.
1288        pub(super) deploy_generation: Option<u64>,
1289    }
1290
1291    impl fmt::Display for Key {
1292        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1293            if self.deploy_generation.is_some() {
1294                // current format
1295                let s = serde_json::to_string(self).expect("JSON serializable");
1296                f.write_str(&s)
1297            } else {
1298                // pre-26.0 format
1299                write!(f, "{}-{}", self.global_id, self.build_version)
1300            }
1301        }
1302    }
1303
1304    impl FromStr for Key {
1305        type Err = String;
1306
1307        fn from_str(s: &str) -> Result<Self, String> {
1308            // current format
1309            if let Ok(key) = serde_json::from_str(s) {
1310                return Ok(key);
1311            };
1312
1313            // pre-26.0 format
1314            let parts: Vec<_> = s.splitn(2, '-').collect();
1315            let &[global_id, build_version] = parts.as_slice() else {
1316                return Err(format!("invalid Key '{s}'"));
1317            };
1318            let global_id = global_id.parse::<u64>().map_err(|e| e.to_string())?;
1319            let build_version = build_version
1320                .parse::<Version>()
1321                .map_err(|e| e.to_string())?;
1322            Ok(Key {
1323                global_id,
1324                build_version,
1325                deploy_generation: None,
1326            })
1327        }
1328    }
1329
1330    impl Default for Key {
1331        fn default() -> Self {
1332            Self {
1333                global_id: Default::default(),
1334                build_version: Version::new(0, 0, 0),
1335                deploy_generation: Some(0),
1336            }
1337        }
1338    }
1339
1340    impl Codec for Key {
1341        type Schema = KeySchema;
1342        type Storage = ();
1343
1344        fn codec_name() -> String {
1345            "TableKey".into()
1346        }
1347
1348        fn encode<B: BufMut>(&self, buf: &mut B) {
1349            buf.put(self.to_string().as_bytes())
1350        }
1351
1352        fn decode<'a>(buf: &'a [u8], _schema: &KeySchema) -> Result<Self, String> {
1353            let s = str::from_utf8(buf).map_err(|e| e.to_string())?;
1354            s.parse()
1355        }
1356
1357        fn encode_schema(_schema: &KeySchema) -> Bytes {
1358            Bytes::new()
1359        }
1360
1361        fn decode_schema(buf: &Bytes) -> Self::Schema {
1362            assert_eq!(*buf, Bytes::new());
1363            KeySchema
1364        }
1365    }
1366
1367    impl SimpleColumnarData for Key {
1368        type ArrowBuilder = StringBuilder;
1369        type ArrowColumn = StringArray;
1370
1371        fn goodbytes(builder: &Self::ArrowBuilder) -> usize {
1372            builder.values_slice().len()
1373        }
1374
1375        fn push(&self, builder: &mut Self::ArrowBuilder) {
1376            builder.append_value(&self.to_string());
1377        }
1378
1379        fn push_null(builder: &mut Self::ArrowBuilder) {
1380            builder.append_null();
1381        }
1382
1383        fn read(&mut self, idx: usize, column: &Self::ArrowColumn) {
1384            *self = column.value(idx).parse().expect("valid Key");
1385        }
1386    }
1387
1388    #[derive(Debug, PartialEq)]
1389    pub(super) struct KeySchema;
1390
1391    impl Schema<Key> for KeySchema {
1392        type ArrowColumn = StringArray;
1393        type Statistics = NoneStats;
1394        type Decoder = SimpleColumnarDecoder<Key>;
1395        type Encoder = SimpleColumnarEncoder<Key>;
1396
1397        fn encoder(&self) -> anyhow::Result<SimpleColumnarEncoder<Key>> {
1398            Ok(SimpleColumnarEncoder::default())
1399        }
1400
1401        fn decoder(&self, col: StringArray) -> anyhow::Result<SimpleColumnarDecoder<Key>> {
1402            Ok(SimpleColumnarDecoder::new(col))
1403        }
1404    }
1405}
1406
1407#[cfg(test)]
1408#[path = "builtin_schema_migration_tests.rs"]
1409mod tests;