order by refactoring
This commit is contained in:
20
usql.cpp
20
usql.cpp
@@ -208,11 +208,8 @@ void USql::execute_order_by(SelectFromTableNode &node, Table *table, Table *resu
|
||||
ColValue *a_val = a.ith_column(col_def.order);
|
||||
ColValue *b_val = b.ith_column(col_def.order);
|
||||
|
||||
if (a_val->isNull() && b_val->isNull()) return true; // both is null so a goes to end
|
||||
if (!a_val->isNull() && b_val->isNull()) return true; // b is null so goes to end
|
||||
if (a_val->isNull() && !b_val->isNull()) return false; // a is null so goes to end
|
||||
int compare = a_val->compare(b_val);
|
||||
|
||||
int compare = compare_col_values(col_def, a_val, b_val);
|
||||
if (compare < 0) return order_by_col_def.ascending ? true : false;
|
||||
if (compare > 0) return order_by_col_def.ascending ? false : true;
|
||||
}
|
||||
@@ -231,21 +228,6 @@ void USql::execute_offset_limit(OffsetLimitNode &node, Table *result) const {
|
||||
result->m_rows.erase(result->m_rows.begin() + node.limit, result->m_rows.end());
|
||||
}
|
||||
|
||||
int USql::compare_col_values(const ColDefNode &col_def, ColValue *a_val, ColValue *b_val) const {
|
||||
double c;
|
||||
switch (col_def.type) {
|
||||
case (ColumnType::integer_type):
|
||||
return a_val->getIntValue() - b_val->getIntValue();
|
||||
case (ColumnType::float_type):
|
||||
c = a_val->getDoubleValue() - b_val->getDoubleValue();
|
||||
return c < 0 ? -1 : c==0.0 ? 0 : 1;
|
||||
case (ColumnType::varchar_type):
|
||||
return a_val->getStringValue().compare(b_val->getStringValue());
|
||||
default:
|
||||
throw Exception("Unsupported data type");
|
||||
}
|
||||
}
|
||||
|
||||
std::tuple<int, ColDefNode> USql::get_column_definition(Table *table, SelectColNode *select_col_node, int col_order ) {
|
||||
std::string new_col_name = select_col_node->name;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user