Skip to content

Commit 4be343b

Browse files
authored
fix! revert TableRef changes (#1004)
# Rationale for this change * #998 added a breaking change that #1001 attempted to resolve. However, this fix is not enough to resolve some downstream issues. This PR is an alternative, non-breaking, replacement to #998. # What changes are included in this PR? * revert the offending commits (first 2 commits) * 6bdb70a reverts 50de226 which is all of #1001 * e928513 reverts 015b7e3 from #998 * rewrite the remaining code in #998 alternatively (last commit) # Are these changes tested? * Yes, existing tests pass, including the new test that #998 introduced to cover the bug
2 parents 50de226 + 7e6d3fc commit 4be343b

File tree

3 files changed

+37
-85
lines changed

3 files changed

+37
-85
lines changed

crates/proof-of-sql/src/base/database/table_ref.rs

Lines changed: 30 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,9 @@ use sqlparser::ast::Ident;
1111

1212
/// Expression for an SQL table
1313
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
14-
pub enum TableRef {
15-
/// Fully qualified table reference with schema and table name
16-
FullyQualified {
17-
/// Schema name
18-
schema_name: Ident,
19-
/// Table name
20-
table_name: Ident,
21-
},
22-
/// Table reference without schema
23-
TableOnly {
24-
/// Table name
25-
table_name: Ident,
26-
},
27-
/// No table reference
28-
None,
14+
pub struct TableRef {
15+
schema_name: Option<Ident>,
16+
table_name: Ident,
2917
}
3018

3119
impl TableRef {
@@ -36,62 +24,45 @@ impl TableRef {
3624
let schema = schema_name.as_ref();
3725
let table = table_name.as_ref();
3826

39-
if schema.is_empty() {
40-
Self::TableOnly {
41-
table_name: Ident::new(table.to_string()),
42-
}
43-
} else {
44-
Self::FullyQualified {
45-
schema_name: Ident::new(schema.to_string()),
46-
table_name: Ident::new(table.to_string()),
47-
}
27+
Self {
28+
schema_name: if schema.is_empty() {
29+
None
30+
} else {
31+
Some(Ident::new(schema.to_string()))
32+
},
33+
table_name: Ident::new(table.to_string()),
4834
}
4935
}
5036

51-
/// Returns the identifier of the schema if it exists. Otherwise returns `None`.
37+
/// Returns the identifier of the schema
38+
/// # Panics
5239
#[must_use]
5340
pub fn schema_id(&self) -> Option<&Ident> {
54-
match self {
55-
Self::FullyQualified { schema_name, .. } => Some(schema_name),
56-
Self::TableOnly { .. } | Self::None => None,
57-
}
41+
self.schema_name.as_ref()
5842
}
5943

60-
/// Returns the identifier of the table if it exists. Otherwise returns `None`.
44+
/// Returns the identifier of the table
45+
/// # Panics
6146
#[must_use]
62-
pub fn table_id(&self) -> Option<&Ident> {
63-
match self {
64-
Self::FullyQualified { table_name, .. } | Self::TableOnly { table_name } => {
65-
Some(table_name)
66-
}
67-
Self::None => None,
68-
}
47+
pub fn table_id(&self) -> &Ident {
48+
&self.table_name
6949
}
7050

7151
/// Creates a new table reference from an optional schema and table name.
7252
#[must_use]
7353
pub fn from_names(schema_name: Option<&str>, table_name: &str) -> Self {
74-
if let Some(schema) = schema_name {
75-
Self::FullyQualified {
76-
schema_name: Ident::new(schema.to_string()),
77-
table_name: Ident::new(table_name.to_string()),
78-
}
79-
} else {
80-
Self::TableOnly {
81-
table_name: Ident::new(table_name.to_string()),
82-
}
54+
Self {
55+
schema_name: schema_name.map(|s| Ident::new(s.to_string())),
56+
table_name: Ident::new(table_name.to_string()),
8357
}
8458
}
8559

8660
/// Creates a `TableRef` directly from `Option<Ident>` for schema and `Ident` for table.
8761
#[must_use]
8862
pub fn from_idents(schema_name: Option<Ident>, table_name: Ident) -> Self {
89-
match schema_name {
90-
Some(schema) => Self::FullyQualified {
91-
schema_name: schema,
92-
table_name,
93-
},
94-
None => Self::TableOnly { table_name },
63+
Self {
64+
schema_name,
65+
table_name,
9566
}
9667
}
9768

@@ -133,8 +104,8 @@ impl TryFrom<&str> for TableRef {
133104
/// Note: We just need this conversion trait until `SelectStatement` refactor is done
134105
impl From<ResourceId> for TableRef {
135106
fn from(id: ResourceId) -> Self {
136-
Self::FullyQualified {
137-
schema_name: Ident::from(id.schema()),
107+
TableRef {
108+
schema_name: Some(Ident::from(id.schema())),
138109
table_name: Ident::from(id.object_name()),
139110
}
140111
}
@@ -150,35 +121,16 @@ impl FromStr for TableRef {
150121

151122
impl Equivalent<TableRef> for &TableRef {
152123
fn equivalent(&self, key: &TableRef) -> bool {
153-
match (self, key) {
154-
(
155-
TableRef::FullyQualified {
156-
schema_name: s1,
157-
table_name: t1,
158-
},
159-
TableRef::FullyQualified {
160-
schema_name: s2,
161-
table_name: t2,
162-
},
163-
) => s1 == s2 && t1 == t2,
164-
(TableRef::TableOnly { table_name: t1 }, TableRef::TableOnly { table_name: t2 }) => {
165-
t1 == t2
166-
}
167-
(TableRef::None, TableRef::None) => true,
168-
_ => false,
169-
}
124+
self.schema_name == key.schema_name && self.table_name == key.table_name
170125
}
171126
}
172127

173128
impl Display for TableRef {
174129
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
175-
match self {
176-
TableRef::FullyQualified {
177-
schema_name,
178-
table_name,
179-
} => write!(f, "{}.{}", schema_name.value, table_name.value),
180-
TableRef::TableOnly { table_name } => write!(f, "{}", table_name.value),
181-
TableRef::None => write!(f, "<no_table>"),
130+
if let Some(schema) = &self.schema_name {
131+
write!(f, "{}.{}", schema.value, self.table_name.value)
132+
} else {
133+
write!(f, "{}", self.table_name.value)
182134
}
183135
}
184136
}

crates/proof-of-sql/src/sql/evm_proof_plan/exprs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl EVMColumnExpr {
153153
.get_index_of(expr.column_ref())
154154
.or_else(|| {
155155
column_refs.get_index_of(&ColumnRef::new(
156-
TableRef::None,
156+
TableRef::from_names(None, ""),
157157
expr.column_id(),
158158
expr.data_type(),
159159
))

crates/proof-of-sql/src/sql/evm_proof_plan/plans.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl EVMProjectionExec {
241241
.input()
242242
.get_column_result_fields()
243243
.into_iter()
244-
.map(|f| ColumnRef::new(TableRef::None, f.name(), f.data_type()))
244+
.map(|f| ColumnRef::new(TableRef::from_names(None, ""), f.name(), f.data_type()))
245245
.collect();
246246
Ok(Self {
247247
input_plan: Box::new(EVMDynProofPlan::try_from_proof_plan(
@@ -271,7 +271,7 @@ impl EVMProjectionExec {
271271
let input_result_column_refs = input
272272
.get_column_result_fields()
273273
.into_iter()
274-
.map(|f| ColumnRef::new(TableRef::None, f.name(), f.data_type()))
274+
.map(|f| ColumnRef::new(TableRef::from_names(None, ""), f.name(), f.data_type()))
275275
.collect();
276276
Ok(ProjectionExec::new(
277277
self.results
@@ -1192,12 +1192,12 @@ mod tests {
11921192
expr: DynProofExpr::Add(
11931193
AddExpr::try_new(
11941194
Box::new(DynProofExpr::Column(ColumnExpr::new(ColumnRef::new(
1195-
TableRef::None,
1195+
TableRef::from_names(None, ""),
11961196
ident_a.clone(),
11971197
ColumnType::BigInt,
11981198
)))),
11991199
Box::new(DynProofExpr::Column(ColumnExpr::new(ColumnRef::new(
1200-
TableRef::None,
1200+
TableRef::from_names(None, ""),
12011201
ident_b.clone(),
12021202
ColumnType::BigInt,
12031203
)))),
@@ -1216,12 +1216,12 @@ mod tests {
12161216
expr: DynProofExpr::Add(
12171217
AddExpr::try_new(
12181218
Box::new(DynProofExpr::Column(ColumnExpr::new(ColumnRef::new(
1219-
TableRef::None,
1219+
TableRef::from_names(None, ""),
12201220
ident_a.clone(),
12211221
ColumnType::BigInt,
12221222
)))),
12231223
Box::new(DynProofExpr::Column(ColumnExpr::new(ColumnRef::new(
1224-
TableRef::None,
1224+
TableRef::from_names(None, ""),
12251225
ident_b.clone(),
12261226
ColumnType::BigInt,
12271227
)))),

0 commit comments

Comments
 (0)