@@ -253,3 +253,35 @@ def test_get_select_query(self):
253
253
self .trino_api ._get_select_query (database , table ),
254
254
expected_statement
255
255
)
256
+
257
+
258
+ def test_explain (self ):
259
+ with patch ('notebook.connectors.trino.TrinoQuery' ) as TrinoQuery :
260
+ snippet = {'statement' : 'SELECT * FROM tpch.sf1.partsupp LIMIT 100;' , 'database' : 'tpch.sf1' }
261
+ output = [['Trino version: 432\n Fragment 0 [SINGLE]\n Output layout: [partkey, suppkey, availqty, supplycost, comment]\n ' \
262
+ 'Output partitioning: SINGLE []\n Output[columnNames = [partkey, suppkey, availqty, supplycost, comment]]\n │ ' \
263
+ 'Layout: [partkey:bigint, suppkey:bigint, availqty:integer, supplycost:double, comment:varchar(199)]\n │ ' \
264
+ 'Estimates: {rows: 100 (15.67kB), cpu: 0, memory: 0B, network: 0B}\n └─ Limit[count = 100]\n │ ' \
265
+ 'Layout: [partkey:bigint, suppkey:bigint, availqty:integer, supplycost:double, comment:varchar(199)]\n │ ' \
266
+ 'Estimates: {rows: 100 (15.67kB), cpu: 15.67k, memory: 0B, network: 0B}\n └─ LocalExchange[partitioning = SINGLE]\n ' \
267
+ '│ Layout: [partkey:bigint, suppkey:bigint, availqty:integer, supplycost:double, comment:varchar(199)]\n │ ' \
268
+ 'Estimates: {rows: 100 (15.67kB), cpu: 0, memory: 0B, network: 0B}\n └─ RemoteSource[sourceFragmentIds = [1]]\n ' \
269
+ ' Layout: [partkey:bigint, suppkey:bigint, availqty:integer, supplycost:double, comment:varchar(199)]\n \n ' \
270
+ 'Fragment 1 [SOURCE]\n Output layout: [partkey, suppkey, availqty, supplycost, comment]\n Output partitioning: SINGLE []\n ' \
271
+ ' LimitPartial[count = 100]\n │ Layout: [partkey:bigint, suppkey:bigint, availqty:integer, supplycost:double, ' \
272
+ 'comment:varchar(199)]\n │ Estimates: {rows: 100 (15.67kB), cpu: 15.67k, memory: 0B, network: 0B}\n └─ ' \
273
+ 'TableScan[table = tpch:sf1:partsupp]\n Layout: [partkey:bigint, suppkey:bigint, availqty:integer, supplycost:double, comment:varchar(199)]\n ' \
274
+ ' Estimates: {rows: 800000 (122.44MB), cpu: 122.44M, memory: 0B, network: 0B}\n partkey := tpch:partkey\n ' \
275
+ ' availqty := tpch:availqty\n supplycost := tpch:supplycost\n comment := tpch:comment\n ' \
276
+ ' suppkey := tpch:suppkey\n \n ' ]]
277
+ # Mock TrinoQuery object and its execute method
278
+ query_instance = TrinoQuery .return_value
279
+ query_instance .execute .return_value = MagicMock (next_uri = None , id = '123' , rows = output , columns = [])
280
+
281
+ # Call the explain method
282
+ result = self .trino_api .explain (notebook = None , snippet = snippet )
283
+
284
+ # Assert the result
285
+ self .assertEqual (result ['status' ], 0 )
286
+ self .assertEqual (result ['explanation' ], output )
287
+ self .assertEqual (result ['statement' ], 'SELECT * FROM tpch.sf1.partsupp LIMIT 100' )
0 commit comments