mz_expr/
id.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
10use std::fmt;
11
12use mz_lowertest::MzReflect;
13use mz_repr::GlobalId;
14use serde::{Deserialize, Serialize};
15
16/// An opaque identifier for a dataflow component. In other words, identifies
17/// the target of a [`MirRelationExpr::Get`](crate::MirRelationExpr::Get).
18#[derive(
19    Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, MzReflect,
20)]
21pub enum Id {
22    /// An identifier that refers to a local component of a dataflow.
23    Local(LocalId),
24    /// An identifier that refers to a global dataflow.
25    Global(GlobalId),
26}
27
28impl fmt::Display for Id {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        match self {
31            Id::Local(id) => id.fmt(f),
32            Id::Global(id) => id.fmt(f),
33        }
34    }
35}
36
37/// The identifier for a local component of a dataflow.
38#[derive(
39    Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, MzReflect,
40)]
41pub struct LocalId(pub(crate) u64);
42
43impl LocalId {
44    /// Constructs a new local identifier. It is the caller's responsibility
45    /// to provide a unique `v`.
46    pub fn new(v: u64) -> LocalId {
47        LocalId(v)
48    }
49}
50
51impl From<&LocalId> for u64 {
52    fn from(id: &LocalId) -> Self {
53        id.0
54    }
55}
56
57impl fmt::Display for LocalId {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        write!(f, "l{}", self.0)
60    }
61}
62
63/// Unique identifier for an instantiation of a source.
64#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
65pub struct SourceInstanceId {
66    /// The ID of the source, shared across all instances.
67    pub source_id: GlobalId,
68    /// The ID of the timely dataflow containing this instantiation of this
69    /// source.
70    pub dataflow_id: usize,
71}
72
73impl fmt::Display for SourceInstanceId {
74    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75        write!(f, "{}/{}", self.source_id, self.dataflow_id)
76    }
77}