order by refactoring
This commit is contained in:
parent
70c036f08c
commit
4a344d0e77
20
row.cpp
20
row.cpp
|
|
@ -3,6 +3,26 @@
|
||||||
|
|
||||||
namespace usql {
|
namespace usql {
|
||||||
|
|
||||||
|
int ColNullValue::compare(ColValue * other) {
|
||||||
|
return other->isNull() ? 0 : -1; // null goes to end
|
||||||
|
}
|
||||||
|
|
||||||
|
int ColIntegerValue::compare(ColValue * other) {
|
||||||
|
return other->isNull() ? 1 : m_integer - other->getIntValue(); // null goes to end
|
||||||
|
}
|
||||||
|
|
||||||
|
int ColDoubleValue::compare(ColValue * other) {
|
||||||
|
if (other->isNull()) { // null goes to end
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
double c = m_double - other->getDoubleValue();
|
||||||
|
return c < 0 ? -1 : c == 0.0 ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ColStringValue::compare(ColValue * other) {
|
||||||
|
return other->isNull() ? 1 : m_string.compare(other->getStringValue()); // null goes to end
|
||||||
|
}
|
||||||
|
|
||||||
Row::Row(int cols_count) {
|
Row::Row(int cols_count) {
|
||||||
m_columns.reserve(cols_count);
|
m_columns.reserve(cols_count);
|
||||||
for (int i = 0; i < cols_count; i++) {
|
for (int i = 0; i < cols_count; i++) {
|
||||||
|
|
|
||||||
46
row.h
46
row.h
|
|
@ -11,16 +11,24 @@ namespace usql {
|
||||||
struct ColValue {
|
struct ColValue {
|
||||||
|
|
||||||
virtual bool isNull() { return false; };
|
virtual bool isNull() { return false; };
|
||||||
virtual long getIntValue() { throw Exception("Not supported"); };
|
virtual long getIntValue() = 0;
|
||||||
virtual double getDoubleValue() { throw Exception("Not supported"); };
|
virtual double getDoubleValue() = 0;
|
||||||
virtual std::string getStringValue() { throw Exception("Not supported"); };
|
virtual std::string getStringValue() = 0;
|
||||||
|
|
||||||
|
virtual int compare(ColValue * other) = 0;
|
||||||
|
|
||||||
|
virtual ~ColValue() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct ColNullValue : ColValue {
|
struct ColNullValue : ColValue {
|
||||||
|
|
||||||
virtual bool isNull() { return true; };
|
bool isNull() override { return true; };
|
||||||
virtual std::string getStringValue() { return "null"; };
|
long getIntValue() override { throw Exception("Not supported"); };
|
||||||
|
double getDoubleValue() override { throw Exception("Not supported"); };
|
||||||
|
std::string getStringValue() override { return "null"; };
|
||||||
|
|
||||||
|
int compare(ColValue * other) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -29,11 +37,13 @@ namespace usql {
|
||||||
ColIntegerValue(long value) : m_integer(value) {};
|
ColIntegerValue(long value) : m_integer(value) {};
|
||||||
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {};
|
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {};
|
||||||
|
|
||||||
virtual long getIntValue() { return m_integer; };
|
long getIntValue() override { return m_integer; };
|
||||||
virtual double getDoubleValue() { return (double) m_integer; };
|
double getDoubleValue() override { return (double) m_integer; };
|
||||||
virtual std::string getStringValue() { return std::to_string(m_integer); };
|
std::string getStringValue() override { return std::to_string(m_integer); };
|
||||||
|
|
||||||
int m_integer;
|
int compare(ColValue * other) override;
|
||||||
|
|
||||||
|
long m_integer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -42,9 +52,11 @@ namespace usql {
|
||||||
ColDoubleValue(double value) : m_double(value) {};
|
ColDoubleValue(double value) : m_double(value) {};
|
||||||
ColDoubleValue(const ColDoubleValue &other) : m_double(other.m_double) {}
|
ColDoubleValue(const ColDoubleValue &other) : m_double(other.m_double) {}
|
||||||
|
|
||||||
virtual long getIntValue() { return (long) m_double; };
|
long getIntValue() override { return (long) m_double; };
|
||||||
virtual double getDoubleValue() { return m_double; };
|
double getDoubleValue() override { return m_double; };
|
||||||
virtual std::string getStringValue() { return std::to_string(m_double); };
|
std::string getStringValue() override { return std::to_string(m_double); };
|
||||||
|
|
||||||
|
int compare(ColValue * other) override;
|
||||||
|
|
||||||
double m_double;
|
double m_double;
|
||||||
};
|
};
|
||||||
|
|
@ -52,12 +64,14 @@ namespace usql {
|
||||||
|
|
||||||
struct ColStringValue : ColValue {
|
struct ColStringValue : ColValue {
|
||||||
|
|
||||||
ColStringValue(const std::string value) : m_string(value) {};
|
ColStringValue(const std::string &value) : m_string(value) {};
|
||||||
ColStringValue(const ColStringValue &other) : m_string(other.m_string) {};
|
ColStringValue(const ColStringValue &other) : m_string(other.m_string) {};
|
||||||
|
|
||||||
virtual long getIntValue() { return std::stoi(m_string); };
|
long getIntValue() override { return std::stoi(m_string); };
|
||||||
virtual double getDoubleValue() { return std::stod(m_string); };
|
double getDoubleValue() override { return std::stod(m_string); };
|
||||||
virtual std::string getStringValue() { return m_string; };
|
std::string getStringValue() override { return m_string; };
|
||||||
|
|
||||||
|
int compare(ColValue * other) override;
|
||||||
|
|
||||||
std::string m_string;
|
std::string m_string;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
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 *a_val = a.ith_column(col_def.order);
|
||||||
ColValue *b_val = b.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
|
int compare = a_val->compare(b_val);
|
||||||
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 = 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 ? true : false;
|
||||||
if (compare > 0) return order_by_col_def.ascending ? false : true;
|
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());
|
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::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;
|
std::string new_col_name = select_col_node->name;
|
||||||
|
|
||||||
|
|
|
||||||
2
usql.h
2
usql.h
|
|
@ -55,8 +55,6 @@ private:
|
||||||
Parser m_parser;
|
Parser m_parser;
|
||||||
std::list<Table> m_tables;
|
std::list<Table> m_tables;
|
||||||
|
|
||||||
int compare_col_values(const ColDefNode &col_def, ColValue *a_val, ColValue *b_val) const;
|
|
||||||
|
|
||||||
void execute_order_by(SelectFromTableNode &node, Table *table, Table *result) const;
|
void execute_order_by(SelectFromTableNode &node, Table *table, Table *result) const;
|
||||||
|
|
||||||
void execute_offset_limit(OffsetLimitNode &node, Table *result) const;
|
void execute_offset_limit(OffsetLimitNode &node, Table *result) const;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue