diff --git a/lexer/lexer.mll b/lexer/lexer.mll index 4116cb7..405ce52 100644 --- a/lexer/lexer.mll +++ b/lexer/lexer.mll @@ -22,6 +22,8 @@ rule read_token = parse | "UNION" { UNION } | "JOIN" { JOIN } | "ON" { ON } + | "WHERE" { WHERE } + | "HAVING" { HAVING } | "GROUP" { GROUP } | "BY" { BY } | "*" { ASTERISK } @@ -31,6 +33,5 @@ rule read_token = parse | ")" { RIGHT_PAREN } | "," { COMMA } | whitespace { read_token lexbuf } - | "WHERE" { WHERE } | alpha alphanumeric* as ident { IDENT ident } | eof { EOF } diff --git a/lib/ast.ml b/lib/ast.ml index 5debe2f..ce4839e 100644 --- a/lib/ast.ml +++ b/lib/ast.ml @@ -16,12 +16,13 @@ and join_type = | Union | Natural and condition = - | Condition of string * comparison + | Condition of string * predicate | And of condition * condition | Or of condition * condition | Not of condition -and comparison = +and predicate = | Comparison of operator * string + | Between of string * string and operator = | Equals | NotEquals @@ -29,5 +30,7 @@ and operator = | GreaterThan | LessEquals | GreaterEquals +and filter = + | Filter of string and search_condition = | Search of string diff --git a/lib/logical_plan.ml b/lib/logical_plan.ml index db81796..7337ac3 100644 --- a/lib/logical_plan.ml +++ b/lib/logical_plan.ml @@ -1,17 +1,13 @@ type logical_plan = | Scan of string (* Table name *) - (*| Filter of logical_plan * condition*) + | Filter of logical_plan * condition | Join of logical_plan * Ast.join_type * logical_plan +and condition = + | Condition of string -let rec generate_logical_plan ast = - match ast with - | Ast.Query(Select(_, tables)) -> - let base_plan = generate_from_clause tables in - base_plan - -and generate_from_clause tables = +let rec generate_from_clause tables = match tables with - | [Table(name)] -> Scan(name) + | [Ast.Table(name)] -> Scan(name) | [Ast.Join(left, j_type, right, _)] -> Join( generate_from_clause [left], @@ -21,6 +17,12 @@ and generate_from_clause tables = | _ -> failwith "Unsupported table structure" +let generate_logical_plan ast = + match ast with + | Ast.Query(Select(_, tables)) -> + let base_plan = generate_from_clause tables in + base_plan + (*let evaluate_plan plan = match plan with | Scan(table) -> diff --git a/parser/parser.mly b/parser/parser.mly index c72b6d4..4907d70 100644 --- a/parser/parser.mly +++ b/parser/parser.mly @@ -3,11 +3,12 @@ open Ast %} -%token SELECT ALL DISTINCT FROM WHERE +%token SELECT ALL DISTINCT FROM WHERE HAVING BETWEEN %token LEFT RIGHT FULL INNER OUTER %token CROSS NATURAL UNION JOIN %token GREATER_THAN_OPERATOR LESS_THAN_OPERATOR EQUALS_OPERATOR %token MAX MIN SUM COUNT AVG +%token ASYMMETRIC SYMMETRIC %token IDENT %token COMMA DOT %token LEFT_PAREN RIGHT_PAREN @@ -130,10 +131,12 @@ outer_join_type: | RIGHT { Right } | FULL { Full } - where_clause : | WHERE search_condition { } +having_clause : + | HAVING search_condition {} + search_condition: (*| IDENT EQUALS_OPERATOR IDENT {}*) | boolean_value_expression { $1 } @@ -159,6 +162,7 @@ boolean_primary : predicate : | comparison_predicate { $1 } +(* | between_predicate { $1 }*) comparison_predicate : | row_value_predicand comparison_predicate_part2 { Condition($1, $2) } @@ -166,6 +170,18 @@ comparison_predicate : comparison_predicate_part2: | comp_op row_value_predicand { Comparison($1, $2) } +between_predicate : + | row_value_predicand between_predicate_part2 { } + +between_predicate_part2 : + | BETWEEN between_symetry row_value_predicand AND row_value_predicand {} + | NOT BETWEEN between_symetry row_value_predicand AND row_value_predicand {} + +between_symetry : + | {} + | ASYMMETRIC {} + | SYMMETRIC {} + comp_op : | EQUALS_OPERATOR { Equals } | not_equals_operator { NotEquals } diff --git a/test/logical_plan_test.ml b/test/logical_plan_test.ml index 9e1ffea..6226d9e 100644 --- a/test/logical_plan_test.ml +++ b/test/logical_plan_test.ml @@ -11,4 +11,3 @@ let () = Logical_plan.Scan("t2") ) ); -