Skip to content

Commit

Permalink
fix grafting
Browse files Browse the repository at this point in the history
  • Loading branch information
zorancv committed May 31, 2024
1 parent b53a561 commit 668b7c4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 52 deletions.
20 changes: 10 additions & 10 deletions store/postgres/src/deployment_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,16 +2104,16 @@ impl IndexList {
let mut arr = vec![];
if let Some(vec) = self.indexes.get(table_name) {
for ci in vec {
if ci.index_correct(dest_table) {
if !ci.is_constraint() && !ci.is_pkey() && !ci.is_imm_id(dest_table.immutable) {
if postponed == ci.to_postpone() {
if let Ok(sql) = ci
.with_nsp(namespace.clone())
.to_sql(concurent_if_not_exist, concurent_if_not_exist)
{
arr.push((ci.name(), sql))
}
}
if ci.fields_exist_in_dest(dest_table)
&& !ci.is_default_non_attr_index()
&& !ci.is_id_immutable_table(dest_table.immutable)
&& postponed == ci.to_postpone()
{
if let Ok(sql) = ci
.with_nsp(namespace.clone())
.to_sql(concurent_if_not_exist, concurent_if_not_exist)
{
arr.push((ci.name(), sql))
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions store/postgres/src/relational/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ impl Table {
out: &mut String,
) -> fmt::Result {
self.create_table(out)?;
self.create_time_travel_indexes(catalog, out)?;
if index_def.is_some() && ENV_VARS.postpone_attribute_index_creation {
let arr = index_def.unwrap().indexes_for_table(
&self.namespace.to_string(),
Expand All @@ -401,12 +402,10 @@ impl Table {
for (_, sql) in arr {
writeln!(out, "{};", sql).expect("properly formated index statements")
}
Ok(())
} else {
self.create_time_travel_indexes(catalog, out)?;
self.create_attribute_indexes(out)?;
self.create_aggregate_indexes(schema, out)
}
self.create_aggregate_indexes(schema, out)
}

pub fn exclusion_ddl(&self, out: &mut String) -> fmt::Result {
Expand Down
71 changes: 32 additions & 39 deletions store/postgres/src/relational/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ impl Expr {
}
}

fn is_same_kind_columns(current: &Vec<Expr>, orig: &Vec<Expr>) -> bool {
if orig.len() != current.len() {
return false;
}
for i in 0..orig.len() {
let o = orig[i].to_sql();
let n = current[i].to_sql();

// check that string n starts with o
if n.len() < o.len() || n[0..o.len()] != o {
return false;
}
}
true
}

fn to_sql(&self) -> String {
match self {
Expr::Column(name) => name.to_string(),
Expand Down Expand Up @@ -471,8 +487,7 @@ impl CreateIndex {
}
}

/// Return `true` if `self` is one of the indexes we create by default
pub fn is_default_index(&self) -> bool {
pub fn is_default_non_attr_index(&self) -> bool {
lazy_static! {
static ref DEFAULT_INDEXES: Vec<CreateIndex> = {
fn dummy(
Expand Down Expand Up @@ -513,7 +528,12 @@ impl CreateIndex {
};
}

self.is_attribute_index() || DEFAULT_INDEXES.iter().any(|idx| self.is_same_index(idx))
DEFAULT_INDEXES.iter().any(|idx| self.is_same_index(idx))
}

/// Return `true` if `self` is one of the indexes we create by default
pub fn is_default_index(&self) -> bool {
self.is_attribute_index() || self.is_default_non_attr_index()
}

fn is_same_index(&self, other: &CreateIndex) -> bool {
Expand Down Expand Up @@ -543,21 +563,21 @@ impl CreateIndex {
) => {
unique == o_unique
&& method == o_method
&& columns == o_columns
&& Expr::is_same_kind_columns(columns, o_columns)
&& cond == o_cond
&& with == o_with
}
}
}

pub fn is_imm_id(&self, immutable: bool) -> bool {
// on imutable tables the id constraint is specified on tabe creation
pub fn is_id_immutable_table(&self, immutable: bool) -> bool {
// on imutable tables the id constraint is specified at table creation
if immutable {
match self {
CreateIndex::Unknown { .. } => (),
CreateIndex::Parsed { columns, .. } => {
if columns.len() == 1 {
if columns[0].to_string() == "id" {
if columns[0].is_id() {
return true;
}
}
Expand All @@ -567,30 +587,11 @@ impl CreateIndex {
false
}

pub fn is_pkey(&self) -> bool {
let suffix = "_pkey";
match self {
CreateIndex::Unknown { .. } => false,
CreateIndex::Parsed {
unique,
name,
columns,
..
} => {
*unique && has_suffix(name, suffix) && columns.len() == 1 && columns[0] == Expr::Vid
}
}
}

pub fn is_constraint(&self) -> bool {
let suffix = format!("{}_excl", BLOCK_RANGE_COLUMN);
match self {
CreateIndex::Unknown { .. } => false,
CreateIndex::Parsed { name, .. } => has_suffix(name, &suffix),
}
}

pub fn to_postpone(&self) -> bool {
fn has_prefix(s: &str, prefix: &str) -> bool {
s.starts_with(prefix)
|| s.ends_with("\"") && s.starts_with(format!("\"{}", prefix).as_str())
}
match self {
CreateIndex::Unknown { .. } => false,
CreateIndex::Parsed {
Expand All @@ -617,7 +618,7 @@ impl CreateIndex {
}
}

pub fn index_correct(&self, dest_table: &Table) -> bool {
pub fn fields_exist_in_dest(&self, dest_table: &Table) -> bool {
fn column_exists(columns: &Vec<String>, column_name: &String) -> bool {
for c in columns.iter() {
if *c == *column_name {
Expand Down Expand Up @@ -727,14 +728,6 @@ impl CreateIndex {
}
}

fn has_suffix(s: &str, suffix: &str) -> bool {
s.ends_with(suffix) || s.starts_with("\"") && s.ends_with(format!("{}\"", suffix).as_str())
}

fn has_prefix(s: &str, prefix: &str) -> bool {
s.starts_with(prefix) || s.ends_with("\"") && s.starts_with(format!("\"{}", prefix).as_str())
}

#[test]
fn parse() {
use Method::*;
Expand Down

0 comments on commit 668b7c4

Please sign in to comment.