Skip to content

Commit c01e47e

Browse files
committed
SQLParser : interpretation fix in create index command, improve select performance : better use of index OSearchResult
1 parent cc540dc commit c01e47e

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

core/src/main/java/com/orientechnologies/orient/core/sql/command/OCommandCreateIndex.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public OCommandCreateIndex parse(final OCommandRequest iRequest) {
8282
final List<OSQLParser.ReferenceContext> words = ctx.reference();
8383
oClass = findClass(words.get(0).getText());
8484
fields = new String[words.size()-1];
85-
for(int k=1;k<fields.length;k++){
85+
for(int k=1;k<words.size();k++){
8686
fields[k-1] = visitAsString(words.get(k));
8787
}
8888
}
@@ -100,8 +100,8 @@ public OCommandCreateIndex parse(final OCommandRequest iRequest) {
100100
final String text = visitAsString(candidate.reference(i));
101101
keyTypes.add(OType.valueOf(text));
102102
}
103-
this.keyTypes = keyTypes.toArray(new OType[0]);
104-
if (this.fields.length != this.keyTypes.length) {
103+
this.keyTypes = (keyTypes.isEmpty()) ? null : keyTypes.toArray(new OType[0]);
104+
if (this.keyTypes != null && (this.fields.length != this.keyTypes.length)) {
105105
throw new OCommandSQLParsingException("Count of fields doesn't match with count of property types. " + "Fields: "
106106
+ Arrays.toString(this.fields) + "; Types: " + Arrays.toString(this.keyTypes));
107107
}

core/src/main/java/com/orientechnologies/orient/core/sql/command/OCommandSelect.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ private void search(final List<OExpression> projections, final long skip, final
265265
}else{
266266
//merge safe and candidates list
267267
final Collection<OIdentifiable> included = searchResult.getIncluded();
268-
final Collection<OIdentifiable> candidates = searchResult.getIncluded();
269-
final Collection<OIdentifiable> excluded = searchResult.getIncluded();
268+
final Collection<OIdentifiable> candidates = searchResult.getCandidates();
269+
final Collection<OIdentifiable> excluded = searchResult.getExcluded();
270270

271271
if(included == OSearchResult.ALL){
272272
//guarantee all result match, we can safely ignore the filter
@@ -276,6 +276,10 @@ private void search(final List<OExpression> projections, final long skip, final
276276
//guarantee no result match, we can complete skip the search
277277
target = Collections.EMPTY_LIST;
278278
simplifiedFilter = OExpression.EXCLUDE;
279+
}else if(included != null && (candidates==null || candidates.isEmpty()) ){
280+
//we have only included results, filter can be skipped
281+
target = source.createIteratorFilterCandidates(included);
282+
simplifiedFilter = OExpression.INCLUDE;
279283
}else{
280284
//reduce the search
281285
if(included != null || candidates != null){

core/src/main/java/com/orientechnologies/orient/core/sql/model/OName.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public OName(String name) {
3636

3737
public OName(String alias, String name) {
3838
super(alias);
39+
//by default name expression has the same alias
40+
if(alias == null){
41+
setAlias(name);
42+
}
3943
this.name = name;
4044
}
4145

core/src/main/java/com/orientechnologies/orient/core/sql/model/OQuerySource.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils;
3838
import static com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils.*;
3939
import java.util.AbstractCollection;
40+
import java.util.Arrays;
4041
import java.util.Collection;
4142
import java.util.HashSet;
4243
import java.util.Iterator;
@@ -112,6 +113,28 @@ public Iterable<OIdentifiable> createIteratorFilterCandidates(Collection<OIdenti
112113
final Set<OIdentifiable> cross = new HashSet<OIdentifiable>(ids);
113114
cross.retainAll((Collection)targetRecords);
114115
return cross;
116+
}else if(targetClasse != null){
117+
if(targetCluster == null){
118+
//we can simply copy the given list
119+
final Set<OIdentifiable> copy = new HashSet<OIdentifiable>(ids);
120+
return copy;
121+
}else{
122+
//exclude ids which are not in the searched cluster
123+
final ODatabaseRecord db = getDatabase();
124+
final int[] clIds = new int[]{db.getClusterIdByName(targetCluster)};
125+
final Set<OIdentifiable> copy = new HashSet<OIdentifiable>();
126+
idloop:
127+
for(OIdentifiable id : ids){
128+
final int idc = id.getIdentity().getClusterId();
129+
for(int cid : clIds){
130+
if(cid == idc){
131+
copy.add(id);
132+
continue idloop;
133+
}
134+
}
135+
}
136+
return copy;
137+
}
115138
}
116139

117140
//can't not optimize, wrap the full iterator and exclude wrong results
@@ -200,7 +223,7 @@ private ClippedCollection(Iterable<? extends OIdentifiable> source, Collection<O
200223

201224
@Override
202225
public Iterator<OIdentifiable> iterator() {
203-
throw new UnsupportedOperationException("Not supported yet.");
226+
return new ClippedIterator();
204227
}
205228

206229
@Override

core/src/test/java/com/orientechnologies/orient/core/sql/command/SelectTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ public void selectField(){
189189
final List<ODocument> docs = db.query(query);
190190
assertEquals(docs.size(), 4);
191191
assertEquals(docs.get(0).fieldNames().length, 1);
192-
assertEquals(docs.get(0).field("0"), "tempo");
193-
assertEquals(docs.get(1).field("0"), "fiesta");
194-
assertEquals(docs.get(2).field("0"), null);
195-
assertEquals(docs.get(3).field("0"), "supreme");
192+
assertEquals(docs.get(0).field("name"), "tempo");
193+
assertEquals(docs.get(1).field("name"), "fiesta");
194+
assertEquals(docs.get(2).field("name"), null);
195+
assertEquals(docs.get(3).field("name"), "supreme");
196196
}
197197

198198
@Test

0 commit comments

Comments
 (0)