Skip to content

Commit 6b58d12

Browse files
committed
implement get_attribute
1 parent cb2b63e commit 6b58d12

File tree

5 files changed

+116
-7
lines changed

5 files changed

+116
-7
lines changed

optd-cost-model/src/common/types.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,33 @@ impl Display for EpochId {
4949
write!(f, "Epoch#{}", self.0)
5050
}
5151
}
52+
53+
impl From<GroupId> for i32 {
54+
fn from(id: GroupId) -> i32 {
55+
id.0 as i32
56+
}
57+
}
58+
59+
impl From<ExprId> for i32 {
60+
fn from(id: ExprId) -> i32 {
61+
id.0 as i32
62+
}
63+
}
64+
65+
impl From<TableId> for i32 {
66+
fn from(id: TableId) -> i32 {
67+
id.0 as i32
68+
}
69+
}
70+
71+
impl From<AttrId> for i32 {
72+
fn from(id: AttrId) -> i32 {
73+
id.0 as i32
74+
}
75+
}
76+
77+
impl From<EpochId> for i32 {
78+
fn from(id: EpochId) -> i32 {
79+
id.0 as i32
80+
}
81+
}

optd-cost-model/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub enum SemanticError {
4040
// TODO: Add more error types
4141
UnknownStatisticType,
4242
VersionedStatisticNotFound,
43-
AttributeNotFound,
43+
AttributeNotFound(TableId, i32), // (table_id, attribute_base_index)
4444
}
4545

4646
#[derive(Debug)]
@@ -50,6 +50,12 @@ pub enum CostModelError {
5050
SemanticError(SemanticError),
5151
}
5252

53+
impl From<BackendError> for CostModelError {
54+
fn from(err: BackendError) -> Self {
55+
CostModelError::ORMError(err)
56+
}
57+
}
58+
5359
pub trait CostModel: 'static + Send + Sync {
5460
/// TODO: documentation
5561
fn compute_operation_cost(

optd-cost-model/src/storage.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
1+
#![allow(unused_variables)]
12
use std::sync::Arc;
23

3-
use optd_persistent::CostModelStorageLayer;
4+
use optd_persistent::{cost_model::interface::Attr, BackendManager, CostModelStorageLayer};
5+
6+
use crate::{
7+
common::types::TableId, stats::AttributeCombValueStats, CostModelError, CostModelResult,
8+
SemanticError,
9+
};
410

511
/// TODO: documentation
612
pub struct CostModelStorageManager<S: CostModelStorageLayer> {
713
pub backend_manager: Arc<S>,
814
// TODO: in-memory cache
915
}
1016

11-
impl<S: CostModelStorageLayer> CostModelStorageManager<S> {
12-
/// TODO: documentation
13-
pub fn new(backend_manager: Arc<S>) -> Self {
17+
impl CostModelStorageManager<BackendManager> {
18+
pub fn new(backend_manager: Arc<BackendManager>) -> Self {
1419
Self { backend_manager }
1520
}
21+
22+
/// TODO: documentation
23+
/// TODO: if we have memory cache,
24+
/// we should add the reference. (&Field)
25+
pub async fn get_attribute_info(
26+
&self,
27+
table_id: TableId,
28+
attribute_base_index: i32,
29+
) -> CostModelResult<Attr> {
30+
let attr = self
31+
.backend_manager
32+
.get_attribute(table_id.into(), attribute_base_index)
33+
.await?;
34+
attr.ok_or_else(|| {
35+
CostModelError::SemanticError(SemanticError::AttributeNotFound(
36+
table_id,
37+
attribute_base_index,
38+
))
39+
})
40+
}
41+
42+
/// TODO: documentation
43+
/// TODO: if we have memory cache,
44+
/// we should add the reference. (&AttributeCombValueStats)
45+
pub fn get_attributes_comb_statistics(
46+
&self,
47+
table_id: TableId,
48+
attr_comb: &[usize],
49+
) -> CostModelResult<Option<AttributeCombValueStats>> {
50+
todo!()
51+
}
1652
}

optd-persistent/src/cost_model/interface.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![allow(dead_code, unused_imports)]
22

3-
use crate::entities::attribute::Model as AttributeEntity;
43
use crate::entities::cascades_group;
54
use crate::entities::logical_expression;
65
use crate::entities::physical_expression;
@@ -83,6 +82,16 @@ pub struct Cost {
8382
pub estimated_statistic: i32,
8483
}
8584

85+
#[derive(Clone, Debug)]
86+
pub struct Attr {
87+
pub table_id: i32,
88+
pub name: String,
89+
pub compression_method: String,
90+
pub attr_type: i32,
91+
pub base_index: i32,
92+
pub nullable: bool,
93+
}
94+
8695
/// TODO: documentation
8796
#[trait_variant::make(Send)]
8897
pub trait CostModelStorageLayer {
@@ -133,4 +142,10 @@ pub trait CostModelStorageLayer {
133142
) -> StorageResult<Option<Cost>>;
134143

135144
async fn get_cost(&self, expr_id: ExprId) -> StorageResult<Option<Cost>>;
145+
146+
async fn get_attribute(
147+
&self,
148+
table_id: TableId,
149+
attribute_base_index: i32,
150+
) -> StorageResult<Option<Attr>>;
136151
}

optd-persistent/src/cost_model/orm.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use sea_orm::{
1313
use serde_json::json;
1414

1515
use super::catalog::mock_catalog::{self, MockCatalog};
16-
use super::interface::{AttrId, CatalogSource, EpochId, EpochOption, ExprId, Stat, StatId};
16+
use super::interface::{
17+
Attr, AttrId, CatalogSource, EpochId, EpochOption, ExprId, Stat, StatId, TableId,
18+
};
1719

1820
impl BackendManager {
1921
fn get_description_from_attr_ids(&self, attr_ids: Vec<AttrId>) -> String {
@@ -505,6 +507,26 @@ impl CostModelStorageLayer for BackendManager {
505507
let _ = PlanCost::insert(new_cost).exec(&self.db).await?;
506508
Ok(())
507509
}
510+
511+
async fn get_attribute(
512+
&self,
513+
table_id: TableId,
514+
attribute_base_index: i32,
515+
) -> StorageResult<Option<Attr>> {
516+
Ok(Attribute::find()
517+
.filter(attribute::Column::TableId.eq(table_id))
518+
.filter(attribute::Column::BaseAttributeNumber.eq(attribute_base_index))
519+
.one(&self.db)
520+
.await?
521+
.map(|attr| Attr {
522+
table_id,
523+
name: attr.name,
524+
compression_method: attr.compression_method,
525+
attr_type: attr.variant_tag,
526+
base_index: attribute_base_index,
527+
nullable: !attr.is_not_null,
528+
}))
529+
}
508530
}
509531

510532
#[cfg(test)]

0 commit comments

Comments
 (0)