Skip to content

Commit ccf7d6c

Browse files
Add ViewInfo to TableSchema
1 parent 49225a2 commit ccf7d6c

File tree

1 file changed

+53
-15
lines changed

1 file changed

+53
-15
lines changed

crates/schema/src/schema.rs

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ pub trait Schema: Sized {
5050
fn check_compatible(&self, module_def: &ModuleDef, def: &Self::Def) -> Result<(), anyhow::Error>;
5151
}
5252

53+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
54+
pub struct ViewInfo {
55+
pub view_id: ViewId,
56+
pub has_args: bool,
57+
pub is_anonymous: bool,
58+
}
59+
5360
/// A wrapper around a [`TableSchema`] for views.
5461
#[derive(Debug, Clone, PartialEq, Eq)]
5562
pub struct TableOrViewSchema {
5663
pub table_id: TableId,
57-
pub view_id: Option<(ViewId, bool)>,
64+
pub view_info: Option<ViewInfo>,
5865
pub table_name: Box<str>,
5966
pub table_access: StAccess,
6067
inner: Arc<TableSchema>,
@@ -64,7 +71,7 @@ impl From<Arc<TableSchema>> for TableOrViewSchema {
6471
fn from(inner: Arc<TableSchema>) -> Self {
6572
Self {
6673
table_id: inner.table_id,
67-
view_id: inner.view_id,
74+
view_info: inner.view_info,
6875
table_name: inner.table_name.clone(),
6976
table_access: inner.table_access,
7077
inner,
@@ -75,12 +82,12 @@ impl From<Arc<TableSchema>> for TableOrViewSchema {
7582
impl TableOrViewSchema {
7683
/// Is this schema that of a view?
7784
pub fn is_view(&self) -> bool {
78-
self.view_id.is_some()
85+
self.view_info.is_some()
7986
}
8087

8188
/// Is this schema that of an anonymous view?
8289
pub fn is_anonymous_view(&self) -> bool {
83-
self.view_id.is_some_and(|(_, is_anonymous)| is_anonymous)
90+
self.view_info.as_ref().is_some_and(|view_info| view_info.is_anonymous)
8491
}
8592

8693
/// Returns the [`TableSchema`] of the underlying datastore table.
@@ -97,10 +104,29 @@ impl TableOrViewSchema {
97104
/// For views in particular it will not include the internal `sender` and `arg_id` columns.
98105
/// Hence columns in this list should be looked up by their [`ColId`] - not their position.
99106
pub fn public_columns(&self) -> &[ColumnSchema] {
100-
if self.is_view() {
101-
return &self.inner.columns[2..];
107+
match self.view_info {
108+
Some(ViewInfo {
109+
has_args: true,
110+
is_anonymous: false,
111+
..
112+
}) => &self.inner.columns[2..],
113+
Some(ViewInfo {
114+
has_args: true,
115+
is_anonymous: true,
116+
..
117+
}) => &self.inner.columns[1..],
118+
Some(ViewInfo {
119+
has_args: false,
120+
is_anonymous: false,
121+
..
122+
}) => &self.inner.columns[1..],
123+
Some(ViewInfo {
124+
has_args: false,
125+
is_anonymous: true,
126+
..
127+
})
128+
| None => &self.inner.columns,
102129
}
103-
&self.inner.columns
104130
}
105131

106132
/// Check if the `col_name` exist on this [`TableOrViewSchema`]
@@ -123,8 +149,7 @@ pub struct TableSchema {
123149
pub table_name: Box<str>,
124150

125151
/// Is this the backing table of a view?
126-
/// Is it an anonymous view?
127-
pub view_id: Option<(ViewId, bool)>,
152+
pub view_info: Option<ViewInfo>,
128153

129154
/// The columns of the table.
130155
/// The ordering of the columns is significant. Columns are frequently identified by `ColId`, that is, position in this list.
@@ -171,7 +196,7 @@ impl TableSchema {
171196
pub fn new(
172197
table_id: TableId,
173198
table_name: Box<str>,
174-
view_id: Option<(ViewId, bool)>,
199+
view_info: Option<ViewInfo>,
175200
columns: Vec<ColumnSchema>,
176201
indexes: Vec<IndexSchema>,
177202
constraints: Vec<ConstraintSchema>,
@@ -185,7 +210,7 @@ impl TableSchema {
185210
row_type: columns_to_row_type(&columns),
186211
table_id,
187212
table_name,
188-
view_id,
213+
view_info,
189214
columns,
190215
indexes,
191216
constraints,
@@ -230,12 +255,12 @@ impl TableSchema {
230255

231256
/// Is this the backing table for a view?
232257
pub fn is_view(&self) -> bool {
233-
self.view_id.is_some()
258+
self.view_info.is_some()
234259
}
235260

236261
/// Is this the backing table for an anonymous view?
237262
pub fn is_anonymous_view(&self) -> bool {
238-
self.view_id.is_some_and(|(_, is_anonymous)| is_anonymous)
263+
self.view_info.as_ref().is_some_and(|view_info| view_info.is_anonymous)
239264
}
240265

241266
/// Update the table id of this schema.
@@ -681,6 +706,7 @@ impl TableSchema {
681706
name,
682707
is_public,
683708
is_anonymous,
709+
param_columns,
684710
return_columns,
685711
..
686712
} = view_def;
@@ -699,10 +725,16 @@ impl TableSchema {
699725
StAccess::Private
700726
};
701727

728+
let view_info = ViewInfo {
729+
view_id: ViewId::SENTINEL,
730+
has_args: !param_columns.is_empty(),
731+
is_anonymous: *is_anonymous,
732+
};
733+
702734
TableSchema::new(
703735
TableId::SENTINEL,
704736
(*name).clone().into(),
705-
Some((ViewId::SENTINEL, *is_anonymous)),
737+
Some(view_info),
706738
columns,
707739
vec![],
708740
vec![],
@@ -815,10 +847,16 @@ impl TableSchema {
815847
StAccess::Private
816848
};
817849

850+
let view_info = ViewInfo {
851+
view_id: ViewId::SENTINEL,
852+
has_args: !param_columns.is_empty(),
853+
is_anonymous: *is_anonymous,
854+
};
855+
818856
TableSchema::new(
819857
TableId::SENTINEL,
820858
(*name).clone().into(),
821-
Some((ViewId::SENTINEL, *is_anonymous)),
859+
Some(view_info),
822860
columns,
823861
indexes,
824862
vec![],

0 commit comments

Comments
 (0)