FrankChen021 commented on code in PR #13071:
URL: https://github.com/apache/druid/pull/13071#discussion_r990609380
##########
processing/src/main/java/org/apache/druid/query/QueryContext.java:
##########
@@ -29,227 +33,545 @@
import java.util.TreeMap;
/**
- * Holder for query context parameters. There are 3 ways to set context params
today.
- *
- * - Default parameters. These are set mostly via {@link
DefaultQueryConfig#context}.
- * Auto-generated queryId or sqlQueryId are also set as default parameters.
These default parameters can
- * be overridden by user or system parameters.
- * - User parameters. These are the params set by the user. User params
override default parameters but
- * are overridden by system parameters.
- * - System parameters. These are the params set by the Druid query engine for
internal use only.
- *
- * You can use {@code getX} methods or {@link #getMergedParams()} to compute
the context params
- * merging 3 types of params above.
- *
- * Currently, this class is mainly used for query context parameter
authorization,
- * such as HTTP query endpoints or JDBC endpoint. Its usage can be expanded in
the future if we
- * want to track user parameters and separate them from others during query
processing.
+ * Immutable holder for query context parameters with typed access methods.
+ * Code builds up a map of context values from serialization or during
+ * planning. Once that map is handed to the {@code QueryContext}, that map
+ * is effectively immutable.
+ * <p>
+ * The implementation uses a {@link TreeMap} so that the serialized form of a
query
+ * lists context values in a deterministic order. Jackson will call
+ * {@code getContext()} on the query, which will call {@link #asMap()} here,
+ * which returns the sorted {@code TreeMap}.
+ * <p>
+ * The {@code TreeMap} is a mutable class. We'd prefer an immutable class, but
+ * we can choose either ordering or immutability. Since the semantics of the
context
+ * is that it is immutable once it is placed in a query. Code should NEVER get
the
+ * context map from a query and modify it, even if the actual implementation
+ * allows it.
*/
public class QueryContext
{
- private final Map<String, Object> defaultParams;
- private final Map<String, Object> userParams;
- private final Map<String, Object> systemParams;
+ private static final QueryContext EMPTY = new QueryContext(null);
+
+ private final Map<String, Object> context;
+
+ public QueryContext(Map<String, Object> context)
+ {
+ // There is no semantic difference between an empty and a null context.
+ // Ensure that a context always exists to avoid the need to check for
+ // a null context. Jackson serialization will omit empty contexts.
+ this.context = context == null ? Collections.emptyMap() : new
TreeMap<>(context);
Review Comment:
```suggestion
this.context = context == null ? Collections.emptyMap() :
Collections.unmodifiableMap(new TreeMap<>(context));
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]