ladybird/Userland/Libraries/LibSQL/AST/Select.cpp
Jan de Visser 7fc901d1b3 LibSQL+SQLServer: Implement first cut of SELECT ... ORDER BY foo
Ordering is done by replacing the straight Vector holding the query
result in the SQLResult object with a dedicated Vector subclass that
inserts result rows according to their sort key using a binary search.
This is done in the ResultSet class.

There are limitations:
- "SELECT ... ORDER BY 1" (or 2 or 3 etc) is supposed to sort by the
n-th result column. This doesn't work yet
- "SELECT ... column-expression alias ... ORDER BY alias" is supposed to
sort by the column with the given alias. This doesn't work yet

What does work however is something like
```SELECT foo FROM bar SORT BY quux```
i.e. sorted by a column not in the result set. Once functions are
supported it should be possible to sort by random functions.
2022-01-16 11:17:15 +01:00

120 lines
4.9 KiB
C++

/*
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibSQL/AST/AST.h>
#include <LibSQL/Database.h>
#include <LibSQL/Meta.h>
#include <LibSQL/ResultSet.h>
#include <LibSQL/Row.h>
namespace SQL::AST {
RefPtr<SQLResult> Select::execute(ExecutionContext& context) const
{
NonnullRefPtrVector<ResultColumn> columns;
for (auto& table_descriptor : table_or_subquery_list()) {
if (!table_descriptor.is_table())
return SQLResult::construct(SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented");
auto table_def_or_error = context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name());
if (table_def_or_error.is_error())
return SQLResult::construct(SQLCommand::Select, SQLErrorCode::InternalError, table_def_or_error.error());
auto table = table_def_or_error.value();
if (!table) {
return SQLResult::construct(SQLCommand::Select, SQLErrorCode::TableDoesNotExist, table_descriptor.table_name());
}
if (result_column_list().size() == 1 && result_column_list()[0].type() == ResultType::All) {
for (auto& col : table->columns()) {
columns.append(
create_ast_node<ResultColumn>(
create_ast_node<ColumnNameExpression>(table->parent()->name(), table->name(), col.name()),
""));
}
}
}
VERIFY(!result_column_list().is_empty());
if (result_column_list().size() != 1 || result_column_list()[0].type() != ResultType::All) {
for (auto& col : result_column_list()) {
if (col.type() == ResultType::All)
// FIXME can have '*' for example in conjunction with computed columns
return SQLResult::construct(SQL::SQLCommand::Select, SQLErrorCode::SyntaxError, "*");
columns.append(col);
}
}
context.result = SQLResult::construct();
AK::NonnullRefPtr<TupleDescriptor> descriptor = AK::adopt_ref(*new TupleDescriptor);
Tuple tuple(descriptor);
Vector<Tuple> rows;
descriptor->empend("__unity__");
tuple.append(Value(SQLType::Boolean, true));
rows.append(tuple);
for (auto& table_descriptor : table_or_subquery_list()) {
if (!table_descriptor.is_table())
return SQLResult::construct(SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented");
auto table_def_or_error = context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name());
if (table_def_or_error.is_error())
return SQLResult::construct(SQLCommand::Select, SQLErrorCode::InternalError, table_def_or_error.error());
auto table = table_def_or_error.value();
if (table->num_columns() == 0)
continue;
auto old_descriptor_size = descriptor->size();
descriptor->extend(table->to_tuple_descriptor());
for (auto cartesian_row = rows.first(); cartesian_row.size() == old_descriptor_size; cartesian_row = rows.first()) {
rows.remove(0);
auto table_rows_or_error = context.database->select_all(*table);
if (table_rows_or_error.is_error())
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::InternalError, table_rows_or_error.error());
for (auto& table_row : table_rows_or_error.value()) {
auto new_row = cartesian_row;
new_row.extend(table_row);
rows.append(new_row);
}
}
}
bool has_ordering { false };
AK::NonnullRefPtr<TupleDescriptor> sort_descriptor = AK::adopt_ref(*new TupleDescriptor);
for (auto& term : m_ordering_term_list) {
sort_descriptor->append(TupleElementDescriptor { .order = term.order() });
has_ordering = true;
}
Tuple sort_key(sort_descriptor);
for (auto& row : rows) {
context.current_row = &row;
if (where_clause()) {
auto where_result = where_clause()->evaluate(context);
if (context.result->has_error())
return context.result;
if (!where_result)
continue;
}
tuple.clear();
for (auto& col : columns) {
auto value = col.expression()->evaluate(context);
if (context.result->has_error())
return context.result;
tuple.append(value);
}
if (has_ordering) {
sort_key.clear();
for (auto& term : m_ordering_term_list) {
auto value = term.expression()->evaluate(context);
if (context.result->has_error())
return context.result;
sort_key.append(value);
}
}
context.result->insert(tuple, sort_key);
}
return context.result;
}
}