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