SOLR-8654: SQL: Unlimited SELECT without an ORDER BY should sort by _version_ desc

This commit is contained in:
jbernste 2016-02-07 18:16:32 -05:00
parent f77feb718a
commit a01c518b94
3 changed files with 58 additions and 8 deletions

View File

@ -612,11 +612,12 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware {
}
} else {
if(sqlVisitor.limit < 0) {
throw new IOException("order by is required for unlimited select statements.");
siBuf.append("_version_ desc");
fl = fl+",_version_";
} else {
siBuf.append("score desc");
if(!score) {
fl = fl+(",score");
fl = fl+",score";
}
}
}
@ -640,11 +641,7 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware {
tupleStream = new CloudSolrStream(zkHost, collection, params);
}
if(sqlVisitor.hasColumnAliases) {
return new SelectStream(tupleStream, sqlVisitor.columnAliases);
} else {
return tupleStream;
}
return new SelectStream(tupleStream, sqlVisitor.columnAliases);
}
private static boolean sortsEqual(Bucket[] buckets, String direction, List<SortItem> sortItems, Map<String, String> reverseColumnAliases) {

View File

@ -499,7 +499,7 @@
<field name="tlong" type="tlong" indexed="true" stored="true" />
<field name="_version_" type="long" indexed="true" stored="true"/>
<field name="_version_" type="long" indexed="true" docValues="true" stored="true"/>
<!-- Dynamic field definitions. If a field name is not found, dynamicFields
will be used if the name matches any of the patterns.

View File

@ -281,6 +281,59 @@ public class TestSQLHandler extends AbstractFullDistribZkTestBase {
assert(tuple.getLong("field_i") == 7);
assert(tuple.get("str_s").equals("a"));
params = new HashMap();
params.put(CommonParams.QT, "/sql");
//Test unlimited unsorted result. Should sort on _version_ desc
params.put("stmt", "select 'id', field_i, str_s from collection1 where 'text'='XXXX'");
solrStream = new SolrStream(jetty.url, params);
tuples = getTuples(solrStream);
assert(tuples.size() == 8);
tuple = tuples.get(0);
assert(tuple.getLong("id") == 8);
assert(tuple.getLong("field_i") == 60);
assert(tuple.get("str_s").equals("c"));
tuple = tuples.get(1);
assert(tuple.getLong("id") == 7);
assert(tuple.getLong("field_i") == 50);
assert(tuple.get("str_s").equals("c"));
tuple = tuples.get(2);
assert(tuple.getLong("id") == 6);
assert(tuple.getLong("field_i") == 40);
assert(tuple.get("str_s").equals("c"));
tuple = tuples.get(3);
assert(tuple.getLong("id") == 5);
assert(tuple.getLong("field_i") == 30);
assert(tuple.get("str_s").equals("c"));
tuple = tuples.get(4);
assert(tuple.getLong("id") == 4);
assert(tuple.getLong("field_i") == 11);
assert(tuple.get("str_s").equals("b"));
tuple = tuples.get(5);
assert(tuple.getLong("id") == 3);
assert(tuple.getLong("field_i") == 20);
assert(tuple.get("str_s").equals("a"));
tuple = tuples.get(6);
assert(tuple.getLong("id") == 2);
assert(tuple.getLong("field_i") == 8);
assert(tuple.get("str_s").equals("b"));
tuple = tuples.get(7);
assert(tuple.getLong("id") == 1);
assert(tuple.getLong("field_i") == 7);
assert(tuple.get("str_s").equals("a"));
params = new HashMap();
params.put(CommonParams.QT, "/sql");
params.put("stmt", "select id, field_i, str_s from collection1 where text='XXXX' order by field_i desc limit 1");