-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Clean up query contexts #12633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Clean up query contexts #12633
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import org.apache.druid.segment.QueryableIndexStorageAdapter; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.TreeMap; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @PublicApi | ||
|
|
@@ -71,6 +72,12 @@ public class QueryContexts | |
| public static final String BROKER_SERVICE_NAME = "brokerService"; | ||
| public static final String IN_SUB_QUERY_THRESHOLD_KEY = "inSubQueryThreshold"; | ||
| public static final String TIME_BOUNDARY_PLANNING_KEY = "enableTimeBoundaryPlanning"; | ||
| public static final String POPULATE_CACHE_KEY = "populateCache"; | ||
| public static final String POPULATE_RESULT_LEVEL_CACHE_KEY = "populateResultLevelCache"; | ||
| public static final String USE_RESULT_LEVEL_CACHE_KEY = "useResultLevelCache"; | ||
| public static final String SERIALIZE_DATE_TIME_AS_LONG_KEY = "serializeDateTimeAsLong"; | ||
| public static final String SERIALIZE_DATE_TIME_AS_LONG_INNER_KEY = "serializeDateTimeAsLongInner"; | ||
| public static final String UNCOVERED_INTERVALS_LIMIT_KEY = "uncoveredIntervalsLimit"; | ||
|
|
||
| public static final boolean DEFAULT_BY_SEGMENT = false; | ||
| public static final boolean DEFAULT_POPULATE_CACHE = true; | ||
|
|
@@ -158,7 +165,7 @@ public static <T> boolean isPopulateCache(Query<T> query) | |
|
|
||
| public static <T> boolean isPopulateCache(Query<T> query, boolean defaultValue) | ||
| { | ||
| return parseBoolean(query, "populateCache", defaultValue); | ||
| return parseBoolean(query, POPULATE_CACHE_KEY, defaultValue); | ||
| } | ||
|
|
||
| public static <T> boolean isUseCache(Query<T> query) | ||
|
|
@@ -178,7 +185,7 @@ public static <T> boolean isPopulateResultLevelCache(Query<T> query) | |
|
|
||
| public static <T> boolean isPopulateResultLevelCache(Query<T> query, boolean defaultValue) | ||
| { | ||
| return parseBoolean(query, "populateResultLevelCache", defaultValue); | ||
| return parseBoolean(query, POPULATE_RESULT_LEVEL_CACHE_KEY, defaultValue); | ||
| } | ||
|
|
||
| public static <T> boolean isUseResultLevelCache(Query<T> query) | ||
|
|
@@ -188,22 +195,22 @@ public static <T> boolean isUseResultLevelCache(Query<T> query) | |
|
|
||
| public static <T> boolean isUseResultLevelCache(Query<T> query, boolean defaultValue) | ||
| { | ||
| return parseBoolean(query, "useResultLevelCache", defaultValue); | ||
| return parseBoolean(query, USE_RESULT_LEVEL_CACHE_KEY, defaultValue); | ||
| } | ||
|
|
||
| public static <T> boolean isFinalize(Query<T> query, boolean defaultValue) | ||
|
|
||
| { | ||
| return parseBoolean(query, FINALIZE_KEY, defaultValue); | ||
| } | ||
|
|
||
| public static <T> boolean isSerializeDateTimeAsLong(Query<T> query, boolean defaultValue) | ||
| { | ||
| return parseBoolean(query, "serializeDateTimeAsLong", defaultValue); | ||
| return parseBoolean(query, SERIALIZE_DATE_TIME_AS_LONG_KEY, defaultValue); | ||
| } | ||
|
|
||
| public static <T> boolean isSerializeDateTimeAsLongInner(Query<T> query, boolean defaultValue) | ||
| { | ||
| return parseBoolean(query, "serializeDateTimeAsLongInner", defaultValue); | ||
| return parseBoolean(query, SERIALIZE_DATE_TIME_AS_LONG_INNER_KEY, defaultValue); | ||
| } | ||
|
|
||
| public static <T> Vectorize getVectorize(Query<T> query) | ||
|
|
@@ -248,7 +255,7 @@ public static <T> int getUncoveredIntervalsLimit(Query<T> query) | |
|
|
||
| public static <T> int getUncoveredIntervalsLimit(Query<T> query, int defaultValue) | ||
| { | ||
| return parseInt(query, "uncoveredIntervalsLimit", defaultValue); | ||
| return parseInt(query, UNCOVERED_INTERVALS_LIMIT_KEY, defaultValue); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason for (Btw, is the behavior the same? I haven't checked) |
||
| } | ||
|
|
||
| public static <T> int getPriority(Query<T> query) | ||
|
|
@@ -450,32 +457,124 @@ public static String getBrokerServiceName(Map<String, Object> queryContext) | |
|
|
||
| static <T> long parseLong(Query<T> query, String key, long defaultValue) | ||
| { | ||
| final Object val = query.getContextValue(key); | ||
| return val == null ? defaultValue : Numbers.parseLong(val); | ||
| return getAsLong(key, query.getContextValue(key), defaultValue); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| static <T> long parseLong(Map<String, Object> context, String key, long defaultValue) | ||
| { | ||
| return getAsLong(key, context.get(key), defaultValue); | ||
| } | ||
|
|
||
| static <T> int parseInt(Query<T> query, String key, int defaultValue) | ||
| { | ||
| final Object val = query.getContextValue(key); | ||
| return val == null ? defaultValue : Numbers.parseInt(val); | ||
| return getAsInt(key, query.getContextValue(key), defaultValue); | ||
| } | ||
|
|
||
| static int parseInt(Map<String, Object> context, String key, int defaultValue) | ||
| { | ||
| final Object val = context.get(key); | ||
| return val == null ? defaultValue : Numbers.parseInt(val); | ||
| return getAsInt(key, context.get(key), defaultValue); | ||
| } | ||
|
|
||
| static <T> boolean parseBoolean(Query<T> query, String key, boolean defaultValue) | ||
| { | ||
| final Object val = query.getContextValue(key); | ||
| return val == null ? defaultValue : Numbers.parseBoolean(val); | ||
| return getAsBoolean(key, query.getContextValue(key), defaultValue); | ||
| } | ||
|
|
||
| static boolean parseBoolean(Map<String, Object> context, String key, boolean defaultValue) | ||
| { | ||
| final Object val = context.get(key); | ||
| return val == null ? defaultValue : Numbers.parseBoolean(val); | ||
| return getAsBoolean(key, context.get(key), defaultValue); | ||
| } | ||
|
|
||
| public static String getAsString( | ||
| final String parameter, | ||
| final Object value, | ||
| final String defaultValue | ||
| ) | ||
| { | ||
| if (value == null) { | ||
| return defaultValue; | ||
| } else if (value instanceof String) { | ||
| return (String) value; | ||
| } else { | ||
| throw new IAE("Expected parameter [%s] to be String", parameter); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the value of a parameter as a {@code boolean}. The parameter is expected | ||
| * to be {@code null}, a string or a {@code Boolean} object. | ||
| */ | ||
| public static boolean getAsBoolean( | ||
| final String parameter, | ||
| final Object value, | ||
| final boolean defaultValue | ||
| ) | ||
| { | ||
| if (value == null) { | ||
| return defaultValue; | ||
| } else if (value instanceof String) { | ||
| return Boolean.parseBoolean((String) value); | ||
| } else if (value instanceof Boolean) { | ||
| return (Boolean) value; | ||
| } else { | ||
| throw new IAE("Expected parameter [%s] to be a boolean", parameter); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the value of a parameter as an {@code int}. The parameter is expected | ||
| * to be {@code null}, a string or a {@code Number} object. | ||
| */ | ||
| public static int getAsInt( | ||
| final String parameter, | ||
| final Object value, | ||
| final int defaultValue | ||
| ) | ||
| { | ||
| if (value == null) { | ||
| return defaultValue; | ||
| } else if (value instanceof String) { | ||
| return Numbers.parseInt(value); | ||
| } else if (value instanceof Number) { | ||
| return ((Number) value).intValue(); | ||
| } else { | ||
| throw new IAE("Expected parameter [%s] to be an integer", parameter); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the value of a parameter as an {@code long}. The parameter is expected | ||
| * to be {@code null}, a string or a {@code Number} object. | ||
| */ | ||
| public static long getAsLong( | ||
| final String parameter, | ||
| final Object value, | ||
| final long defaultValue) | ||
| { | ||
| if (value == null) { | ||
| return defaultValue; | ||
| } else if (value instanceof String) { | ||
| return Numbers.parseLong(value); | ||
| } else if (value instanceof Number) { | ||
| return ((Number) value).longValue(); | ||
| } else { | ||
| throw new IAE("Expected parameter [%s] to be a long", parameter); | ||
| } | ||
| } | ||
|
|
||
| public static Map<String, Object> override( | ||
| final Map<String, Object> context, | ||
| final Map<String, Object> overrides | ||
| ) | ||
| { | ||
| Map<String, Object> overridden = new TreeMap<>(); | ||
| if (context != null) { | ||
| overridden.putAll(context); | ||
| } | ||
| overridden.putAll(overrides); | ||
|
|
||
| return overridden; | ||
| } | ||
|
|
||
| private QueryContexts() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason for
parseBooleanandgetAsBooleanto remain different? Now that we havegetAsBoolean, shall it replaceparseBoolean?(Btw, is the behavior the same? I haven't checked)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I didn't really pay attention to the
parseFoomethods. Those methods appear to only handle the value-as-string case, where as thegetAsFoomethod handle maps with values of the expected type. Went ahead and changed theparseFoomethods to usegetAsFooso we're consistent.