完成语义检查,整理项目
This commit is contained in:
1 parent
0791fe0446
commit
91ce26d7f0
17 files changed
+447
-301
No files matched your search
+10
-37
@@ -1,18 +1,18 @@
|
||||
module Main where
|
||||
|
||||
import ChuSQL.Algebra.Op (RelOp (..))
|
||||
import ChuSQL.Algebra.Op (renderPlan)
|
||||
import ChuSQL.Algebra.Optimize (optimize)
|
||||
import ChuSQL.Algebra.Planner (translate)
|
||||
import ChuSQL.Engine
|
||||
import ChuSQL.Model
|
||||
import ChuSQL.SQLSyntax.AST
|
||||
import ChuSQL.SQLSyntax.Executor
|
||||
import ChuSQL.SQLSyntax.Parser
|
||||
import ChuSQL.Syntax.AST
|
||||
import ChuSQL.Syntax.Parser
|
||||
|
||||
users :: Table
|
||||
users =
|
||||
Table
|
||||
{ tableName = "users"
|
||||
, tableCols = ["id", "name", "age"]
|
||||
, tableCols = [("id", TInt), ("name", TStr), ("age", TInt)]
|
||||
, tableRows =
|
||||
[ [("id", VInt 1), ("name", VStr "Alice"), ("age", VInt 25)]
|
||||
, [("id", VInt 2), ("name", VStr "Bob"), ("age", VInt 17)]
|
||||
@@ -24,7 +24,7 @@ orders :: Table
|
||||
orders =
|
||||
Table
|
||||
{ tableName = "orders"
|
||||
, tableCols = ["id", "user_id", "product"]
|
||||
, tableCols = [("id", TInt), ("user_id", TInt), ("product", TStr)]
|
||||
, tableRows =
|
||||
[ [("id", VInt 1), ("user_id", VInt 1), ("product", VStr "Book")]
|
||||
, [("id", VInt 2), ("user_id", VInt 2), ("product", VStr "Pen")]
|
||||
@@ -35,13 +35,9 @@ orders =
|
||||
db :: Database
|
||||
db = [("users", users), ("orders", orders)]
|
||||
|
||||
-- runQuery 返回 (更新后的数据库, 结果行);演示只关心结果行
|
||||
rowsOf :: Either String (Database, [Row]) -> Either String [Row]
|
||||
rowsOf = fmap snd
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
putStrLn "==================== Parser Test ====================="
|
||||
putStrLn "==================== Engine ====================="
|
||||
let q1 = makeSelect ["name"] "users" (Just (Gt (Col "age") (LitInt 18)))
|
||||
print (rowsOf (runQuery db q1))
|
||||
|
||||
@@ -73,35 +69,12 @@ main = do
|
||||
\JOIN orders o ON u.id = o.user_id WHERE u.age > 18"
|
||||
case parseQuery planSql of
|
||||
Left err -> putStrLn err
|
||||
Right query -> case translate db query of
|
||||
Right query -> case translate query of
|
||||
Left err -> putStrLn err
|
||||
Right plan -> do
|
||||
putStrLn "--- plan before optimization ---"
|
||||
putStrLn (dump 0 plan)
|
||||
putStrLn (renderPlan plan)
|
||||
putStrLn "--- plan after optimization ---"
|
||||
putStrLn (dump 0 (optimize db plan))
|
||||
putStrLn (renderPlan (optimize db plan))
|
||||
putStrLn "--- rows ---"
|
||||
print (rowsOf (runQuery db query))
|
||||
|
||||
dump :: Int -> RelOp -> String
|
||||
dump ind op = case op of
|
||||
Scan a t ->
|
||||
pad ++ "Scan " ++ show a ++ " " ++ show t
|
||||
Filter e x ->
|
||||
pad ++ "Filter " ++ show e ++ "\n" ++ dump (ind + 1) x
|
||||
Project c x ->
|
||||
pad ++ "Project " ++ show c ++ "\n" ++ dump (ind + 1) x
|
||||
Sort s x ->
|
||||
pad ++ "Sort " ++ show s ++ "\n" ++ dump (ind + 1) x
|
||||
Limit n x ->
|
||||
pad ++ "Limit " ++ show n ++ "\n" ++ dump (ind + 1) x
|
||||
Join l r c ->
|
||||
pad
|
||||
++ "Join on "
|
||||
++ show c
|
||||
++ "\n"
|
||||
++ dump (ind + 1) l
|
||||
++ "\n"
|
||||
++ dump (ind + 1) r
|
||||
where
|
||||
pad = replicate (ind * 2) ' '
|
||||
Reference in new issue
Block a user