@@ -99,41 +99,43 @@ def run_sql_query(query_str, db):
9999async def analyze (database : str = Body (...), query : str = Body (...), real_card : bool = Body (...)):
100100 if database not in ["tpch-1" , "tpcds-1" , "uni" , "job" ]:
101101 raise RuntimeError ("Unknown Database" )
102- with tempfile .NamedTemporaryFile (mode = "w" , delete = True ) as f :
103- f .write (query )
104- query_file = f .name
105- f .flush ()
106- with tempfile .TemporaryDirectory () as snapshotdir :
107- print (BINARY_DIR + "run-sql " + query_file + " " + DATA_ROOT + database )
108- output = subprocess .check_output ([BINARY_DIR + "run-sql" , query_file , DATA_ROOT + database ],
109- universal_newlines = True , stderr = subprocess .STDOUT , timeout = 20 ,
110- env = {** os .environ ,"LINGODB_SNAPSHOT_DIR" : snapshotdir ,
111- "LINGODB_SNAPSHOT_PASSES" : "true" ,
112- "LINGODB_SNAPSHOT_LEVEL" : "important" ,
113- "LINGODB_EXECUTION_MODE" : "NONE" })
114- result = subprocess .run (
115- f"bash { SCRIPT_DIR } /clean-snapshot.sh { BINARY_DIR } { snapshotdir } /important-snapshot-qopt.mlir { snapshotdir } /important-snapshot-qopt.mlir.alt" ,
116- universal_newlines = True , stderr = subprocess .STDOUT , shell = True )
117- print (result )
118- result = subprocess .run (
119- f"bash { SCRIPT_DIR } /clean-snapshot.sh { BINARY_DIR } { snapshotdir } /important-snapshot-subop-opt.mlir { snapshotdir } /important-snapshot-subop-opt.mlir.alt" ,
120- universal_newlines = True , stderr = subprocess .STDOUT , shell = True )
121- print (os .listdir (snapshotdir ))
122-
123- relalg_plan = subprocess .check_output (
124- [BINARY_DIR + "mlir-to-json" , snapshotdir + "/important-snapshot-qopt.mlir.alt" ] + (
125- [DATA_ROOT + database ] if real_card else []),
126- universal_newlines = True , stderr = subprocess .STDOUT )
127- subop_plan = subprocess .check_output (
128- [BINARY_DIR + "mlir-subop-to-json" , snapshotdir + "/important-snapshot-subop-opt.mlir.alt" ],
129- universal_newlines = True )
130- analyzed_snapshots = subprocess .check_output (
131- [BINARY_DIR + "mlir-analyze-snapshots" , snapshotdir + "/important-snapshot-info.json" ],
132- universal_newlines = True )
133- print (os .listdir (snapshotdir ))
134- return {"plan" : json .loads (relalg_plan .split ("\n " )[0 ]), "subopplan" : json .loads (subop_plan .split ("\n " )[0 ]),
135- "mlir" : json .loads (analyzed_snapshots )}
136-
102+ try :
103+ with tempfile .NamedTemporaryFile (mode = "w" , delete = True ) as f :
104+ f .write (query )
105+ query_file = f .name
106+ f .flush ()
107+ with tempfile .TemporaryDirectory () as snapshotdir :
108+ print (BINARY_DIR + "run-sql " + query_file + " " + DATA_ROOT + database )
109+ output = subprocess .check_output ([BINARY_DIR + "run-sql" , query_file , DATA_ROOT + database ],
110+ universal_newlines = True , stderr = subprocess .STDOUT , timeout = 20 ,
111+ env = {** os .environ ,"LINGODB_SNAPSHOT_DIR" : snapshotdir ,
112+ "LINGODB_SNAPSHOT_PASSES" : "true" ,
113+ "LINGODB_SNAPSHOT_LEVEL" : "important" ,
114+ "LINGODB_EXECUTION_MODE" : "NONE" })
115+ result = subprocess .run (
116+ f"bash { SCRIPT_DIR } /clean-snapshot.sh { BINARY_DIR } { snapshotdir } /important-snapshot-qopt.mlir { snapshotdir } /important-snapshot-qopt.mlir.alt" ,
117+ universal_newlines = True , stderr = subprocess .STDOUT , shell = True )
118+ print (result )
119+ result = subprocess .run (
120+ f"bash { SCRIPT_DIR } /clean-snapshot.sh { BINARY_DIR } { snapshotdir } /important-snapshot-subop-opt.mlir { snapshotdir } /important-snapshot-subop-opt.mlir.alt" ,
121+ universal_newlines = True , stderr = subprocess .STDOUT , shell = True )
122+ print (os .listdir (snapshotdir ))
123+
124+ relalg_plan = subprocess .check_output (
125+ [BINARY_DIR + "mlir-to-json" , snapshotdir + "/important-snapshot-qopt.mlir.alt" ] + (
126+ [DATA_ROOT + database ] if real_card else []),
127+ universal_newlines = True , stderr = subprocess .STDOUT )
128+ subop_plan = subprocess .check_output (
129+ [BINARY_DIR + "mlir-subop-to-json" , snapshotdir + "/important-snapshot-subop-opt.mlir.alt" ],
130+ universal_newlines = True )
131+ analyzed_snapshots = subprocess .check_output (
132+ [BINARY_DIR + "mlir-analyze-snapshots" , snapshotdir + "/important-snapshot-info.json" ],
133+ universal_newlines = True )
134+ print (os .listdir (snapshotdir ))
135+ return {"plan" : json .loads (relalg_plan .split ("\n " )[0 ]), "subopplan" : json .loads (subop_plan .split ("\n " )[0 ]),
136+ "mlir" : json .loads (analyzed_snapshots )}
137+ except Exception as e :
138+ raise HTTPException (status_code = 400 , detail = "Error during query analyze" )
137139 return {}
138140
139141
0 commit comments