replace 'in' queries with a simpler syntax
field in (val1, val2) was replaced with field=val1,val2 or field=(val1, val2)
This commit is contained in:
@@ -9,13 +9,16 @@ start : expression EOF ;
|
||||
expression
|
||||
: LPAREN expression RPAREN #parenExpression
|
||||
| NOT expression #notExpression
|
||||
| prop=identifier eq=equal value=propValue #propertyExpression
|
||||
//| prop=identifier in=inExpr LPAREN listOfProperties=listOfPropValues RPAREN #inExpression
|
||||
| '_in' prop=identifier in=inExpr LPAREN listOfProperties=listOfPropValues RPAREN #inExpression
|
||||
| prop=identifier eq=equal enclosedListOfPropValues #propertyExpression
|
||||
| left=expression AND right=expression #binaryAndExpression
|
||||
| left=expression OR right=expression #binaryOrExpression
|
||||
;
|
||||
|
||||
enclosedListOfPropValues
|
||||
: listOfPropValues
|
||||
| LPAREN listOfProperties=listOfPropValues RPAREN
|
||||
;
|
||||
|
||||
listOfPropValues
|
||||
: value=propValue
|
||||
| leftValue=propValue COMMA listOfProperties=listOfPropValues
|
||||
@@ -29,7 +32,6 @@ propValue
|
||||
;
|
||||
|
||||
equal : EQUAL ;
|
||||
inExpr : IN ;
|
||||
|
||||
AND : 'and' ;
|
||||
OR : 'or' ;
|
||||
|
||||
@@ -40,7 +40,7 @@ public class Proposer {
|
||||
result = ProposerParser.parse(q.toString(), dataStore, caretIndex + 1);
|
||||
}
|
||||
|
||||
return CollectionUtils.filter(result, p -> p.hasResults());
|
||||
return CollectionUtils.filter(result, Proposal::hasResults);
|
||||
}
|
||||
|
||||
private SortedSet<Proposal> proposeForAllKeys() {
|
||||
|
||||
@@ -26,7 +26,7 @@ public class ProposerParser {
|
||||
final CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
|
||||
final QueryCompletionPdbLangParser parser = new QueryCompletionPdbLangParser(tokens);
|
||||
parser.setTrace(true);
|
||||
parser.setTrace(false);
|
||||
|
||||
final Listener listener = parser.new Listener(query, dataStore, caretIndex);
|
||||
parser.addErrorListener(listener);
|
||||
|
||||
@@ -60,7 +60,8 @@ public class QueryCompletionPdbLangParser extends PdbLangParser {
|
||||
if (_ctx.getParent() instanceof ListOfPropValuesContext) {
|
||||
// for in-expressions, e.g. key in (val)
|
||||
ParserRuleContext parent = _ctx.getParent();
|
||||
while (parent instanceof ListOfPropValuesContext) {
|
||||
while (parent instanceof ListOfPropValuesContext
|
||||
|| parent instanceof EnclosedListOfPropValuesContext) {
|
||||
parent = parent.getParent();
|
||||
}
|
||||
|
||||
@@ -274,26 +275,10 @@ public class QueryCompletionPdbLangParser extends PdbLangParser {
|
||||
public void enterEqual(final EqualContext ctx) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterInExpr(final InExprContext ctx) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterInExpression(final InExpressionContext ctx) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitEqual(final EqualContext ctx) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitInExpr(final InExprContext ctx) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitInExpression(final InExpressionContext ctx) {
|
||||
}
|
||||
|
||||
private boolean isEOF(final TerminalNode node) {
|
||||
return node.getSymbol().getType() < 0;
|
||||
}
|
||||
@@ -326,6 +311,14 @@ public class QueryCompletionPdbLangParser extends PdbLangParser {
|
||||
@Override
|
||||
public void exitListOfPropValues(final ListOfPropValuesContext ctx) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterEnclosedListOfPropValues(final EnclosedListOfPropValuesContext ctx) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitEnclosedListOfPropValues(final EnclosedListOfPropValuesContext ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
public QueryCompletionPdbLangParser(final TokenStream input) {
|
||||
|
||||
@@ -13,16 +13,14 @@ import org.lucares.pdb.datastore.lang.Expression.InExpression;
|
||||
import org.lucares.pdb.datastore.lang.Expression.ListOfPropertyValues;
|
||||
import org.lucares.pdb.datastore.lang.Expression.Not;
|
||||
import org.lucares.pdb.datastore.lang.Expression.OrTemporary;
|
||||
import org.lucares.pdb.datastore.lang.Expression.Property;
|
||||
import org.lucares.pdb.datastore.lang.Expression.TemporaryExpression;
|
||||
import org.lucares.pdb.datastore.lang.Expression.Terminal;
|
||||
import org.lucares.pdb.datastore.lang.PdbLangParser.BinaryAndExpressionContext;
|
||||
import org.lucares.pdb.datastore.lang.PdbLangParser.BinaryOrExpressionContext;
|
||||
import org.lucares.pdb.datastore.lang.PdbLangParser.EnclosedListOfPropValuesContext;
|
||||
import org.lucares.pdb.datastore.lang.PdbLangParser.IdentifierExpressionContext;
|
||||
import org.lucares.pdb.datastore.lang.PdbLangParser.InExpressionContext;
|
||||
import org.lucares.pdb.datastore.lang.PdbLangParser.ListOfPropValuesContext;
|
||||
import org.lucares.pdb.datastore.lang.PdbLangParser.NotExpressionContext;
|
||||
import org.lucares.pdb.datastore.lang.PdbLangParser.PropertyExpressionContext;
|
||||
import org.lucares.pdb.datastore.lang.PdbLangParser.PropertyTerminalExpressionContext;
|
||||
|
||||
public class QueryLanguage {
|
||||
@@ -72,16 +70,16 @@ public class QueryLanguage {
|
||||
|
||||
stack.push(new Terminal(ctx.getText(), line, startIndex, stopIndex));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitPropertyExpression(final PropertyExpressionContext ctx) {
|
||||
// System.out.println("property expression");
|
||||
|
||||
final Expression value = stack.pop();
|
||||
final Terminal property = (Terminal) stack.pop();
|
||||
|
||||
stack.push(new Property(property.getValue(), (Terminal) value));
|
||||
}
|
||||
// TODO remove
|
||||
// @Override
|
||||
// public void exitPropertyExpression(final PropertyExpressionContext ctx) {
|
||||
// // System.out.println("property expression");
|
||||
//
|
||||
// final Expression value = stack.pop();
|
||||
// final Terminal property = (Terminal) stack.pop();
|
||||
//
|
||||
// stack.push(new Property(property.getValue(), (Terminal) value));
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void exitNotExpression(final NotExpressionContext ctx) {
|
||||
@@ -134,7 +132,7 @@ public class QueryLanguage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitInExpression(final InExpressionContext ctx) {
|
||||
public void exitEnclosedListOfPropValues(final EnclosedListOfPropValuesContext ctx) {
|
||||
|
||||
final ListOfPropertyValues propertyValues = (ListOfPropertyValues) stack.pop();
|
||||
final Terminal propertyName = (Terminal) stack.pop();
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.lucares.pdb.datastore.internal;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -77,16 +76,12 @@ public class DataStoreTest {
|
||||
assertSearch("dog=*lab*dor*", labradorJenny, labradorTim);
|
||||
|
||||
// 'in' queries
|
||||
// TODO fix in queries
|
||||
/*
|
||||
* assertSearch("bird in (eagle, pigeon, flamingo)", eagleTim, pigeonJennifer,
|
||||
* flamingoJennifer);
|
||||
* assertSearch("dog in (labrador) and name in (Tim, Jennifer)", labradorTim);
|
||||
* assertSearch("name in (Jenn*)", pigeonJennifer, flamingoJennifer,
|
||||
* labradorJenny); assertSearch("name in (*) and dog=labrador", labradorJenny,
|
||||
* labradorTim); assertSearch("name in (XYZ, *) and dog=labrador",
|
||||
* labradorJenny, labradorTim);
|
||||
*/
|
||||
assertSearch("bird=(eagle, pigeon, flamingo)", eagleTim, pigeonJennifer, flamingoJennifer);
|
||||
assertSearch("dog = (labrador) and name =Tim,Jennifer", labradorTim);
|
||||
assertSearch("name =Jenn*", pigeonJennifer, flamingoJennifer, labradorJenny);
|
||||
assertSearch("name = (*) and dog=labrador", labradorJenny, labradorTim);
|
||||
assertSearch("name =XYZ, * and dog=labrador", labradorJenny, labradorTim);
|
||||
|
||||
}
|
||||
|
||||
public void testGetByTags() throws IOException {
|
||||
@@ -120,30 +115,4 @@ public class DataStoreTest {
|
||||
Assert.assertEquals(actual, expectedPaths, "Query: " + query + " Found: " + actual);
|
||||
}
|
||||
|
||||
// private List<Tags> getTagsForPaths(final List<Path> paths) {
|
||||
//
|
||||
// final List<Tags> result = new ArrayList<>();
|
||||
//
|
||||
// for (final Path path : paths) {
|
||||
// result.add(getTagForPath(path));
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
//
|
||||
// private Tags getTagForPath(final Path path) {
|
||||
// for (final Entry<Tags, Long> e : tagsToBlockStorageRootBlockNumber.entrySet()) {
|
||||
//
|
||||
// if (e.getValue().equals(path)) {
|
||||
// return e.getKey();
|
||||
// }
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
private void assertSearch(final DataStore dataStore, final String query, final Path... paths) {
|
||||
final List<Doc> actualDocs = dataStore.search(query);
|
||||
final List<Long> actual = CollectionUtils.map(actualDocs, Doc::getRootBlockNumber);
|
||||
|
||||
Assert.assertEquals(actual, Arrays.asList(paths));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,11 +81,11 @@ public class ProposerTest {
|
||||
|
||||
public void testPrefixOfValue() throws Exception {
|
||||
assertProposals("name =Tim", 9, //
|
||||
new Proposal("Timothy", "name =Timothy ", true, "name =Timothy ", 14));
|
||||
new Proposal("Timothy", "name =Timothy", true, "name =Timothy", 13));
|
||||
|
||||
assertProposals("name =Je", 8, //
|
||||
new Proposal("Jennifer", "name =Jennifer ", true, "name =Jennifer ", 15), //
|
||||
new Proposal("Jenny", "name =Jenny ", true, "name =Jenny ", 12) //
|
||||
new Proposal("Jennifer", "name =Jennifer", true, "name =Jennifer", 14), //
|
||||
new Proposal("Jenny", "name =Jenny", true, "name =Jenny", 11) //
|
||||
);
|
||||
|
||||
assertProposals("bird=eagle and n", 16, //
|
||||
@@ -95,22 +95,21 @@ public class ProposerTest {
|
||||
*/
|
||||
}
|
||||
|
||||
// TODO fix the in expression
|
||||
@Test(enabled = false)
|
||||
@Test(enabled = true)
|
||||
public void testInExpressions() throws Exception {
|
||||
assertProposals("name in (Timothy,)", 17, //
|
||||
new Proposal("Jennifer", "name in (Timothy,Jennifer)", true, "name in (Timothy,Jennifer)", 25), //
|
||||
new Proposal("Jenny", "name in (Timothy,Jenny)", true, "name in (Timothy,Jenny)", 22), //
|
||||
new Proposal("Tim", "name in (Timothy,Tim)", true, "name in (Timothy,Tim)", 20), //
|
||||
new Proposal("Timothy", "name in (Timothy,Timothy)", true, "name in (Timothy,Timothy)", 24)//
|
||||
assertProposals("name = (Timothy,)", 16, //
|
||||
new Proposal("Jennifer", "name = (Timothy,Jennifer)", true, "name = (Timothy,Jennifer)", 24), //
|
||||
new Proposal("Jenny", "name = (Timothy,Jenny)", true, "name = (Timothy,Jenny)", 21), //
|
||||
new Proposal("Tim", "name = (Timothy,Tim)", true, "name = (Timothy,Tim)", 19), //
|
||||
new Proposal("Timothy", "name = (Timothy,Timothy)", true, "name = (Timothy,Timothy)", 23)//
|
||||
);
|
||||
|
||||
assertProposals("name in (Timothy, J)", 19, //
|
||||
new Proposal("Jennifer", "name in (Timothy, Jennifer)", true, "name in (Timothy, Jennifer)", 26), //
|
||||
new Proposal("Jenny", "name in (Timothy, Jenny)", true, "name in (Timothy, Jenny)", 23));
|
||||
assertProposals("name = (Timothy, J)", 18, //
|
||||
new Proposal("Jennifer", "name = (Timothy, Jennifer)", true, "name = (Timothy, Jennifer)", 25), //
|
||||
new Proposal("Jenny", "name = (Timothy, Jenny)", true, "name = (Timothy, Jenny)", 22));
|
||||
|
||||
assertProposals("name in (Tim)", 12, //
|
||||
new Proposal("Timothy", "name in (Timothy)", true, "name in (Timothy)", 16));
|
||||
assertProposals("name = (Tim)", 11, //
|
||||
new Proposal("Timothy", "name = (Timothy)", true, "name = (Timothy)", 15));
|
||||
|
||||
/*
|
||||
*/
|
||||
@@ -118,10 +117,10 @@ public class ProposerTest {
|
||||
|
||||
public void testProposalOnEmptyValuePrefix() throws Exception {
|
||||
assertProposals("name=", 5, //
|
||||
new Proposal("Jennifer", "name=Jennifer ", true, "name=Jennifer ", 14), //
|
||||
new Proposal("Jenny", "name=Jenny ", true, "name=Jenny ", 11), //
|
||||
new Proposal("Tim", "name=Tim ", true, "name=Tim ", 9), //
|
||||
new Proposal("Timothy", "name=Timothy ", true, "name=Timothy ", 13) //
|
||||
new Proposal("Jennifer", "name=Jennifer", true, "name=Jennifer", 13), //
|
||||
new Proposal("Jenny", "name=Jenny", true, "name=Jenny", 10), //
|
||||
new Proposal("Tim", "name=Tim", true, "name=Tim", 8), //
|
||||
new Proposal("Timothy", "name=Timothy", true, "name=Timothy", 12) //
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user