Skip to content

Commit

Permalink
新增了null存储功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick16262 committed Jun 12, 2024
1 parent fe0843b commit 3fcdf40
Show file tree
Hide file tree
Showing 17 changed files with 590 additions and 472 deletions.
135 changes: 68 additions & 67 deletions src/observer/common/rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,74 @@ See the Mulan PSL v2 for more details. */
* @enum RC
*/

#define DEFINE_RCS \
DEFINE_RC(SUCCESS) \
DEFINE_RC(NULL_VALUE) \
DEFINE_RC(UNIMPLENMENT) \
DEFINE_RC(SQL_SYNTAX) \
DEFINE_RC(INTERNAL) \
DEFINE_RC(NOMEM) \
DEFINE_RC(NOTFOUND) \
DEFINE_RC(EMPTY) \
DEFINE_RC(FULL) \
DEFINE_RC(EXIST) \
DEFINE_RC(NOT_EXIST) \
DEFINE_RC(BUFFERPOOL_OPEN) \
DEFINE_RC(BUFFERPOOL_NOBUF) \
DEFINE_RC(BUFFERPOOL_INVALID_PAGE_NUM) \
DEFINE_RC(RECORD_OPENNED) \
DEFINE_RC(RECORD_INVALID_RID) \
DEFINE_RC(RECORD_INVALID_KEY) \
DEFINE_RC(RECORD_DUPLICATE_KEY) \
DEFINE_RC(RECORD_NOMEM) \
DEFINE_RC(RECORD_EOF) \
DEFINE_RC(RECORD_NOT_EXIST) \
DEFINE_RC(RECORD_INVISIBLE) \
DEFINE_RC(SCHEMA_DB_EXIST) \
DEFINE_RC(SCHEMA_DB_NOT_EXIST) \
DEFINE_RC(SCHEMA_DB_NOT_OPENED) \
DEFINE_RC(SCHEMA_TABLE_NOT_EXIST) \
DEFINE_RC(SCHEMA_TABLE_EXIST) \
DEFINE_RC(SCHEMA_FIELD_AMBIGUOUS) \
DEFINE_RC(SCHEMA_FIELD_NOT_EXIST) \
DEFINE_RC(SCHEMA_FIELD_MISSING) \
DEFINE_RC(SCHEMA_FIELD_TYPE_MISMATCH) \
DEFINE_RC(SCHEMA_INDEX_NAME_REPEAT) \
DEFINE_RC(SUBQUERY_MORE_THAN_ONE_ROW) \
DEFINE_RC(SUBQUERY_MORE_THAN_ONE_COL) \
DEFINE_RC(ALIAS_NOT_EXIST) \
DEFINE_RC(IOERR_READ) \
DEFINE_RC(IOERR_WRITE) \
DEFINE_RC(IOERR_ACCESS) \
DEFINE_RC(IOERR_OPEN) \
DEFINE_RC(IOERR_DELETE) \
DEFINE_RC(IOERR_CLOSE) \
DEFINE_RC(IOERR_SEEK) \
DEFINE_RC(IOERR_TOO_LONG) \
DEFINE_RC(IOERR_SYNC) \
DEFINE_RC(INCORRECT_DATE_VALUE) \
DEFINE_RC(INCORRECT_DATE_FORMAT) \
DEFINE_RC(INVALID_ARGUMENT) \
DEFINE_RC(INVALID_AGGREGATE) \
DEFINE_RC(INVALID_GROUPING) \
DEFINE_RC(INVALID_FUNCTION_NAME) \
DEFINE_RC(LOCKED_UNLOCK) \
DEFINE_RC(LOCKED_NEED_WAIT) \
DEFINE_RC(LOCKED_CONCURRENCY_CONFLICT) \
DEFINE_RC(FILE_EXIST) \
DEFINE_RC(FILE_NOT_EXIST) \
DEFINE_RC(FILE_NAME) \
DEFINE_RC(FILE_BOUND) \
DEFINE_RC(FILE_CREATE) \
DEFINE_RC(FILE_OPEN) \
DEFINE_RC(FILE_NOT_OPENED) \
DEFINE_RC(FILE_CLOSE) \
DEFINE_RC(FILE_REMOVE) \
DEFINE_RC(VARIABLE_NOT_EXISTS) \
DEFINE_RC(VARIABLE_NOT_VALID) \
DEFINE_RC(LOGBUF_FULL) \
DEFINE_RC(LOG_FILE_FULL) \
#define DEFINE_RCS \
DEFINE_RC(SUCCESS) \
DEFINE_RC(NULL_VALUE) \
DEFINE_RC(UNIMPLENMENT) \
DEFINE_RC(SQL_SYNTAX) \
DEFINE_RC(INTERNAL) \
DEFINE_RC(NOMEM) \
DEFINE_RC(NOTFOUND) \
DEFINE_RC(EMPTY) \
DEFINE_RC(FULL) \
DEFINE_RC(EXIST) \
DEFINE_RC(NOT_EXIST) \
DEFINE_RC(BUFFERPOOL_OPEN) \
DEFINE_RC(BUFFERPOOL_NOBUF) \
DEFINE_RC(BUFFERPOOL_INVALID_PAGE_NUM) \
DEFINE_RC(RECORD_OPENNED) \
DEFINE_RC(RECORD_INVALID_RID) \
DEFINE_RC(RECORD_INVALID_KEY) \
DEFINE_RC(RECORD_DUPLICATE_KEY) \
DEFINE_RC(RECORD_NOMEM) \
DEFINE_RC(RECORD_EOF) \
DEFINE_RC(RECORD_NOT_EXIST) \
DEFINE_RC(RECORD_INVISIBLE) \
DEFINE_RC(SCHEMA_DB_EXIST) \
DEFINE_RC(SCHEMA_DB_NOT_EXIST) \
DEFINE_RC(SCHEMA_DB_NOT_OPENED) \
DEFINE_RC(SCHEMA_TABLE_NOT_EXIST) \
DEFINE_RC(SCHEMA_TABLE_EXIST) \
DEFINE_RC(SCHEMA_FIELD_AMBIGUOUS) \
DEFINE_RC(SCHEMA_FIELD_NOT_EXIST) \
DEFINE_RC(SCHEMA_FIELD_MISSING) \
DEFINE_RC(SCHEMA_FIELD_TYPE_MISMATCH) \
DEFINE_RC(SCHEMA_FIELD_NON_NULL) \
DEFINE_RC(SCHEMA_INDEX_NAME_REPEAT) \
DEFINE_RC(SUBQUERY_MORE_THAN_ONE_ROW) \
DEFINE_RC(SUBQUERY_MORE_THAN_ONE_COL) \
DEFINE_RC(ALIAS_NOT_EXIST) \
DEFINE_RC(IOERR_READ) \
DEFINE_RC(IOERR_WRITE) \
DEFINE_RC(IOERR_ACCESS) \
DEFINE_RC(IOERR_OPEN) \
DEFINE_RC(IOERR_DELETE) \
DEFINE_RC(IOERR_CLOSE) \
DEFINE_RC(IOERR_SEEK) \
DEFINE_RC(IOERR_TOO_LONG) \
DEFINE_RC(IOERR_SYNC) \
DEFINE_RC(INCORRECT_DATE_VALUE) \
DEFINE_RC(INCORRECT_DATE_FORMAT) \
DEFINE_RC(INVALID_ARGUMENT) \
DEFINE_RC(INVALID_AGGREGATE) \
DEFINE_RC(INVALID_GROUPING) \
DEFINE_RC(INVALID_FUNCTION_NAME) \
DEFINE_RC(LOCKED_UNLOCK) \
DEFINE_RC(LOCKED_NEED_WAIT) \
DEFINE_RC(LOCKED_CONCURRENCY_CONFLICT) \
DEFINE_RC(FILE_EXIST) \
DEFINE_RC(FILE_NOT_EXIST) \
DEFINE_RC(FILE_NAME) \
DEFINE_RC(FILE_BOUND) \
DEFINE_RC(FILE_CREATE) \
DEFINE_RC(FILE_OPEN) \
DEFINE_RC(FILE_NOT_OPENED) \
DEFINE_RC(FILE_CLOSE) \
DEFINE_RC(FILE_REMOVE) \
DEFINE_RC(VARIABLE_NOT_EXISTS) \
DEFINE_RC(VARIABLE_NOT_VALID) \
DEFINE_RC(LOGBUF_FULL) \
DEFINE_RC(LOG_FILE_FULL) \
DEFINE_RC(LOG_ENTRY_INVALID)

enum class RC
Expand Down
13 changes: 11 additions & 2 deletions src/observer/sql/expr/tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ See the Mulan PSL v2 for more details. */
#include <utility>
#include <vector>

#include "common/lang/bitmap.h"
#include "common/log/log.h"
#include "sql/expr/expr_type.h"
#include "sql/expr/expression.h"
#include "sql/expr/tuple_cell.h"
#include "sql/parser/value.h"
#include "sql/stmt/table_ref_desc.h"
#include "storage/field/field_meta.h"
#include "storage/record/record.h"

class Table;
Expand Down Expand Up @@ -159,6 +161,13 @@ class RowTuple : public Tuple
return RC::INVALID_ARGUMENT;
}

const FieldMeta *null_bitmap_field_meta = table_->table_meta().null_bitmap_field();
common::Bitmap bitmap(record_->data() + null_bitmap_field_meta->offset(), null_bitmap_field_meta->len() * 8);
if (bitmap.get_bit(index)) {
cell.set_null();
return RC::SUCCESS;
}

FieldExpr *field_expr = fields_[index];
const FieldMeta *field_meta = field_expr->field().meta();
cell.set_type(field_meta->type());
Expand Down Expand Up @@ -408,12 +417,12 @@ class AggregateTuple : public Tuple

const Value &value = aggr_values_[index];

if (aggr_types_[index] == AggregateType::AVG) {
if (aggr_types_[index] == AggregateType::AVG && value.attr_type() != NULLS) {
int count;
float sum;
sscanf(value.get_string().c_str(), "count: %d, sum: %f", &count, &sum);
cell.set_float(sum / count);
} else {
}else {
cell = value;
}

Expand Down
9 changes: 8 additions & 1 deletion src/observer/sql/operator/table_scan_physical_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the Mulan PSL v2 for more details. */

#include "sql/operator/table_scan_physical_operator.h"
#include "event/sql_debug.h"
#include "sql/parser/value.h"
#include "storage/table/table.h"
#include "storage/trx/trx.h"

Expand Down Expand Up @@ -86,7 +87,13 @@ RC TableScanPhysicalOperator::filter(RowTuple &tuple, bool &result)
return rc;
}

bool tmp_result = value.get_boolean();
bool tmp_result;
try {
tmp_result = value.get_boolean();
} catch (null_cast_exception) {
tmp_result = false;
}

if (!tmp_result) {
result = false;
return rc;
Expand Down
6 changes: 4 additions & 2 deletions src/observer/sql/optimizer/physical_plan_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ See the Mulan PSL v2 for more details. */
#include "sql/operator/table_scan_physical_operator.h"
#include "sql/operator/update_physical_operator.h"
#include "sql/optimizer/logical_plan_generator.h"
#include "sql/parser/value.h"
#include "sql/optimizer/physical_plan_generator.h"

using namespace std;
Expand Down Expand Up @@ -115,7 +116,8 @@ RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, u
unique_ptr<Expression> &left_expr = comparison_expr->left();
unique_ptr<Expression> &right_expr = comparison_expr->right();
// 左右比较的一边最少是一个值
if (left_expr->type() != ExprType::VALUE && right_expr->type() != ExprType::VALUE) {
if (!((left_expr->type() == ExprType::VALUE && left_expr->value_type() != NULLS) ||
(right_expr->type() == ExprType::VALUE && right_expr->value_type() != NULLS))) {
continue;
}

Expand Down Expand Up @@ -401,7 +403,7 @@ RC PhysicalPlanGenerator::create_plan(GroupLogicalOperator &logical_oper, std::u
return rc;
}

oper = std::make_unique< GroupPhysicalOperator>(std::move(logical_oper.aggr_descs()));
oper = std::make_unique<GroupPhysicalOperator>(std::move(logical_oper.aggr_descs()));
oper->add_child(std::move(child_physical_oper));
return rc;
}
8 changes: 4 additions & 4 deletions src/observer/sql/parser/defs/sql_node_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class TableReferenceSqlNode
*/
struct AttrInfoSqlNode
{
AttrType type; ///< Type of attribute
std::string name; ///< Attribute name
size_t length; ///< Length of attribute
AttrType type; ///< Type of attribute
std::string name; ///< Attribute name
size_t length; ///< Length of attribute
bool nullable; ///< Whether the attribute is nullable
};

Loading

0 comments on commit 3fcdf40

Please sign in to comment.