Skip to content

Commit

Permalink
Merge pull request #204 from jussisaurio/multi-way-join
Browse files Browse the repository at this point in the history
Refactor join processing / support multiway joins
  • Loading branch information
penberg authored Jul 24, 2024
2 parents 6946505 + 9b75601 commit 1cdd1f8
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 399 deletions.
31 changes: 13 additions & 18 deletions core/translate/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ pub fn build_select<'a>(schema: &Schema, select: &'a ast::Select) -> Result<Sele
Some(table) => table,
None => anyhow::bail!("Parse error: no such table: {}", table_name),
};
let identifier = normalize_ident(maybe_alias.unwrap_or(table_name));
let mut joins = Vec::new();
joins.push(SrcTable {
table: Table::BTree(table.clone()),
alias: maybe_alias,
identifier,
join_info: None,
});
if let Some(selected_joins) = &from.joins {
Expand All @@ -60,9 +61,11 @@ pub fn build_select<'a>(schema: &Schema, select: &'a ast::Select) -> Result<Sele
Some(table) => table,
None => anyhow::bail!("Parse error: no such table: {}", table_name),
};
let identifier = normalize_ident(maybe_alias.unwrap_or(table_name));

joins.push(SrcTable {
table: Table::BTree(table),
alias: maybe_alias,
identifier,
join_info: Some(join),
});
}
Expand Down Expand Up @@ -293,7 +296,7 @@ pub fn translate_expr(
};
for arg in args {
let reg = program.alloc_register();
let _ = translate_expr(program, select, &arg, reg, cursor_hint)?;
let _ = translate_expr(program, select, arg, reg, cursor_hint)?;
match arg {
ast::Expr::Literal(_) => program.mark_last_insn_constant(),
_ => {}
Expand Down Expand Up @@ -652,31 +655,27 @@ fn wrap_eval_jump_expr(
program.preassign_label_to_next_insn(if_true_label);
}

pub fn resolve_ident_qualified<'a>(
pub fn resolve_ident_qualified(
program: &ProgramBuilder,
table_name: &String,
ident: &String,
select: &'a Select,
select: &Select,
cursor_hint: Option<usize>,
) -> Result<(usize, Type, usize, bool)> {
let ident = normalize_ident(ident);
let table_name = normalize_ident(table_name);
for join in &select.src_tables {
match join.table {
Table::BTree(ref table) => {
let table_identifier = normalize_ident(match join.alias {
Some(alias) => alias,
None => &table.name,
});
if table_identifier == *table_name {
if *join.identifier == table_name {
let res = table
.columns
.iter()
.enumerate()
.find(|(_, col)| col.name == *ident);
if res.is_some() {
let (idx, col) = res.unwrap();
let cursor_id = program.resolve_cursor_id(&table_identifier, cursor_hint);
let cursor_id = program.resolve_cursor_id(&join.identifier, cursor_hint);
return Ok((idx, col.ty, cursor_id, col.primary_key));
}
}
Expand All @@ -691,21 +690,17 @@ pub fn resolve_ident_qualified<'a>(
);
}

pub fn resolve_ident_table<'a>(
pub fn resolve_ident_table(
program: &ProgramBuilder,
ident: &String,
select: &'a Select,
select: &Select,
cursor_hint: Option<usize>,
) -> Result<(usize, Type, usize, bool)> {
let ident = normalize_ident(ident);
let mut found = Vec::new();
for join in &select.src_tables {
match join.table {
Table::BTree(ref table) => {
let table_identifier = normalize_ident(match join.alias {
Some(alias) => alias,
None => &table.name,
});
let res = table
.columns
.iter()
Expand All @@ -731,7 +726,7 @@ pub fn resolve_ident_table<'a>(
is_primary_key = res.1.primary_key;
}
}
let cursor_id = program.resolve_cursor_id(&table_identifier, cursor_hint);
let cursor_id = program.resolve_cursor_id(&join.identifier, cursor_hint);
found.push((idx, col_type, cursor_id, is_primary_key));
}
}
Expand Down
Loading

0 comments on commit 1cdd1f8

Please sign in to comment.