1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:38:11 +00:00

LibSQL: Implement table joins

This patch introduces table joins. It uses a pretty dumb algorithm-
starting with a singleton '__unity__' row consisting of a single boolean
value, a cartesian product of all tables in the 'FROM' clause is built.
This cartesian product is then filtered through the 'WHERE' clause,
again without any smarts just using brute force.

This patch required a bunch of busy work to allow for example the
ColumnNameExpression having to deal with multiple tables potentially
having columns with the same name.
This commit is contained in:
Jan de Visser 2021-11-02 16:49:54 -04:00 committed by Andreas Kling
parent 87f4c1677b
commit 3425730294
4 changed files with 175 additions and 50 deletions

View file

@ -170,11 +170,21 @@ Value ColumnNameExpression::evaluate(ExecutionContext& context) const
{
auto& descriptor = *context.current_row->descriptor();
VERIFY(context.current_row->size() == descriptor.size());
Optional<size_t> index_in_row;
for (auto ix = 0u; ix < context.current_row->size(); ix++) {
auto& column_descriptor = descriptor[ix];
if (column_descriptor.name == column_name())
return { (*context.current_row)[ix] };
if (!table_name().is_empty() && column_descriptor.table != table_name())
continue;
if (column_descriptor.name == column_name()) {
if (index_in_row.has_value()) {
context.result->set_error(SQLErrorCode::AmbiguousColumnName, column_name());
return Value::null();
}
index_in_row = ix;
}
}
if (index_in_row.has_value())
return (*context.current_row)[index_in_row.value()];
context.result->set_error(SQLErrorCode::ColumnDoesNotExist, column_name());
return Value::null();
}