-
Notifications
You must be signed in to change notification settings - Fork 674
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
SOLR-17022: Support for glob patterns for fields in Export handler, Stream handler and with SelectStream streaming expression #1996
Changes from 12 commits
b210b1e
8a04a11
fe66c1f
23626fb
ac25ebb
bfe31fa
fad2f18
0d862cf
f1c62f9
0382445
19e97ae
7f7b49d
91133ae
c1ebac1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,13 @@ | |
import java.io.PrintWriter; | ||
import java.lang.invoke.MethodHandles; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import org.apache.lucene.index.FieldInfo; | ||
import org.apache.lucene.index.LeafReader; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.index.SortedDocValues; | ||
|
@@ -53,6 +57,7 @@ | |
import org.apache.solr.common.params.CommonParams; | ||
import org.apache.solr.common.params.SolrParams; | ||
import org.apache.solr.common.params.StreamParams; | ||
import org.apache.solr.common.util.GlobPatternUtil; | ||
import org.apache.solr.common.util.JavaBinCodec; | ||
import org.apache.solr.core.SolrCore; | ||
import org.apache.solr.metrics.SolrMetricsContext; | ||
|
@@ -487,19 +492,14 @@ void writeDoc( | |
|
||
public FieldWriter[] getFieldWriters(String[] fields, SolrIndexSearcher searcher) | ||
throws IOException { | ||
IndexSchema schema = searcher.getSchema(); | ||
FieldWriter[] writers = new FieldWriter[fields.length]; | ||
DocValuesIteratorCache dvIterCache = new DocValuesIteratorCache(searcher, false); | ||
for (int i = 0; i < fields.length; i++) { | ||
String field = fields[i]; | ||
SchemaField schemaField = null; | ||
|
||
try { | ||
schemaField = schema.getField(field); | ||
} catch (Exception e) { | ||
throw new IOException(e); | ||
} | ||
List<SchemaField> expandedFields = expandFieldList(fields, searcher); | ||
|
||
FieldWriter[] writers = new FieldWriter[expandedFields.size()]; | ||
for (int i = 0; i < expandedFields.size(); i++) { | ||
SchemaField schemaField = expandedFields.get(i); | ||
String field = schemaField.getName(); | ||
if (!schemaField.hasDocValues()) { | ||
throw new IOException(schemaField + " must have DocValues to use this feature."); | ||
} | ||
|
@@ -844,4 +844,60 @@ public String getMessage() { | |
return "Early Client Disconnect"; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a complete field list using the provided field list by expanding any glob patterns into | ||
* field names | ||
* | ||
* @param fields the original set of fields provided | ||
* @param searcher an index searcher to access schema info | ||
* @return a complete list of fields included any fields matching glob patterns | ||
* @throws IOException if a provided field does not exist or cannot be retrieved from the schema | ||
* info | ||
*/ | ||
private List<SchemaField> expandFieldList(String[] fields, SolrIndexSearcher searcher) | ||
throws IOException { | ||
List<SchemaField> expandedFields = new ArrayList<>(fields.length); | ||
Set<String> fieldsProcessed = new HashSet<>(); | ||
for (String field : fields) { | ||
try { | ||
if (field.contains("*")) { | ||
getGlobFields(field, searcher, fieldsProcessed, expandedFields); | ||
} else { | ||
if (fieldsProcessed.add(field)) { | ||
expandedFields.add(searcher.getSchema().getField(field)); | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
return expandedFields; | ||
} | ||
|
||
/** | ||
* Create a list of schema fields that match a given glob pattern | ||
* | ||
* @param fieldPattern the glob pattern to match | ||
* @param searcher an index search to access schema info | ||
* @param fieldsProcessed the set of field names already processed to avoid duplicating | ||
* @param expandedFields the list of fields to add expanded field names into | ||
*/ | ||
private void getGlobFields( | ||
String fieldPattern, | ||
SolrIndexSearcher searcher, | ||
Set<String> fieldsProcessed, | ||
List<SchemaField> expandedFields) { | ||
for (FieldInfo fi : searcher.getFieldInfos()) { | ||
if (GlobPatternUtil.matches(fieldPattern, fi.getName())) { | ||
SchemaField schemaField = searcher.getSchema().getField(fi.getName()); | ||
if (fieldsProcessed.add(fi.getName()) | ||
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. This line might add to fieldsProcessed, yet exclude the field because doesn't "hasDocValues". This looks suspicious to me. |
||
&& schemaField.hasDocValues() | ||
&& schemaField.useDocValuesAsStored()) { | ||
expandedFields.add(schemaField); | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser; | ||
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionValue; | ||
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory; | ||
import org.apache.solr.common.util.GlobPatternUtil; | ||
|
||
/** | ||
* Selects fields from the incoming stream and applies optional field renaming. Does not reorder the | ||
|
@@ -52,14 +53,21 @@ public class SelectStream extends TupleStream implements Expressible { | |
private TupleStream stream; | ||
private StreamContext streamContext; | ||
private Map<String, String> selectedFields; | ||
private List<String> selectedFieldGlobPatterns; | ||
private Map<StreamEvaluator, String> selectedEvaluators; | ||
private List<StreamOperation> operations; | ||
|
||
public SelectStream(TupleStream stream, List<String> selectedFields) throws IOException { | ||
this.stream = stream; | ||
this.selectedFields = new HashMap<>(); | ||
this.selectedFieldGlobPatterns = new ArrayList<>(); | ||
for (String selectedField : selectedFields) { | ||
this.selectedFields.put(selectedField, selectedField); | ||
if (selectedField.contains("*")) { | ||
// selected field is a glob pattern | ||
this.selectedFieldGlobPatterns.add(selectedField); | ||
} else { | ||
this.selectedFields.put(selectedField, selectedField); | ||
} | ||
} | ||
operations = new ArrayList<>(); | ||
selectedEvaluators = new LinkedHashMap<>(); | ||
|
@@ -68,6 +76,7 @@ public SelectStream(TupleStream stream, List<String> selectedFields) throws IOEx | |
public SelectStream(TupleStream stream, Map<String, String> selectedFields) throws IOException { | ||
this.stream = stream; | ||
this.selectedFields = selectedFields; | ||
selectedFieldGlobPatterns = new ArrayList<>(); | ||
operations = new ArrayList<>(); | ||
selectedEvaluators = new LinkedHashMap<>(); | ||
} | ||
|
@@ -123,6 +132,7 @@ public SelectStream(StreamExpression expression, StreamFactory factory) throws I | |
stream = factory.constructStream(streamExpressions.get(0)); | ||
|
||
selectedFields = new HashMap<>(); | ||
selectedFieldGlobPatterns = new ArrayList<>(); | ||
selectedEvaluators = new LinkedHashMap<>(); | ||
for (StreamExpressionParameter parameter : selectAsFieldsExpressions) { | ||
StreamExpressionValue selectField = (StreamExpressionValue) parameter; | ||
|
@@ -175,7 +185,11 @@ public SelectStream(StreamExpression expression, StreamFactory factory) throws I | |
selectedFields.put(asValue, asName); | ||
} | ||
} else { | ||
selectedFields.put(value, value); | ||
if (value.contains("*")) { | ||
selectedFieldGlobPatterns.add(value); | ||
} else { | ||
selectedFields.put(value, value); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -217,6 +231,11 @@ private StreamExpression toExpression(StreamFactory factory, boolean includeStre | |
} | ||
} | ||
|
||
// selected glob patterns | ||
for (String selectFieldGlobPattern : selectedFieldGlobPatterns) { | ||
expression.addParameter(selectFieldGlobPattern); | ||
} | ||
|
||
// selected evaluators | ||
for (Map.Entry<StreamEvaluator, String> selectedEvaluator : selectedEvaluators.entrySet()) { | ||
expression.addParameter( | ||
|
@@ -308,6 +327,13 @@ public Tuple read() throws IOException { | |
workingForEvaluators.put(fieldName, original.get(fieldName)); | ||
if (selectedFields.containsKey(fieldName)) { | ||
workingToReturn.put(selectedFields.get(fieldName), original.get(fieldName)); | ||
} else { | ||
for (String globPattern : selectedFieldGlobPatterns) { | ||
if (GlobPatternUtil.matches(globPattern, fieldName)) { | ||
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. Shouldn't SelectStream also use SolrReturnFields and not use lower level GlobPattern stuff (it's something SRF can handle)? Disclaimer: I haven't looked at this PR in a long time. 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.
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. Oh right; of course. |
||
workingToReturn.put(fieldName, original.get(fieldName)); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.solr.common.util; | ||
justinrsweeney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import java.nio.file.FileSystems; | ||
import java.nio.file.Paths; | ||
|
||
/** Provides methods for matching glob patterns against input strings. */ | ||
public class GlobPatternUtil { | ||
dsmiley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Matches an input string against a provided glob patterns. This uses Java NIO FileSystems | ||
* PathMatcher to match glob patterns in the same way to how glob patterns are matches for file | ||
* paths, rather than implementing our own glob pattern matching. | ||
* | ||
* @param pattern the glob pattern to match against | ||
* @param input the input string to match against a glob pattern | ||
* @return true if the input string matches the glob pattern, false otherwise | ||
*/ | ||
public static boolean matches(String pattern, String input) { | ||
return FileSystems.getDefault().getPathMatcher("glob:" + pattern).matches(Paths.get(input)); | ||
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. This is bizarre just for some field wildcard support. If there is a reason we use FileSystems then a comment is necessary. 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. I took a look at how it's implemented. If only we could call ZipUtils.toRegexPattern but the class is package protected. It's a shame to recompile the glob on each call to matches! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.solr.common.util; | ||
justinrsweeney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import org.apache.solr.SolrTestCase; | ||
|
||
public class TestGlobPatternUtil extends SolrTestCase { | ||
|
||
public void testMatches() { | ||
assertTrue(GlobPatternUtil.matches("*_str", "user_str")); | ||
assertFalse(GlobPatternUtil.matches("*_str", "str_user")); | ||
assertTrue(GlobPatternUtil.matches("str_*", "str_user")); | ||
assertFalse(GlobPatternUtil.matches("str_*", "user_str")); | ||
assertTrue(GlobPatternUtil.matches("str?", "str1")); | ||
assertFalse(GlobPatternUtil.matches("str?", "str_user")); | ||
assertTrue(GlobPatternUtil.matches("user_*_str", "user_type_str")); | ||
assertFalse(GlobPatternUtil.matches("user_*_str", "user_str")); | ||
} | ||
} |
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.
There can be many fieldInfo, and you're looping over a "matches" call that is going to internally build a regex each time. Maybe you should first do the hasDocValues etc. checks so we do this matches check last?