File tree Expand file tree Collapse file tree 2 files changed +58
-1
lines changed
llmfoundry/command_utils/data_prep
tests/a_scripts/data_prep Expand file tree Collapse file tree 2 files changed +58
-1
lines changed Original file line number Diff line number Diff line change @@ -233,7 +233,27 @@ def run_query(
233
233
elif method == 'dbconnect' :
234
234
if spark == None :
235
235
raise ValueError (f'sparkSession is required for dbconnect' )
236
- df = spark .sql (query )
236
+
237
+ try :
238
+ df = spark .sql (query )
239
+ except Exception as e :
240
+ from pyspark .errors import AnalysisException
241
+ if isinstance (e , AnalysisException ):
242
+ if 'INSUFFICIENT_PERMISSIONS' in e .message : # pyright: ignore
243
+ match = re .search (
244
+ r"Schema\s+'([^']+)'" ,
245
+ e .message , # pyright: ignore
246
+ )
247
+ if match :
248
+ schema_name = match .group (1 )
249
+ action = f'using the schema { schema_name } '
250
+ else :
251
+ action = 'using the schema'
252
+ raise InsufficientPermissionsError (action = action ,) from e
253
+ raise RuntimeError (
254
+ f'Error in querying into schema. Restart sparkSession and try again' ,
255
+ ) from e
256
+
237
257
if collect :
238
258
return df .collect ()
239
259
return df
@@ -461,6 +481,8 @@ def fetch(
461
481
raise InsufficientPermissionsError (
462
482
action = f'reading from { tablename } ' ,
463
483
) from e
484
+ if isinstance (e , InsufficientPermissionsError ):
485
+ raise e
464
486
raise RuntimeError (
465
487
f'Error in get rows from { tablename } . Restart sparkSession and try again' ,
466
488
) from e
Original file line number Diff line number Diff line change 1
1
# Copyright 2022 MosaicML LLM Foundry authors
2
2
# SPDX-License-Identifier: Apache-2.0
3
3
4
+ import sys
4
5
import unittest
5
6
from argparse import Namespace
6
7
from typing import Any
7
8
from unittest .mock import MagicMock , mock_open , patch
8
9
9
10
from llmfoundry .command_utils .data_prep .convert_delta_to_json import (
11
+ InsufficientPermissionsError ,
10
12
download ,
11
13
fetch_DT ,
12
14
format_tablename ,
17
19
18
20
class TestConvertDeltaToJsonl (unittest .TestCase ):
19
21
22
+ def test_run_query_dbconnect_insufficient_permissions (self ):
23
+ error_message = (
24
+ '[INSUFFICIENT_PERMISSIONS] Insufficient privileges: User does not have USE SCHEMA '
25
+ "on Schema 'main.oogabooga'. SQLSTATE: 42501"
26
+ )
27
+
28
+ class MockAnalysisException (Exception ):
29
+
30
+ def __init__ (self , message : str ):
31
+ self .message = message
32
+
33
+ with patch .dict ('sys.modules' , {'pyspark.errors' : MagicMock ()}):
34
+ sys .modules [
35
+ 'pyspark.errors'
36
+ ].AnalysisException = MockAnalysisException # pyright: ignore
37
+
38
+ mock_spark = MagicMock ()
39
+ mock_spark .sql .side_effect = MockAnalysisException (error_message )
40
+
41
+ with self .assertRaises (InsufficientPermissionsError ) as context :
42
+ run_query (
43
+ 'SELECT * FROM table' ,
44
+ method = 'dbconnect' ,
45
+ cursor = None ,
46
+ spark = mock_spark ,
47
+ )
48
+
49
+ self .assertIn (
50
+ 'using the schema main.oogabooga' ,
51
+ str (context .exception ),
52
+ )
53
+ mock_spark .sql .assert_called_once_with ('SELECT * FROM table' )
54
+
20
55
@patch (
21
56
'databricks.sql.connect' ,
22
57
)
You can’t perform that action at this time.
0 commit comments