ocaml_sql_parser/lib/logical_plan.ml
2024-12-24 14:42:51 +01:00

50 lines
1.4 KiB
OCaml

type logical_plan =
| Project of logical_plan option * string list
| Scan of string (* Table name *)
| Filter of logical_plan
| Join of logical_plan * Ast.join_type * logical_plan
| Group of logical_plan * string list
let rec ast_to_logical ast =
match ast with
| Ast.Select(cols, table_exp) -> Project((table_expression_to_logical table_exp), (columns_to_logical cols))
| _ -> failwith "Query not supported yet"
and table_expression_to_logical table_exp =
match table_exp with
| Ast.TableExpression(tables, None, None) -> tables_to_logical tables
| _ -> failwith "Table expression not supported yet"
and columns_to_logical columns =
let rec aux cols acc =
match cols with
| [] -> acc
| Ast.Column(col,_)::l ->
let s = expression_to_logical col in
aux l (s::acc)
| _ -> failwith "Not supported"
in
aux columns []
and expression_to_logical exp =
match exp with
| Ast.Ref(s) -> s
| Ast.StringLiteral(s) -> s
| _ -> failwith "Expression not supported yet"
and tables_to_logical tables =
let aux t =
match t with
| [] -> None
| [Ast.Table(table_name)] -> Some(Scan(table_name))
| _ -> failwith "tables not supported"
in
match tables with
| None -> None
| Some(t) -> aux t
let pp_logical_plan plan =
match plan with
| Project(_, name) -> (String.concat ";" name)
| _ -> "Not supported"