diff --git a/src/observer/common/rc.h b/src/observer/common/rc.h index ab58d9f..2ac35db 100644 --- a/src/observer/common/rc.h +++ b/src/observer/common/rc.h @@ -53,6 +53,7 @@ See the Mulan PSL v2 for more details. */ DEFINE_RC(IOERR_ACCESS) \ DEFINE_RC(IOERR_OPEN) \ DEFINE_RC(IOERR_CLOSE) \ + DEFINE_RC(TEXT_OVERFLOW) \ DEFINE_RC(IOERR_SEEK) \ DEFINE_RC(IOERR_TOO_LONG) \ DEFINE_RC(IOERR_SYNC) \ diff --git a/src/observer/net/plain_communicator.cpp b/src/observer/net/plain_communicator.cpp index 6c5e160..0277031 100644 --- a/src/observer/net/plain_communicator.cpp +++ b/src/observer/net/plain_communicator.cpp @@ -19,6 +19,8 @@ See the Mulan PSL v2 for more details. */ #include "net/buffered_writer.h" #include "session/session.h" #include "sql/expr/tuple.h" +#include +#include "sql/operator/project_physical_operator.h" using namespace std; @@ -39,7 +41,7 @@ RC PlainCommunicator::read_event(SessionEvent *&event) int data_len = 0; int read_len = 0; - const int max_packet_size = 8192; + const int max_packet_size = 81920; vector buf(max_packet_size); // 持续接收消息,直到遇到'\0'。将'\0'遇到的后续数据直接丢弃没有处理,因为目前仅支持一收一发的模式 @@ -180,6 +182,62 @@ RC PlainCommunicator::write_result(SessionEvent *event, bool &need_disconnect) return rc; } +RC PlainCommunicator::write_tuple(SqlResult *sql_result) { + RC rc = RC::SUCCESS; + + Tuple *tuple = nullptr; + bool aggregate = false; + std::vector last_values; + + + while (RC::SUCCESS == (rc = sql_result->next_tuple(tuple))) { + int cell_num = tuple->cell_num(); + + + for (int i = 0; i < cell_num; i++) { + if (i != 0 && !aggregate) { + const char *delim = " | "; + writer_->writen(delim, strlen(delim)); + } + + Value value; + rc = tuple->cell_at(i, value); + if (rc != RC::SUCCESS) + return rc; + + if (value.attr_type() == TEXTS) + { + RID *rid = reinterpret_cast(const_cast(value.data())); + size_t len = rid->text_value; + char *ss = new char[len + 1]; + char *p = ss; + while (rid != nullptr && rid->init) { + Record rec_new; + tuple->get_text_record(rec_new, rid); + memcpy(p, rec_new.data(), rec_new.len()); + p += rec_new.len(); + rid = rid->next_RID; + } + writer_->writen(ss, len); + delete[] ss; + } + if (value.attr_type() != TEXTS) { + writer_->writen(value.to_string().c_str(), value.to_string().size()); + } + + if (last_values.size() == (size_t)cell_num) + last_values[i] = value; + else + last_values.push_back(value); + } + return RC::SUCCESS; + + writer_->writen("\n", 1); + } + + return rc; +} + RC PlainCommunicator::write_result_internal(SessionEvent *event, bool &need_disconnect) { RC rc = RC::SUCCESS; @@ -199,7 +257,7 @@ RC PlainCommunicator::write_result_internal(SessionEvent *event, bool &need_disc return write_state(event, need_disconnect); } - const TupleSchema &schema = sql_result->tuple_schema(); + const TupleSchema &schema = const_cast(sql_result->tuple_schema()); const int cell_num = schema.cell_num(); for (int i = 0; i < cell_num; i++) { @@ -238,6 +296,16 @@ RC PlainCommunicator::write_result_internal(SessionEvent *event, bool &need_disc } } + // rc = write_tuple(sql_result); + // if (rc == RC::RECORD_EOF) { + // rc = RC::SUCCESS; + // } else { + // LOG_WARN("write tuple failed: %s", strrc(rc)); + // sql_result->close(); + // sql_result->set_return_code(rc); + // return write_state(event, need_disconnect); + // } + rc = RC::SUCCESS; Tuple *tuple = nullptr; @@ -286,6 +354,11 @@ RC PlainCommunicator::write_result_internal(SessionEvent *event, bool &need_disc if (rc == RC::RECORD_EOF) { rc = RC::SUCCESS; + }else { + LOG_WARN("write tuple failed: %s", strrc(rc)); + sql_result->close(); + sql_result->set_return_code(rc); + return write_state(event, need_disconnect); } if (cell_num == 0) { diff --git a/src/observer/net/plain_communicator.h b/src/observer/net/plain_communicator.h index 01db59b..07faa56 100644 --- a/src/observer/net/plain_communicator.h +++ b/src/observer/net/plain_communicator.h @@ -17,6 +17,7 @@ See the Mulan PSL v2 for more details. */ #include #include "net/communicator.h" +#include "sql/executor/sql_result.h" /** * @brief 与客户端进行通讯 @@ -35,6 +36,7 @@ class PlainCommunicator : public Communicator private: RC write_state(SessionEvent *event, bool &need_disconnect); RC write_debug(SessionEvent *event, bool &need_disconnect); + RC write_tuple(SqlResult *sql_result); RC write_result_internal(SessionEvent *event, bool &need_disconnect); protected: diff --git a/src/observer/sql/executor/sql_result.h b/src/observer/sql/executor/sql_result.h index 68a930e..e0f04a3 100644 --- a/src/observer/sql/executor/sql_result.h +++ b/src/observer/sql/executor/sql_result.h @@ -40,7 +40,10 @@ class SqlResult void set_state_string(const std::string &state_string) { state_string_ = state_string; } void set_operator(std::unique_ptr oper); - +std::unique_ptr &get_operator() + { + return operator_; + } bool has_operator() const { return operator_ != nullptr; } const TupleSchema &tuple_schema() const { return tuple_schema_; } RC return_code() const { return return_code_; } @@ -49,7 +52,8 @@ class SqlResult RC open(); RC close(); RC next_tuple(Tuple *&tuple); - + +bool correlated_query_; private: Session *session_ = nullptr; ///< 当前所属会话 std::unique_ptr operator_; ///< 执行计划 diff --git a/src/observer/sql/expr/tuple.h b/src/observer/sql/expr/tuple.h index 260b804..77af5d0 100644 --- a/src/observer/sql/expr/tuple.h +++ b/src/observer/sql/expr/tuple.h @@ -86,6 +86,8 @@ class Tuple */ virtual RC cell_at(int index, Value &cell) const = 0; + virtual RC get_text_record(Record &rec,RID *rid) = 0; + /** * @brief 根据cell的描述,获取cell的值 * @@ -194,6 +196,16 @@ class RowTuple : public Tuple Record &record() { return *record_; } const Record &record() const { return *record_; } + const Table *table() const { return table_; } + void set_table(const Table *table) { table_ = table; } + + std::vector &speces() { return speces_; } + void set_speces(std::vector &speces) { speces_ = speces; }; + + RC get_text_record(Record &rec,RID *rid) override { + RC rc = const_cast(table_)->get_record(*rid, rec); + return rc; + } private: Record *record_ = nullptr; @@ -237,6 +249,11 @@ class ProjectTuple : public Tuple const TupleCellSpec *spec = speces_[index]; return tuple_->find_cell(*spec, cell); } + RC get_text_record(Record &rec,RID *rid) override { + static_cast(tuple_)->get_text_record(rec, rid); + RC rc = RC::SUCCESS; + return rc; + } RC find_cell(const TupleCellSpec &spec, Value &cell) const override { return tuple_->find_cell(spec, cell); } @@ -283,6 +300,10 @@ class ExpressionTuple : public Tuple } return RC::NOTFOUND; } + RC get_text_record(Record &rec,RID *rid) override { + RC rc = RC::SUCCESS; + return rc; + } private: const std::vector> &expressions_; @@ -313,6 +334,11 @@ class ValueListTuple : public Tuple } virtual RC find_cell(const TupleCellSpec &spec, Value &cell) const override { return RC::INTERNAL; } + + RC get_text_record(Record &rec,RID *rid) override { + RC rc = RC::SUCCESS; + return rc; + } private: std::vector cells_; @@ -357,6 +383,11 @@ class JoinedTuple : public Tuple return right_->find_cell(spec, value); } + + RC get_text_record(Record &rec,RID *rid) override { + RC rc = RC::SUCCESS; + return rc; + } private: Tuple *left_ = nullptr; diff --git a/src/observer/sql/operator/insert_physical_operator.cpp b/src/observer/sql/operator/insert_physical_operator.cpp index fd1bc90..399d01f 100644 --- a/src/observer/sql/operator/insert_physical_operator.cpp +++ b/src/observer/sql/operator/insert_physical_operator.cpp @@ -26,13 +26,13 @@ InsertPhysicalOperator::InsertPhysicalOperator(Table *table, vector &&val RC InsertPhysicalOperator::open(Trx *trx) { Record record; - RC rc = table_->make_record(static_cast(values_.size()), values_.data(), record); + RC rc = table_->make_record(static_cast(values_.size()), values_.data(), record); //构建一个record if (rc != RC::SUCCESS) { LOG_WARN("failed to make record. rc=%s", strrc(rc)); return rc; } - rc = trx->insert_record(table_, record); + rc = trx->insert_record(table_, record); //按照record插入到表中,调用RecordFileHandler if (rc != RC::SUCCESS) { LOG_WARN("failed to insert record by transaction. rc=%s", strrc(rc)); } diff --git a/src/observer/sql/operator/update_physical_operator.h b/src/observer/sql/operator/update_physical_operator.h index a76a534..e819c6b 100644 --- a/src/observer/sql/operator/update_physical_operator.h +++ b/src/observer/sql/operator/update_physical_operator.h @@ -32,4 +32,5 @@ class UpdatePhysicalOperator : public PhysicalOperator Field *field_ = nullptr; Trx *trx_ = nullptr; char *data_; + std::vector field_metas_; }; \ No newline at end of file diff --git a/src/observer/sql/parser/lex_sql.cpp b/src/observer/sql/parser/lex_sql.cpp index 43657a0..e6181b6 100644 --- a/src/observer/sql/parser/lex_sql.cpp +++ b/src/observer/sql/parser/lex_sql.cpp @@ -385,8 +385,8 @@ static void yynoreturn yy_fatal_error ( const char* msg , yyscan_t yyscanner ); yyg->yy_hold_char = *yy_cp; \ *yy_cp = '\0'; \ yyg->yy_c_buf_p = yy_cp; -#define YY_NUM_RULES 58 -#define YY_END_OF_BUFFER 59 +#define YY_NUM_RULES 59 +#define YY_END_OF_BUFFER 60 /* This struct is not used in this scanner, but its presence is necessary. */ struct yy_trans_info @@ -394,26 +394,26 @@ struct yy_trans_info flex_int32_t yy_verify; flex_int32_t yy_nxt; }; -static const flex_int16_t yy_accept[167] = +static const flex_int16_t yy_accept[170] = { 0, - 0, 0, 0, 0, 59, 57, 1, 2, 57, 57, - 57, 41, 42, 53, 51, 43, 52, 6, 54, 3, - 5, 48, 44, 50, 40, 40, 40, 40, 40, 40, - 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, - 40, 58, 47, 0, 55, 0, 56, 3, 0, 45, - 46, 49, 40, 40, 40, 40, 40, 40, 40, 40, - 40, 40, 40, 40, 40, 40, 40, 40, 40, 15, - 40, 40, 40, 40, 40, 40, 40, 40, 4, 22, - 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, - 40, 40, 40, 40, 40, 40, 40, 32, 40, 40, - - 40, 28, 40, 40, 40, 40, 40, 40, 40, 19, - 33, 40, 40, 37, 35, 40, 9, 11, 7, 40, - 40, 20, 8, 40, 40, 40, 24, 36, 40, 40, - 16, 17, 40, 40, 40, 40, 29, 40, 40, 40, - 40, 34, 14, 40, 40, 40, 40, 12, 40, 40, - 21, 30, 10, 26, 40, 38, 23, 40, 18, 13, - 27, 25, 39, 40, 31, 0 + 0, 0, 0, 0, 60, 58, 1, 2, 58, 58, + 58, 42, 43, 54, 52, 44, 53, 6, 55, 3, + 5, 49, 45, 51, 41, 41, 41, 41, 41, 41, + 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, + 41, 59, 48, 0, 56, 0, 57, 3, 0, 46, + 47, 50, 41, 41, 41, 41, 41, 41, 41, 41, + 41, 41, 41, 41, 41, 41, 41, 41, 41, 15, + 41, 41, 41, 41, 41, 41, 41, 41, 41, 4, + 22, 41, 41, 41, 41, 41, 41, 41, 41, 41, + 41, 41, 41, 41, 41, 41, 41, 41, 32, 41, + + 41, 41, 28, 41, 41, 41, 41, 41, 41, 41, + 41, 19, 33, 41, 41, 38, 35, 41, 9, 11, + 7, 41, 41, 20, 8, 41, 41, 41, 24, 37, + 41, 41, 16, 17, 41, 36, 41, 41, 41, 29, + 41, 41, 41, 41, 34, 14, 41, 41, 41, 41, + 12, 41, 41, 21, 30, 10, 26, 41, 39, 23, + 41, 18, 13, 27, 25, 40, 41, 31, 0 } ; static const YY_CHAR yy_ec[256] = @@ -459,53 +459,53 @@ static const YY_CHAR yy_meta[67] = 2, 2, 2, 2, 2, 2 } ; -static const flex_int16_t yy_base[172] = +static const flex_int16_t yy_base[175] = { 0, - 0, 0, 0, 0, 438, 439, 439, 439, 419, 431, - 429, 439, 439, 439, 439, 439, 419, 439, 439, 54, - 439, 52, 439, 415, 53, 57, 60, 59, 58, 61, - 417, 74, 69, 67, 73, 76, 111, 70, 78, 101, - 112, 439, 439, 419, 439, 415, 439, 115, 405, 439, - 439, 439, 0, 404, 117, 116, 119, 126, 128, 132, - 129, 134, 138, 133, 140, 142, 167, 164, 166, 403, - 169, 177, 178, 163, 188, 199, 189, 192, 402, 401, - 190, 212, 211, 213, 217, 229, 215, 214, 236, 226, - 235, 237, 233, 239, 252, 241, 260, 244, 257, 264, - - 267, 399, 268, 240, 275, 283, 277, 281, 287, 398, - 397, 284, 290, 396, 394, 292, 393, 392, 391, 304, - 295, 389, 388, 296, 306, 298, 386, 381, 310, 320, - 378, 376, 299, 307, 326, 334, 374, 329, 339, 347, - 331, 373, 370, 349, 337, 350, 354, 362, 357, 363, - 365, 361, 311, 270, 369, 202, 191, 364, 154, 143, - 136, 97, 93, 122, 84, 439, 425, 427, 429, 76, - 75 + 0, 0, 0, 0, 447, 448, 448, 448, 428, 440, + 438, 448, 448, 448, 448, 448, 428, 448, 448, 54, + 448, 52, 448, 424, 53, 57, 60, 59, 58, 61, + 418, 74, 69, 67, 73, 76, 111, 112, 93, 70, + 115, 448, 448, 427, 448, 425, 448, 124, 415, 448, + 448, 448, 0, 414, 98, 126, 62, 128, 125, 131, + 129, 132, 116, 143, 134, 149, 150, 173, 155, 413, + 169, 174, 159, 170, 187, 172, 192, 182, 194, 412, + 411, 202, 209, 204, 206, 226, 228, 229, 234, 210, + 212, 236, 242, 232, 243, 245, 251, 250, 260, 266, + + 268, 267, 410, 244, 273, 269, 282, 287, 277, 289, + 281, 406, 405, 290, 291, 404, 403, 294, 402, 401, + 400, 306, 295, 399, 398, 296, 297, 309, 396, 393, + 315, 312, 392, 391, 325, 387, 326, 329, 342, 379, + 331, 347, 348, 339, 374, 373, 350, 332, 360, 344, + 363, 361, 364, 371, 369, 368, 320, 366, 256, 214, + 388, 179, 167, 146, 138, 119, 382, 97, 448, 435, + 437, 439, 97, 76 } ; -static const flex_int16_t yy_def[172] = +static const flex_int16_t yy_def[175] = { 0, - 166, 1, 167, 167, 166, 166, 166, 166, 166, 168, - 169, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 170, 170, 170, 170, 170, 170, - 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, - 170, 166, 166, 168, 166, 169, 166, 166, 166, 166, - 166, 166, 171, 170, 170, 170, 170, 170, 170, 170, - 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, - 170, 170, 170, 170, 170, 170, 170, 170, 166, 170, - 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, - 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, - - 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, - 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, - 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, - 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, - 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, - 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, - 170, 170, 170, 170, 170, 0, 166, 166, 166, 166, - 166 + 169, 1, 170, 170, 169, 169, 169, 169, 169, 171, + 172, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169, 173, 173, 173, 173, 173, 173, + 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, + 173, 169, 169, 171, 169, 172, 169, 169, 169, 169, + 169, 169, 174, 173, 173, 173, 173, 173, 173, 173, + 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, + 173, 173, 173, 173, 173, 173, 173, 173, 173, 169, + 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, + 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, + + 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, + 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, + 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, + 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, + 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, + 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, + 173, 173, 173, 173, 173, 173, 173, 173, 0, 169, + 169, 169, 169, 169 } ; -static const flex_int16_t yy_nxt[506] = +static const flex_int16_t yy_nxt[515] = { 0, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, @@ -514,58 +514,59 @@ static const flex_int16_t yy_nxt[506] = 41, 31, 31, 25, 26, 27, 28, 29, 30, 31, 32, 33, 31, 34, 31, 31, 35, 31, 36, 37, 38, 39, 40, 41, 31, 31, 49, 53, 48, 50, - 51, 53, 53, 53, 53, 53, 53, 54, 61, 57, - 56, 53, 62, 53, 53, 55, 58, 53, 53, 75, - 53, 65, 53, 59, 63, 60, 66, 67, 53, 64, + 51, 53, 53, 53, 53, 53, 53, 53, 61, 57, + 56, 53, 62, 53, 53, 55, 58, 53, 53, 78, + 53, 65, 83, 59, 63, 60, 66, 67, 54, 64, 69, 68, 61, 57, 56, 70, 62, 53, 55, 71, - 58, 53, 76, 75, 65, 53, 59, 63, 60, 66, - 77, 67, 64, 69, 68, 53, 53, 49, 70, 48, - 53, 53, 71, 53, 72, 76, 53, 73, 78, 80, - 53, 81, 53, 53, 77, 83, 53, 53, 53, 82, - 53, 165, 53, 74, 53, 85, 53, 53, 72, 84, - 90, 73, 78, 80, 87, 81, 86, 91, 53, 83, - 88, 89, 82, 92, 165, 93, 74, 53, 53, 85, - 53, 53, 84, 53, 90, 99, 95, 87, 96, 86, - 91, 53, 53, 88, 89, 104, 92, 94, 93, 100, - - 97, 98, 53, 53, 53, 53, 53, 101, 105, 99, - 95, 103, 96, 53, 102, 108, 53, 109, 104, 107, - 94, 106, 100, 97, 98, 53, 53, 53, 53, 53, - 101, 53, 105, 110, 103, 117, 113, 102, 116, 108, - 53, 109, 107, 53, 112, 106, 111, 53, 114, 53, - 53, 53, 115, 53, 53, 53, 121, 110, 53, 117, - 113, 132, 116, 119, 122, 120, 53, 112, 125, 111, - 118, 53, 114, 123, 53, 124, 115, 127, 53, 128, - 121, 53, 53, 126, 53, 132, 119, 122, 120, 53, - 130, 53, 125, 118, 129, 53, 123, 53, 53, 124, - - 127, 53, 134, 128, 53, 133, 53, 126, 131, 53, - 53, 138, 53, 53, 130, 135, 136, 129, 53, 137, - 53, 53, 148, 141, 53, 53, 134, 139, 133, 140, - 146, 131, 142, 145, 53, 138, 144, 143, 135, 136, - 53, 147, 137, 53, 149, 53, 148, 141, 53, 150, - 139, 53, 140, 53, 146, 142, 145, 151, 155, 144, - 143, 53, 153, 53, 53, 147, 152, 149, 53, 158, - 154, 53, 156, 150, 157, 53, 53, 53, 53, 53, - 161, 151, 155, 53, 53, 164, 153, 53, 53, 152, - 53, 159, 53, 158, 154, 53, 156, 157, 160, 162, - - 53, 163, 53, 53, 161, 53, 53, 53, 53, 164, - 53, 53, 53, 53, 159, 53, 79, 53, 53, 79, - 47, 160, 162, 45, 163, 42, 42, 44, 44, 46, - 46, 53, 52, 48, 47, 45, 43, 166, 5, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - - 166, 166, 166, 166, 166 + 58, 53, 53, 78, 65, 83, 59, 63, 60, 66, + 81, 67, 64, 69, 68, 53, 53, 77, 70, 53, + 53, 75, 71, 53, 72, 76, 49, 73, 48, 53, + 53, 79, 53, 53, 81, 53, 53, 84, 53, 90, + 77, 82, 53, 74, 86, 75, 85, 53, 72, 76, + 53, 73, 88, 53, 53, 79, 87, 93, 89, 53, + 91, 84, 90, 53, 100, 82, 74, 92, 86, 85, + 95, 53, 94, 53, 53, 88, 53, 53, 53, 87, + 93, 89, 104, 53, 91, 96, 53, 97, 100, 101, + + 92, 53, 105, 95, 102, 94, 53, 106, 53, 98, + 99, 103, 109, 107, 108, 104, 53, 110, 53, 96, + 53, 97, 101, 53, 53, 105, 53, 102, 53, 111, + 112, 106, 98, 99, 103, 109, 107, 114, 108, 113, + 53, 110, 53, 53, 120, 115, 53, 116, 53, 121, + 53, 117, 118, 111, 112, 119, 53, 53, 53, 53, + 114, 123, 113, 124, 53, 53, 122, 120, 126, 115, + 53, 116, 121, 128, 53, 117, 118, 125, 127, 119, + 53, 53, 53, 53, 133, 123, 124, 53, 130, 122, + 132, 53, 126, 129, 134, 53, 53, 128, 131, 135, + + 125, 53, 127, 53, 53, 53, 137, 133, 53, 53, + 53, 53, 130, 140, 132, 138, 129, 141, 134, 136, + 53, 131, 135, 53, 139, 144, 53, 147, 142, 53, + 137, 143, 145, 150, 53, 149, 140, 146, 138, 53, + 53, 141, 136, 53, 148, 53, 53, 139, 151, 144, + 147, 142, 153, 53, 143, 145, 53, 150, 53, 149, + 146, 53, 53, 152, 53, 154, 158, 148, 155, 160, + 156, 157, 151, 159, 53, 53, 153, 53, 53, 161, + 53, 162, 53, 53, 164, 53, 152, 53, 53, 154, + 158, 155, 160, 53, 156, 157, 53, 159, 166, 163, + + 165, 53, 53, 161, 162, 53, 53, 53, 164, 167, + 53, 168, 53, 53, 53, 53, 53, 53, 53, 53, + 53, 166, 163, 165, 53, 53, 80, 53, 53, 80, + 47, 45, 53, 167, 168, 42, 42, 44, 44, 46, + 46, 52, 48, 47, 45, 43, 169, 5, 169, 169, + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169 } ; -static const flex_int16_t yy_chk[506] = +static const flex_int16_t yy_chk[515] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, @@ -574,55 +575,56 @@ static const flex_int16_t yy_chk[506] = 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 20, 25, 20, 22, - 22, 26, 29, 28, 27, 30, 171, 170, 28, 27, - 26, 34, 28, 33, 38, 25, 27, 35, 32, 38, - 36, 30, 39, 27, 28, 27, 30, 32, 165, 29, - - 34, 33, 28, 27, 26, 35, 28, 163, 25, 36, - 27, 162, 39, 38, 30, 40, 27, 28, 27, 30, - 40, 32, 29, 34, 33, 37, 41, 48, 35, 48, - 56, 55, 36, 57, 37, 39, 164, 37, 41, 55, - 58, 56, 59, 61, 40, 58, 60, 64, 62, 57, - 161, 164, 63, 37, 65, 60, 66, 160, 37, 59, - 64, 37, 41, 55, 62, 56, 61, 64, 159, 58, - 62, 63, 57, 65, 164, 66, 37, 74, 68, 60, - 69, 67, 59, 71, 64, 69, 68, 62, 68, 61, - 64, 72, 73, 62, 63, 74, 65, 67, 66, 71, - - 68, 68, 75, 77, 81, 157, 78, 72, 75, 69, - 68, 73, 68, 76, 72, 78, 156, 81, 74, 77, - 67, 76, 71, 68, 68, 83, 82, 84, 88, 87, - 72, 85, 75, 82, 73, 88, 85, 72, 87, 78, - 90, 81, 77, 86, 84, 76, 83, 93, 86, 91, - 89, 92, 86, 94, 104, 96, 92, 82, 98, 88, - 85, 104, 87, 90, 93, 91, 95, 84, 96, 83, - 89, 99, 86, 94, 97, 95, 86, 98, 100, 99, - 92, 101, 103, 97, 154, 104, 90, 93, 91, 105, - 101, 107, 96, 89, 100, 108, 94, 106, 112, 95, - - 98, 109, 106, 99, 113, 105, 116, 97, 103, 121, - 124, 112, 126, 133, 101, 107, 108, 100, 120, 109, - 125, 134, 133, 120, 129, 153, 106, 113, 105, 116, - 129, 103, 121, 126, 130, 112, 125, 124, 107, 108, - 135, 130, 109, 138, 134, 141, 133, 120, 136, 135, - 113, 145, 116, 139, 129, 121, 126, 136, 141, 125, - 124, 140, 139, 144, 146, 130, 138, 134, 147, 146, - 140, 149, 144, 135, 145, 152, 148, 150, 158, 151, - 149, 136, 141, 155, 143, 158, 139, 142, 137, 138, - 132, 147, 131, 146, 140, 128, 144, 145, 148, 150, - - 127, 155, 123, 122, 149, 119, 118, 117, 115, 158, - 114, 111, 110, 102, 147, 80, 79, 70, 54, 49, - 46, 148, 150, 44, 155, 167, 167, 168, 168, 169, - 169, 31, 24, 17, 11, 10, 9, 5, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, - - 166, 166, 166, 166, 166 + 22, 26, 29, 28, 27, 30, 57, 174, 28, 27, + 26, 34, 28, 33, 40, 25, 27, 35, 32, 40, + 36, 30, 57, 27, 28, 27, 30, 32, 173, 29, + + 34, 33, 28, 27, 26, 35, 28, 39, 25, 36, + 27, 168, 55, 40, 30, 57, 27, 28, 27, 30, + 55, 32, 29, 34, 33, 37, 38, 39, 35, 41, + 63, 38, 36, 166, 37, 38, 48, 37, 48, 59, + 56, 41, 58, 61, 55, 60, 62, 58, 65, 63, + 39, 56, 165, 37, 60, 38, 59, 64, 37, 38, + 164, 37, 62, 66, 67, 41, 61, 65, 62, 69, + 64, 58, 63, 73, 69, 56, 37, 64, 60, 59, + 67, 163, 66, 71, 74, 62, 76, 68, 72, 61, + 65, 62, 73, 162, 64, 68, 78, 68, 69, 71, + + 64, 75, 74, 67, 72, 66, 77, 75, 79, 68, + 68, 72, 78, 76, 77, 73, 82, 79, 84, 68, + 85, 68, 71, 83, 90, 74, 91, 72, 160, 82, + 83, 75, 68, 68, 72, 78, 76, 85, 77, 84, + 86, 79, 87, 88, 90, 86, 94, 87, 89, 91, + 92, 87, 88, 82, 83, 89, 93, 95, 104, 96, + 85, 93, 84, 94, 98, 97, 92, 90, 96, 86, + 159, 87, 91, 98, 99, 87, 88, 95, 97, 89, + 100, 102, 101, 106, 104, 93, 94, 105, 100, 92, + 102, 109, 96, 99, 105, 111, 107, 98, 101, 106, + + 95, 108, 97, 110, 114, 115, 108, 104, 118, 123, + 126, 127, 100, 111, 102, 109, 99, 114, 105, 107, + 122, 101, 106, 128, 110, 122, 132, 127, 115, 131, + 108, 118, 123, 132, 157, 131, 111, 126, 109, 135, + 137, 114, 107, 138, 128, 141, 148, 110, 135, 122, + 127, 115, 138, 144, 118, 123, 139, 132, 150, 131, + 126, 142, 143, 137, 147, 139, 144, 128, 141, 148, + 142, 143, 135, 147, 149, 152, 138, 151, 153, 149, + 158, 150, 156, 155, 152, 154, 137, 146, 145, 139, + 144, 141, 148, 140, 142, 143, 167, 147, 158, 151, + + 153, 136, 161, 149, 150, 134, 133, 130, 152, 161, + 129, 167, 125, 124, 121, 120, 119, 117, 116, 113, + 112, 158, 151, 153, 103, 81, 80, 70, 54, 49, + 46, 44, 31, 161, 167, 170, 170, 171, 171, 172, + 172, 24, 17, 11, 10, 9, 5, 169, 169, 169, + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + + 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, + 169, 169, 169, 169 } ; /* The intent behind this definition is that it'll catch @@ -657,7 +659,7 @@ extern int atoi(); extern double atof(); #define RETURN_TOKEN(token) LOG_DEBUG("%s", #token);return token -#line 661 "lex_sql.cpp" +#line 663 "lex_sql.cpp" /* Prevent the need for linking with -lfl */ #define YY_NO_INPUT 1 /* 不区分大小写 */ @@ -666,7 +668,7 @@ extern double atof(); /* 1. 匹配的规则长的优先 */ /* 2. 写在最前面的优先 */ /* yylval 就可以认为是 yacc 中 %union 定义的结构体(union 结构) */ -#line 670 "lex_sql.cpp" +#line 672 "lex_sql.cpp" #define INITIAL 0 #define STR 1 @@ -952,7 +954,7 @@ YY_DECL #line 75 "lex_sql.l" -#line 956 "lex_sql.cpp" +#line 958 "lex_sql.cpp" while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */ { @@ -979,13 +981,13 @@ YY_DECL while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) { yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 167 ) + if ( yy_current_state >= 170 ) yy_c = yy_meta[yy_c]; } yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; ++yy_cp; } - while ( yy_base[yy_current_state] != 439 ); + while ( yy_base[yy_current_state] != 448 ); yy_find_action: yy_act = yy_accept[yy_current_state]; @@ -1188,57 +1190,57 @@ RETURN_TOKEN(DATE_T); case 36: YY_RULE_SETUP #line 114 "lex_sql.l" -RETURN_TOKEN(LOAD); +RETURN_TOKEN(TEXT_T); YY_BREAK case 37: YY_RULE_SETUP #line 115 "lex_sql.l" -RETURN_TOKEN(DATA); +RETURN_TOKEN(LOAD); YY_BREAK case 38: YY_RULE_SETUP #line 116 "lex_sql.l" -RETURN_TOKEN(INFILE); +RETURN_TOKEN(DATA); YY_BREAK case 39: YY_RULE_SETUP #line 117 "lex_sql.l" -RETURN_TOKEN(EXPLAIN); +RETURN_TOKEN(INFILE); YY_BREAK case 40: YY_RULE_SETUP #line 118 "lex_sql.l" -yylval->string=strdup(yytext); RETURN_TOKEN(ID); +RETURN_TOKEN(EXPLAIN); YY_BREAK case 41: YY_RULE_SETUP #line 119 "lex_sql.l" -RETURN_TOKEN(LBRACE); +yylval->string=strdup(yytext); RETURN_TOKEN(ID); YY_BREAK case 42: YY_RULE_SETUP #line 120 "lex_sql.l" -RETURN_TOKEN(RBRACE); +RETURN_TOKEN(LBRACE); YY_BREAK case 43: YY_RULE_SETUP -#line 122 "lex_sql.l" -RETURN_TOKEN(COMMA); +#line 121 "lex_sql.l" +RETURN_TOKEN(RBRACE); YY_BREAK case 44: YY_RULE_SETUP #line 123 "lex_sql.l" -RETURN_TOKEN(EQ); +RETURN_TOKEN(COMMA); YY_BREAK case 45: YY_RULE_SETUP #line 124 "lex_sql.l" -RETURN_TOKEN(LE); +RETURN_TOKEN(EQ); YY_BREAK case 46: YY_RULE_SETUP #line 125 "lex_sql.l" -RETURN_TOKEN(NE); +RETURN_TOKEN(LE); YY_BREAK case 47: YY_RULE_SETUP @@ -1248,34 +1250,33 @@ RETURN_TOKEN(NE); case 48: YY_RULE_SETUP #line 127 "lex_sql.l" -RETURN_TOKEN(LT); +RETURN_TOKEN(NE); YY_BREAK case 49: YY_RULE_SETUP #line 128 "lex_sql.l" -RETURN_TOKEN(GE); +RETURN_TOKEN(LT); YY_BREAK case 50: YY_RULE_SETUP #line 129 "lex_sql.l" -RETURN_TOKEN(GT); +RETURN_TOKEN(GE); YY_BREAK case 51: -#line 132 "lex_sql.l" +YY_RULE_SETUP +#line 130 "lex_sql.l" +RETURN_TOKEN(GT); + YY_BREAK case 52: #line 133 "lex_sql.l" case 53: #line 134 "lex_sql.l" case 54: -YY_RULE_SETUP -#line 134 "lex_sql.l" -{ return yytext[0]; } - YY_BREAK +#line 135 "lex_sql.l" case 55: -/* rule 55 can match eol */ YY_RULE_SETUP #line 135 "lex_sql.l" -yylval->string = strdup(yytext); RETURN_TOKEN(SSS); +{ return yytext[0]; } YY_BREAK case 56: /* rule 56 can match eol */ @@ -1284,16 +1285,22 @@ YY_RULE_SETUP yylval->string = strdup(yytext); RETURN_TOKEN(SSS); YY_BREAK case 57: +/* rule 57 can match eol */ YY_RULE_SETUP -#line 138 "lex_sql.l" -LOG_DEBUG("Unknown character [%c]",yytext[0]); return yytext[0]; +#line 137 "lex_sql.l" +yylval->string = strdup(yytext); RETURN_TOKEN(SSS); YY_BREAK case 58: YY_RULE_SETUP #line 139 "lex_sql.l" +LOG_DEBUG("Unknown character [%c]",yytext[0]); return yytext[0]; + YY_BREAK +case 59: +YY_RULE_SETUP +#line 140 "lex_sql.l" ECHO; YY_BREAK -#line 1297 "lex_sql.cpp" +#line 1304 "lex_sql.cpp" case YY_STATE_EOF(INITIAL): case YY_STATE_EOF(STR): yyterminate(); @@ -1593,7 +1600,7 @@ static int yy_get_next_buffer (yyscan_t yyscanner) while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) { yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 167 ) + if ( yy_current_state >= 170 ) yy_c = yy_meta[yy_c]; } yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; @@ -1622,11 +1629,11 @@ static int yy_get_next_buffer (yyscan_t yyscanner) while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) { yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 167 ) + if ( yy_current_state >= 170 ) yy_c = yy_meta[yy_c]; } yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; - yy_is_jam = (yy_current_state == 166); + yy_is_jam = (yy_current_state == 169); (void)yyg; return yy_is_jam ? 0 : yy_current_state; @@ -2449,7 +2456,7 @@ void yyfree (void * ptr , yyscan_t yyscanner) #define YYTABLES_NAME "yytables" -#line 139 "lex_sql.l" +#line 140 "lex_sql.l" void scan_string(const char *str, yyscan_t scanner) { diff --git a/src/observer/sql/parser/lex_sql.h b/src/observer/sql/parser/lex_sql.h index 22100d4..ffba8bd 100644 --- a/src/observer/sql/parser/lex_sql.h +++ b/src/observer/sql/parser/lex_sql.h @@ -541,7 +541,7 @@ extern int yylex \ #undef yyTABLES_NAME #endif -#line 139 "lex_sql.l" +#line 140 "lex_sql.l" #line 548 "lex_sql.h" diff --git a/src/observer/sql/parser/lex_sql.l b/src/observer/sql/parser/lex_sql.l index 08a8333..09a638d 100644 --- a/src/observer/sql/parser/lex_sql.l +++ b/src/observer/sql/parser/lex_sql.l @@ -111,6 +111,7 @@ INT RETURN_TOKEN(INT_T); CHAR RETURN_TOKEN(STRING_T); FLOAT RETURN_TOKEN(FLOAT_T); DATE RETURN_TOKEN(DATE_T); +TEXT RETURN_TOKEN(TEXT_T); LOAD RETURN_TOKEN(LOAD); DATA RETURN_TOKEN(DATA); INFILE RETURN_TOKEN(INFILE); diff --git a/src/observer/sql/parser/value.cpp b/src/observer/sql/parser/value.cpp index 11ec808..24d4108 100644 --- a/src/observer/sql/parser/value.cpp +++ b/src/observer/sql/parser/value.cpp @@ -18,8 +18,10 @@ See the Mulan PSL v2 for more details. */ #include "sql/parser/date.h" #include "common/log/log.h" #include +#include + +const char *ATTR_TYPE_NAME[] = {"undefined", "chars", "ints", "dates", "floats", "texts","booleans"}; -const char *ATTR_TYPE_NAME[] = {"undefined", "chars", "ints", "dates", "floats", "booleans"}; const char *attr_type_to_string(AttrType type) { @@ -70,6 +72,10 @@ void Value::set_data(char *data, int length) num_value_.bool_value_ = *(int *)data != 0; length_ = length; } break; + case TEXTS: { + set_text(data,length); + length_ = length; + } break; default: { LOG_WARN("unknown data type: %d", attr_type_); } break; @@ -87,6 +93,7 @@ void Value::set_float(float val) attr_type_ = FLOATS; num_value_.float_value_ = val; length_ = sizeof(val); + str_value_.clear(); } void Value::set_date(Date date) { @@ -112,6 +119,14 @@ void Value::set_string(const char *s, int len /*= 0*/) length_ = str_value_.length(); } + +void Value::set_text(const char *s, int len /*= 0*/) +{ + attr_type_ = TEXTS; + str_value_.assign(s, len); + length_ = str_value_.length(); +} + void Value::set_value(const Value &value) { switch (value.attr_type_) { @@ -130,6 +145,9 @@ void Value::set_value(const Value &value) case BOOLEANS: { set_boolean(value.get_boolean()); } break; + case TEXTS: { + set_text(value.get_string().c_str()); + } break; case UNDEFINED: { ASSERT(false, "got an invalid value type"); } break; @@ -139,6 +157,7 @@ void Value::set_value(const Value &value) const char *Value::data() const { switch (attr_type_) { + case TEXTS: case CHARS: { return str_value_.c_str(); } break; @@ -164,6 +183,10 @@ std::string Value::to_string() const case BOOLEANS: { os << num_value_.bool_value_; } break; + case TEXTS: + { + os << str_value_; + } break; case CHARS: { os << str_value_; } break; @@ -178,6 +201,7 @@ int Value::compare(const Value &other) const { if (this->attr_type_ == other.attr_type_) { switch (this->attr_type_) { + case INTS: { return common::compare_int((void *)&this->num_value_.int_value_, (void *)&other.num_value_.int_value_); } break; @@ -220,6 +244,7 @@ int Value::compare(const Value &other) const int Value::get_int() const { switch (attr_type_) { + case TEXTS: case CHARS: { try { return (int)(std::stol(str_value_)); @@ -248,6 +273,7 @@ int Value::get_int() const float Value::get_float() const { switch (attr_type_) { + case TEXTS: case CHARS: { try { return std::stof(str_value_); @@ -275,9 +301,16 @@ float Value::get_float() const std::string Value::get_string() const { return this->to_string(); } +// char *Value::get_fiexed_string() const { +// std::memset((void *)num_value_.str_value_, 0, 4); +// std::strncpy((char *)num_value_.str_value_, str_value_.c_str(), 4); +// return (char *)num_value_.str_value_; +// } + bool Value::get_boolean() const { switch (attr_type_) { + case TEXTS: case CHARS: { try { float val = std::stof(str_value_); @@ -335,6 +368,10 @@ bool Value::convert(AttrType from, AttrType to, Value &value) { value.set_int(value.get_int()); return true; } + if (from == CHARS && to == TEXTS) { + value.set_text(value.get_string().c_str()); + return true; + } return false; } @@ -344,4 +381,23 @@ Date Value::get_date() const { case CHARS: return Date(str_value_); default: return Date(-1); } +} + +bool is_float(const std::string& str) { + try { + size_t pos; // 用于保存解析后的位置 + std::stof(str, &pos); + + // 检查是否解析了整个字符串 + if (pos == str.size()) { + return true; + } else { + return false; + } + + } catch (const std::invalid_argument& e) { + return false; + } catch (const std::out_of_range& e) { + return false; + } } \ No newline at end of file diff --git a/src/observer/sql/parser/value.h b/src/observer/sql/parser/value.h index 0b43247..b0763a9 100644 --- a/src/observer/sql/parser/value.h +++ b/src/observer/sql/parser/value.h @@ -14,93 +14,111 @@ See the Mulan PSL v2 for more details. */ #pragma once -#include #include "sql/parser/date.h" +#include +#include +#include +#include + +const int INVALID_COMPARE = std::numeric_limits::min(); /** * @brief 属性的类型 - * + * */ -enum AttrType -{ +enum AttrType { UNDEFINED, - CHARS, ///< 字符串类型 - INTS, ///< 整数类型(4字节) - DATES, /// 日期类型(4字节) - FLOATS, ///< 浮点数类型(4字节) - BOOLEANS, ///< boolean类型,当前不是由parser解析出来的,是程序内部使用的 + CHARS, ///< 字符串类型 + INTS, ///< 整数类型(4字节) + DATES, ///< 日期类型(4字节) + FLOATS, ///< 浮点数类型(4字节) + TEXTS, ///< text + BOOLEANS, ///< boolean类型,当前不是由parser解析出来的,是程序内部使用的 }; +int attr_type_to_size(AttrType type); const char *attr_type_to_string(AttrType type); -AttrType attr_type_from_string(const char *s); +AttrType attr_type_from_string(const char *s); + + +bool is_float(const std::string& str); /** * @brief 属性的值 - * + * */ -class Value -{ +class Value { public: Value() = default; - Value(AttrType attr_type, char *data, int length = 4) : attr_type_(attr_type) { this->set_data(data, length); } //初始化数据和长度 + Value(AttrType attr_type, char *data, int length = 4) : attr_type_(attr_type) { this->set_data(data, length); } explicit Value(int val); explicit Value(float val); - explicit Value(Date val); explicit Value(bool val); explicit Value(const char *s, int len = 0); - + explicit Value(Date date); - Value(const Value &other) = default; + Value(const Value &other) = default; Value &operator=(const Value &other) = default; void set_type(AttrType type) { this->attr_type_ = type; } void set_data(char *data, int length); void set_data(const char *data, int length) { this->set_data(const_cast(data), length); } void set_int(int val); - void set_date(Date val); void set_float(float val); void set_boolean(bool val); void set_string(const char *s, int len = 0); + void set_date(Date date); void set_value(const Value &value); + void set_text(const char *s, int len = 0); std::string to_string() const; int compare(const Value &other) const; const char *data() const; - int length() const { return length_; } + int length() const { return length_; } AttrType attr_type() const { return attr_type_; } - public: - /** - * 语法分析层面是否可以将from转换为to - */ - static bool convert(AttrType from, AttrType to, Value &value); + // std::strong_ordering operator<=>(const Value &value) const; + + void set_length(int len) { length_ = len; } + std::string text_data() { + return str_value_; + } + void set_text_f() { + attr_type_ = TEXTS; + } public: /** * 获取对应的值 * 如果当前的类型与期望获取的类型不符,就会执行转换操作 */ - int get_int() const; - Date get_date() const; - float get_float() const; + int get_int() const; + float get_float() const; std::string get_string() const; - bool get_boolean() const; + bool get_boolean() const; + Date get_date() const; + +public: + /** + * 语法分析层面是否可以将from转换为to + */ + static bool convert(AttrType from, AttrType to, Value &value); + private: AttrType attr_type_ = UNDEFINED; - int length_ = 0; + int length_ = 0; - union - { - int int_value_; - Date date_value_; + union { + int int_value_; float float_value_; - bool bool_value_; + bool bool_value_; + Date date_value_; } num_value_; std::string str_value_; -}; \ No newline at end of file +}; diff --git a/src/observer/sql/parser/yacc_sql.cpp b/src/observer/sql/parser/yacc_sql.cpp index bb8ad2e..6e22d20 100644 --- a/src/observer/sql/parser/yacc_sql.cpp +++ b/src/observer/sql/parser/yacc_sql.cpp @@ -164,77 +164,78 @@ enum yysymbol_kind_t YYSYMBOL_TRX_ROLLBACK = 22, /* TRX_ROLLBACK */ YYSYMBOL_INT_T = 23, /* INT_T */ YYSYMBOL_DATE_T = 24, /* DATE_T */ - YYSYMBOL_STRING_T = 25, /* STRING_T */ - YYSYMBOL_FLOAT_T = 26, /* FLOAT_T */ - YYSYMBOL_HELP = 27, /* HELP */ - YYSYMBOL_EXIT = 28, /* EXIT */ - YYSYMBOL_DOT = 29, /* DOT */ - YYSYMBOL_INTO = 30, /* INTO */ - YYSYMBOL_VALUES = 31, /* VALUES */ - YYSYMBOL_FROM = 32, /* FROM */ - YYSYMBOL_WHERE = 33, /* WHERE */ - YYSYMBOL_AND = 34, /* AND */ - YYSYMBOL_SET = 35, /* SET */ - YYSYMBOL_ON = 36, /* ON */ - YYSYMBOL_LOAD = 37, /* LOAD */ - YYSYMBOL_DATA = 38, /* DATA */ - YYSYMBOL_INFILE = 39, /* INFILE */ - YYSYMBOL_EXPLAIN = 40, /* EXPLAIN */ - YYSYMBOL_EQ = 41, /* EQ */ - YYSYMBOL_LT = 42, /* LT */ - YYSYMBOL_GT = 43, /* GT */ - YYSYMBOL_LE = 44, /* LE */ - YYSYMBOL_GE = 45, /* GE */ - YYSYMBOL_NE = 46, /* NE */ - YYSYMBOL_NUMBER = 47, /* NUMBER */ - YYSYMBOL_FLOAT = 48, /* FLOAT */ - YYSYMBOL_ID = 49, /* ID */ - YYSYMBOL_SSS = 50, /* SSS */ - YYSYMBOL_51_ = 51, /* '+' */ - YYSYMBOL_52_ = 52, /* '-' */ - YYSYMBOL_53_ = 53, /* '*' */ - YYSYMBOL_54_ = 54, /* '/' */ - YYSYMBOL_UMINUS = 55, /* UMINUS */ - YYSYMBOL_YYACCEPT = 56, /* $accept */ - YYSYMBOL_commands = 57, /* commands */ - YYSYMBOL_command_wrapper = 58, /* command_wrapper */ - YYSYMBOL_exit_stmt = 59, /* exit_stmt */ - YYSYMBOL_help_stmt = 60, /* help_stmt */ - YYSYMBOL_sync_stmt = 61, /* sync_stmt */ - YYSYMBOL_begin_stmt = 62, /* begin_stmt */ - YYSYMBOL_commit_stmt = 63, /* commit_stmt */ - YYSYMBOL_rollback_stmt = 64, /* rollback_stmt */ - YYSYMBOL_drop_table_stmt = 65, /* drop_table_stmt */ - YYSYMBOL_show_tables_stmt = 66, /* show_tables_stmt */ - YYSYMBOL_desc_table_stmt = 67, /* desc_table_stmt */ - YYSYMBOL_create_index_stmt = 68, /* create_index_stmt */ - YYSYMBOL_drop_index_stmt = 69, /* drop_index_stmt */ - YYSYMBOL_create_table_stmt = 70, /* create_table_stmt */ - YYSYMBOL_attr_def_list = 71, /* attr_def_list */ - YYSYMBOL_attr_def = 72, /* attr_def */ - YYSYMBOL_number = 73, /* number */ - YYSYMBOL_type = 74, /* type */ - YYSYMBOL_insert_stmt = 75, /* insert_stmt */ - YYSYMBOL_value_list = 76, /* value_list */ - YYSYMBOL_value = 77, /* value */ - YYSYMBOL_delete_stmt = 78, /* delete_stmt */ - YYSYMBOL_update_stmt = 79, /* update_stmt */ - YYSYMBOL_select_stmt = 80, /* select_stmt */ - YYSYMBOL_calc_stmt = 81, /* calc_stmt */ - YYSYMBOL_expression_list = 82, /* expression_list */ - YYSYMBOL_expression = 83, /* expression */ - YYSYMBOL_select_attr = 84, /* select_attr */ - YYSYMBOL_rel_attr = 85, /* rel_attr */ - YYSYMBOL_attr_list = 86, /* attr_list */ - YYSYMBOL_rel_list = 87, /* rel_list */ - YYSYMBOL_where = 88, /* where */ - YYSYMBOL_condition_list = 89, /* condition_list */ - YYSYMBOL_condition = 90, /* condition */ - YYSYMBOL_comp_op = 91, /* comp_op */ - YYSYMBOL_load_data_stmt = 92, /* load_data_stmt */ - YYSYMBOL_explain_stmt = 93, /* explain_stmt */ - YYSYMBOL_set_variable_stmt = 94, /* set_variable_stmt */ - YYSYMBOL_opt_semicolon = 95 /* opt_semicolon */ + YYSYMBOL_TEXT_T = 25, /* TEXT_T */ + YYSYMBOL_STRING_T = 26, /* STRING_T */ + YYSYMBOL_FLOAT_T = 27, /* FLOAT_T */ + YYSYMBOL_HELP = 28, /* HELP */ + YYSYMBOL_EXIT = 29, /* EXIT */ + YYSYMBOL_DOT = 30, /* DOT */ + YYSYMBOL_INTO = 31, /* INTO */ + YYSYMBOL_VALUES = 32, /* VALUES */ + YYSYMBOL_FROM = 33, /* FROM */ + YYSYMBOL_WHERE = 34, /* WHERE */ + YYSYMBOL_AND = 35, /* AND */ + YYSYMBOL_SET = 36, /* SET */ + YYSYMBOL_ON = 37, /* ON */ + YYSYMBOL_LOAD = 38, /* LOAD */ + YYSYMBOL_DATA = 39, /* DATA */ + YYSYMBOL_INFILE = 40, /* INFILE */ + YYSYMBOL_EXPLAIN = 41, /* EXPLAIN */ + YYSYMBOL_EQ = 42, /* EQ */ + YYSYMBOL_LT = 43, /* LT */ + YYSYMBOL_GT = 44, /* GT */ + YYSYMBOL_LE = 45, /* LE */ + YYSYMBOL_GE = 46, /* GE */ + YYSYMBOL_NE = 47, /* NE */ + YYSYMBOL_NUMBER = 48, /* NUMBER */ + YYSYMBOL_FLOAT = 49, /* FLOAT */ + YYSYMBOL_ID = 50, /* ID */ + YYSYMBOL_SSS = 51, /* SSS */ + YYSYMBOL_52_ = 52, /* '+' */ + YYSYMBOL_53_ = 53, /* '-' */ + YYSYMBOL_54_ = 54, /* '*' */ + YYSYMBOL_55_ = 55, /* '/' */ + YYSYMBOL_UMINUS = 56, /* UMINUS */ + YYSYMBOL_YYACCEPT = 57, /* $accept */ + YYSYMBOL_commands = 58, /* commands */ + YYSYMBOL_command_wrapper = 59, /* command_wrapper */ + YYSYMBOL_exit_stmt = 60, /* exit_stmt */ + YYSYMBOL_help_stmt = 61, /* help_stmt */ + YYSYMBOL_sync_stmt = 62, /* sync_stmt */ + YYSYMBOL_begin_stmt = 63, /* begin_stmt */ + YYSYMBOL_commit_stmt = 64, /* commit_stmt */ + YYSYMBOL_rollback_stmt = 65, /* rollback_stmt */ + YYSYMBOL_drop_table_stmt = 66, /* drop_table_stmt */ + YYSYMBOL_show_tables_stmt = 67, /* show_tables_stmt */ + YYSYMBOL_desc_table_stmt = 68, /* desc_table_stmt */ + YYSYMBOL_create_index_stmt = 69, /* create_index_stmt */ + YYSYMBOL_drop_index_stmt = 70, /* drop_index_stmt */ + YYSYMBOL_create_table_stmt = 71, /* create_table_stmt */ + YYSYMBOL_attr_def_list = 72, /* attr_def_list */ + YYSYMBOL_attr_def = 73, /* attr_def */ + YYSYMBOL_number = 74, /* number */ + YYSYMBOL_type = 75, /* type */ + YYSYMBOL_insert_stmt = 76, /* insert_stmt */ + YYSYMBOL_value_list = 77, /* value_list */ + YYSYMBOL_value = 78, /* value */ + YYSYMBOL_delete_stmt = 79, /* delete_stmt */ + YYSYMBOL_update_stmt = 80, /* update_stmt */ + YYSYMBOL_select_stmt = 81, /* select_stmt */ + YYSYMBOL_calc_stmt = 82, /* calc_stmt */ + YYSYMBOL_expression_list = 83, /* expression_list */ + YYSYMBOL_expression = 84, /* expression */ + YYSYMBOL_select_attr = 85, /* select_attr */ + YYSYMBOL_rel_attr = 86, /* rel_attr */ + YYSYMBOL_attr_list = 87, /* attr_list */ + YYSYMBOL_rel_list = 88, /* rel_list */ + YYSYMBOL_where = 89, /* where */ + YYSYMBOL_condition_list = 90, /* condition_list */ + YYSYMBOL_condition = 91, /* condition */ + YYSYMBOL_comp_op = 92, /* comp_op */ + YYSYMBOL_load_data_stmt = 93, /* load_data_stmt */ + YYSYMBOL_explain_stmt = 94, /* explain_stmt */ + YYSYMBOL_set_variable_stmt = 95, /* set_variable_stmt */ + YYSYMBOL_opt_semicolon = 96 /* opt_semicolon */ }; typedef enum yysymbol_kind_t yysymbol_kind_t; @@ -565,19 +566,19 @@ union yyalloc /* YYFINAL -- State number of the termination state. */ #define YYFINAL 65 /* YYLAST -- Last index in YYTABLE. */ -#define YYLAST 156 +#define YYLAST 139 /* YYNTOKENS -- Number of terminals. */ -#define YYNTOKENS 56 +#define YYNTOKENS 57 /* YYNNTS -- Number of nonterminals. */ #define YYNNTS 40 /* YYNRULES -- Number of rules. */ -#define YYNRULES 90 +#define YYNRULES 91 /* YYNSTATES -- Number of states. */ -#define YYNSTATES 164 +#define YYNSTATES 165 /* YYMAXUTOK -- Last valid token kind. */ -#define YYMAXUTOK 306 +#define YYMAXUTOK 307 /* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM @@ -595,7 +596,7 @@ static const yytype_int8 yytranslate[] = 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 53, 51, 2, 52, 2, 54, 2, 2, + 2, 2, 54, 52, 2, 53, 2, 55, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, @@ -621,23 +622,23 @@ static const yytype_int8 yytranslate[] = 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, - 45, 46, 47, 48, 49, 50, 55 + 45, 46, 47, 48, 49, 50, 51, 56 }; #if YYDEBUG /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ static const yytype_int16 yyrline[] = { - 0, 174, 174, 182, 183, 184, 185, 186, 187, 188, - 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, - 199, 200, 201, 205, 211, 216, 222, 228, 234, 240, - 247, 253, 261, 275, 285, 305, 308, 321, 329, 339, - 342, 343, 344, 345, 348, 365, 368, 379, 383, 387, - 396, 408, 423, 445, 455, 460, 471, 474, 477, 480, - 483, 487, 490, 498, 505, 517, 522, 533, 536, 550, - 553, 566, 569, 575, 578, 583, 590, 602, 614, 626, - 641, 642, 643, 644, 645, 646, 650, 663, 671, 681, - 682 + 0, 175, 175, 183, 184, 185, 186, 187, 188, 189, + 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, + 200, 201, 202, 206, 212, 217, 223, 229, 235, 241, + 248, 254, 262, 276, 286, 306, 309, 322, 330, 340, + 343, 344, 345, 346, 347, 350, 367, 370, 381, 385, + 389, 398, 410, 425, 447, 457, 462, 473, 476, 479, + 482, 485, 489, 492, 500, 507, 519, 524, 535, 538, + 552, 555, 568, 571, 577, 580, 585, 592, 604, 616, + 628, 643, 644, 645, 646, 647, 648, 652, 665, 673, + 683, 684 }; #endif @@ -656,11 +657,11 @@ static const char *const yytname[] = "\"end of file\"", "error", "\"invalid token\"", "SEMICOLON", "CREATE", "DROP", "TABLE", "TABLES", "INDEX", "CALC", "SELECT", "DESC", "SHOW", "SYNC", "INSERT", "DELETE", "UPDATE", "LBRACE", "RBRACE", "COMMA", - "TRX_BEGIN", "TRX_COMMIT", "TRX_ROLLBACK", "INT_T", "DATE_T", "STRING_T", - "FLOAT_T", "HELP", "EXIT", "DOT", "INTO", "VALUES", "FROM", "WHERE", - "AND", "SET", "ON", "LOAD", "DATA", "INFILE", "EXPLAIN", "EQ", "LT", - "GT", "LE", "GE", "NE", "NUMBER", "FLOAT", "ID", "SSS", "'+'", "'-'", - "'*'", "'/'", "UMINUS", "$accept", "commands", "command_wrapper", + "TRX_BEGIN", "TRX_COMMIT", "TRX_ROLLBACK", "INT_T", "DATE_T", "TEXT_T", + "STRING_T", "FLOAT_T", "HELP", "EXIT", "DOT", "INTO", "VALUES", "FROM", + "WHERE", "AND", "SET", "ON", "LOAD", "DATA", "INFILE", "EXPLAIN", "EQ", + "LT", "GT", "LE", "GE", "NE", "NUMBER", "FLOAT", "ID", "SSS", "'+'", + "'-'", "'*'", "'/'", "UMINUS", "$accept", "commands", "command_wrapper", "exit_stmt", "help_stmt", "sync_stmt", "begin_stmt", "commit_stmt", "rollback_stmt", "drop_table_stmt", "show_tables_stmt", "desc_table_stmt", "create_index_stmt", "drop_index_stmt", @@ -679,7 +680,7 @@ yysymbol_name (yysymbol_kind_t yysymbol) } #endif -#define YYPACT_NINF (-106) +#define YYPACT_NINF (-107) #define yypact_value_is_default(Yyn) \ ((Yyn) == YYPACT_NINF) @@ -693,23 +694,23 @@ yysymbol_name (yysymbol_kind_t yysymbol) STATE-NUM. */ static const yytype_int8 yypact[] = { - -1, 51, 67, 8, 25, -47, -2, -106, -13, 5, - 27, -106, -106, -106, -106, -106, 34, 0, -1, 61, - 81, -106, -106, -106, -106, -106, -106, -106, -106, -106, - -106, -106, -106, -106, -106, -106, -106, -106, -106, -106, - -106, 36, 37, 38, 39, 8, -106, -106, -106, 8, - -106, -106, 12, 60, -106, 58, 72, -106, -106, 43, - 44, 59, 54, 57, -106, -106, -106, -106, 80, 62, - -106, 63, -11, -106, 8, 8, 8, 8, 8, 52, - 53, 55, -106, 69, 70, 64, -18, 65, 68, 71, - 73, -106, -106, -30, -30, -106, -106, -106, 86, 72, - 89, -3, -106, 66, -106, 78, 56, 90, 93, -106, - 74, 70, -106, -18, 26, 26, -106, 77, -18, 106, - -106, -106, -106, -106, 97, 68, 98, 75, 86, -106, - 99, -106, -106, -106, -106, -106, -106, -3, -3, -3, - 70, 76, 79, 90, -106, 101, -106, -18, 103, -106, - -106, -106, -106, -106, -106, -106, -106, 109, -106, -106, - 99, -106, -106, -106 + -2, 16, 38, 8, 33, -46, 44, -107, -16, -10, + -5, -107, -107, -107, -107, -107, 2, 19, -2, 60, + 59, -107, -107, -107, -107, -107, -107, -107, -107, -107, + -107, -107, -107, -107, -107, -107, -107, -107, -107, -107, + -107, 24, 25, 34, 35, 8, -107, -107, -107, 8, + -107, -107, 12, 46, -107, 53, 73, -107, -107, 43, + 45, 58, 54, 57, -107, -107, -107, -107, 81, 62, + -107, 63, -12, -107, 8, 8, 8, 8, 8, 51, + 52, 56, -107, 71, 70, 61, -19, 64, 66, 67, + 68, -107, -107, -17, -17, -107, -107, -107, 86, 73, + 90, 40, -107, 72, -107, 77, 55, 91, 92, -107, + 69, 70, -107, -19, 26, 26, -107, 78, -19, 106, + -107, -107, -107, -107, -107, 103, 66, 104, 74, 86, + -107, 102, -107, -107, -107, -107, -107, -107, 40, 40, + 40, 70, 75, 79, 91, -107, 105, -107, -19, 108, + -107, -107, -107, -107, -107, -107, -107, -107, 110, -107, + -107, 102, -107, -107, -107 }; /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. @@ -719,39 +720,39 @@ static const yytype_int8 yydefact[] = { 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 26, 27, 28, 24, 23, 0, 0, 0, 0, - 89, 22, 21, 14, 15, 16, 17, 9, 10, 11, + 90, 22, 21, 14, 15, 16, 17, 9, 10, 11, 12, 13, 8, 5, 7, 6, 4, 3, 18, 19, - 20, 0, 0, 0, 0, 0, 47, 48, 49, 0, - 62, 53, 54, 65, 63, 0, 67, 31, 30, 0, - 0, 0, 0, 0, 87, 1, 90, 2, 0, 0, - 29, 0, 0, 61, 0, 0, 0, 0, 0, 0, - 0, 0, 64, 0, 71, 0, 0, 0, 0, 0, - 0, 60, 55, 56, 57, 58, 59, 66, 69, 67, - 0, 73, 50, 0, 88, 0, 0, 35, 0, 33, - 0, 71, 68, 0, 0, 0, 72, 74, 0, 0, - 40, 43, 41, 42, 38, 0, 0, 0, 69, 52, - 45, 80, 81, 82, 83, 84, 85, 0, 0, 73, - 71, 0, 0, 35, 34, 0, 70, 0, 0, 77, - 79, 76, 78, 75, 51, 86, 39, 0, 36, 32, - 45, 44, 37, 46 + 20, 0, 0, 0, 0, 0, 48, 49, 50, 0, + 63, 54, 55, 66, 64, 0, 68, 31, 30, 0, + 0, 0, 0, 0, 88, 1, 91, 2, 0, 0, + 29, 0, 0, 62, 0, 0, 0, 0, 0, 0, + 0, 0, 65, 0, 72, 0, 0, 0, 0, 0, + 0, 61, 56, 57, 58, 59, 60, 67, 70, 68, + 0, 74, 51, 0, 89, 0, 0, 35, 0, 33, + 0, 72, 69, 0, 0, 0, 73, 75, 0, 0, + 40, 43, 44, 41, 42, 38, 0, 0, 0, 70, + 53, 46, 81, 82, 83, 84, 85, 86, 0, 0, + 74, 72, 0, 0, 35, 34, 0, 71, 0, 0, + 78, 80, 77, 79, 76, 52, 87, 39, 0, 36, + 32, 46, 45, 37, 47 }; /* YYPGOTO[NTERM-NUM]. */ static const yytype_int8 yypgoto[] = { - -106, -106, 110, -106, -106, -106, -106, -106, -106, -106, - -106, -106, -106, -106, -106, -14, 6, -106, -106, -106, - -28, -85, -106, -106, -106, -106, 82, -27, -106, -4, - 31, 7, -105, 1, -106, 21, -106, -106, -106, -106 + -107, -107, 111, -107, -107, -107, -107, -107, -107, -107, + -107, -107, -107, -107, -107, -14, 5, -107, -107, -107, + -29, -85, -107, -107, -107, -107, 65, -28, -107, -4, + 37, 4, -106, -3, -107, 23, -107, -107, -107, -107 }; /* YYDEFGOTO[NTERM-NUM]. */ static const yytype_uint8 yydefgoto[] = { 0, 19, 20, 21, 22, 23, 24, 25, 26, 27, - 28, 29, 30, 31, 32, 126, 107, 157, 124, 33, - 148, 50, 34, 35, 36, 37, 51, 52, 55, 115, - 82, 111, 102, 116, 117, 137, 38, 39, 40, 67 + 28, 29, 30, 31, 32, 127, 107, 158, 125, 33, + 149, 50, 34, 35, 36, 37, 51, 52, 55, 115, + 82, 111, 102, 116, 117, 138, 38, 39, 40, 67 }; /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If @@ -759,42 +760,38 @@ static const yytype_uint8 yydefgoto[] = number is the opposite. If YYTABLE_NINF, syntax error. */ static const yytype_uint8 yytable[] = { - 56, 104, 57, 1, 2, 58, 129, 91, 3, 4, - 5, 6, 7, 8, 9, 10, 114, 59, 72, 11, - 12, 13, 73, 77, 78, 45, 14, 15, 130, 46, - 47, 74, 48, 140, 16, 154, 17, 60, 63, 18, - 75, 76, 77, 78, 46, 47, 53, 48, 93, 94, - 95, 96, 149, 151, 114, 46, 47, 41, 48, 42, - 49, 65, 160, 75, 76, 77, 78, 131, 132, 133, - 134, 135, 136, 43, 53, 44, 61, 99, 54, 120, - 121, 122, 123, 62, 66, 68, 69, 70, 71, 79, - 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, - 100, 97, 98, 101, 53, 110, 113, 118, 119, 125, - 127, 139, 141, 103, 142, 105, 144, 106, 147, 159, - 108, 161, 109, 128, 145, 155, 156, 162, 64, 158, - 112, 143, 163, 150, 152, 146, 138, 0, 0, 0, - 153, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 92 + 56, 104, 1, 2, 57, 130, 91, 3, 4, 5, + 6, 7, 8, 9, 10, 59, 114, 72, 11, 12, + 13, 73, 41, 60, 42, 45, 14, 15, 131, 46, + 47, 74, 48, 141, 16, 155, 17, 77, 78, 18, + 75, 76, 77, 78, 43, 61, 44, 93, 94, 95, + 96, 58, 62, 150, 152, 114, 46, 47, 63, 48, + 65, 49, 66, 161, 75, 76, 77, 78, 132, 133, + 134, 135, 136, 137, 68, 69, 79, 99, 120, 121, + 122, 123, 124, 53, 70, 71, 80, 54, 46, 47, + 53, 48, 81, 83, 85, 84, 86, 87, 88, 89, + 90, 97, 98, 100, 101, 110, 53, 113, 119, 128, + 126, 103, 142, 140, 118, 105, 106, 108, 109, 129, + 143, 148, 145, 160, 146, 156, 162, 157, 163, 64, + 159, 144, 164, 147, 151, 153, 112, 154, 139, 92 }; -static const yytype_int16 yycheck[] = +static const yytype_uint8 yycheck[] = { - 4, 86, 49, 4, 5, 7, 111, 18, 9, 10, - 11, 12, 13, 14, 15, 16, 101, 30, 45, 20, - 21, 22, 49, 53, 54, 17, 27, 28, 113, 47, - 48, 19, 50, 118, 35, 140, 37, 32, 38, 40, - 51, 52, 53, 54, 47, 48, 49, 50, 75, 76, - 77, 78, 137, 138, 139, 47, 48, 6, 50, 8, - 52, 0, 147, 51, 52, 53, 54, 41, 42, 43, - 44, 45, 46, 6, 49, 8, 49, 81, 53, 23, - 24, 25, 26, 49, 3, 49, 49, 49, 49, 29, - 32, 19, 49, 49, 35, 41, 39, 17, 36, 36, - 31, 49, 49, 33, 49, 19, 17, 41, 30, 19, - 17, 34, 6, 49, 17, 50, 18, 49, 19, 18, - 49, 18, 49, 49, 49, 49, 47, 18, 18, 143, - 99, 125, 160, 137, 138, 128, 115, -1, -1, -1, - 139, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, 74 + 4, 86, 4, 5, 50, 111, 18, 9, 10, 11, + 12, 13, 14, 15, 16, 31, 101, 45, 20, 21, + 22, 49, 6, 33, 8, 17, 28, 29, 113, 48, + 49, 19, 51, 118, 36, 141, 38, 54, 55, 41, + 52, 53, 54, 55, 6, 50, 8, 75, 76, 77, + 78, 7, 50, 138, 139, 140, 48, 49, 39, 51, + 0, 53, 3, 148, 52, 53, 54, 55, 42, 43, + 44, 45, 46, 47, 50, 50, 30, 81, 23, 24, + 25, 26, 27, 50, 50, 50, 33, 54, 48, 49, + 50, 51, 19, 50, 36, 50, 42, 40, 17, 37, + 37, 50, 50, 32, 34, 19, 50, 17, 31, 17, + 19, 50, 6, 35, 42, 51, 50, 50, 50, 50, + 17, 19, 18, 18, 50, 50, 18, 48, 18, 18, + 144, 126, 161, 129, 138, 139, 99, 140, 115, 74 }; /* YYSTOS[STATE-NUM] -- The symbol kind of the accessing symbol of @@ -802,37 +799,37 @@ static const yytype_int16 yycheck[] = static const yytype_int8 yystos[] = { 0, 4, 5, 9, 10, 11, 12, 13, 14, 15, - 16, 20, 21, 22, 27, 28, 35, 37, 40, 57, - 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, - 68, 69, 70, 75, 78, 79, 80, 81, 92, 93, - 94, 6, 8, 6, 8, 17, 47, 48, 50, 52, - 77, 82, 83, 49, 53, 84, 85, 49, 7, 30, - 32, 49, 49, 38, 58, 0, 3, 95, 49, 49, - 49, 49, 83, 83, 19, 51, 52, 53, 54, 29, - 32, 19, 86, 49, 49, 35, 41, 39, 17, 36, - 36, 18, 82, 83, 83, 83, 83, 49, 49, 85, - 31, 33, 88, 49, 77, 50, 49, 72, 49, 49, - 19, 87, 86, 17, 77, 85, 89, 90, 41, 30, - 23, 24, 25, 26, 74, 19, 71, 17, 49, 88, - 77, 41, 42, 43, 44, 45, 46, 91, 91, 34, - 77, 6, 17, 72, 18, 49, 87, 19, 76, 77, - 85, 77, 85, 89, 88, 49, 47, 73, 71, 18, - 77, 18, 18, 76 + 16, 20, 21, 22, 28, 29, 36, 38, 41, 58, + 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, + 69, 70, 71, 76, 79, 80, 81, 82, 93, 94, + 95, 6, 8, 6, 8, 17, 48, 49, 51, 53, + 78, 83, 84, 50, 54, 85, 86, 50, 7, 31, + 33, 50, 50, 39, 59, 0, 3, 96, 50, 50, + 50, 50, 84, 84, 19, 52, 53, 54, 55, 30, + 33, 19, 87, 50, 50, 36, 42, 40, 17, 37, + 37, 18, 83, 84, 84, 84, 84, 50, 50, 86, + 32, 34, 89, 50, 78, 51, 50, 73, 50, 50, + 19, 88, 87, 17, 78, 86, 90, 91, 42, 31, + 23, 24, 25, 26, 27, 75, 19, 72, 17, 50, + 89, 78, 42, 43, 44, 45, 46, 47, 92, 92, + 35, 78, 6, 17, 73, 18, 50, 88, 19, 77, + 78, 86, 78, 86, 90, 89, 50, 48, 74, 72, + 18, 78, 18, 18, 77 }; /* YYR1[RULE-NUM] -- Symbol kind of the left-hand side of rule RULE-NUM. */ static const yytype_int8 yyr1[] = { - 0, 56, 57, 58, 58, 58, 58, 58, 58, 58, - 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, - 58, 58, 58, 59, 60, 61, 62, 63, 64, 65, - 66, 67, 68, 69, 70, 71, 71, 72, 72, 73, - 74, 74, 74, 74, 75, 76, 76, 77, 77, 77, - 78, 79, 80, 81, 82, 82, 83, 83, 83, 83, - 83, 83, 83, 84, 84, 85, 85, 86, 86, 87, - 87, 88, 88, 89, 89, 89, 90, 90, 90, 90, - 91, 91, 91, 91, 91, 91, 92, 93, 94, 95, - 95 + 0, 57, 58, 59, 59, 59, 59, 59, 59, 59, + 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, + 59, 59, 59, 60, 61, 62, 63, 64, 65, 66, + 67, 68, 69, 70, 71, 72, 72, 73, 73, 74, + 75, 75, 75, 75, 75, 76, 77, 77, 78, 78, + 78, 79, 80, 81, 82, 83, 83, 84, 84, 84, + 84, 84, 84, 84, 85, 85, 86, 86, 87, 87, + 88, 88, 89, 89, 90, 90, 90, 91, 91, 91, + 91, 92, 92, 92, 92, 92, 92, 93, 94, 95, + 96, 96 }; /* YYR2[RULE-NUM] -- Number of symbols on the right-hand side of rule RULE-NUM. */ @@ -842,12 +839,12 @@ static const yytype_int8 yyr2[] = 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2, 8, 5, 7, 0, 3, 5, 2, 1, - 1, 1, 1, 1, 8, 0, 3, 1, 1, 1, - 4, 7, 6, 2, 1, 3, 3, 3, 3, 3, - 3, 2, 1, 1, 2, 1, 3, 0, 3, 0, - 3, 0, 2, 0, 1, 3, 3, 3, 3, 3, - 1, 1, 1, 1, 1, 1, 7, 2, 4, 0, - 1 + 1, 1, 1, 1, 1, 8, 0, 3, 1, 1, + 1, 4, 7, 6, 2, 1, 3, 3, 3, 3, + 3, 3, 2, 1, 1, 2, 1, 3, 0, 3, + 0, 3, 0, 2, 0, 1, 3, 3, 3, 3, + 3, 1, 1, 1, 1, 1, 1, 7, 2, 4, + 0, 1 }; @@ -1709,93 +1706,93 @@ YYLTYPE yylloc = yyloc_default; switch (yyn) { case 2: /* commands: command_wrapper opt_semicolon */ -#line 175 "yacc_sql.y" +#line 176 "yacc_sql.y" { std::unique_ptr sql_node = std::unique_ptr((yyvsp[-1].sql_node)); sql_result->add_sql_node(std::move(sql_node)); } -#line 1718 "yacc_sql.cpp" +#line 1715 "yacc_sql.cpp" break; case 23: /* exit_stmt: EXIT */ -#line 205 "yacc_sql.y" +#line 206 "yacc_sql.y" { (void)yynerrs; // 这么写为了消除yynerrs未使用的告警。如果你有更好的方法欢迎提PR (yyval.sql_node) = new ParsedSqlNode(SCF_EXIT); } -#line 1727 "yacc_sql.cpp" +#line 1724 "yacc_sql.cpp" break; case 24: /* help_stmt: HELP */ -#line 211 "yacc_sql.y" +#line 212 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_HELP); } -#line 1735 "yacc_sql.cpp" +#line 1732 "yacc_sql.cpp" break; case 25: /* sync_stmt: SYNC */ -#line 216 "yacc_sql.y" +#line 217 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_SYNC); } -#line 1743 "yacc_sql.cpp" +#line 1740 "yacc_sql.cpp" break; case 26: /* begin_stmt: TRX_BEGIN */ -#line 222 "yacc_sql.y" +#line 223 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_BEGIN); } -#line 1751 "yacc_sql.cpp" +#line 1748 "yacc_sql.cpp" break; case 27: /* commit_stmt: TRX_COMMIT */ -#line 228 "yacc_sql.y" +#line 229 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_COMMIT); } -#line 1759 "yacc_sql.cpp" +#line 1756 "yacc_sql.cpp" break; case 28: /* rollback_stmt: TRX_ROLLBACK */ -#line 234 "yacc_sql.y" +#line 235 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_ROLLBACK); } -#line 1767 "yacc_sql.cpp" +#line 1764 "yacc_sql.cpp" break; case 29: /* drop_table_stmt: DROP TABLE ID */ -#line 240 "yacc_sql.y" +#line 241 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_DROP_TABLE); (yyval.sql_node)->drop_table.relation_name = (yyvsp[0].string); free((yyvsp[0].string)); } -#line 1777 "yacc_sql.cpp" +#line 1774 "yacc_sql.cpp" break; case 30: /* show_tables_stmt: SHOW TABLES */ -#line 247 "yacc_sql.y" +#line 248 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_SHOW_TABLES); } -#line 1785 "yacc_sql.cpp" +#line 1782 "yacc_sql.cpp" break; case 31: /* desc_table_stmt: DESC ID */ -#line 253 "yacc_sql.y" +#line 254 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_DESC_TABLE); (yyval.sql_node)->desc_table.relation_name = (yyvsp[0].string); free((yyvsp[0].string)); } -#line 1795 "yacc_sql.cpp" +#line 1792 "yacc_sql.cpp" break; case 32: /* create_index_stmt: CREATE INDEX ID ON ID LBRACE ID RBRACE */ -#line 262 "yacc_sql.y" +#line 263 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_CREATE_INDEX); CreateIndexSqlNode &create_index = (yyval.sql_node)->create_index; @@ -1806,11 +1803,11 @@ YYLTYPE yylloc = yyloc_default; free((yyvsp[-3].string)); free((yyvsp[-1].string)); } -#line 1810 "yacc_sql.cpp" +#line 1807 "yacc_sql.cpp" break; case 33: /* drop_index_stmt: DROP INDEX ID ON ID */ -#line 276 "yacc_sql.y" +#line 277 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_DROP_INDEX); (yyval.sql_node)->drop_index.index_name = (yyvsp[-2].string); @@ -1818,11 +1815,11 @@ YYLTYPE yylloc = yyloc_default; free((yyvsp[-2].string)); free((yyvsp[0].string)); } -#line 1822 "yacc_sql.cpp" +#line 1819 "yacc_sql.cpp" break; case 34: /* create_table_stmt: CREATE TABLE ID LBRACE attr_def attr_def_list RBRACE */ -#line 286 "yacc_sql.y" +#line 287 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_CREATE_TABLE); CreateTableSqlNode &create_table = (yyval.sql_node)->create_table; @@ -1839,19 +1836,19 @@ YYLTYPE yylloc = yyloc_default; std::reverse(create_table.attr_infos.begin(), create_table.attr_infos.end()); delete (yyvsp[-2].attr_info); } -#line 1843 "yacc_sql.cpp" +#line 1840 "yacc_sql.cpp" break; case 35: /* attr_def_list: %empty */ -#line 305 "yacc_sql.y" +#line 306 "yacc_sql.y" { (yyval.attr_infos) = nullptr; } -#line 1851 "yacc_sql.cpp" +#line 1848 "yacc_sql.cpp" break; case 36: /* attr_def_list: COMMA attr_def attr_def_list */ -#line 309 "yacc_sql.y" +#line 310 "yacc_sql.y" { if ((yyvsp[0].attr_infos) != nullptr) { (yyval.attr_infos) = (yyvsp[0].attr_infos); @@ -1861,11 +1858,11 @@ YYLTYPE yylloc = yyloc_default; (yyval.attr_infos)->emplace_back(*(yyvsp[-1].attr_info)); delete (yyvsp[-1].attr_info); } -#line 1865 "yacc_sql.cpp" +#line 1862 "yacc_sql.cpp" break; case 37: /* attr_def: ID type LBRACE number RBRACE */ -#line 322 "yacc_sql.y" +#line 323 "yacc_sql.y" { (yyval.attr_info) = new AttrInfoSqlNode; (yyval.attr_info)->type = (AttrType)(yyvsp[-3].number); @@ -1873,11 +1870,11 @@ YYLTYPE yylloc = yyloc_default; (yyval.attr_info)->length = (yyvsp[-1].number); free((yyvsp[-4].string)); } -#line 1877 "yacc_sql.cpp" +#line 1874 "yacc_sql.cpp" break; case 38: /* attr_def: ID type */ -#line 330 "yacc_sql.y" +#line 331 "yacc_sql.y" { (yyval.attr_info) = new AttrInfoSqlNode; (yyval.attr_info)->type = (AttrType)(yyvsp[0].number); @@ -1885,41 +1882,47 @@ YYLTYPE yylloc = yyloc_default; (yyval.attr_info)->length = 4; free((yyvsp[-1].string)); } -#line 1889 "yacc_sql.cpp" +#line 1886 "yacc_sql.cpp" break; case 39: /* number: NUMBER */ -#line 339 "yacc_sql.y" +#line 340 "yacc_sql.y" {(yyval.number) = (yyvsp[0].number);} -#line 1895 "yacc_sql.cpp" +#line 1892 "yacc_sql.cpp" break; case 40: /* type: INT_T */ -#line 342 "yacc_sql.y" +#line 343 "yacc_sql.y" { (yyval.number)=INTS; } -#line 1901 "yacc_sql.cpp" +#line 1898 "yacc_sql.cpp" break; case 41: /* type: STRING_T */ -#line 343 "yacc_sql.y" +#line 344 "yacc_sql.y" { (yyval.number)=CHARS; } -#line 1907 "yacc_sql.cpp" +#line 1904 "yacc_sql.cpp" break; case 42: /* type: FLOAT_T */ -#line 344 "yacc_sql.y" +#line 345 "yacc_sql.y" { (yyval.number)=FLOATS; } -#line 1913 "yacc_sql.cpp" +#line 1910 "yacc_sql.cpp" break; case 43: /* type: DATE_T */ -#line 345 "yacc_sql.y" +#line 346 "yacc_sql.y" { (yyval.number)=DATES; } -#line 1919 "yacc_sql.cpp" +#line 1916 "yacc_sql.cpp" + break; + + case 44: /* type: TEXT_T */ +#line 347 "yacc_sql.y" + { (yyval.number)=TEXTS; } +#line 1922 "yacc_sql.cpp" break; - case 44: /* insert_stmt: INSERT INTO ID VALUES LBRACE value value_list RBRACE */ -#line 349 "yacc_sql.y" + case 45: /* insert_stmt: INSERT INTO ID VALUES LBRACE value value_list RBRACE */ +#line 351 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_INSERT); (yyval.sql_node)->insertion.relation_name = (yyvsp[-5].string); @@ -1932,19 +1935,19 @@ YYLTYPE yylloc = yyloc_default; delete (yyvsp[-2].value); free((yyvsp[-5].string)); } -#line 1936 "yacc_sql.cpp" +#line 1939 "yacc_sql.cpp" break; - case 45: /* value_list: %empty */ -#line 365 "yacc_sql.y" + case 46: /* value_list: %empty */ +#line 367 "yacc_sql.y" { (yyval.value_list) = nullptr; } -#line 1944 "yacc_sql.cpp" +#line 1947 "yacc_sql.cpp" break; - case 46: /* value_list: COMMA value value_list */ -#line 368 "yacc_sql.y" + case 47: /* value_list: COMMA value value_list */ +#line 370 "yacc_sql.y" { if ((yyvsp[0].value_list) != nullptr) { (yyval.value_list) = (yyvsp[0].value_list); @@ -1954,40 +1957,40 @@ YYLTYPE yylloc = yyloc_default; (yyval.value_list)->emplace_back(*(yyvsp[-1].value)); delete (yyvsp[-1].value); } -#line 1958 "yacc_sql.cpp" +#line 1961 "yacc_sql.cpp" break; - case 47: /* value: NUMBER */ -#line 379 "yacc_sql.y" + case 48: /* value: NUMBER */ +#line 381 "yacc_sql.y" { (yyval.value) = new Value((int)(yyvsp[0].number)); (yyloc) = (yylsp[0]); } -#line 1967 "yacc_sql.cpp" +#line 1970 "yacc_sql.cpp" break; - case 48: /* value: FLOAT */ -#line 383 "yacc_sql.y" + case 49: /* value: FLOAT */ +#line 385 "yacc_sql.y" { (yyval.value) = new Value((float)(yyvsp[0].floats)); (yyloc) = (yylsp[0]); } -#line 1976 "yacc_sql.cpp" +#line 1979 "yacc_sql.cpp" break; - case 49: /* value: SSS */ -#line 387 "yacc_sql.y" + case 50: /* value: SSS */ +#line 389 "yacc_sql.y" { char *tmp = common::substr((yyvsp[0].string),1,strlen((yyvsp[0].string))-2); (yyval.value) = new Value(tmp); free(tmp); free((yyvsp[0].string)); } -#line 1987 "yacc_sql.cpp" +#line 1990 "yacc_sql.cpp" break; - case 50: /* delete_stmt: DELETE FROM ID where */ -#line 397 "yacc_sql.y" + case 51: /* delete_stmt: DELETE FROM ID where */ +#line 399 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_DELETE); (yyval.sql_node)->deletion.relation_name = (yyvsp[-1].string); @@ -1997,11 +2000,11 @@ YYLTYPE yylloc = yyloc_default; } free((yyvsp[-1].string)); } -#line 2001 "yacc_sql.cpp" +#line 2004 "yacc_sql.cpp" break; - case 51: /* update_stmt: UPDATE ID SET ID EQ value where */ -#line 409 "yacc_sql.y" + case 52: /* update_stmt: UPDATE ID SET ID EQ value where */ +#line 411 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_UPDATE); (yyval.sql_node)->update.relation_name = (yyvsp[-5].string); @@ -2014,11 +2017,11 @@ YYLTYPE yylloc = yyloc_default; free((yyvsp[-5].string)); free((yyvsp[-3].string)); } -#line 2018 "yacc_sql.cpp" +#line 2021 "yacc_sql.cpp" break; - case 52: /* select_stmt: SELECT select_attr FROM ID rel_list where */ -#line 424 "yacc_sql.y" + case 53: /* select_stmt: SELECT select_attr FROM ID rel_list where */ +#line 426 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_SELECT); if ((yyvsp[-4].rel_attr_list) != nullptr) { @@ -2038,31 +2041,31 @@ YYLTYPE yylloc = yyloc_default; } free((yyvsp[-2].string)); } -#line 2042 "yacc_sql.cpp" +#line 2045 "yacc_sql.cpp" break; - case 53: /* calc_stmt: CALC expression_list */ -#line 446 "yacc_sql.y" + case 54: /* calc_stmt: CALC expression_list */ +#line 448 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_CALC); std::reverse((yyvsp[0].expression_list)->begin(), (yyvsp[0].expression_list)->end()); (yyval.sql_node)->calc.expressions.swap(*(yyvsp[0].expression_list)); delete (yyvsp[0].expression_list); } -#line 2053 "yacc_sql.cpp" +#line 2056 "yacc_sql.cpp" break; - case 54: /* expression_list: expression */ -#line 456 "yacc_sql.y" + case 55: /* expression_list: expression */ +#line 458 "yacc_sql.y" { (yyval.expression_list) = new std::vector; (yyval.expression_list)->emplace_back((yyvsp[0].expression)); } -#line 2062 "yacc_sql.cpp" +#line 2065 "yacc_sql.cpp" break; - case 55: /* expression_list: expression COMMA expression_list */ -#line 461 "yacc_sql.y" + case 56: /* expression_list: expression COMMA expression_list */ +#line 463 "yacc_sql.y" { if ((yyvsp[0].expression_list) != nullptr) { (yyval.expression_list) = (yyvsp[0].expression_list); @@ -2071,70 +2074,70 @@ YYLTYPE yylloc = yyloc_default; } (yyval.expression_list)->emplace_back((yyvsp[-2].expression)); } -#line 2075 "yacc_sql.cpp" +#line 2078 "yacc_sql.cpp" break; - case 56: /* expression: expression '+' expression */ -#line 471 "yacc_sql.y" + case 57: /* expression: expression '+' expression */ +#line 473 "yacc_sql.y" { (yyval.expression) = create_arithmetic_expression(ArithmeticExpr::Type::ADD, (yyvsp[-2].expression), (yyvsp[0].expression), sql_string, &(yyloc)); } -#line 2083 "yacc_sql.cpp" +#line 2086 "yacc_sql.cpp" break; - case 57: /* expression: expression '-' expression */ -#line 474 "yacc_sql.y" + case 58: /* expression: expression '-' expression */ +#line 476 "yacc_sql.y" { (yyval.expression) = create_arithmetic_expression(ArithmeticExpr::Type::SUB, (yyvsp[-2].expression), (yyvsp[0].expression), sql_string, &(yyloc)); } -#line 2091 "yacc_sql.cpp" +#line 2094 "yacc_sql.cpp" break; - case 58: /* expression: expression '*' expression */ -#line 477 "yacc_sql.y" + case 59: /* expression: expression '*' expression */ +#line 479 "yacc_sql.y" { (yyval.expression) = create_arithmetic_expression(ArithmeticExpr::Type::MUL, (yyvsp[-2].expression), (yyvsp[0].expression), sql_string, &(yyloc)); } -#line 2099 "yacc_sql.cpp" +#line 2102 "yacc_sql.cpp" break; - case 59: /* expression: expression '/' expression */ -#line 480 "yacc_sql.y" + case 60: /* expression: expression '/' expression */ +#line 482 "yacc_sql.y" { (yyval.expression) = create_arithmetic_expression(ArithmeticExpr::Type::DIV, (yyvsp[-2].expression), (yyvsp[0].expression), sql_string, &(yyloc)); } -#line 2107 "yacc_sql.cpp" +#line 2110 "yacc_sql.cpp" break; - case 60: /* expression: LBRACE expression RBRACE */ -#line 483 "yacc_sql.y" + case 61: /* expression: LBRACE expression RBRACE */ +#line 485 "yacc_sql.y" { (yyval.expression) = (yyvsp[-1].expression); (yyval.expression)->set_name(token_name(sql_string, &(yyloc))); } -#line 2116 "yacc_sql.cpp" +#line 2119 "yacc_sql.cpp" break; - case 61: /* expression: '-' expression */ -#line 487 "yacc_sql.y" + case 62: /* expression: '-' expression */ +#line 489 "yacc_sql.y" { (yyval.expression) = create_arithmetic_expression(ArithmeticExpr::Type::NEGATIVE, (yyvsp[0].expression), nullptr, sql_string, &(yyloc)); } -#line 2124 "yacc_sql.cpp" +#line 2127 "yacc_sql.cpp" break; - case 62: /* expression: value */ -#line 490 "yacc_sql.y" + case 63: /* expression: value */ +#line 492 "yacc_sql.y" { (yyval.expression) = new ValueExpr(*(yyvsp[0].value)); (yyval.expression)->set_name(token_name(sql_string, &(yyloc))); delete (yyvsp[0].value); } -#line 2134 "yacc_sql.cpp" +#line 2137 "yacc_sql.cpp" break; - case 63: /* select_attr: '*' */ -#line 498 "yacc_sql.y" + case 64: /* select_attr: '*' */ +#line 500 "yacc_sql.y" { (yyval.rel_attr_list) = new std::vector; RelAttrSqlNode attr; @@ -2142,11 +2145,11 @@ YYLTYPE yylloc = yyloc_default; attr.attribute_name = "*"; (yyval.rel_attr_list)->emplace_back(attr); } -#line 2146 "yacc_sql.cpp" +#line 2149 "yacc_sql.cpp" break; - case 64: /* select_attr: rel_attr attr_list */ -#line 505 "yacc_sql.y" + case 65: /* select_attr: rel_attr attr_list */ +#line 507 "yacc_sql.y" { if ((yyvsp[0].rel_attr_list) != nullptr) { (yyval.rel_attr_list) = (yyvsp[0].rel_attr_list); @@ -2156,21 +2159,21 @@ YYLTYPE yylloc = yyloc_default; (yyval.rel_attr_list)->emplace_back(*(yyvsp[-1].rel_attr)); delete (yyvsp[-1].rel_attr); } -#line 2160 "yacc_sql.cpp" +#line 2163 "yacc_sql.cpp" break; - case 65: /* rel_attr: ID */ -#line 517 "yacc_sql.y" + case 66: /* rel_attr: ID */ +#line 519 "yacc_sql.y" { (yyval.rel_attr) = new RelAttrSqlNode; (yyval.rel_attr)->attribute_name = (yyvsp[0].string); free((yyvsp[0].string)); } -#line 2170 "yacc_sql.cpp" +#line 2173 "yacc_sql.cpp" break; - case 66: /* rel_attr: ID DOT ID */ -#line 522 "yacc_sql.y" + case 67: /* rel_attr: ID DOT ID */ +#line 524 "yacc_sql.y" { (yyval.rel_attr) = new RelAttrSqlNode; (yyval.rel_attr)->relation_name = (yyvsp[-2].string); @@ -2178,19 +2181,19 @@ YYLTYPE yylloc = yyloc_default; free((yyvsp[-2].string)); free((yyvsp[0].string)); } -#line 2182 "yacc_sql.cpp" +#line 2185 "yacc_sql.cpp" break; - case 67: /* attr_list: %empty */ -#line 533 "yacc_sql.y" + case 68: /* attr_list: %empty */ +#line 535 "yacc_sql.y" { (yyval.rel_attr_list) = nullptr; } -#line 2190 "yacc_sql.cpp" +#line 2193 "yacc_sql.cpp" break; - case 68: /* attr_list: COMMA rel_attr attr_list */ -#line 536 "yacc_sql.y" + case 69: /* attr_list: COMMA rel_attr attr_list */ +#line 538 "yacc_sql.y" { if ((yyvsp[0].rel_attr_list) != nullptr) { (yyval.rel_attr_list) = (yyvsp[0].rel_attr_list); @@ -2201,19 +2204,19 @@ YYLTYPE yylloc = yyloc_default; (yyval.rel_attr_list)->emplace_back(*(yyvsp[-1].rel_attr)); delete (yyvsp[-1].rel_attr); } -#line 2205 "yacc_sql.cpp" +#line 2208 "yacc_sql.cpp" break; - case 69: /* rel_list: %empty */ -#line 550 "yacc_sql.y" + case 70: /* rel_list: %empty */ +#line 552 "yacc_sql.y" { (yyval.relation_list) = nullptr; } -#line 2213 "yacc_sql.cpp" +#line 2216 "yacc_sql.cpp" break; - case 70: /* rel_list: COMMA ID rel_list */ -#line 553 "yacc_sql.y" + case 71: /* rel_list: COMMA ID rel_list */ +#line 555 "yacc_sql.y" { if ((yyvsp[0].relation_list) != nullptr) { (yyval.relation_list) = (yyvsp[0].relation_list); @@ -2224,55 +2227,55 @@ YYLTYPE yylloc = yyloc_default; (yyval.relation_list)->push_back((yyvsp[-1].string)); free((yyvsp[-1].string)); } -#line 2228 "yacc_sql.cpp" +#line 2231 "yacc_sql.cpp" break; - case 71: /* where: %empty */ -#line 566 "yacc_sql.y" + case 72: /* where: %empty */ +#line 568 "yacc_sql.y" { (yyval.condition_list) = nullptr; } -#line 2236 "yacc_sql.cpp" +#line 2239 "yacc_sql.cpp" break; - case 72: /* where: WHERE condition_list */ -#line 569 "yacc_sql.y" + case 73: /* where: WHERE condition_list */ +#line 571 "yacc_sql.y" { (yyval.condition_list) = (yyvsp[0].condition_list); } -#line 2244 "yacc_sql.cpp" +#line 2247 "yacc_sql.cpp" break; - case 73: /* condition_list: %empty */ -#line 575 "yacc_sql.y" + case 74: /* condition_list: %empty */ +#line 577 "yacc_sql.y" { (yyval.condition_list) = nullptr; } -#line 2252 "yacc_sql.cpp" +#line 2255 "yacc_sql.cpp" break; - case 74: /* condition_list: condition */ -#line 578 "yacc_sql.y" + case 75: /* condition_list: condition */ +#line 580 "yacc_sql.y" { (yyval.condition_list) = new std::vector; (yyval.condition_list)->emplace_back(*(yyvsp[0].condition)); delete (yyvsp[0].condition); } -#line 2262 "yacc_sql.cpp" +#line 2265 "yacc_sql.cpp" break; - case 75: /* condition_list: condition AND condition_list */ -#line 583 "yacc_sql.y" + case 76: /* condition_list: condition AND condition_list */ +#line 585 "yacc_sql.y" { (yyval.condition_list) = (yyvsp[0].condition_list); (yyval.condition_list)->emplace_back(*(yyvsp[-2].condition)); delete (yyvsp[-2].condition); } -#line 2272 "yacc_sql.cpp" +#line 2275 "yacc_sql.cpp" break; - case 76: /* condition: rel_attr comp_op value */ -#line 591 "yacc_sql.y" + case 77: /* condition: rel_attr comp_op value */ +#line 593 "yacc_sql.y" { (yyval.condition) = new ConditionSqlNode; (yyval.condition)->left_is_attr = 1; @@ -2284,11 +2287,11 @@ YYLTYPE yylloc = yyloc_default; delete (yyvsp[-2].rel_attr); delete (yyvsp[0].value); } -#line 2288 "yacc_sql.cpp" +#line 2291 "yacc_sql.cpp" break; - case 77: /* condition: value comp_op value */ -#line 603 "yacc_sql.y" + case 78: /* condition: value comp_op value */ +#line 605 "yacc_sql.y" { (yyval.condition) = new ConditionSqlNode; (yyval.condition)->left_is_attr = 0; @@ -2300,11 +2303,11 @@ YYLTYPE yylloc = yyloc_default; delete (yyvsp[-2].value); delete (yyvsp[0].value); } -#line 2304 "yacc_sql.cpp" +#line 2307 "yacc_sql.cpp" break; - case 78: /* condition: rel_attr comp_op rel_attr */ -#line 615 "yacc_sql.y" + case 79: /* condition: rel_attr comp_op rel_attr */ +#line 617 "yacc_sql.y" { (yyval.condition) = new ConditionSqlNode; (yyval.condition)->left_is_attr = 1; @@ -2316,11 +2319,11 @@ YYLTYPE yylloc = yyloc_default; delete (yyvsp[-2].rel_attr); delete (yyvsp[0].rel_attr); } -#line 2320 "yacc_sql.cpp" +#line 2323 "yacc_sql.cpp" break; - case 79: /* condition: value comp_op rel_attr */ -#line 627 "yacc_sql.y" + case 80: /* condition: value comp_op rel_attr */ +#line 629 "yacc_sql.y" { (yyval.condition) = new ConditionSqlNode; (yyval.condition)->left_is_attr = 0; @@ -2332,47 +2335,47 @@ YYLTYPE yylloc = yyloc_default; delete (yyvsp[-2].value); delete (yyvsp[0].rel_attr); } -#line 2336 "yacc_sql.cpp" +#line 2339 "yacc_sql.cpp" break; - case 80: /* comp_op: EQ */ -#line 641 "yacc_sql.y" + case 81: /* comp_op: EQ */ +#line 643 "yacc_sql.y" { (yyval.comp) = EQUAL_TO; } -#line 2342 "yacc_sql.cpp" +#line 2345 "yacc_sql.cpp" break; - case 81: /* comp_op: LT */ -#line 642 "yacc_sql.y" + case 82: /* comp_op: LT */ +#line 644 "yacc_sql.y" { (yyval.comp) = LESS_THAN; } -#line 2348 "yacc_sql.cpp" +#line 2351 "yacc_sql.cpp" break; - case 82: /* comp_op: GT */ -#line 643 "yacc_sql.y" + case 83: /* comp_op: GT */ +#line 645 "yacc_sql.y" { (yyval.comp) = GREAT_THAN; } -#line 2354 "yacc_sql.cpp" +#line 2357 "yacc_sql.cpp" break; - case 83: /* comp_op: LE */ -#line 644 "yacc_sql.y" + case 84: /* comp_op: LE */ +#line 646 "yacc_sql.y" { (yyval.comp) = LESS_EQUAL; } -#line 2360 "yacc_sql.cpp" +#line 2363 "yacc_sql.cpp" break; - case 84: /* comp_op: GE */ -#line 645 "yacc_sql.y" + case 85: /* comp_op: GE */ +#line 647 "yacc_sql.y" { (yyval.comp) = GREAT_EQUAL; } -#line 2366 "yacc_sql.cpp" +#line 2369 "yacc_sql.cpp" break; - case 85: /* comp_op: NE */ -#line 646 "yacc_sql.y" + case 86: /* comp_op: NE */ +#line 648 "yacc_sql.y" { (yyval.comp) = NOT_EQUAL; } -#line 2372 "yacc_sql.cpp" +#line 2375 "yacc_sql.cpp" break; - case 86: /* load_data_stmt: LOAD DATA INFILE SSS INTO TABLE ID */ -#line 651 "yacc_sql.y" + case 87: /* load_data_stmt: LOAD DATA INFILE SSS INTO TABLE ID */ +#line 653 "yacc_sql.y" { char *tmp_file_name = common::substr((yyvsp[-3].string), 1, strlen((yyvsp[-3].string)) - 2); @@ -2382,20 +2385,20 @@ YYLTYPE yylloc = yyloc_default; free((yyvsp[0].string)); free(tmp_file_name); } -#line 2386 "yacc_sql.cpp" +#line 2389 "yacc_sql.cpp" break; - case 87: /* explain_stmt: EXPLAIN command_wrapper */ -#line 664 "yacc_sql.y" + case 88: /* explain_stmt: EXPLAIN command_wrapper */ +#line 666 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_EXPLAIN); (yyval.sql_node)->explain.sql_node = std::unique_ptr((yyvsp[0].sql_node)); } -#line 2395 "yacc_sql.cpp" +#line 2398 "yacc_sql.cpp" break; - case 88: /* set_variable_stmt: SET ID EQ value */ -#line 672 "yacc_sql.y" + case 89: /* set_variable_stmt: SET ID EQ value */ +#line 674 "yacc_sql.y" { (yyval.sql_node) = new ParsedSqlNode(SCF_SET_VARIABLE); (yyval.sql_node)->set_variable.name = (yyvsp[-2].string); @@ -2403,11 +2406,11 @@ YYLTYPE yylloc = yyloc_default; free((yyvsp[-2].string)); delete (yyvsp[0].value); } -#line 2407 "yacc_sql.cpp" +#line 2410 "yacc_sql.cpp" break; -#line 2411 "yacc_sql.cpp" +#line 2414 "yacc_sql.cpp" default: break; } @@ -2636,7 +2639,7 @@ YYLTYPE yylloc = yyloc_default; return yyresult; } -#line 684 "yacc_sql.y" +#line 686 "yacc_sql.y" //_____________________________________________________________________ extern void scan_string(const char *str, yyscan_t scanner); diff --git a/src/observer/sql/parser/yacc_sql.hpp b/src/observer/sql/parser/yacc_sql.hpp index 6c4d125..62680c8 100644 --- a/src/observer/sql/parser/yacc_sql.hpp +++ b/src/observer/sql/parser/yacc_sql.hpp @@ -76,33 +76,34 @@ extern int yydebug; TRX_ROLLBACK = 277, /* TRX_ROLLBACK */ INT_T = 278, /* INT_T */ DATE_T = 279, /* DATE_T */ - STRING_T = 280, /* STRING_T */ - FLOAT_T = 281, /* FLOAT_T */ - HELP = 282, /* HELP */ - EXIT = 283, /* EXIT */ - DOT = 284, /* DOT */ - INTO = 285, /* INTO */ - VALUES = 286, /* VALUES */ - FROM = 287, /* FROM */ - WHERE = 288, /* WHERE */ - AND = 289, /* AND */ - SET = 290, /* SET */ - ON = 291, /* ON */ - LOAD = 292, /* LOAD */ - DATA = 293, /* DATA */ - INFILE = 294, /* INFILE */ - EXPLAIN = 295, /* EXPLAIN */ - EQ = 296, /* EQ */ - LT = 297, /* LT */ - GT = 298, /* GT */ - LE = 299, /* LE */ - GE = 300, /* GE */ - NE = 301, /* NE */ - NUMBER = 302, /* NUMBER */ - FLOAT = 303, /* FLOAT */ - ID = 304, /* ID */ - SSS = 305, /* SSS */ - UMINUS = 306 /* UMINUS */ + TEXT_T = 280, /* TEXT_T */ + STRING_T = 281, /* STRING_T */ + FLOAT_T = 282, /* FLOAT_T */ + HELP = 283, /* HELP */ + EXIT = 284, /* EXIT */ + DOT = 285, /* DOT */ + INTO = 286, /* INTO */ + VALUES = 287, /* VALUES */ + FROM = 288, /* FROM */ + WHERE = 289, /* WHERE */ + AND = 290, /* AND */ + SET = 291, /* SET */ + ON = 292, /* ON */ + LOAD = 293, /* LOAD */ + DATA = 294, /* DATA */ + INFILE = 295, /* INFILE */ + EXPLAIN = 296, /* EXPLAIN */ + EQ = 297, /* EQ */ + LT = 298, /* LT */ + GT = 299, /* GT */ + LE = 300, /* LE */ + GE = 301, /* GE */ + NE = 302, /* NE */ + NUMBER = 303, /* NUMBER */ + FLOAT = 304, /* FLOAT */ + ID = 305, /* ID */ + SSS = 306, /* SSS */ + UMINUS = 307 /* UMINUS */ }; typedef enum yytokentype yytoken_kind_t; #endif @@ -111,7 +112,7 @@ extern int yydebug; #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED union YYSTYPE { -#line 103 "yacc_sql.y" +#line 104 "yacc_sql.y" ParsedSqlNode * sql_node; ConditionSqlNode * condition; @@ -130,7 +131,7 @@ union YYSTYPE int number; float floats; -#line 134 "yacc_sql.hpp" +#line 135 "yacc_sql.hpp" }; typedef union YYSTYPE YYSTYPE; diff --git a/src/observer/sql/parser/yacc_sql.y b/src/observer/sql/parser/yacc_sql.y index 6af9aeb..7db5e27 100644 --- a/src/observer/sql/parser/yacc_sql.y +++ b/src/observer/sql/parser/yacc_sql.y @@ -76,6 +76,7 @@ ArithmeticExpr *create_arithmetic_expression(ArithmeticExpr::Type type, TRX_ROLLBACK INT_T DATE_T + TEXT_T STRING_T FLOAT_T HELP @@ -343,6 +344,7 @@ type: | STRING_T { $$=CHARS; } | FLOAT_T { $$=FLOATS; } | DATE_T { $$=DATES; } + | TEXT_T { $$=TEXTS; } ; insert_stmt: /*insert 语句的语法解析树*/ INSERT INTO ID VALUES LBRACE value value_list RBRACE diff --git a/src/observer/sql/stmt/insert_stmt.cpp b/src/observer/sql/stmt/insert_stmt.cpp index 91803fe..20e2c9f 100644 --- a/src/observer/sql/stmt/insert_stmt.cpp +++ b/src/observer/sql/stmt/insert_stmt.cpp @@ -49,10 +49,14 @@ RC InsertStmt::create(Db *db, const InsertSqlNode &inserts, Stmt *&stmt) // check fields type const int sys_field_num = table_meta.sys_field_num();// + + + for (int i = 0; i < value_num; i++) { const FieldMeta *field_meta = table_meta.field(i + sys_field_num); const AttrType field_type = field_meta->type(); const AttrType value_type = values[i].attr_type(); + if (!Value::convert(value_type, field_type, const_cast(values[i]))) { // TODO try to convert the value type to field type @@ -60,9 +64,19 @@ RC InsertStmt::create(Db *db, const InsertSqlNode &inserts, Stmt *&stmt) table_name, field_meta->name(), field_type, value_type); return RC::SCHEMA_FIELD_TYPE_MISMATCH; } + + bool match = field_meta->match(const_cast(values[i])); + if (!match) { + LOG_WARN("field does not match value(%s and %s)", + attr_type_to_string(field_meta->type()), + attr_type_to_string(values[i].attr_type())); + return RC::SCHEMA_FIELD_TYPE_MISMATCH; + } + } // everything alright stmt = new InsertStmt(table, values, value_num); + return RC::SUCCESS; } diff --git a/src/observer/storage/buffer/frame.h b/src/observer/storage/buffer/frame.h index ba5a4f0..4691c99 100644 --- a/src/observer/storage/buffer/frame.h +++ b/src/observer/storage/buffer/frame.h @@ -127,6 +127,13 @@ class Frame void read_unlatch(); void read_unlatch(intptr_t xid); + void set_text() { + if_text_ = true; + } + bool get_text() { + return if_text_; + } + friend std::string to_string(const Frame &frame); private: @@ -137,6 +144,7 @@ class Frame unsigned long acc_time_ = 0; int file_desc_ = -1; Page page_; + bool if_text_ = false; /// 在非并发编译时,加锁解锁动作将什么都不做 common::RecursiveSharedMutex lock_; diff --git a/src/observer/storage/field/field_meta.cpp b/src/observer/storage/field/field_meta.cpp index 4bae4c1..f7da347 100644 --- a/src/observer/storage/field/field_meta.cpp +++ b/src/observer/storage/field/field_meta.cpp @@ -18,6 +18,7 @@ See the Mulan PSL v2 for more details. */ #include "sql/parser/parse_defs.h" #include "json/json.h" +#include const static Json::StaticString FIELD_NAME("name"); const static Json::StaticString FIELD_TYPE("type"); @@ -66,6 +67,48 @@ int FieldMeta::len() const { return attr_len_; } bool FieldMeta::visible() const { return visible_; } +bool FieldMeta::match(Value &value) const { + if (value.attr_type() == BOOLEANS) { + value.set_int(value.get_int()); + } + if (attr_type_ == FLOATS && value.attr_type() == INTS) { + value.set_float(value.get_int()); + return true; + } + if (attr_type_ == INTS && value.attr_type() == FLOATS) { + value.set_int(std::round(value.get_float())); + return true; + } + if (attr_type_ == CHARS) { + value.set_string(value.to_string().c_str()); + return true; + } + if (attr_type_ == TEXTS) { + if (value.get_string().size() > 65535) + return false; + + value.set_text(value.get_string().c_str()); + value.set_text_f(); + return true; + } + if (value.attr_type() == CHARS) { + if (!is_float(value.to_string())) return false; + if (attr_type_ == INTS) { + value.set_int(std::stoi(value.to_string())); + } else if (attr_type_ == FLOATS) { + value.set_int(std::stof(value.to_string())); + } + return true; + } + if (attr_len_ < value.length() || attr_type_ != value.attr_type()) return false; + return true; +} + +bool FieldMeta::match(const Value &value) const { + if (attr_len_ < value.length() || attr_type_ != value.attr_type()) return false; + return true; +} + void FieldMeta::desc(std::ostream &os) const { os << "field name=" << name_ << ", type=" << attr_type_to_string(attr_type_) << ", len=" << attr_len_ diff --git a/src/observer/storage/field/field_meta.h b/src/observer/storage/field/field_meta.h index 799729e..b70c4d1 100644 --- a/src/observer/storage/field/field_meta.h +++ b/src/observer/storage/field/field_meta.h @@ -42,6 +42,8 @@ class FieldMeta int offset() const; int len() const; bool visible() const; + bool match(Value &value) const; + bool match(const Value &value) const; public: void desc(std::ostream &os) const; diff --git a/src/observer/storage/record/record.h b/src/observer/storage/record/record.h index a926cdb..44ebee7 100644 --- a/src/observer/storage/record/record.h +++ b/src/observer/storage/record/record.h @@ -26,6 +26,7 @@ See the Mulan PSL v2 for more details. */ #include "storage/index/index_meta.h" class Field; +class Table; /** * @brief 标识一个记录的位置 @@ -36,6 +37,11 @@ struct RID PageNum page_num; // record's page number SlotNum slot_num; // record's slot number + RID *next_RID = nullptr; + bool init = false; + size_t over_len; + size_t text_value = 0; + RID() = default; RID(const PageNum _page_num, const SlotNum _slot_num) : page_num(_page_num), slot_num(_slot_num) {} @@ -60,6 +66,18 @@ struct RID } } + bool set_overflow_rid(RID *rid) { + if (rid == nullptr) { + return false; + } + next_RID = rid; + return true; + } + + RID *get_overflow_rid() { + return next_RID; + } + /** * 返回一个不可能出现的最小的RID * 虽然page num 0和slot num 0都是合法的,但是page num 0通常用于存放meta数据,所以对数据部分来说都是 @@ -95,8 +113,10 @@ class Record ~Record() { if (owner_ && data_ != nullptr) { - free(data_); - data_ = nullptr; + if (!if_text_){ + free(data_); + data_ = nullptr; + } } } @@ -134,7 +154,7 @@ class Record void set_data_owner(char *data, int len) { ASSERT(len != 0, "the len of data should not be 0"); - this->~Record(); + //this->~Record(); this->data_ = data; this->len_ = len; @@ -145,6 +165,26 @@ class Record const char *data() const { return this->data_; } int len() const { return this->len_; } + void set_owner() { + owner_ = true; + } + void reset_owner() { + owner_ = false; + } + void set_if_text() { + if_text_ =true; + } + bool get_if_text() const{ + return if_text_; + } + void add_offset_text(int v) { + offset_text.push_back(v); + } + std::vector get_offset_text() const { + return offset_text; + } + + void set_rid(const RID &rid) { this->rid_ = rid; } void set_rid(const PageNum page_num, const SlotNum slot_num) { @@ -160,4 +200,6 @@ class Record char *data_ = nullptr; int len_ = 0; /// 如果不是record自己来管理内存,这个字段可能是无效的 bool owner_ = false; /// 表示当前是否由record来管理内存 + bool if_text_ =false; + std::vector offset_text; }; diff --git a/src/observer/storage/record/record_manager.cpp b/src/observer/storage/record/record_manager.cpp index 1bcc1be..cdaa803 100644 --- a/src/observer/storage/record/record_manager.cpp +++ b/src/observer/storage/record/record_manager.cpp @@ -182,6 +182,26 @@ RC RecordPageHandler::cleanup() return RC::SUCCESS; } +RC RecordPageHandler::update_record(const char *data, const RID *rid) { + ASSERT(readonly_ == false, "cannot update record into page while the page is readonly"); + if (rid->slot_num >= page_header_->record_capacity) { + LOG_ERROR("Invalid slot_num %d, exceed page's record capacity, page_num %d.", rid->slot_num, frame_->page_num()); + return RC::INVALID_ARGUMENT; + } + + Bitmap bitmap(bitmap_, page_header_->record_capacity); + if (bitmap.get_bit(rid->slot_num)) { + Record therecord; + get_record(rid, &therecord); + memcpy(therecord.data(), data, page_header_->record_real_size); + frame_->mark_dirty(); + return RC::SUCCESS; + } else { + LOG_DEBUG("Invalid slot_num %d, slot is empty, page_num %d.", rid->slot_num, frame_->page_num()); + return RC::RECORD_NOT_EXIST; + } +} + RC RecordPageHandler::insert_record(const char *data, RID *rid) { ASSERT(readonly_ == false, "cannot insert record into page while the page is readonly"); @@ -212,6 +232,38 @@ RC RecordPageHandler::insert_record(const char *data, RID *rid) return RC::SUCCESS; } +RC RecordPageHandler::insert_text_record(const char *data, size_t len, RID *rid) +{ + ASSERT(readonly_ == false, "cannot insert record into page while the page is readonly"); + + if (page_header_->record_num == page_header_->record_capacity) { + LOG_WARN("Page is full, page_num %d:%d.", disk_buffer_pool_->file_desc(), frame_->page_num()); + return RC::RECORD_NOMEM; + } + + // 找到空闲位置 + Bitmap bitmap(bitmap_, page_header_->record_capacity); + int index = bitmap.next_unsetted_bit(0); + bitmap.set_bit(index); + page_header_->record_num++; + + // assert index < page_header_->record_capacity + char *record_data = get_record_data(index); + memcpy(record_data, data, len); + + frame_->mark_dirty(); + + if (rid) { + rid->page_num = get_page_num(); + rid->slot_num = index; + rid->init = true; + rid->over_len = len; + } + + LOG_TRACE("Insert record. rid page_num=%d, slot num=%d", get_page_num(), index); + return RC::SUCCESS; +} + RC RecordPageHandler::recover_insert_record(const char *data, const RID &rid) { if (rid.slot_num >= page_header_->record_capacity) { @@ -391,6 +443,81 @@ RC RecordFileHandler::init_free_pages() return rc; } +RC RecordFileHandler::insert_text_record(const char *data, size_t record_size, RID *rid) +{ + RC ret = RC::SUCCESS; + + RecordPageHandler record_page_handler; + bool page_found = false; + PageNum current_page_num = 0; + + // 当前要访问free_pages对象,所以需要加锁。在非并发编译模式下,不需要考虑这个锁 + lock_.lock(); + + // 当前要访问free_pages对象,所以需要加锁。在非并发编译模式下,不需要考虑这个锁 + lock_.lock(); + + // 找到没有填满的页面 + while (!free_pages_.empty()) { + current_page_num = *free_pages_.begin(); + + ret = record_page_handler.init(*disk_buffer_pool_, current_page_num, false /*readonly*/); + if (ret != RC::SUCCESS) { + lock_.unlock(); + LOG_WARN("failed to init record page handler. page num=%d, rc=%d:%s", current_page_num, ret, strrc(ret)); + return ret; + }else{ + LOG_TRACE("Insert record FILEHandler insert_text_record))))))))))=>>"); + } + + if (!record_page_handler.is_full() && record_page_handler.if_text()) { + page_found = true; + record_page_handler.cleanup(); + free_pages_.erase(free_pages_.begin()); + break; + } + record_page_handler.cleanup(); + free_pages_.erase(free_pages_.begin()); + } + lock_.unlock(); // 如果找到了一个有效的页面,那么此时已经拿到了页面的写锁 + + // 找不到就分配一个新的页面 + if (!page_found) { + Frame *frame = nullptr; + if ((ret = disk_buffer_pool_->allocate_page(&frame)) != RC::SUCCESS) { + LOG_ERROR("Failed to allocate page while inserting record. ret:%d", ret); + return ret; + } + + current_page_num = frame->page_num(); + + ret = record_page_handler.init_empty_page(*disk_buffer_pool_, current_page_num, record_size); + if (ret != RC::SUCCESS) { + frame->unpin(); + LOG_ERROR("Failed to init empty page. ret:%d", ret); + // this is for allocate_page + return ret; + } + + // frame 在allocate_page的时候,是有一个pin的,在init_empty_page时又会增加一个,所以这里手动释放一个 + frame->unpin(); + + // 这里的加锁顺序看起来与上面是相反的,但是不会出现死锁 + // 上面的逻辑是先加lock锁,然后加页面写锁,这里是先加上 + // 了页面写锁,然后加lock的锁,但是不会引起死锁。 + // 为什么? + lock_.lock(); + free_pages_.insert(current_page_num); + lock_.unlock(); + + // 找到空闲位置 + free_pages_.erase(free_pages_.begin()); + frame->set_text(); + } + RC rc = record_page_handler.insert_text_record(data, record_size, rid); + return rc; +} + RC RecordFileHandler::insert_record(const char *data, int record_size, RID *rid) { RC ret = RC::SUCCESS; @@ -471,6 +598,17 @@ RC RecordFileHandler::recover_insert_record(const char *data, int record_size, c return record_page_handler.recover_insert_record(data, rid); } +RC RecordFileHandler::update_record(const char *data, int record_size, const RID *rid) { + RC rc = RC::SUCCESS; + RecordPageHandler page_handler; + if ((rc = page_handler.init(*disk_buffer_pool_, rid->page_num, false /*readonly*/)) != RC::SUCCESS) { + LOG_ERROR("Failed to init record page handler.page number=%d. rc=%s", rid->page_num, strrc(rc)); + return rc; + } + rc = page_handler.update_record(data, rid); + return rc; +} + RC RecordFileHandler::update_record(const RID *rid, Record &record, Field *field, const Value *value) { RC rc = RC::SUCCESS; @@ -491,6 +629,10 @@ RC RecordFileHandler::delete_record(const RID *rid) { RC rc = RC::SUCCESS; + if (rid->next_RID != nullptr) { + rc = delete_record(rid->next_RID); + } + RecordPageHandler page_handler; if ((rc = page_handler.init(*disk_buffer_pool_, rid->page_num, false /*readonly*/)) != RC::SUCCESS) { LOG_ERROR("Failed to init record page handler.page number=%d. rc=%s", rid->page_num, strrc(rc)); @@ -587,7 +729,7 @@ RC RecordFileScanner::open_scan( RC RecordFileScanner::fetch_next_record() { RC rc = RC::SUCCESS; - if (record_page_iterator_.is_valid()) { + if (record_page_iterator_.is_valid()&& !if_text()) { // 当前页面还是有效的,尝试看一下是否有有效记录 rc = fetch_next_record_in_page(); if (rc == RC::SUCCESS || rc != RC::RECORD_EOF) { @@ -603,6 +745,7 @@ RC RecordFileScanner::fetch_next_record() PageNum page_num = bp_iterator_.next(); record_page_handler_.cleanup(); rc = record_page_handler_.init(*disk_buffer_pool_, page_num, readonly_); + if (record_page_handler_.if_text()) continue; if (OB_FAIL(rc)) { LOG_WARN("failed to init record page handler. page_num=%d, rc=%s", page_num, strrc(rc)); return rc; @@ -630,6 +773,7 @@ RC RecordFileScanner::fetch_next_record() RC RecordFileScanner::fetch_next_record_in_page() { RC rc = RC::SUCCESS; + while (record_page_iterator_.has_next()) { rc = record_page_iterator_.next(next_record_); if (rc != RC::SUCCESS) { diff --git a/src/observer/storage/record/record_manager.h b/src/observer/storage/record/record_manager.h index e89852a..a832571 100644 --- a/src/observer/storage/record/record_manager.h +++ b/src/observer/storage/record/record_manager.h @@ -62,6 +62,12 @@ class Field; * 从这个页头描述的信息来看,当前仅支持定长行/记录。如果要支持变长记录, * 或者超长(超出一页)的记录,这么做是不合适的。 */ + +struct RecordDirectoryEntry { + RID location; + +}; + struct PageHeader { int32_t record_num; ///< 当前页面记录的个数 @@ -69,6 +75,7 @@ struct PageHeader int32_t record_size; ///< 每条记录占用实际空间大小(可能对齐) int32_t record_capacity; ///< 最大记录个数 int32_t first_record_offset; ///< 第一条记录的偏移量 + std::map text_length; ///< 变长数据的长度 }; /** @@ -168,6 +175,9 @@ class RecordPageHandler */ RC insert_record(const char *data, RID *rid); + RC update_record(const char *data, const RID *rid); + RC insert_text_record(const char *data, size_t len, RID *rid); + /** * @brief 更新一条记录 * @@ -209,6 +219,10 @@ class RecordPageHandler */ bool is_full() const; + bool if_text() const { + return frame_->get_text(); + } + protected: /** * @details @@ -258,6 +272,8 @@ class RecordFileHandler RecordFileHandler() = default; ~RecordFileHandler(); + +std::map overflow_pages; /** * @brief 初始化 * @@ -276,8 +292,14 @@ class RecordFileHandler * @param rid 待删除记录的标识符 */ RC delete_record(const RID *rid); - + +/** + * @brief 从指定文件中更新指定槽位的记录 + * @param data 纪录内容 + * @param rid 待更新记录的标识符 + */ RC update_record(const RID *rid,Record &record, Field *field, const Value *value); + RC update_record(const char *data, int record_size, const RID *rid); /** * @brief 插入一个新的记录到指定文件中,并返回该记录的标识符 @@ -288,6 +310,8 @@ class RecordFileHandler */ RC insert_record(const char *data, int record_size, RID *rid); + RC insert_text_record(const char *data, size_t record_size, RID *rid); + /** * @brief 数据库恢复时,在指定文件指定位置插入数据 * @@ -382,6 +406,10 @@ class RecordFileScanner * @brief 获取一个页面内的下一条记录 */ RC fetch_next_record_in_page(); + + bool if_text() { + return record_page_handler_.if_text(); + } private: // TODO 对于一个纯粹的record遍历器来说,不应该关心表和事务 diff --git a/src/observer/storage/table/table.cpp b/src/observer/storage/table/table.cpp index 292cfbc..5c09693 100644 --- a/src/observer/storage/table/table.cpp +++ b/src/observer/storage/table/table.cpp @@ -30,6 +30,7 @@ See the Mulan PSL v2 for more details. */ #include "storage/trx/trx.h" #include "storage/field/field.h" +RC make_text_value(RecordFileHandler *record_handler, Value &value); Table::~Table() { if (record_handler_ != nullptr) { @@ -244,8 +245,9 @@ RC Table::insert_record(Record &record) if (rc != RC::SUCCESS) { LOG_ERROR("Insert record failed. table name=%s, rc=%s", table_meta_.name(), strrc(rc)); return rc; + }else{ + LOG_TRACE("Insert record FILEHandler insert_record agin!!!!!!!!!))))))))))=>>"); } - rc = insert_entry_of_indexes(record.data(), record.rid()); if (rc != RC::SUCCESS) { // 可能出现了键值重复 RC rc2 = delete_entry_of_indexes(record.data(), record.rid(), false /*error_on_not_exists*/); @@ -335,23 +337,47 @@ RC Table::make_record(int value_num, const Value *values, Record &record) field->name(), field->type(), value.attr_type()); return RC::SCHEMA_FIELD_TYPE_MISMATCH; } - } + } + if (!field->match(value)) { + LOG_ERROR("Invalid value type. table name=%s, field name=%s, type=%d, but given=%d", + table_meta_.name(), field->name(), field->type(), value.attr_type()); + return RC::SCHEMA_FIELD_TYPE_MISMATCH; + }else{ + LOG_TRACE("Insert record. make_record))))))))))=>>>%s",value.get_string()); + } } // 复制所有字段的值 int record_size = table_meta_.record_size(); char *record_data = (char *)malloc(record_size); + //int column = table_meta_.field_num(); + for (int i = 0; i < value_num; i++) { const FieldMeta *field = table_meta_.field(i + normal_field_start_index); - const Value &value = values[i]; + Value &value = const_cast(values[i]); +// 如果是字符串类型,则拷贝字符串的len + 1字节。 + if (value.attr_type() == TEXTS) { + if (value.get_string().length() > 65535) { + return RC::TEXT_OVERFLOW; + } + handle_text_value(record_handler_, value); //如果现在的字段是text,那么立马进行插入,并且将value的值变成rid指针 + record.add_offset_text(field->offset()); + record.set_if_text(); + } + + size_t copy_len = field->len(); if (field->type() == CHARS) { const size_t data_len = value.length(); if (copy_len > data_len) { copy_len = data_len + 1; } + if (field->type() == TEXTS) { + copy_len = sizeof(RID); //拷贝RID大小,因为data就是RID + } } + memcpy(record_data + field->offset(), value.data(), copy_len); } @@ -523,6 +549,7 @@ RC Table::update_record(Record &record, Field *field, const Value *value) // main update section rc = record_handler_->update_record(&record.rid(), record, field, value); + // RC update_record(const char *data, int record_size, const RID *rid); // 更新索引 if (rc == RC::SUCCESS) { @@ -600,3 +627,52 @@ RC Table::sync() LOG_INFO("Sync table over. table=%s", name()); return rc; } + +RC handle_text_value(RecordFileHandler *record_handler, Value &value) +{ + RC rc = RC::SUCCESS; + int MAX_SIZE = 8000; //记录存储的最大值 + char *datass = new char[value.text_data().length() + 1]; //Text的总长度 + strcpy(datass, value.text_data().c_str()); + const char *end = datass + value.text_data().length(); //从后往前存,加上一个本身的长度到达字符串末尾 + + RID *rid = new RID; //最后的空指针 + while (end - datass > MAX_SIZE) { + RID *new_rid = new RID; + rc = record_handler->insert_text_record(end - MAX_SIZE, MAX_SIZE, new_rid); //存进一个record中 + + if (rc != RC::SUCCESS) { + LOG_ERROR("Insert text chunk failed: %s", strrc(rc)); + return rc; + }else{ + LOG_TRACE("Insert record.))))))))))=>>>%s",value.text_data()); + return rc; + } + + if (rid->init == true) { + new_rid->next_RID = rid; + } + rid = new_rid; + + end -= MAX_SIZE; + } + + RID *rid_last = new RID; + rc = record_handler->insert_text_record(datass, end - datass, rid_last); //将最后剩下的存进去 + if (rc != RC::SUCCESS) { + LOG_ERROR("Insert text chunk failed: %s", strrc(rc)); + return rc; + }else{ + LOG_TRACE("Insert record handle_text_value))))))))))=>>>%s",datass); + } + + if (rid->init == true) { + rid_last->next_RID = rid; + } + rid = rid_last; //存放链表头 + rid->text_value = value.text_data().length(); + value.set_data(reinterpret_cast(rid),sizeof(RID)); //将vaule的data设置成指针地址 + + delete[] datass; + return rc; +} diff --git a/src/observer/storage/table/table_meta.cpp b/src/observer/storage/table/table_meta.cpp index 3b55dbe..1542c7d 100644 --- a/src/observer/storage/table/table_meta.cpp +++ b/src/observer/storage/table/table_meta.cpp @@ -19,6 +19,8 @@ See the Mulan PSL v2 for more details. */ #include "storage/table/table_meta.h" #include "storage/trx/trx.h" #include "json/json.h" +#include "sql/parser/parse.h" + using namespace std; @@ -76,7 +78,10 @@ RC TableMeta::init(int32_t table_id, const char *name, int field_num, const Attr } for (int i = 0; i < field_num; i++) { - const AttrInfoSqlNode &attr_info = attributes[i]; + AttrInfoSqlNode attr_info = attributes[i]; + if (attr_info.type == TEXTS) { + attr_info.length = sizeof(RID); + } rc = fields_[i + trx_field_num].init( attr_info.name.c_str(), attr_info.type, field_offset, attr_info.length, true /*visible*/); if (rc != RC::SUCCESS) {