17
17
import com .google .inject .Inject ;
18
18
import io .trino .Session ;
19
19
import io .trino .execution .warnings .WarningCollector ;
20
- import io .trino .metadata .SessionPropertyManager ;
21
20
import io .trino .security .AccessControl ;
22
21
import io .trino .security .SecurityContext ;
23
22
import io .trino .spi .TrinoException ;
24
- import io .trino .spi .connector .CatalogHandle ;
25
- import io .trino .spi .session .PropertyMetadata ;
26
- import io .trino .spi .type .Type ;
27
- import io .trino .sql .PlannerContext ;
28
23
import io .trino .sql .tree .Expression ;
29
24
import io .trino .sql .tree .QualifiedName ;
30
25
import io .trino .sql .tree .SetSession ;
33
28
34
29
import static com .google .common .util .concurrent .Futures .immediateVoidFuture ;
35
30
import static io .trino .execution .ParameterExtractor .bindParameters ;
36
- import static io .trino .metadata .MetadataUtil .getRequiredCatalogHandle ;
37
- import static io .trino .metadata .SessionPropertyManager .evaluatePropertyValue ;
38
- import static io .trino .metadata .SessionPropertyManager .serializeSessionProperty ;
39
31
import static io .trino .spi .StandardErrorCode .INVALID_SESSION_PROPERTY ;
40
- import static io .trino .sql .analyzer .SemanticExceptions .semanticException ;
41
- import static java .lang .String .format ;
42
32
import static java .util .Objects .requireNonNull ;
43
33
44
34
public class SetSessionTask
45
35
implements DataDefinitionTask <SetSession >
46
36
{
47
- private final PlannerContext plannerContext ;
37
+ private final SessionEvaluator sessionEvaluator ;
48
38
private final AccessControl accessControl ;
49
- private final SessionPropertyManager sessionPropertyManager ;
50
39
51
40
@ Inject
52
- public SetSessionTask (PlannerContext plannerContext , AccessControl accessControl , SessionPropertyManager sessionPropertyManager )
41
+ public SetSessionTask (SessionEvaluator sessionEvaluator , AccessControl accessControl )
53
42
{
54
- this .plannerContext = requireNonNull (plannerContext , "plannerContext is null" );
43
+ this .sessionEvaluator = requireNonNull (sessionEvaluator , "sessionEvaluator is null" );
55
44
this .accessControl = requireNonNull (accessControl , "accessControl is null" );
56
- this .sessionPropertyManager = requireNonNull (sessionPropertyManager , "sessionPropertyManager is null" );
57
45
}
58
46
59
47
@ Override
@@ -69,50 +57,23 @@ public ListenableFuture<Void> execute(
69
57
List <Expression > parameters ,
70
58
WarningCollector warningCollector )
71
59
{
72
- Session session = stateMachine .getSession ();
73
60
QualifiedName propertyName = statement .getName ();
74
61
List <String > parts = propertyName .getParts ();
75
- if (parts .size () > 2 ) {
76
- throw semanticException (INVALID_SESSION_PROPERTY , statement , "Invalid session property '%s'" , propertyName );
77
- }
62
+ Session session = stateMachine .getSession ();
78
63
79
- // validate the property name
80
- PropertyMetadata <?> propertyMetadata ;
81
64
if (parts .size () == 1 ) {
82
- accessControl .checkCanSetSystemSessionProperty (session .getIdentity (), session .getQueryId (), parts .get (0 ));
83
- propertyMetadata = sessionPropertyManager .getSystemSessionPropertyMetadata (parts .get (0 ))
84
- .orElseThrow (() -> semanticException (INVALID_SESSION_PROPERTY , statement , "Session property '%s' does not exist" , statement .getName ()));
85
- }
86
- else {
87
- CatalogHandle catalogHandle = getRequiredCatalogHandle (plannerContext .getMetadata (), stateMachine .getSession (), statement , parts .get (0 ));
88
- accessControl .checkCanSetCatalogSessionProperty (SecurityContext .of (session ), parts .get (0 ), parts .get (1 ));
89
- propertyMetadata = sessionPropertyManager .getConnectorSessionPropertyMetadata (catalogHandle , parts .get (1 ))
90
- .orElseThrow (() -> semanticException (INVALID_SESSION_PROPERTY , statement , "Session property '%s' does not exist" , statement .getName ()));
91
- }
92
-
93
- Type type = propertyMetadata .getSqlType ();
94
- Object objectValue ;
95
-
96
- try {
97
- objectValue = evaluatePropertyValue (statement .getValue (), type , session , plannerContext , accessControl , bindParameters (statement , parameters ));
98
- }
99
- catch (TrinoException e ) {
100
- throw new TrinoException (
101
- INVALID_SESSION_PROPERTY ,
102
- format ("Unable to set session property '%s' to '%s': %s" , propertyName , statement .getValue (), e .getRawMessage ()));
65
+ accessControl .checkCanSetSystemSessionProperty (session .getIdentity (), session .getQueryId (), parts .getFirst ());
103
66
}
104
-
105
- String value = serializeSessionProperty (type , objectValue );
106
-
107
- // verify the SQL value can be decoded by the property
108
- try {
109
- propertyMetadata .decode (objectValue );
67
+ else if (parts .size () == 2 ) {
68
+ accessControl .checkCanSetCatalogSessionProperty (SecurityContext .of (session ), parts .getFirst (), parts .getLast ());
110
69
}
111
- catch ( RuntimeException e ) {
112
- throw semanticException (INVALID_SESSION_PROPERTY , statement , "%s" , e . getMessage ( ));
70
+ else {
71
+ throw new TrinoException (INVALID_SESSION_PROPERTY , "Invalid session property '%s'" . formatted ( propertyName ));
113
72
}
114
73
115
- stateMachine .addSetSessionProperties (propertyName .toString (), value );
74
+ stateMachine .addSetSessionProperties (
75
+ statement .getName ().toString (),
76
+ sessionEvaluator .evaluate (stateMachine .getSession (), statement .getName (), statement .getValue (), bindParameters (statement , parameters )));
116
77
117
78
return immediateVoidFuture ();
118
79
}
0 commit comments