-
Notifications
You must be signed in to change notification settings - Fork 25k
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
SQL: add support for index aliases for SYS COLUMNS command #53525
Conversation
Pinging @elastic/es-search (:Search/SQL) |
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.
Left some comments
.local(true) | ||
.aliases("*") | ||
.indices(indicesList) | ||
.indicesOptions(IndicesOptions.lenientExpandOpen()); |
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.
The IndicesOption
depends on whether includeFrozen
is true or not.
@@ -460,15 +463,31 @@ public void resolveAsSeparateMappings(String indexWildcard, String javaRegex, bo | |||
FieldCapabilitiesRequest fieldRequest = createFieldCapsRequest(indexWildcard, includeFrozen); | |||
client.fieldCaps(fieldRequest, | |||
ActionListener.wrap( |
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.
Maybe use static imports on ActionListener
to make the code a bit more readable.
@@ -460,15 +463,31 @@ public void resolveAsSeparateMappings(String indexWildcard, String javaRegex, bo | |||
FieldCapabilitiesRequest fieldRequest = createFieldCapsRequest(indexWildcard, includeFrozen); | |||
client.fieldCaps(fieldRequest, | |||
ActionListener.wrap( | |||
response -> listener.onResponse( | |||
separateMappings(typeRegistry, indexWildcard, javaRegex, response.getIndices(), response.get())), | |||
response -> { |
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.
Small nit - maybe it makes sense to extract this method into a separate one - it's not reusable but it might make the call easier to read:
client.fieldCaps(fieldRequest, wrap(response -> getIndexAliases(response, wrap(aliases -> ...))
x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java
Show resolved
Hide resolved
x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java
Show resolved
Hide resolved
} | ||
|
||
List<String> resolvedIndices = new ArrayList<>(asList(indexNames)); | ||
Map<String, Fields> indices = new LinkedHashMap<>(resolvedIndices.size() + resolvedAliases.size()); |
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.
The size is incorrect - use CollectionUtils.mapSize
return emptyList(); | ||
} | ||
|
||
final List<String> resolvedIndices = asList(indexNames); | ||
Map<String, Fields> indices = new LinkedHashMap<>(resolvedIndices.size()); | ||
Set<String> resolvedAliases = new LinkedHashSet<>(); |
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 it okay to put all aliases in the same map?
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.
With this I wanted to count them and create a list of indices and aliases appropriately sized.
ObjectObjectCursor<String, List<AliasMetaData>> index = iter.next(); | ||
for (AliasMetaData aliasMetaData : index.value) { | ||
String aliasName = aliasMetaData.alias(); | ||
typesErrors.putIfAbsent(aliasName, new HashSet<>()); |
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.
this creates a HashSet regardless of the key presence - better to do the check
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.
Actually, this was intended like this. See this line where the field type for this field belonging to this alias is added to the list of unique field types. And here the size of the set is checked to see if there is more than one field type across all indices belonging to this alias. If there is more than one field type, then the field is considered "invalid" for this alias.
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.
I don't follow - putIfAbsent
does if (key == null) put(key, value)
- I'm pointing out that a new HashSet
is created every time.
What I'm suggesting:
if (key == null) {
put(key, new HashSet())
}
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.
I am aware of the putIfAbsent
functionality. I think I can simplify the logic a bit.
return emptyMap(); | ||
} | ||
Map<String, InvalidMappedField> invalidFields = new HashMap<>(); | ||
Map<String, Set<String>> typesErrors = new HashMap<>(); // map holding aliases and a list of unique field types accross its indices |
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.
nit: s/accross/across.
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.
LGTM
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.
LGTM. A second pass could improve the code further by moving the merging code into one place so that separate
and mergeMappings
have the same code flow since aliases, even without a pattern, force a mapping merge.
Function<String, String> indexNameProcessor, | ||
BiFunction<String, Map<String, FieldCapabilities>, InvalidMappedField> validityVerifier) { | ||
|
||
if (indexNames == null || indexNames.length == 0) { | ||
if ((indexNames == null || indexNames.length == 0) && (aliases == null || aliases.size() == 0)) { |
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.
Use org.elasticsearch.common.util.CollectionUtils.isEmpty
instead.
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.
That's not possible unfortunately, because aliases
is an ImmutableOpenMap, which is not a Collection.
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.
LGTM. Nicely done! Left 2 minor comments
if (aliases != null) { | ||
Iterator<ObjectObjectCursor<String, List<AliasMetaData>>> iterator = aliases.iterator(); | ||
while (iterator.hasNext()) { | ||
for(AliasMetaData alias : iterator.next().value) { |
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.
for(AliasMetaData alias : iterator.next().value) { | |
for (AliasMetaData alias : iterator.next().value) { |
ObjectObjectCursor<String, List<AliasMetaData>> index = iter.next(); | ||
for (AliasMetaData aliasMetaData : index.value) { | ||
String aliasName = aliasMetaData.alias(); | ||
aliasToIndices.putIfAbsent(aliasName, new HashSet<String>()); |
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.
Can it be simply <>
?
aliasToIndices.putIfAbsent(aliasName, new HashSet<String>()); | |
aliasToIndices.putIfAbsent(aliasName, new HashSet<>()); |
Fixes #31609.