ob_sql_parameterization.h 5.4 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef OCEANBASE_SQL_PLAN_CACHE_OB_SQL_PARAMETERIZATION_
#define OCEANBASE_SQL_PLAN_CACHE_OB_SQL_PARAMETERIZATION_

#include "lib/hash/ob_link_hashmap.h"
#include "lib/string/ob_string.h"
#include "lib/container/ob_bit_set.h"
#include "lib/container/ob_vector.h"
#include "lib/container/ob_se_array.h"
#include "lib/utility/ob_print_utils.h"
#include "common/object/ob_object.h"
#include "sql/parser/ob_parser.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/plan_cache/ob_id_manager_allocator.h"
#include "sql/plan_cache/ob_plan_cache_util.h"

namespace oceanbase {
namespace common {
class ObIAllocator;
}

namespace sql {
typedef common::LinkHashValue<common::ObString> ParameterizationHashValue;

struct SqlInfo : public ParameterizationHashValue {
  SqlInfo& operator=(const SqlInfo& that)
  {
    total_ = that.total_;
    not_param_index_ = that.not_param_index_;
    neg_param_index_ = that.neg_param_index_;
    fixed_param_index_ = that.fixed_param_index_;
    param_charset_type_ = that.param_charset_type_;
    sql_traits_ = that.sql_traits_;
    last_active_time_ = that.last_active_time_;
    hit_count_ = that.hit_count_;
    change_char_index_ = that.change_char_index_;
    trans_from_minus_index_ = that.trans_from_minus_index_;
    must_be_positive_index_ = that.must_be_positive_index_;
    return *this;
  }
  void destroy()
  {}
  int64_t total_;
  common::ObBitSet<> not_param_index_;
  common::ObBitSet<> neg_param_index_;
  common::ObBitSet<> fixed_param_index_;
  common::ObBitSet<> trans_from_minus_index_;
  common::ObBitSet<> must_be_positive_index_;
  common::ObSEArray<common::ObCharsetType, 16> param_charset_type_;
  ObSqlTraits sql_traits_;

  int64_t last_active_time_;
  uint64_t hit_count_;

  common::ObBitSet<> change_char_index_;

  SqlInfo();
};

class TransformTreeCtx;
struct SelectItemTraverseCtx;

class ObSqlParameterization {
G
gm 已提交
75
public:
O
oceanbase-admin 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  static const int64_t SQL_PARAMETERIZATION_BUCKET_NUM = 1L << 20;
  static const int64_t NO_VALUES = -1;
  static const int64_t VALUE_LIST_LEVEL = 0;
  static const int64_t VALUE_VECTOR_LEVEL = 1;

  ObSqlParameterization()
  {}
  virtual ~ObSqlParameterization()
  {}
  static int fast_parser(common::ObIAllocator& allocator, ObSQLMode sql_mode,
      common::ObCollationType connection_collation, const common::ObString& sql, const bool enable_batched_multi_stmt,
      ObFastParserResult& fp_result);

  static int transform_syntax_tree(common::ObIAllocator& allocator, const ObSQLSessionInfo& session,
      const ObIArray<ObPCParam*>* raw_params, const int64_t sql_offset, ParseNode* tree, SqlInfo& sql_info,
      ParamStore& params, SelectItemParamInfoArray* select_item_param_infos,
      share::schema::ObMaxConcurrentParam::FixParamStore& fixed_param_store, bool is_transform_outline);
  static int raw_fast_parameterize_sql(common::ObIAllocator& allocator, const ObSQLSessionInfo& session,
      const common::ObString& sql, common::ObString& no_param_sql, common::ObIArray<ObPCParam*>& raw_params,
      ParseMode parse_mode = FP_MODE);
  static int check_and_generate_param_info(const common::ObIArray<ObPCParam*>& raw_params,
      const SqlInfo& not_param_info, common::ObIArray<ObPCParam*>& special_param_info);
  static int construct_sql(const common::ObString& no_param_sql, common::ObIArray<ObPCParam*>& not_params, char* buf,
      int32_t buf_len, int32_t& pos);
  static int parameterize_syntax_tree(common::ObIAllocator& allocator, bool is_transform_outline,
      ObPlanCacheCtx& pc_ctx, ParseNode* tree, ParamStore& params);
  static int gen_special_param_info(SqlInfo& sql_info, ObPlanCacheCtx& pc_ctx);
  static int insert_neg_sign(common::ObIAllocator& alloc_buf, ParseNode* node);
  static bool is_tree_not_param(const ParseNode* tree);

G
gm 已提交
106
private:
O
oceanbase-admin 已提交
107 108 109 110 111 112 113 114 115
  DISALLOW_COPY_AND_ASSIGN(ObSqlParameterization);
  static int is_fast_parse_const(TransformTreeCtx& ctx);

  static bool is_node_not_param(TransformTreeCtx& ctx);
  static int transform_tree(TransformTreeCtx& ctx, const ObSQLSessionInfo& session_info);
  static int add_param_flag(const ParseNode* node, SqlInfo& sql_info);
  static int add_not_param_flag(const ParseNode* node, SqlInfo& sql_info);
  static int add_varchar_charset(const ParseNode* node, SqlInfo& sql_info);
  static int mark_args(ParseNode* arg_tree, const bool* mark_arr, int64_t arg_num);
116
  static int mark_tree(ParseNode *tree, SqlInfo &sql_info);
O
oceanbase-admin 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  static int get_related_user_vars(const ParseNode* tree, common::ObIArray<common::ObString>& user_vars);

  static int get_select_item_param_info(const common::ObIArray<ObPCParam*>& raw_params, ParseNode* tree,
      SelectItemParamInfoArray* select_item_param_infos);
  static int parameterize_fields(SelectItemTraverseCtx& ctx);

  static int resolve_paramed_const(SelectItemTraverseCtx& ctx);
  static int transform_minus_op(ObIAllocator&, ParseNode*);

  static int find_leftest_const_node(ParseNode& cur_node, ParseNode*& const_node);
};

}  // namespace sql
}  // namespace oceanbase

#endif /* _OB_SQL_PARAMETERIZATION_H */