Skip to main content

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