This commit is contained in:
simon petit 2024-11-28 14:28:46 +00:00
parent bfc6e531cb
commit d32580b143
5 changed files with 36 additions and 15 deletions

View File

@ -22,6 +22,8 @@ rule read_token = parse
| "UNION" { UNION } | "UNION" { UNION }
| "JOIN" { JOIN } | "JOIN" { JOIN }
| "ON" { ON } | "ON" { ON }
| "WHERE" { WHERE }
| "HAVING" { HAVING }
| "GROUP" { GROUP } | "GROUP" { GROUP }
| "BY" { BY } | "BY" { BY }
| "*" { ASTERISK } | "*" { ASTERISK }
@ -31,6 +33,5 @@ rule read_token = parse
| ")" { RIGHT_PAREN } | ")" { RIGHT_PAREN }
| "," { COMMA } | "," { COMMA }
| whitespace { read_token lexbuf } | whitespace { read_token lexbuf }
| "WHERE" { WHERE }
| alpha alphanumeric* as ident { IDENT ident } | alpha alphanumeric* as ident { IDENT ident }
| eof { EOF } | eof { EOF }

View File

@ -16,12 +16,13 @@ and join_type =
| Union | Union
| Natural | Natural
and condition = and condition =
| Condition of string * comparison | Condition of string * predicate
| And of condition * condition | And of condition * condition
| Or of condition * condition | Or of condition * condition
| Not of condition | Not of condition
and comparison = and predicate =
| Comparison of operator * string | Comparison of operator * string
| Between of string * string
and operator = and operator =
| Equals | Equals
| NotEquals | NotEquals
@ -29,5 +30,7 @@ and operator =
| GreaterThan | GreaterThan
| LessEquals | LessEquals
| GreaterEquals | GreaterEquals
and filter =
| Filter of string
and search_condition = and search_condition =
| Search of string | Search of string

View File

@ -1,17 +1,13 @@
type logical_plan = type logical_plan =
| Scan of string (* Table name *) | Scan of string (* Table name *)
(*| Filter of logical_plan * condition*) | Filter of logical_plan * condition
| Join of logical_plan * Ast.join_type * logical_plan | Join of logical_plan * Ast.join_type * logical_plan
and condition =
| Condition of string
let rec generate_logical_plan ast = let rec generate_from_clause tables =
match ast with
| Ast.Query(Select(_, tables)) ->
let base_plan = generate_from_clause tables in
base_plan
and generate_from_clause tables =
match tables with match tables with
| [Table(name)] -> Scan(name) | [Ast.Table(name)] -> Scan(name)
| [Ast.Join(left, j_type, right, _)] -> | [Ast.Join(left, j_type, right, _)] ->
Join( Join(
generate_from_clause [left], generate_from_clause [left],
@ -21,6 +17,12 @@ and generate_from_clause tables =
| _ -> failwith "Unsupported table structure" | _ -> 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 = (*let evaluate_plan plan =
match plan with match plan with
| Scan(table) -> | Scan(table) ->

View File

@ -3,11 +3,12 @@
open Ast open Ast
%} %}
%token SELECT ALL DISTINCT FROM WHERE %token SELECT ALL DISTINCT FROM WHERE HAVING BETWEEN
%token LEFT RIGHT FULL INNER OUTER %token LEFT RIGHT FULL INNER OUTER
%token CROSS NATURAL UNION JOIN %token CROSS NATURAL UNION JOIN
%token GREATER_THAN_OPERATOR LESS_THAN_OPERATOR EQUALS_OPERATOR %token GREATER_THAN_OPERATOR LESS_THAN_OPERATOR EQUALS_OPERATOR
%token MAX MIN SUM COUNT AVG %token MAX MIN SUM COUNT AVG
%token ASYMMETRIC SYMMETRIC
%token <string> IDENT %token <string> IDENT
%token COMMA DOT %token COMMA DOT
%token LEFT_PAREN RIGHT_PAREN %token LEFT_PAREN RIGHT_PAREN
@ -130,10 +131,12 @@ outer_join_type:
| RIGHT { Right } | RIGHT { Right }
| FULL { Full } | FULL { Full }
where_clause : where_clause :
| WHERE search_condition { } | WHERE search_condition { }
having_clause :
| HAVING search_condition {}
search_condition: search_condition:
(*| IDENT EQUALS_OPERATOR IDENT {}*) (*| IDENT EQUALS_OPERATOR IDENT {}*)
| boolean_value_expression { $1 } | boolean_value_expression { $1 }
@ -159,6 +162,7 @@ boolean_primary :
predicate : predicate :
| comparison_predicate { $1 } | comparison_predicate { $1 }
(* | between_predicate { $1 }*)
comparison_predicate : comparison_predicate :
| row_value_predicand comparison_predicate_part2 { Condition($1, $2) } | row_value_predicand comparison_predicate_part2 { Condition($1, $2) }
@ -166,6 +170,18 @@ comparison_predicate :
comparison_predicate_part2: comparison_predicate_part2:
| comp_op row_value_predicand { Comparison($1, $2) } | 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 : comp_op :
| EQUALS_OPERATOR { Equals } | EQUALS_OPERATOR { Equals }
| not_equals_operator { NotEquals } | not_equals_operator { NotEquals }

View File

@ -11,4 +11,3 @@ let () =
Logical_plan.Scan("t2") Logical_plan.Scan("t2")
) )
); );