From 94e1d4dcfb09844e6a5ef3ca4d1d1db4f96c255d Mon Sep 17 00:00:00 2001 From: Wen Xin Date: Thu, 16 Nov 2023 16:23:26 +0800 Subject: [PATCH] include --- include/ast.h | 1806 +++++++++++++++++++++++++++++++++++++++++ include/connector.h | 44 + include/define.h | 797 ++++++++++++++++++ include/instantiate.h | 459 +++++++++++ include/mutate.h | 207 +++++ 5 files changed, 3313 insertions(+) create mode 100755 include/ast.h create mode 100755 include/connector.h create mode 100755 include/define.h create mode 100755 include/instantiate.h create mode 100755 include/mutate.h diff --git a/include/ast.h b/include/ast.h new file mode 100755 index 0000000..ea406eb --- /dev/null +++ b/include/ast.h @@ -0,0 +1,1806 @@ +#ifndef __AST_H__ +#define __AST_H__ +#include +#include +#include "define.h" +#include + +using namespace std; + +enum IRTYPE{ +#define DECLARE_TYPE(v) \ + v, +ALLTYPE(DECLARE_TYPE) +#undef DECLARE_TYPE +}; + +enum TERMINAL { +#define DECLARE_TERMINAL(v) \ + v, +ALLTERMINAL(DECLARE_TERMINAL) +#undef DECLARE_TERMINAL +}; + +#define DECLARE_CLASS(v) \ + class v ; +ALLCLASS(DECLARE_CLASS); +#undef DECLARE_CLASS + +enum CASEIDX{ + CASE0, CASE1, CASE2, CASE3, CASE4, CASE5, CASE6, CASE7, CASE8, + CASE9, CASE10, CASE11, CASE12, CASE13, CASE14, CASE15, CASE16, + CASE17, CASE18, CASE19, CASE20, CASE21, CASE22, CASE23, CASE24, + CASE25, CASE26, CASE27, CASE28, CASE29, CASE30, CASE31, CASE32, + CASE33, CASE34, CASE35, CASE36, CASE37, CASE38, CASE39, CASE40, + CASE41, CASE42, CASE43, CASE44, CASE45, CASE46, CASE47, CASE48, + CASE49, CASE50, CASE51, CASE52, CASE53, CASE54, CASE55, CASE56, + CASE57, CASE58, CASE59, CASE60, CASE61, CASE62, CASE63 +}; + +class IROperator{ +public: + IROperator(TERMINAL prefix, TERMINAL middle, TERMINAL suffix): + prefix_(prefix), middle_(middle), suffix_(suffix) {} + + TERMINAL prefix_; + TERMINAL middle_; + TERMINAL suffix_; +}; + +class IR{ +public: + IR(IRTYPE type, IROperator * op, IR * left, IR* right): type_(type), op_(op), left_(left), right_(right) {} + + IR(const IR* ir, IR* left, IR* right){ + this->type_ = ir->type_; + + if(ir->op_ != NULL) + this->op_ = OP3(ir->op_->prefix_, ir->op_->middle_, ir->op_->suffix_); + else + this->op_ = OP3(tEmpty, tEmpty, tEmpty); + + this->left_ = left; + this->right_ = right; + + this->str_val_ = ir->str_val_; + this->long_val_ = ir->long_val_; + } + + union{ + int int_val_; + long long_val_; + double float_val_; + bool bool_val_; + }; + + IRTYPE type_; + + string str_val_; + + IROperator* op_; + IR* left_; + IR* right_; + + string to_string(); + string to_string_core(); + void trim_string(string&); +}; + +class Node{ +public: + Node(){}; + ~Node(){}; + + char length_; + unsigned long int vector_; + + unsigned int case_idx_; + + virtual IR* translate() {}; + virtual void deep_delete() {}; +}; + +IR * deep_copy(const IR * root); +void deep_delete(IR * root); + +class ParseToplevel: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Stmtmulti* stmtmulti_; +}; + +class Stmtmulti: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Stmtmulti* stmtmulti_; + Stmt* stmt_; +}; + +class Stmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + CreateTableStmt* create_table_stmt_; + CreateViewStmt* create_view_stmt_; + CreateIndexStmt* create_index_stmt_; + SelectStmt* select_stmt_; + DropIndexStmt* drop_index_stmt_; + DropTableStmt* drop_table_stmt_; + DropViewStmt* drop_view_stmt_; + DeleteStmt* delete_stmt_; + UpdateStmt* update_stmt_; + InsertStmt* insert_stmt_; + AlterTableStmt* alter_table_stmt_; +}; + +class DeleteStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptSimpleLimit* opt_simple_limit_; + OptOrderClause* opt_order_clause_; + OptWhereClause* opt_where_clause_; + OptAlias* opt_alias_; + TableName* table_name_; + OptDeleteOptions* opt_delete_options_; + OptWithClause* opt_with_clause_; + TableReferenceList* table_reference_list_; + TableList* table_list_; +}; + +class OptDeleteOptions: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + DeleteOptions* delete_options_; +}; + +class DeleteOptions: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + DeleteOption* delete_option_; + DeleteOptions* delete_options_; +}; + +class DeleteOption: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class AlterTableStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + AlterList* alter_list_; + TableName* table_name_; +}; + +class AlterList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + AlterListItem* alter_list_item_; + AlterList* alter_list_; +}; + +class AlterListItem: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptPlace* opt_place_; + FieldDef* field_def_; + ColumnName* column_name_1_; + ColumnName* column_name_2_; + TableElementList* table_element_list_; + TableConstraintDef* table_constraint_def_; + ConstraintName* constraint_name_; + IndexName* index_name_1_; + IndexName* index_name_2_; + ExprRoot* expr_root_; + Visibility* visibility_; + TableName* table_name_; +}; + +class OptPlace: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ColumnName* column_name_; +}; + +class DropIndexStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableName* table_name_; + IndexName* index_name_; +}; + +class DropTableStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableList* table_list_; + IfExists* if_exists_; +}; + +class IfExists: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class TableList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableName* table_name_; + TableList* table_list_; +}; + +class DropViewStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableList* table_list_; + IfExists* if_exists_; +}; + +class UpdateStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptSimpleLimit* opt_simple_limit_; + OptOrderClause* opt_order_clause_; + OptWhereClause* opt_where_clause_; + UpdateList* update_list_; + TableReferenceList* table_reference_list_; + OptIgnore* opt_ignore_; + OptLowPriority* opt_low_priority_; + OptWithClause* opt_with_clause_; +}; + +class OptSimpleLimit: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Iconst* iconst_; +}; + +class OptWithClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WithClause* with_clause_; +}; + +class OptLowPriority: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class InsertStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptInsertUpdateList* opt_insert_update_list_; + OptValuesReference* opt_values_reference_; + InsertFromConstructor* insert_from_constructor_; + TableName* table_name_; + OptIgnore* opt_ignore_; + InsertLockOption* insert_lock_option_; + UpdateList* update_list_; + InsertQueryExpression* insert_query_expression_; +}; + +class InsertQueryExpression: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + QueryExpression* query_expression_; + ColumnList* column_list_; +}; + +class InsertFromConstructor: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ValuesList* values_list_; + ColumnList* column_list_; +}; + +class ValuesList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + RowValue* row_value_; + ValuesList* values_list_; +}; + +class RowValue: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptValues* opt_values_; +}; + +class OptValues: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Values* values_; +}; + +class Values: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ExprOrDefault* expr_or_default_; + Values* values_; +}; + +class ExprOrDefault: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ExprRoot* expr_root_; +}; + +class OptValuesReference: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptColumnList* opt_column_list_; + TableName* table_name_; +}; + +class OptInsertUpdateList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + UpdateList* update_list_; +}; + +class UpdateList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + UpdateElem* update_elem_; + UpdateList* update_list_; +}; + +class UpdateElem: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ExprOrDefault* expr_or_default_; + Columnref* columnref_; +}; + +class InsertLockOption: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptIgnore: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class CreateIndexStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + KeyListWithExpression* key_list_with_expression_; + TableName* table_name_; + IndexName* index_name_; + OptUnique* opt_unique_; +}; + +class OptUnique: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class CreateViewStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + QueryExpression* query_expression_; + OptColumnList* opt_column_list_; + ViewName* view_name_; + OptViewAlgorithm* opt_view_algorithm_; + OptOrReplace* opt_or_replace_; +}; + +class OptOrReplace: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptViewAlgorithm: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class CreateTableStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptDuplicateAsQe* opt_duplicate_as_qe_; + TableElementList* table_element_list_; + TableName* table_name_; + OptIfNotExists* opt_if_not_exists_; + OptTemporary* opt_temporary_; + DuplicateAsQe* duplicate_as_qe_; +}; + +class OptTemporary: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptIfNotExists: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptDuplicateAsQe: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + DuplicateAsQe* duplicate_as_qe_; +}; + +class DuplicateAsQe: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + QueryExpression* query_expression_; + Duplicate* duplicate_; +}; + +class Duplicate: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class TableElementList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableElement* table_element_; + TableElementList* table_element_list_; +}; + +class TableElement: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ColumnDef* column_def_; + TableConstraintDef* table_constraint_def_; +}; + +class ColumnDef: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + FieldDef* field_def_; + ColumnName* column_name_; +}; + +class FieldDef: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptColumnAttributeList* opt_column_attribute_list_; + DataType* data_type_; +}; + +class DataType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + NumberType* number_type_; + BoolType* bool_type_; + StringType* string_type_; +}; + +class StringType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptFieldLength* opt_field_length_; + FieldLength* field_length_; +}; + +class BoolType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class NumberType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptFieldOptions* opt_field_options_; + IntType* int_type_; + OptPrecision* opt_precision_; + RealType* real_type_; + FloatOptions* float_options_; + NumericType* numeric_type_; +}; + +class NumericType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class RealType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptPrecision: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Precision* precision_; +}; + +class IntType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptFieldOptions: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + FieldOptionList* field_option_list_; +}; + +class FieldOptionList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + FieldOption* field_option_; + FieldOptionList* field_option_list_; +}; + +class FieldOption: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptColumnAttributeList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ColumnAttributeList* column_attribute_list_; +}; + +class ColumnAttributeList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ColumnAttribute* column_attribute_; + ColumnAttributeList* column_attribute_list_; +}; + +class ColumnAttribute: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + NotNull* not_null_; + DefaultAttribute* default_attribute_; + Visibility* visibility_; +}; + +class DefaultAttribute: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ExprRoot* expr_root_; +}; + +class NotNull: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class Visibility: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptConstraintName: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ConstraintName* constraint_name_; +}; + +class CheckConstraint: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ExprRoot* expr_root_; +}; + +class TableConstraintDef: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + KeyListWithExpression* key_list_with_expression_; + OptIndexName* opt_index_name_; + ConstraintKeyType* constraint_key_type_; + OptConstraintName* opt_constraint_name_; + CheckConstraint* check_constraint_; + References* references_; + KeyList* key_list_; +}; + +class References: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptOnUpdateDelete* opt_on_update_delete_; + ColumnList* column_list_; + TableName* table_name_; +}; + +class OptOnUpdateDelete: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OnUpdateOption* on_update_option_1_; + OnUpdateOption* on_update_option_2_; +}; + +class OnUpdateOption: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class KeyList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + KeyPart* key_part_; + KeyList* key_list_; +}; + +class KeyListWithExpression: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + KeyPartWithExpression* key_part_with_expression_; + KeyListWithExpression* key_list_with_expression_; +}; + +class KeyPartWithExpression: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + KeyPart* key_part_; + OptOrderingDirection* opt_ordering_direction_; + ExprRoot* expr_root_; +}; + +class KeyPart: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptOrderingDirection* opt_ordering_direction_; + ColumnName* column_name_; + Iconst* iconst_; +}; + +class ConstraintKeyType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptIndexName: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + IndexName* index_name_; +}; + +class SelectStmt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + QueryExpression* query_expression_; +}; + +class QueryExpression: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptLimitClause* opt_limit_clause_; + OptOrderClause* opt_order_clause_; + QueryExpressionBody* query_expression_body_; + WithClause* with_clause_; +}; + +class QueryExpressionBody: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + QueryPrimary* query_primary_; + QueryExpressionParens* query_expression_parens_; + QueryExpressionBody* query_expression_body_1_; + QueryExpressionBody* query_expression_body_2_; + OptUnionOption* opt_union_option_; +}; + +class QueryPrimary: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + QuerySpecification* query_specification_; +}; + +class QuerySpecification: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptWindowClause* opt_window_clause_; + OptHavingClause* opt_having_clause_; + OptGroupClause* opt_group_clause_; + OptWhereClause* opt_where_clause_; + OptFromClause* opt_from_clause_; + SelectItemList* select_item_list_; + OptSelectOptions* opt_select_options_; +}; + +class OptWindowClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowDefinitionList* window_definition_list_; +}; + +class WindowDefinitionList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowDefinition* window_definition_; + WindowDefinitionList* window_definition_list_; +}; + +class WindowDefinition: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowSpec* window_spec_; + WindowName* window_name_; +}; + +class WindowSpec: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowSpecDetails* window_spec_details_; +}; + +class WindowSpecDetails: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptWindowFrameClause* opt_window_frame_clause_; + OptWindowOrderByClause* opt_window_order_by_clause_; + OptPartitionClause* opt_partition_clause_; + OptExistingWindowName* opt_existing_window_name_; +}; + +class OptExistingWindowName: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowName* window_name_; +}; + +class OptPartitionClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ExprRootList* expr_root_list_; +}; + +class OptWindowOrderByClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OrderList* order_list_; +}; + +class OrderList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OrderExpr* order_expr_; + OrderList* order_list_; +}; + +class OrderExpr: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptOrderingDirection* opt_ordering_direction_; + ExprRoot* expr_root_; +}; + +class OptOrderingDirection: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptWindowFrameClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowFrameExtent* window_frame_extent_; + WindowFrameUnits* window_frame_units_; +}; + +class WindowFrameUnits: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class WindowFrameExtent: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowFrameStart* window_frame_start_; + WindowFrameBetween* window_frame_between_; +}; + +class WindowFrameStart: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Iconst* iconst_; +}; + +class WindowFrameBetween: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowFrameBound* window_frame_bound_; + WindowFrameStart* window_frame_start_; +}; + +class WindowFrameBound: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowFrameStart* window_frame_start_; + Iconst* iconst_; +}; + +class OptHavingClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ExprRoot* expr_root_; +}; + +class OptGroupClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OlapOpt* olap_opt_; + ExprRootList* expr_root_list_; +}; + +class OlapOpt: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptWhereClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WhereClause* where_clause_; +}; + +class WhereClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ExprRoot* expr_root_; +}; + +class OptFromClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + FromClause* from_clause_; +}; + +class FromClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableReferenceList* table_reference_list_; +}; + +class TableReferenceList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableReference* table_reference_; + TableReferenceList* table_reference_list_; +}; + +class TableReference: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableFactor* table_factor_; + JoinedTable* joined_table_; +}; + +class TableFactor: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + SingleTable* single_table_; + DerivedTable* derived_table_; + JoinedTableParens* joined_table_parens_; + TableReferenceListParens* table_reference_list_parens_; +}; + +class TableReferenceListParens: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableReferenceList* table_reference_list_; + TableReference* table_reference_; +}; + +class JoinedTableParens: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + JoinedTable* joined_table_; +}; + +class DerivedTable: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptColumnList* opt_column_list_; + Alias* alias_; + Subquery* subquery_; +}; + +class OptColumnList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ColumnList* column_list_; +}; + +class ColumnList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ColumnName* column_name_; + ColumnList* column_list_; +}; + +class Subquery: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + QueryExpressionParens* query_expression_parens_; +}; + +class SingleTable: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptAlias* opt_alias_; + TableName* table_name_; +}; + +class OptAlias: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Alias* alias_; +}; + +class JoinedTable: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ExprRoot* expr_root_; + TableReference* table_reference_1_; + TableReference* table_reference_2_; + InnerJoinType* inner_join_type_; + ColumnList* column_list_; + OuterJoinType* outer_join_type_; + TableFactor* table_factor_; + NaturalJoinType* natural_join_type_; +}; + +class InnerJoinType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class NaturalJoinType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptInner* opt_inner_; + OptOuter* opt_outer_; +}; + +class OptInner: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptOuter: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OuterJoinType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptOuter* opt_outer_; +}; + +class SelectItemList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + SelectItem* select_item_; + SelectItemList* select_item_list_; +}; + +class SelectItem: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableWild* table_wild_; + OptAlias* opt_alias_; + ExprRoot* expr_root_; +}; + +class TableWild: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + TableName* table_name_; +}; + +class OptSelectOptions: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + SelectOptionList* select_option_list_; +}; + +class SelectOptionList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + SelectOption* select_option_; + SelectOptionList* select_option_list_; +}; + +class SelectOption: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class QueryExpressionParens: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + QueryExpressionParens* query_expression_parens_; + QueryExpression* query_expression_; +}; + +class OptUnionOption: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptOrderClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OrderClause* order_clause_; +}; + +class OrderClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OrderList* order_list_; +}; + +class OptLimitClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + LimitClause* limit_clause_; +}; + +class LimitClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + LimitOptions* limit_options_; +}; + +class LimitOptions: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Iconst* iconst_1_; + Iconst* iconst_2_; +}; + +class WithClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WithList* with_list_; +}; + +class WithList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + CommonTableExpr* common_table_expr_; + WithList* with_list_; +}; + +class CommonTableExpr: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Subquery* subquery_; + OptColumnList* opt_column_list_; + TableName* table_name_; +}; + +class ExprRootList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ExprRoot* expr_root_; + ExprRootList* expr_root_list_; +}; + +class ExprRoot: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Expr* expr_; +}; + +class Expr: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Expr* expr_1_; + Expr* expr_2_; + BoolPri* bool_pri_; +}; + +class BoolPri: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + BoolPri* bool_pri_; + Predicate* predicate_; + CompOp* comp_op_; + AllSubquery* all_subquery_; +}; + +class Predicate: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + InSubquery* in_subquery_; + BitExpr* bit_expr_1_; + BitExpr* bit_expr_2_; + Expr* expr_; + ExprList* expr_list_; + Predicate* predicate_; + SimpleExpr* simple_expr_; +}; + +class BitExpr: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + BitExpr* bit_expr_1_; + BitExpr* bit_expr_2_; + SimpleExpr* simple_expr_; +}; + +class SimpleExpr: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Columnref* columnref_; + Literal* literal_; + Iconst* iconst_; + Fconst* fconst_; + Sconst* sconst_; + SimpleExpr* simple_expr_; + Subquery* subquery_; + TypeCast* type_cast_; + CaseExpr* case_expr_; + Function* function_; + Expr* expr_; +}; + +class Function: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ControlFunction* control_function_; + MathFunction* math_function_; + StringFunction* string_function_; + AggregateFunction* aggregate_function_; + WindowFunction* window_function_; +}; + +class StringFunction: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Expr* expr_1_; + Expr* expr_2_; + Expr* expr_3_; + Expr* expr_4_; + Expr* expr_5_; + ExprList* expr_list_; +}; + +class MathFunction: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Expr* expr_1_; + Expr* expr_2_; + Expr* expr_3_; +}; + +class WindowFunction: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowingClause* windowing_clause_; + Iconst* iconst_; + OptNullTreatment* opt_null_treatment_; + OptLeadLagInfo* opt_lead_lag_info_; + Expr* expr_; + OptFromFirstLast* opt_from_first_last_; +}; + +class OptNullTreatment: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptFromFirstLast: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptLeadLagInfo: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + LeadLagInfo* lead_lag_info_; +}; + +class LeadLagInfo: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Iconst* iconst_; + Expr* expr_; +}; + +class AggregateFunction: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptWindowingClause* opt_windowing_clause_; + Expr* expr_; + OptDistinct* opt_distinct_; + ExprList* expr_list_; + OptGconcatSeparator* opt_gconcat_separator_; + OptOrderClause* opt_order_clause_; +}; + +class OptGconcatSeparator: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Sconst* sconst_; +}; + +class OptDistinct: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class OptWindowingClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowingClause* windowing_clause_; +}; + +class WindowingClause: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowNameOrSpec* window_name_or_spec_; +}; + +class WindowNameOrSpec: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WindowName* window_name_; + WindowSpec* window_spec_; +}; + +class ControlFunction: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Expr* expr_1_; + Expr* expr_2_; + Expr* expr_3_; +}; + +class CaseExpr: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptElse* opt_else_; + WhenClasueList* when_clasue_list_; + OptExpr* opt_expr_; +}; + +class WhenClasueList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + WhenList* when_list_; + WhenClasueList* when_clasue_list_; +}; + +class WhenList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Expr* expr_1_; + Expr* expr_2_; +}; + +class OptExpr: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Expr* expr_; +}; + +class OptElse: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Expr* expr_; +}; + +class TypeCast: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + CastType* cast_type_; + Expr* expr_; +}; + +class CastType: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + OptFieldLength* opt_field_length_; + FloatOptions* float_options_; +}; + +class FloatOptions: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + FieldLength* field_length_; + Precision* precision_; +}; + +class Precision: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Iconst* iconst_1_; + Iconst* iconst_2_; +}; + +class OptFieldLength: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + FieldLength* field_length_; +}; + +class FieldLength: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Iconst* iconst_; +}; + +class Literal: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class Columnref: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + ColumnName* column_name_; + TableName* table_name_; +}; + +class CompOp: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + +}; + +class AllSubquery: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Subquery* subquery_; +}; + +class InSubquery: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Subquery* subquery_; +}; + +class ExprList: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Expr* expr_; + ExprList* expr_list_; +}; + +class Iconst: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + long int long_val_; +}; + +class Fconst: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + double float_val_; +}; + +class Sconst: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + string str_val_; +}; + +class Ident: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + string str_val_; +}; + +class Alias: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Ident* ident_; +}; + +class ColumnName: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Ident* ident_; +}; + +class WindowName: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Ident* ident_; +}; + +class TableName: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Ident* ident_; +}; + +class ConstraintName: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Ident* ident_; +}; + +class IndexName: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Ident* ident_; +}; + +class ViewName: public Node { +public: + virtual IR* translate(); + virtual void deep_delete(); + + Ident* ident_; +}; + +#endif \ No newline at end of file diff --git a/include/connector.h b/include/connector.h new file mode 100755 index 0000000..7418dc4 --- /dev/null +++ b/include/connector.h @@ -0,0 +1,44 @@ +#ifndef __CONNECTOR_H__ +#define __CONNECTOR_H__ + +#include +#include +#include +#include +#include +#include +#include + +#include "libpq-fe.h" + +using namespace std; + +#define BUF_SIZE 1024 + +enum class SQLSTATUS { + ssNormal, + ssConnectFailed, + ssServerCrash, + ssSyntaxError, + ssSemanticError, + ssNoDatabase +}; + +class Connector { +public: + Connector(const char* host, const char* userName, const char* password, unsigned int port, const char* serverName); + + SQLSTATUS execute(const char* sql); + bool start_db(const char* file, char* const options[]); + bool close_db(); + +private: + string conninfo_; + string serverName_; + + void reset_database(PGconn* conn); + SQLSTATUS reset_db(); + pid_t get_pid_by_name(); +}; + +#endif \ No newline at end of file diff --git a/include/define.h b/include/define.h new file mode 100755 index 0000000..23d0fce --- /dev/null +++ b/include/define.h @@ -0,0 +1,797 @@ +#ifndef __DEFINE_H__ +#define __DEFINE_H__ + +#define SWITCHSTART switch(case_idx_){ +#define SWITCHEND default: assert(0); } + +#define CASESTART(idx) case CASE##idx: { +#define CASEEND break; } + +#define TRANSLATESTART IR *res = nullptr; +#define TRANSLATEEND return res; + +#define SAFETRANSLATE(a) (assert(a != nullptr), a->translate()) +#define SAFEDELETE(a) if(a != nullptr) a->deep_delete() + +#define OP0() new IROperator(TERMINAL::tEmpty, TERMINAL::tEmpty, TERMINAL::tEmpty) +#define OP3(a,b,c) new IROperator(a,b,c) + +#define ALLTYPE(V) \ + V(kNone) \ + V(kParseToplevel) \ + V(kStmtmulti) \ + V(kStmt) \ + V(kDeleteStmt) \ + V(kDeleteStmtTmp1) \ + V(kDeleteStmtTmp2) \ + V(kDeleteStmtTmp3) \ + V(kDeleteStmtTmp4) \ + V(kDeleteStmtTmp5) \ + V(kDeleteStmtTmp6) \ + V(kDeleteStmtTmp7) \ + V(kDeleteStmtTmp8) \ + V(kOptDeleteOptions) \ + V(kDeleteOptions) \ + V(kDeleteOption) \ + V(kAlterTableStmt) \ + V(kAlterList) \ + V(kAlterListItem) \ + V(kAlterListItemTmp1) \ + V(kAlterListItemTmp2) \ + V(kOptPlace) \ + V(kDropIndexStmt) \ + V(kDropTableStmt) \ + V(kIfExists) \ + V(kTableList) \ + V(kDropViewStmt) \ + V(kUpdateStmt) \ + V(kUpdateStmtTmp1) \ + V(kUpdateStmtTmp2) \ + V(kUpdateStmtTmp3) \ + V(kUpdateStmtTmp4) \ + V(kUpdateStmtTmp5) \ + V(kUpdateStmtTmp6) \ + V(kOptSimpleLimit) \ + V(kOptWithClause) \ + V(kOptLowPriority) \ + V(kInsertStmt) \ + V(kInsertStmtTmp1) \ + V(kInsertStmtTmp2) \ + V(kInsertStmtTmp3) \ + V(kInsertStmtTmp4) \ + V(kInsertStmtTmp5) \ + V(kInsertStmtTmp6) \ + V(kInsertStmtTmp7) \ + V(kInsertStmtTmp8) \ + V(kInsertStmtTmp9) \ + V(kInsertStmtTmp10) \ + V(kInsertQueryExpression) \ + V(kInsertFromConstructor) \ + V(kValuesList) \ + V(kRowValue) \ + V(kOptValues) \ + V(kValues) \ + V(kExprOrDefault) \ + V(kOptValuesReference) \ + V(kOptInsertUpdateList) \ + V(kUpdateList) \ + V(kUpdateElem) \ + V(kInsertLockOption) \ + V(kOptIgnore) \ + V(kCreateIndexStmt) \ + V(kCreateIndexStmtTmp1) \ + V(kCreateIndexStmtTmp2) \ + V(kOptUnique) \ + V(kCreateViewStmt) \ + V(kCreateViewStmtTmp1) \ + V(kCreateViewStmtTmp2) \ + V(kCreateViewStmtTmp3) \ + V(kOptOrReplace) \ + V(kOptViewAlgorithm) \ + V(kCreateTableStmt) \ + V(kCreateTableStmtTmp1) \ + V(kCreateTableStmtTmp2) \ + V(kCreateTableStmtTmp3) \ + V(kCreateTableStmtTmp4) \ + V(kCreateTableStmtTmp5) \ + V(kOptTemporary) \ + V(kOptIfNotExists) \ + V(kOptDuplicateAsQe) \ + V(kDuplicateAsQe) \ + V(kDuplicate) \ + V(kTableElementList) \ + V(kTableElement) \ + V(kColumnDef) \ + V(kFieldDef) \ + V(kDataType) \ + V(kStringType) \ + V(kBoolType) \ + V(kNumberType) \ + V(kNumberTypeTmp1) \ + V(kNumberTypeTmp2) \ + V(kNumericType) \ + V(kRealType) \ + V(kOptPrecision) \ + V(kIntType) \ + V(kOptFieldOptions) \ + V(kFieldOptionList) \ + V(kFieldOption) \ + V(kOptColumnAttributeList) \ + V(kColumnAttributeList) \ + V(kColumnAttribute) \ + V(kDefaultAttribute) \ + V(kNotNull) \ + V(kVisibility) \ + V(kOptConstraintName) \ + V(kCheckConstraint) \ + V(kTableConstraintDef) \ + V(kTableConstraintDefTmp1) \ + V(kTableConstraintDefTmp2) \ + V(kTableConstraintDefTmp3) \ + V(kTableConstraintDefTmp4) \ + V(kReferences) \ + V(kReferencesTmp1) \ + V(kOptOnUpdateDelete) \ + V(kOnUpdateOption) \ + V(kKeyList) \ + V(kKeyListWithExpression) \ + V(kKeyPartWithExpression) \ + V(kKeyPart) \ + V(kKeyPartTmp1) \ + V(kConstraintKeyType) \ + V(kOptIndexName) \ + V(kSelectStmt) \ + V(kQueryExpression) \ + V(kQueryExpressionTmp1) \ + V(kQueryExpressionTmp2) \ + V(kQueryExpressionBody) \ + V(kQueryExpressionBodyTmp1) \ + V(kQueryPrimary) \ + V(kQuerySpecification) \ + V(kQuerySpecificationTmp1) \ + V(kQuerySpecificationTmp2) \ + V(kQuerySpecificationTmp3) \ + V(kQuerySpecificationTmp4) \ + V(kQuerySpecificationTmp5) \ + V(kOptWindowClause) \ + V(kWindowDefinitionList) \ + V(kWindowDefinition) \ + V(kWindowSpec) \ + V(kWindowSpecDetails) \ + V(kWindowSpecDetailsTmp1) \ + V(kWindowSpecDetailsTmp2) \ + V(kOptExistingWindowName) \ + V(kOptPartitionClause) \ + V(kOptWindowOrderByClause) \ + V(kOrderList) \ + V(kOrderExpr) \ + V(kOptOrderingDirection) \ + V(kOptWindowFrameClause) \ + V(kWindowFrameUnits) \ + V(kWindowFrameExtent) \ + V(kWindowFrameStart) \ + V(kWindowFrameBetween) \ + V(kWindowFrameBound) \ + V(kOptHavingClause) \ + V(kOptGroupClause) \ + V(kOlapOpt) \ + V(kOptWhereClause) \ + V(kWhereClause) \ + V(kOptFromClause) \ + V(kFromClause) \ + V(kTableReferenceList) \ + V(kTableReference) \ + V(kTableFactor) \ + V(kTableReferenceListParens) \ + V(kJoinedTableParens) \ + V(kDerivedTable) \ + V(kDerivedTableTmp1) \ + V(kOptColumnList) \ + V(kColumnList) \ + V(kSubquery) \ + V(kSingleTable) \ + V(kOptAlias) \ + V(kJoinedTable) \ + V(kJoinedTableTmp1) \ + V(kJoinedTableTmp2) \ + V(kJoinedTableTmp3) \ + V(kJoinedTableTmp4) \ + V(kJoinedTableTmp5) \ + V(kJoinedTableTmp6) \ + V(kJoinedTableTmp7) \ + V(kJoinedTableTmp8) \ + V(kInnerJoinType) \ + V(kNaturalJoinType) \ + V(kOptInner) \ + V(kOptOuter) \ + V(kOuterJoinType) \ + V(kSelectItemList) \ + V(kSelectItem) \ + V(kTableWild) \ + V(kOptSelectOptions) \ + V(kSelectOptionList) \ + V(kSelectOption) \ + V(kQueryExpressionParens) \ + V(kOptUnionOption) \ + V(kOptOrderClause) \ + V(kOrderClause) \ + V(kOptLimitClause) \ + V(kLimitClause) \ + V(kLimitOptions) \ + V(kWithClause) \ + V(kWithList) \ + V(kCommonTableExpr) \ + V(kCommonTableExprTmp1) \ + V(kExprRootList) \ + V(kExprRoot) \ + V(kExpr) \ + V(kBoolPri) \ + V(kBoolPriTmp1) \ + V(kBoolPriTmp2) \ + V(kPredicate) \ + V(kPredicateTmp1) \ + V(kPredicateTmp2) \ + V(kBitExpr) \ + V(kSimpleExpr) \ + V(kFunction) \ + V(kStringFunction) \ + V(kStringFunctionTmp1) \ + V(kStringFunctionTmp2) \ + V(kStringFunctionTmp3) \ + V(kMathFunction) \ + V(kMathFunctionTmp1) \ + V(kWindowFunction) \ + V(kWindowFunctionTmp1) \ + V(kWindowFunctionTmp2) \ + V(kWindowFunctionTmp3) \ + V(kWindowFunctionTmp4) \ + V(kOptNullTreatment) \ + V(kOptFromFirstLast) \ + V(kOptLeadLagInfo) \ + V(kLeadLagInfo) \ + V(kAggregateFunction) \ + V(kAggregateFunctionTmp1) \ + V(kAggregateFunctionTmp2) \ + V(kAggregateFunctionTmp3) \ + V(kAggregateFunctionTmp4) \ + V(kOptGconcatSeparator) \ + V(kOptDistinct) \ + V(kOptWindowingClause) \ + V(kWindowingClause) \ + V(kWindowNameOrSpec) \ + V(kControlFunction) \ + V(kControlFunctionTmp1) \ + V(kCaseExpr) \ + V(kCaseExprTmp1) \ + V(kWhenClasueList) \ + V(kWhenList) \ + V(kOptExpr) \ + V(kOptElse) \ + V(kTypeCast) \ + V(kCastType) \ + V(kFloatOptions) \ + V(kPrecision) \ + V(kOptFieldLength) \ + V(kFieldLength) \ + V(kLiteral) \ + V(kColumnref) \ + V(kCompOp) \ + V(kAllSubquery) \ + V(kInSubquery) \ + V(kExprList) \ + V(kIconst) \ + V(kFconst) \ + V(kSconst) \ + V(kIdent) \ + V(kAlias) \ + V(kColumnName) \ + V(kWindowName) \ + V(kTableName) \ + V(kConstraintName) \ + V(kIndexName) \ + V(kViewName) \ + +#define ALLCLASS(V) \ + V(ParseToplevel) \ + V(Stmtmulti) \ + V(Stmt) \ + V(DeleteStmt) \ + V(OptDeleteOptions) \ + V(DeleteOptions) \ + V(DeleteOption) \ + V(AlterTableStmt) \ + V(AlterList) \ + V(AlterListItem) \ + V(OptPlace) \ + V(DropIndexStmt) \ + V(DropTableStmt) \ + V(IfExists) \ + V(TableList) \ + V(DropViewStmt) \ + V(UpdateStmt) \ + V(OptSimpleLimit) \ + V(OptWithClause) \ + V(OptLowPriority) \ + V(InsertStmt) \ + V(InsertQueryExpression) \ + V(InsertFromConstructor) \ + V(ValuesList) \ + V(RowValue) \ + V(OptValues) \ + V(Values) \ + V(ExprOrDefault) \ + V(OptValuesReference) \ + V(OptInsertUpdateList) \ + V(UpdateList) \ + V(UpdateElem) \ + V(InsertLockOption) \ + V(OptIgnore) \ + V(CreateIndexStmt) \ + V(OptUnique) \ + V(CreateViewStmt) \ + V(OptOrReplace) \ + V(OptViewAlgorithm) \ + V(CreateTableStmt) \ + V(OptTemporary) \ + V(OptIfNotExists) \ + V(OptDuplicateAsQe) \ + V(DuplicateAsQe) \ + V(Duplicate) \ + V(TableElementList) \ + V(TableElement) \ + V(ColumnDef) \ + V(FieldDef) \ + V(DataType) \ + V(StringType) \ + V(BoolType) \ + V(NumberType) \ + V(NumericType) \ + V(RealType) \ + V(OptPrecision) \ + V(IntType) \ + V(OptFieldOptions) \ + V(FieldOptionList) \ + V(FieldOption) \ + V(OptColumnAttributeList) \ + V(ColumnAttributeList) \ + V(ColumnAttribute) \ + V(DefaultAttribute) \ + V(NotNull) \ + V(Visibility) \ + V(OptConstraintName) \ + V(CheckConstraint) \ + V(TableConstraintDef) \ + V(References) \ + V(OptOnUpdateDelete) \ + V(OnUpdateOption) \ + V(KeyList) \ + V(KeyListWithExpression) \ + V(KeyPartWithExpression) \ + V(KeyPart) \ + V(ConstraintKeyType) \ + V(OptIndexName) \ + V(SelectStmt) \ + V(QueryExpression) \ + V(QueryExpressionBody) \ + V(QueryPrimary) \ + V(QuerySpecification) \ + V(OptWindowClause) \ + V(WindowDefinitionList) \ + V(WindowDefinition) \ + V(WindowSpec) \ + V(WindowSpecDetails) \ + V(OptExistingWindowName) \ + V(OptPartitionClause) \ + V(OptWindowOrderByClause) \ + V(OrderList) \ + V(OrderExpr) \ + V(OptOrderingDirection) \ + V(OptWindowFrameClause) \ + V(WindowFrameUnits) \ + V(WindowFrameExtent) \ + V(WindowFrameStart) \ + V(WindowFrameBetween) \ + V(WindowFrameBound) \ + V(OptHavingClause) \ + V(OptGroupClause) \ + V(OlapOpt) \ + V(OptWhereClause) \ + V(WhereClause) \ + V(OptFromClause) \ + V(FromClause) \ + V(TableReferenceList) \ + V(TableReference) \ + V(TableFactor) \ + V(TableReferenceListParens) \ + V(JoinedTableParens) \ + V(DerivedTable) \ + V(OptColumnList) \ + V(ColumnList) \ + V(Subquery) \ + V(SingleTable) \ + V(OptAlias) \ + V(JoinedTable) \ + V(InnerJoinType) \ + V(NaturalJoinType) \ + V(OptInner) \ + V(OptOuter) \ + V(OuterJoinType) \ + V(SelectItemList) \ + V(SelectItem) \ + V(TableWild) \ + V(OptSelectOptions) \ + V(SelectOptionList) \ + V(SelectOption) \ + V(QueryExpressionParens) \ + V(OptUnionOption) \ + V(OptOrderClause) \ + V(OrderClause) \ + V(OptLimitClause) \ + V(LimitClause) \ + V(LimitOptions) \ + V(WithClause) \ + V(WithList) \ + V(CommonTableExpr) \ + V(ExprRootList) \ + V(ExprRoot) \ + V(Expr) \ + V(BoolPri) \ + V(Predicate) \ + V(BitExpr) \ + V(SimpleExpr) \ + V(Function) \ + V(StringFunction) \ + V(MathFunction) \ + V(WindowFunction) \ + V(OptNullTreatment) \ + V(OptFromFirstLast) \ + V(OptLeadLagInfo) \ + V(LeadLagInfo) \ + V(AggregateFunction) \ + V(OptGconcatSeparator) \ + V(OptDistinct) \ + V(OptWindowingClause) \ + V(WindowingClause) \ + V(WindowNameOrSpec) \ + V(ControlFunction) \ + V(CaseExpr) \ + V(WhenClasueList) \ + V(WhenList) \ + V(OptExpr) \ + V(OptElse) \ + V(TypeCast) \ + V(CastType) \ + V(FloatOptions) \ + V(Precision) \ + V(OptFieldLength) \ + V(FieldLength) \ + V(Literal) \ + V(Columnref) \ + V(CompOp) \ + V(AllSubquery) \ + V(InSubquery) \ + V(ExprList) \ + V(Iconst) \ + V(Fconst) \ + V(Sconst) \ + V(Ident) \ + V(Alias) \ + V(ColumnName) \ + V(WindowName) \ + V(TableName) \ + V(ConstraintName) \ + V(IndexName) \ + V(ViewName) \ + +#define ALLTERMINAL(V) \ + V(tOpSemi) \ + V(tFrom) \ + V(tDelete) \ + V(tEmpty) \ + V(tQuick) \ + V(tLowPriority) \ + V(tIgnore) \ + V(tAlterTable) \ + V(tOpComma) \ + V(tAddColumn) \ + V(tOpRp) \ + V(tAddColumnOpLp) \ + V(tAdd) \ + V(tChangeColumn) \ + V(tModifyColumn) \ + V(tDropColumn) \ + V(tDropForeignKey) \ + V(tDropPrimaryKey) \ + V(tDropIndex) \ + V(tDropCheck) \ + V(tDropConstraint) \ + V(tSetDefaultOpLp) \ + V(tAlterColumn) \ + V(tDropDefault) \ + V(tSet) \ + V(tAlterIndex) \ + V(tRenameTo) \ + V(tTo) \ + V(tRenameIndex) \ + V(tRenameColumn) \ + V(tAfter) \ + V(tFirst) \ + V(tOn) \ + V(tDropTable) \ + V(tIfExists) \ + V(tDropView) \ + V(tUpdate) \ + V(tLimit) \ + V(tInto) \ + V(tInsert) \ + V(tOpLp) \ + V(tValues) \ + V(tOpRpValues) \ + V(tDefault) \ + V(tAs) \ + V(tOnDuplicateKeyUpdate) \ + V(tOpEqual) \ + V(tDelayed) \ + V(tHighPriority) \ + V(tIndex) \ + V(tCreate) \ + V(tUnique) \ + V(tView) \ + V(tOrReplace) \ + V(tAlgorithmOpEqualUndefined) \ + V(tAlgorithmOpEqualMerge) \ + V(tAlgorithmOpEqualTemptable) \ + V(tTable) \ + V(tTemporary) \ + V(tIfNotExists) \ + V(tReplace) \ + V(tChar) \ + V(tBinary) \ + V(tVarchar) \ + V(tVarbinary) \ + V(tLongVarbinary) \ + V(tTinyblob) \ + V(tMediumblob) \ + V(tLongblob) \ + V(tBlob) \ + V(tTinytext) \ + V(tMediumtext) \ + V(tLongtext) \ + V(tText) \ + V(tBool) \ + V(tBoolean) \ + V(tFloat) \ + V(tDecimal) \ + V(tNumeric) \ + V(tFixed) \ + V(tReal) \ + V(tDouble) \ + V(tInt) \ + V(tTinyint) \ + V(tSmallint) \ + V(tMediumint) \ + V(tBigint) \ + V(tSigned) \ + V(tUnsigned) \ + V(tZerofill) \ + V(tDefaultOpLp) \ + V(tNotNull) \ + V(tVisible) \ + V(tInvisible) \ + V(tConstraint) \ + V(tCheckOpLp) \ + V(tForeignKey) \ + V(tReferences) \ + V(tOnUpdate) \ + V(tOnDelete) \ + V(tCascade) \ + V(tSetNull) \ + V(tNoAction) \ + V(tSetDefault) \ + V(tPrimaryKey) \ + V(tUnion) \ + V(tExcept) \ + V(tIntersect) \ + V(tSelect) \ + V(tWindow) \ + V(tPartitionBy) \ + V(tOrderBy) \ + V(tAsc) \ + V(tDesc) \ + V(tRows) \ + V(tRange) \ + V(tUnboundedPreceding) \ + V(tPreceding) \ + V(tCurrentRow) \ + V(tAnd) \ + V(tBetween) \ + V(tUnboundedFollowing) \ + V(tFollowing) \ + V(tHaving) \ + V(tGroupBy) \ + V(tWithRollup) \ + V(tWhere) \ + V(tLateral) \ + V(tUsingOpLp) \ + V(tJoin) \ + V(tInnerJoin) \ + V(tCrossJoin) \ + V(tStraightJoin) \ + V(tNatural) \ + V(tNaturalRight) \ + V(tNaturalLeft) \ + V(tInner) \ + V(tOuter) \ + V(tLeft) \ + V(tRight) \ + V(tOpMul) \ + V(tOpDotOpMul) \ + V(tSqlNoCache) \ + V(tDistinct) \ + V(tSqlSmallResult) \ + V(tSqlBigResult) \ + V(tSqlBufferResult) \ + V(tSqlCalcFoundRows) \ + V(tAll) \ + V(tWith) \ + V(tWithRecursive) \ + V(tOr) \ + V(tXor) \ + V(tNot) \ + V(tIsTrue) \ + V(tIsNotTrue) \ + V(tIsFalse) \ + V(tIsNotFalse) \ + V(tIsUnknown) \ + V(tIsNotUnknown) \ + V(tIsNull) \ + V(tIsNotNull) \ + V(tInOpLp) \ + V(tNotInOpLp) \ + V(tNotBetween) \ + V(tSoundsLike) \ + V(tLike) \ + V(tNotLike) \ + V(tRegexp) \ + V(tNotRegexp) \ + V(tOpOr) \ + V(tOpAnd) \ + V(tOpShl) \ + V(tOpShr) \ + V(tOpAdd) \ + V(tOpSub) \ + V(tOpDivide) \ + V(tOpMod) \ + V(tOpXor) \ + V(tOpNot) \ + V(tExists) \ + V(tAsciiOpLp) \ + V(tBinOpLp) \ + V(tBitLengthOpLp) \ + V(tCharLengthOpLp) \ + V(tLengthOpLp) \ + V(tLowerOpLp) \ + V(tLtrimOpLp) \ + V(tOctOpLp) \ + V(tOrdOpLp) \ + V(tQuoteOpLp) \ + V(tReverseOpLp) \ + V(tRtrimOpLp) \ + V(tSpaceOpLp) \ + V(tTrimOpLp) \ + V(tTrimOpLpLeadingFrom) \ + V(tTrimOpLpTrailingFrom) \ + V(tUnhexOpLp) \ + V(tUpperOpLp) \ + V(tFindInSetOpLp) \ + V(tInstrOpLp) \ + V(tLeftOpLp) \ + V(tLocateOpLp) \ + V(tRepeatOpLp) \ + V(tRightOpLp) \ + V(tSubstringOpLp) \ + V(tTrimOpLpLeading) \ + V(tTrimOpLpTrailing) \ + V(tExportSetOpLp) \ + V(tLpadOpLp) \ + V(tReplaceOpLp) \ + V(tRpadOpLp) \ + V(tSubstringIndexOpLp) \ + V(tInsertOpLp) \ + V(tCharOpLp) \ + V(tConcatOpLp) \ + V(tConcatWsOpLp) \ + V(tEltOpLp) \ + V(tFieldOpLp) \ + V(tMakeSetOpLp) \ + V(tPiOpLpOpRp) \ + V(tRandOpLpOpRp) \ + V(tAbsOpLp) \ + V(tAcosOpLp) \ + V(tAsinOpLp) \ + V(tAtanOpLp) \ + V(tCeilingOpLp) \ + V(tCosOpLp) \ + V(tCotOpLp) \ + V(tDegreesOpLp) \ + V(tExpOpLp) \ + V(tFloorOpLp) \ + V(tHexOpLp) \ + V(tLnOpLp) \ + V(tRadiansOpLp) \ + V(tRandOpLp) \ + V(tRoundOpLp) \ + V(tSignOpLp) \ + V(tSinOpLp) \ + V(tSqrtOpLp) \ + V(tTanOpLp) \ + V(tFormatOpLp) \ + V(tLogOpLp) \ + V(tModOpLp) \ + V(tPowOpLp) \ + V(tTruncateOpLp) \ + V(tConvOpLp) \ + V(tRowNumberOpLpOpRp) \ + V(tRankOpLpOpRp) \ + V(tDenseRankOpLpOpRp) \ + V(tCumeDistOpLpOpRp) \ + V(tPercentRankOpLpOpRp) \ + V(tNtileOpLp) \ + V(tLeadOpLp) \ + V(tLagOpLp) \ + V(tFirstValueOpLp) \ + V(tLastValueOpLp) \ + V(tNthValueOpLp) \ + V(tRespectNulls) \ + V(tIgnoreNulls) \ + V(tFromFirst) \ + V(tFromLast) \ + V(tAvgOpLp) \ + V(tBitAndOpLp) \ + V(tBitOrOpLp) \ + V(tBitXorOpLp) \ + V(tCountOpLpOpMulOpRp) \ + V(tCountOpLp) \ + V(tCountOpLpDistinct) \ + V(tMinOpLp) \ + V(tMaxOpLp) \ + V(tSumOpLp) \ + V(tStdOpLp) \ + V(tStddevSampOpLp) \ + V(tVarianceOpLp) \ + V(tVarSampOpLp) \ + V(tGroupConcatOpLp) \ + V(tSeparator) \ + V(tOver) \ + V(tIfOpLp) \ + V(tIfnullOpLp) \ + V(tNullifOpLp) \ + V(tEnd) \ + V(tCase) \ + V(tThen) \ + V(tWhen) \ + V(tElse) \ + V(tCastOpLp) \ + V(tConvertOpLp) \ + V(tNchar) \ + V(tNull) \ + V(tFalse) \ + V(tTrue) \ + V(tOpDot) \ + V(tOpGreatereq) \ + V(tOpGreaterthan) \ + V(tOpLesseq) \ + V(tOpLessthan) \ + V(tOpNotequal) \ + V(tAny) \ + V(tSome) \ + V(tIn) \ + V(tNotIn) \ + V(tIconst) \ + V(tFconst) \ + V(tSconst) \ + V(tIdent) \ + +#define IRNUMBER 273 + +#endif \ No newline at end of file diff --git a/include/instantiate.h b/include/instantiate.h new file mode 100755 index 0000000..9e9afa3 --- /dev/null +++ b/include/instantiate.h @@ -0,0 +1,459 @@ +#ifndef __INSTANTIATE_H__ +#define __INSTANTIATE_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ast.h" +#include "mutate.h" + +using namespace std; + +typedef uint8_t name_t; + +#define UNREFERENCE static_cast(-1) +#define COLUMNNAMEMAX 64 +#define RESERVEDCOLUMNNAME (COLUMNNAMEMAX - 1) + +#define LIMITMIN 1 +#define LIMITMAX 128 +#define OFFSETMIN 0 +#define OFFSETMAX 16 + +#define DEFAULTROWNUMBER -1 +#define DEFAULTCOLUMNNUMBER -1 + +#define MINSTRINGLENGTH 0 +#define MAXSTRINGLENGTH 64 + +#define CAPACITYBASE 80 +#define MAXCAPACITY 120 +#define CAPACITYGROWTHRATE 1.02 +#define TIMETHRESHOLD (3600 * 1000) + +#ifdef DEBUG +#define NT_check(ir, type) (assert(ir->type_ == type), ir) +#else +#define NT_check(ir, type) ir +#endif + +enum class IdentifierType { + iTable, + iColumn, + iWindow, + iIndex, + iConstraint, + iView +}; +enum class ColumnStatus { + csNormal, + csDuplication +}; +enum class ColumnType { + ctNull, ctInt, ctTinyInt, ctSmallInt, ctMediumInt, ctBigInt, + ctReal, ctDouble, ctFloat, ctDecimal, ctNumeric, ctFixed, + ctBool, ctBoolean, + ctChar, ctBinary, ctVarchar, ctVarbinary, ctLongVarbinary, + ctTinyBlob, ctMediumBlob, ctLongBlob, ctBlob, + ctTinyText, ctMediumText, ctLongText, ctText +}; +enum class TypeCompatibility { + tcNoCompatibility, + tcWeakCompatibility, + tcFullyCompatibility +}; +enum class IndexType { + itNormal, + itFunctional +}; +enum class ConstraintType { + ctNull, + ctUnique, + ctPrimaryKey, + ctCheck, + ctForeignKey +}; +enum class StmtLocation { + slNotSubquery, + slSubquery, + slCTE, + slRecursiveCTE, + slRecursivePart1, + slRecursivePart2, + slTableReference, + slWhereClause, + slGroupClause, + slTargetList, + slHavingClause, + slWindowClause, + slOrderClause +}; +enum class WindowStatus { + wsNoInherit, + wsInherit, + wsCantBeInherited, + wsWithOrder +}; +enum class ColumnBasicType { + ctString, + ctNumber, + ctBool +}; + +class Table; +class SelectStmtInfo; +class Instantiator; +class Constraint; + +class Column { +public: + Column(name_t name, ColumnType type); + Column(Column* column); + + bool notNull_ = false; + bool hasDefault_ = false; + bool isVisible_ = true; + name_t identifierName_; + ColumnStatus duplicated_ = ColumnStatus::csNormal; + ColumnType columnType_; + Table* parentTable_ = nullptr; + + static vector> compatibility_; + static map typeTranslation_; + static map typeToBasicType_; + + static void instantiate(); +}; + +class Index { +public: + Index(name_t name); + + name_t identifierName_; + Constraint* constraint_ = nullptr; + map columns_; +}; + +class Constraint { +public: + Constraint(name_t name, ConstraintType type); + + name_t identifierName_; + ConstraintType constraintType_; + Index* index_ = nullptr; + map columns_; + map> refToOtherColumns_; +}; + +class Table { +public: + Table(name_t name, IdentifierType identifierType, SelectStmtInfo* inSelectStmtInfo); + Table(Table* otherTable, SelectStmtInfo* inSelectStmtInfo); + ~Table(); + + bool isSubQuery_ = false; + bool isTemporary_ = false; + bool hasPrimaryKey_ = false; + name_t identifierName_; + IdentifierType identifierType_; + SelectStmtInfo* inSelectStmtInfo_ = nullptr; + + vector acceColumnNames_; + vector columns_; + + vector indexes_; + + vector constraints_; + map> refFromOtherColumns_; + + bool check_drop_table(); + bool exist_duplicated_column(); + void update_duplicationInfo(vector& duplicationInfo); + void update_duplicatedColumns(vector& duplicationInfo); + + bool column_drop_check(Column* column); + Column* get_column(); + Column* get_column(ColumnType type); + name_t get_acce_column_name(); + vector get_column_seq(); + void add_column_first(Column* column); + bool add_column(Column* column, Column* afterColumn = nullptr); + void move_column_first(Column* column); + bool move_column(Column* column, Column* afterColumn); + bool drop_column(Column* column); + + void column_rename(Column* column, name_t oldName, name_t newName); + + Index* get_index(); + vector get_index(Column* column); + bool add_index(Index* index); + bool drop_index(Index* index); + + Constraint* get_constraint(); + vector get_constraint(Column* column); + bool add_constraint(Constraint* constraint, Index* index = nullptr); + bool drop_constraint(Constraint* constraint); + bool set_references(Column* curTableColumn, Column* otherTableColumn); + bool remove_references(Column* curTableColumn, Column* otherTableColumn); + + bool remove_column_name(name_t name); + bool add_column_name(name_t name); + void drop_index(Column* column); + void drop_constraint(Column* column); + + bool index_drop_check(Index* index); + + bool constraint_drop_check(Constraint* constraint); +}; + +class SelectStmtInfo { +public: + SelectStmtInfo(StmtLocation location, int rowNumber, int columnNumber); + ~SelectStmtInfo(); + + bool hasAggregate = false; + + int columnNumber_; + int rowNumber_; + + name_t recursiveCteName_ = UNREFERENCE; + Table* recursiveCte_ = nullptr; + + StmtLocation location_; + + vector innerCteTables_; + vector outCteTables_; + + vector outReferences_; + + vector usedOutColumns_; + vector usedInnerColumns_; + + vector usedTables_; + vector acceColumns_; + + map windowStatusRecord_; + + bool is_duplicated_table(Table* table); + bool exist_duplicated_column(vector& v1, vector& v2); + bool has_column_limit(); + bool has_row_limit(); + + void add_used_table(Table* table); + void clear_local_info(); + void clear_cte(); + + void update_acceColumns(); + void update_acceColumns(vector& usedTables, vector& duplicationInfo); + + void add_usedInnerColumns(Column* column); + void add_usedOutColumns(Column* column); + void add_out_usedColumns(vector& usedColumns); + void add_all_usedColumns(vector& usedColumns); + +private: + vector duplicationInfo_ = vector(COLUMNNAMEMAX, 0); +}; + +class GlobalStatusManger { +public: + ~GlobalStatusManger(); + + stack selectInfoStack_; + + vector ignoreInnerUsedColumns_ = { StmtLocation::slWhereClause, StmtLocation::slTableReference, StmtLocation::slOrderClause }; + + void reset_status(); + + void push_selectInfo(StmtLocation location, int rowNumber, int columnNumber); + void pop_selectInfo(); + + SelectStmtInfo* get_cur_selectInfo(); + + int get_acce_table_number(); + Table* get_acce_table(int index); + vector get_acce_table(IdentifierType type); + + name_t get_new_table_name(); + name_t get_new_view_name(); + name_t get_new_window_name(); + name_t get_new_index_name(); + name_t get_new_constraint_name(); + + bool add_table(Table* table); + bool drop_table(Table* table); + bool drop_table(name_t name, IdentifierType type); + +private: + name_t totalTableName_ = 0; + name_t totalViewName_ = 0; + name_t totalWindowName_ = 0; + name_t totalIndexName_ = 0; + name_t totalConstraintName_ = 0; + + vector globalTables_; + + void pop_cur_selectInfo(); +}; + +class IRTrim { +public: + IRTrim(Instantiator* instantiator); + ~IRTrim(); + + void instantiate_simple_limit(); + void instantiate_simple_column_list(); + void instantiate_simple_alias(); + void instantiate_simple_expr_root(); + void instantiate_simple_expr_root_true(); + void instantiate_simple_expr_root_list(); + void instantiate_simple_simple_select_item_list(); + void instantiate_simple_window_spec(); + void instantiate_simple_index_name(); + void instantiate_simple_constraint_name(); + void instantiate_simple_key_part(); + void instantiate_simple_key_part_list(); + void instantiate_simple_table_element(); + void instantiate_simple_values(); + + IR* get_expr_root_columnref(Column* column); + void set_recursive_limit(IR* expr_root); + + IR* simple_limit_ = nullptr; + IR* simple_column_list_ = nullptr; + IR* simple_alias_ = nullptr; + IR* simple_expr_root_ = nullptr; + IR* simple_expr_root_true_ = nullptr; + IR* simple_expr_root_list_ = nullptr; + IR* simple_select_item_list_ = nullptr; + IR* simple_window_spec_ = nullptr; + IR* simple_index_name_ = nullptr; + IR* simple_constraint_name_ = nullptr; + IR* simple_key_part_ = nullptr; + IR* simple_key_part_list_ = nullptr; + IR* simple_table_element_ = nullptr; + IR* simple_values_ = nullptr; + + IR* expr_root_columnref_ = nullptr; + +private: + vector simpleIR_; + Instantiator* instantiator_; +}; + +class ExprInstantiator { +public: + ExprInstantiator(Instantiator* m): instantiator_(m){} + + vector usedColumnsBuffer_; + + void instantiate_expr_root(IR* expr_root, vector& acceColumns, StmtLocation location); + void instantiate_expr(IR* expr, vector& acceColumns, StmtLocation location); + void instantiate_simple_expr(IR* simple_expr, vector& acceColumns, StmtLocation location); + void instantiate_type_cast(IR* type_cast, vector& acceColumns, StmtLocation location); + void instantiate_function(IR* function, vector& acceColumns, StmtLocation location); + void instantiate_aggregate_function(IR* aggregate_function, vector& acceColumns, StmtLocation location); + void instantiate_window_function(IR* window_function, vector& acceColumns, StmtLocation location); + + long int get_random_integer(long int min = LONG_MIN, long int max = LONG_MAX); + double get_random_float(int min = INT_MIN, int max = INT_MAX); + string get_random_string(int minLength, int maxLength); + +private: + Instantiator* instantiator_; + vector ignoreSimpleExpr_ = { IRTYPE::kSimpleExpr, IRTYPE::kCaseExpr, IRTYPE::kExpr, IRTYPE::kLiteral }; +}; + +class Instantiator { +public: + Instantiator(); + ~Instantiator(); + + chrono::time_point startTime; + unsigned int get_duration_ms(); + + bool instantaite_sql(IR* root); + int calc_capacity(IR* root); + int calc_node(IR* root); + + IRTrim* IRTrim_; + GlobalStatusManger* globalStatusManger_; + ExprInstantiator* exprInstantiator_; + + void instantiate_parse_toplevel(IR* parse_toplevel); + + void instantiate_alter_table_stmt(IR* alter_table_stmt); + void instantiate_alter_list_item(IR* alter_list_item, Table* table); + + void instantiate_insert_stmt(IR* insert_stmt); + void instantiate_values_list(IR* values_list, int chosenColumnNumber); + void instantiate_values(IR* values, int chosenColumnNumber); + + void instantiate_update_stmt(IR* update_stmt); + int instantiate_update_list(IR* update_list, Table* table); + + void instantiate_delete_stmt(IR* delete_stmt); + + void instantiate_drop_table_or_view_stmt(IR* drop_table_stmt, IdentifierType type); + + void instantiate_drop_index_stmt(IR* drop_index_stmt); + + void instantiate_create_index_stmt(IR* create_index_stmt); + + void instantiate_create_view_stmt(IR* create_view_stmt); + + void instantiate_create_table_stmt(IR* create_table_stmt); + void instantiate_table_element_list(IR* table_element_list, Table* table); + pair> instantiate_column_def(IR* column_def, Table* table); + ColumnType instantiate_data_type(IR* data_type); + void instantiate_column_attribute(IR* column_attribute, Column* column, Table* table); + map instantiate_key_list_with_expression(IR* key_list_with_expression, Table* table, ConstraintType type); + bool instantiate_table_constraint_def_index(IR* table_constraint_def, Table* table); + bool instantiate_table_constraint_def_key(IR* table_constraint_def, Table* table); + bool instantiate_table_constraint_def_check(IR* table_constraint_def, Table* table); + bool instantiate_table_constraint_def_foreign_key(IR* table_constraint_def, Table* table); + + void instantiate_select_stmt(IR* select_stmt); + Table* instantiate_query_expression(IR* subquery, StmtLocation location, int rowNumber, int columnNumber); + Table* instantiate_query_expression(IR* query_expression); + Table* instantiate_query_primary(IR* query_primary); + Table* instanitate_union(IR* query_expression_body); + + void instantiate_with_clause(IR* with_clause); + void instantiate_order_clause(IR* order_clause); + void instantiate_limit_clause(IR* limit_clause); + + Table* instantiate_select_item_list(IR* select_item_list); + void instantiate_from_clause(IR* from_clause); + vector instantiate_table_reference(IR* table_reference); + Table* instantiate_single_table(IR* single_table); + Table* instantiate_derived_table(IR* derived_table); + vector instantiate_joined_table(IR* joined_table); + void instantiate_where_clause(IR* where_clause); + void instantiate_group_clause(IR* group_clause); + void instantiate_having_clause(IR* having_clause); + void instantiate_window_clause(IR* window_clause); + void instantiate_window_definition(IR* window_definition); + WindowStatus instantiate_window_spec(IR* window_spec); + + void instantiate_column_list(IR* column_list, vector& columns); + void instantiate_column_list(IR* column_list, vector& columns); + void instantiate_key_list(IR* key_list, vector& columns); + void instantiate_ident(IR* identifier_name, IdentifierType type, name_t name); + + bool check_foreign_key(Table* table, vector& curTableColumn, vector& otherTableColumn); +}; + +#endif diff --git a/include/mutate.h b/include/mutate.h new file mode 100755 index 0000000..800c8a4 --- /dev/null +++ b/include/mutate.h @@ -0,0 +1,207 @@ +#ifndef __MUTATOR_H__ +#define __MUTATOR_H__ + + +#include "ast.h" +#include "define.h" +#include "instantiate.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#define TOPNUMBER 10 + +#ifdef CLOSEMETHOD +#define IRTYPEUPDATETHRESHOLD UINT_MAX +#define IRINFOUPDATETHRESHOLD UINT_MAX +#else +#define IRTYPEUPDATETHRESHOLD 100 +#define IRINFOUPDATETHRESHOLD 100 +#endif + +#define REWARDINCREMENT 10 +#define REWARDNEWCOVERAGE 10 + +#define TIMETHRESHOLD (3600 * 1000) + +#ifdef BALANCE +#define MUTATENUMBER 2 +#endif + +using namespace std; + +#define ERRORINDEX USHRT_MAX + +enum class MutateOperator { + moReplace, + moInsert, + moDelete +}; + +double get_random_double(int min, int max); + +typedef struct { + unsigned char stmt_; + unsigned char inSubquery_; + unsigned short int clause_; + unsigned short int parentIRType_; + unsigned short int parentIRItem_; +} IRContextS; + +class IRInfomation { +public: + IRInfomation(int prefix, int left, int middle, int right, int suffix); + IRInfomation(IR* root); + + bool operator!=(IRInfomation* info); + + TERMINAL prefix_; + IRTYPE left_; + TERMINAL middle_; + IRTYPE right_; + TERMINAL suffix_; +}; + +class IRInfoManger { +public: + IRInfoManger(); + ~IRInfoManger(); + + IRTYPE curIRType_; + + vector IRInfoChosenCount_; + + void create_new_context(IRContextS* context); + + void update_count(IRContextS* context, unsigned short int item, bool hasNewCoverage, unsigned int reward); + + void add_IRInfomation(IRInfomation* info); + IRInfomation* get_IRInfomation(unsigned short int index); + vector get_IRInfo_index(IRContextS* context, double heat); + + unsigned short int get_index_of_IRInfo(IR* root); + + string print_IRInformation_status(); + +private: + vector IRInfomation_; + unordered_map>> contextDependentCount_; +}; + +class OperatorManger { +public: + OperatorManger(); + ~OperatorManger(); + + IRTYPE curIRType_; + vector OpInfoChosenCount_; + vector OpInfoFailedCount_; + + void create_new_context(IRContextS* context); + vector get_OpInfo_index(IRContextS* context, double heat); + MutateOperator get_operator(unsigned short int index); + + void update_count(IRContextS* context, unsigned short int item, bool hasNewCoverage, unsigned int reward); + + string print_OperatorInformation_status(); + +private: + vector OperatorInfomation_; + map>> contextDependentCount_; +}; + +class InputInfo { +public: + InputInfo(); + ~InputInfo(); + + IRTYPE MutateIRType_; + unsigned short int MutateIRInfoItem_; + unsigned short int MutateOperatorItem_; + + IRContextS MutateContext_; + IR* input_; +}; + +class Mutator { +public: + Mutator(); + + ~Mutator(); + + unsigned int failedMutate_ = 0; + unsigned int totalMutate_ = 0; + + IR* IR_parser(const char* sql); + + vector mutate_all(const char* sql); + + void update_status(InputInfo* input, unsigned int execStatus, bool isValid); + + bool init_IR_library(string filePath); + bool init_IR_config_info(string filePath); + + void print_information(string outputPath); + +private: + static vector stmtIRType_; + static vector clauseIRType_; + + chrono::time_point startTime; + + unsigned int seedCount_ = 0; + vector mutateNumber_; + + map IRInfo_; + map OpInfo_; + map> IRTypeCount_; + + set not_mutatable_types_; + set sql_hash_; + + map> IR_library_; + map> IR_library_hash_; + + void free_IR_library(); + void free_IR_config_info(); + + uint64_t hash(string& sql); + vector get_all_files_in_dir(const char* dir_name); + + ParseToplevel* parser(const char* sql); + + bool add_ir_to_library(IR* root); + bool add_ir_to_library_no_deepcopy(IR* root); + + IR* get_ir_from_library(IRTYPE type); + + void extract_struct(IR* root); + + vector mutate(IR* root, IRContextS context, double heat); + IR* get_new_tree(IR* oldIRRoot, IR* curIR, IR* newIR); + + unsigned int get_duration_ms(); + double get_cur_heat(); + unsigned int get_cur_reward(); + + void print_IRType_information(string outputPath); + void print_IRInfo_information(string outputPath); + void print_Mutate_number(string outputPath); + void print_OpInfo_information(string outputPath); + + vector original_mutate(IR* root, IRContextS context, double heat); + + IR* strategy_delete(IR* root); + IR* strategy_insert(IR* root); + IR* strategy_replace(IR* root); + + IR* check_sql(string& sql); +}; + +#endif -- Gitee