Skip to content

Commit

Permalink
Change the way how relevance model arguments are passed.
Browse files Browse the repository at this point in the history
Instead of requiring the argument names to be put in strings, like
'srcId':12345, we will just accept variable identifiers, like
srcId:12345.
  • Loading branch information
Baoqiu Cui committed Jul 15, 2012
1 parent 972a243 commit 5a08a3a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 41 deletions.
38 changes: 25 additions & 13 deletions sensei-core/src/main/antlr3/com/senseidb/bql/parsers/BQL.g
Expand Up @@ -2266,7 +2266,7 @@ python_style_dict returns [JSONObject json]
@init {
$json = new JSONObject();
}
: '{' p=key_value_pair
: '{' p=key_value_pair[true]
{
try {
$json.put($p.key, $p.val);
Expand All @@ -2275,7 +2275,7 @@ python_style_dict returns [JSONObject json]
throw new FailedPredicateException(input, "python_style_dict", "JSONException: " + err.getMessage());
}
}
(COMMA p=key_value_pair
(COMMA p=key_value_pair[true]
{
try {
$json.put($p.key, $p.val);
Expand Down Expand Up @@ -2350,17 +2350,17 @@ except_clause returns [Object json]
;
predicate_props returns [JSONObject json]
: WITH^ prop_list
: WITH^ prop_list[true]
{
$json = $prop_list.json;
}
;
prop_list returns [JSONObject json]
prop_list[boolean needKeyInString] returns [JSONObject json]
@init {
$json = new JSONObject();
}
: LPAR p=key_value_pair
: LPAR p=key_value_pair[needKeyInString]
{
try {
$json.put($p.key, $p.val);
Expand All @@ -2369,7 +2369,7 @@ prop_list returns [JSONObject json]
throw new FailedPredicateException(input, "prop_list", "JSONException: " + err.getMessage());
}
}
(COMMA p=key_value_pair
(COMMA p=key_value_pair[needKeyInString]
{
try {
$json.put($p.key, $p.val);
Expand All @@ -2379,15 +2379,27 @@ prop_list returns [JSONObject json]
}
}
)* RPAR
-> key_value_pair+
;
key_value_pair returns [String key, Object val]
: STRING_LITERAL COLON (v=value | vs=python_style_list | vd=python_style_dict)
key_value_pair[boolean needKeyInString] returns [String key, Object val]
scope {
boolean needString;
}
@init {
$key_value_pair::needString = needKeyInString;
}
: ( {$key_value_pair::needString}?=> STRING_LITERAL
| {!$key_value_pair::needString}?=> IDENT
)
COLON (v=value | vs=python_style_list | vd=python_style_dict)
{
String orig = $STRING_LITERAL.text;
orig = orig.substring(1, orig.length() - 1);
$key = orig;
if ($STRING_LITERAL != null) {
String orig = $STRING_LITERAL.text;
$key = orig.substring(1, orig.length() - 1);
}
else {
$key = $IDENT.text;
}
if (v != null) {
$val = $v.val;
}
Expand Down Expand Up @@ -2973,7 +2985,7 @@ relevance_model_clause returns [JSONObject json]
@init {
$json = new JSONObject();
}
: USING RELEVANCE MODEL IDENT prop_list model=relevance_model?
: USING RELEVANCE MODEL IDENT prop_list[false] model=relevance_model?
{
try {
if (model == null) {
Expand Down
Expand Up @@ -253,7 +253,7 @@ public void testBqlExtraFilter() throws Exception
public void testBqlRelevance1() throws Exception
{
logger.info("Executing test case testBqlRelevance1");
String req = "{\"bql\":\"SELECT * FROM cars USING RELEVANCE MODEL my_model ('thisYear':2001, 'goodYear':[1996]) DEFINED AS (int thisYear, IntOpenHashSet goodYear) BEGIN if (goodYear.contains(year)) return (float)Math.exp(10d); if (year==thisYear) return 87f; return _INNER_SCORE; END\"}";
String req = "{\"bql\":\"SELECT * FROM cars USING RELEVANCE MODEL my_model (thisYear:2001, goodYear:[1996]) DEFINED AS (int thisYear, IntOpenHashSet goodYear) BEGIN if (goodYear.contains(year)) return (float)Math.exp(10d); if (year==thisYear) return 87f; return _INNER_SCORE; END\"}";

JSONObject res = search(new JSONObject(req));

Expand Down
Expand Up @@ -1281,7 +1281,7 @@ public void testRelevanceModel1() throws Exception
"SELECT color, year " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL homepage_top ('srcid':1234)"
"USING RELEVANCE MODEL homepage_top (srcid:1234)"
);

JSONObject expected = new JSONObject("{\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"values\":{\"srcid\":1234},\"predefined_model\":\"homepage_top\"}}},\"selections\":[{\"term\":{\"color\":{\"value\":\"red\"}}}],\"meta\":{\"select_list\":[\"color\",\"year\"]}}");
Expand All @@ -1298,7 +1298,7 @@ public void testRelevanceModelIfStmt() throws Exception
"SELECT color, year " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('srcid':1234) " +
"USING RELEVANCE MODEL my_model (srcid:1234) " +
" DEFINED AS (int intParam1, int intParam2, String strParam, int srcid) " +
" BEGIN " +
" int myInt = 100 + intParam1 + intParam2; " +
Expand Down Expand Up @@ -1326,7 +1326,7 @@ public void testRelevanceModelFloatLiteral() throws Exception
"SELECT color, year " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('srcid':1234) " +
"USING RELEVANCE MODEL my_model (srcid:1234) " +
" DEFINED AS (int srcid) " +
" BEGIN " +
" float x1 = 1.2; " +
Expand Down Expand Up @@ -1355,7 +1355,7 @@ public void testRelevanceModelDataTypes() throws Exception
"SELECT color, year " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('srcid':1234) " +
"USING RELEVANCE MODEL my_model (srcid:1234) " +
" DEFINED AS (int intParam1, int intParam2, String strParam) " +
" BEGIN " +
" int myInt = 100; " +
Expand Down Expand Up @@ -1387,7 +1387,7 @@ public void testRelevanceModelWhileStmt() throws Exception
"SELECT color, year " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('srcid':1234) " +
"USING RELEVANCE MODEL my_model (srcid:1234) " +
" DEFINED AS (int srcid) " +
" BEGIN " +
" int myInt = 100; " +
Expand All @@ -1413,7 +1413,7 @@ public void testRelevanceModelDoWhile() throws Exception
"SELECT color, year " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('srcid':1234) " +
"USING RELEVANCE MODEL my_model (srcid:1234) " +
" DEFINED AS (int srcid) " +
" BEGIN " +
" int myInt = 100; " +
Expand All @@ -1438,7 +1438,7 @@ public void testRelevanceModelForLoop() throws Exception
"SELECT color, year " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('srcid':1234) " +
"USING RELEVANCE MODEL my_model (srcid:1234) " +
" DEFINED AS (int srcid) " +
" BEGIN " +
" int myInt = 0; " +
Expand All @@ -1463,7 +1463,7 @@ public void testRelevanceModelSwitchStmt() throws Exception
"SELECT color, year " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('srcid':1234) " +
"USING RELEVANCE MODEL my_model (srcid:1234) " +
" DEFINED AS (int srcid) " +
" BEGIN " +
" int myInt = 0; " +
Expand Down Expand Up @@ -1494,7 +1494,7 @@ public void testRelevanceModelExpressions() throws Exception
"SELECT color, year " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('srcid':1234, 'timeVal':9999, '_half_time':8888, 'coolTag':'zzz') " +
"USING RELEVANCE MODEL my_model (srcid:1234, timeVal:9999, _half_time:8888, coolTag:'zzz') " +
" DEFINED AS (int srcid, long timeVal, long _half_time, String coolTag) " +
" BEGIN " +
" int myInt = 0; " +
Expand Down Expand Up @@ -1525,7 +1525,7 @@ public void testRelevanceModelParameters() throws Exception
"SELECT color, year " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('srcid':1234) " +
"USING RELEVANCE MODEL my_model (srcid:1234) " +
" DEFINED AS (int intParam1, int intParam2, String strParam, " +
" DoubleOpenHashSet setParam, Int2IntOpenHashMap mapParam) " +
" BEGIN " +
Expand Down Expand Up @@ -1557,7 +1557,7 @@ public void testRelevanceModelExample1() throws Exception
"SELECT * " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('thisYear':2001, 'goodYear':[1996]) " +
"USING RELEVANCE MODEL my_model (thisYear:2001, goodYear:[1996]) " +
" DEFINED AS (int thisYear, IntOpenHashSet goodYear) " +
" BEGIN " +
" if (goodYear.contains(year)) " +
Expand All @@ -1583,7 +1583,7 @@ public void testRelevanceModelExample2() throws Exception
"SELECT * " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('thisYear':2001, 'goodYear':[1996]) " +
"USING RELEVANCE MODEL my_model (thisYear:2001, goodYear:[1996]) " +
" DEFINED AS (int thisYear, IntOpenHashSet goodYear) " +
" BEGIN " +
" if (goodYear.contains(year)) " +
Expand Down Expand Up @@ -1611,7 +1611,7 @@ public void testRelevanceModelVariableScopes() throws Exception
"SELECT * " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('boost':2.5) " +
"USING RELEVANCE MODEL my_model (boost:2.5) " +
" DEFINED AS (float boost) " +
" BEGIN " +
" int x, y; " +
Expand All @@ -1638,7 +1638,7 @@ public void testRelevanceModelMapValue() throws Exception
"SELECT * " +
"FROM cars " +
"WHERE color = 'red' " +
"USING RELEVANCE MODEL my_model ('thisYear':2001, 'myMap':{'aaa':1, 'bbb':2}) " +
"USING RELEVANCE MODEL my_model (thisYear:2001, myMap:{'aaa':1, 'bbb':2}) " +
"ORDER BY relevance"
);

Expand Down
Expand Up @@ -937,13 +937,13 @@ public void testUsingRelevanceOnce() throws Exception
JSONObject json = _compiler.compile(
"select category \n" +
"from cars \n" +
"using relevance model md1 ('srcid':1234) \n" +
"using relevance model md2 ('param1':'abc')"
"using relevance model md1 (srcid:1234) \n" +
"using relevance model md2 (param1:'abc')"
);
}
catch (RecognitionException err)
{
assertEquals("[line:4, col:42] USING RELEVANCE MODEL clause can only appear once. (token=<EOF>)",
assertEquals("[line:4, col:40] USING RELEVANCE MODEL clause can only appear once. (token=<EOF>)",
_compiler.getErrorMessage(err));
caughtException = true;
}
Expand All @@ -965,7 +965,7 @@ public void testRelevanceVarRedefined() throws Exception
JSONObject json = _compiler.compile(
"SELECT * \n" +
"FROM cars \n" +
"USING RELEVANCE MODEL md1 ('srcid':1234) \n" +
"USING RELEVANCE MODEL md1 (srcid:1234) \n" +
" DEFINED AS (int srcid) \n" +
" BEGIN \n" +
" int x, y; \n" +
Expand Down Expand Up @@ -998,7 +998,7 @@ public void testRelevanceUndefinedVar1() throws Exception
JSONObject json = _compiler.compile(
"SELECT * \n" +
"FROM cars \n" +
"USING RELEVANCE MODEL md1 ('srcid':1234) \n" +
"USING RELEVANCE MODEL md1 (srcid:1234) \n" +
" DEFINED AS (int srcid) \n" +
" BEGIN \n" +
" if (x == 5) \n" +
Expand Down Expand Up @@ -1030,7 +1030,7 @@ public void testRelevanceUndefinedVar2() throws Exception
JSONObject json = _compiler.compile(
"SELECT * \n" +
"FROM cars \n" +
"USING RELEVANCE MODEL md1 ('srcid':1234) \n" +
"USING RELEVANCE MODEL md1 (srcid:1234) \n" +
" DEFINED AS (int srcid) \n" +
" BEGIN \n" +
" int x = 5; \n" +
Expand Down Expand Up @@ -1068,7 +1068,7 @@ public void testRelevanceUndefinedVar3() throws Exception
JSONObject json = _compiler.compile(
"SELECT * \n" +
"FROM cars \n" +
"USING RELEVANCE MODEL md1 ('srcid':1234) \n" +
"USING RELEVANCE MODEL md1 (srcid:1234) \n" +
" DEFINED AS (int srcid) \n" +
" BEGIN \n" +
" int total = 0; \n" +
Expand Down Expand Up @@ -1102,7 +1102,7 @@ public void testRelevanceVarDeclError1() throws Exception
JSONObject json = _compiler.compile(
"SELECT * \n" +
"FROM cars \n" +
"USING RELEVANCE MODEL md1 ('srcid':1234) \n" +
"USING RELEVANCE MODEL md1 (srcid:1234) \n" +
" DEFINED AS (int srcid) \n" +
" BEGIN \n" +
" int year; \n" +
Expand Down Expand Up @@ -1132,7 +1132,7 @@ public void testRelevanceVarDeclError2() throws Exception
JSONObject json = _compiler.compile(
"SELECT * \n" +
"FROM cars \n" +
"USING RELEVANCE MODEL md1 ('srcid':1234) \n" +
"USING RELEVANCE MODEL md1 (srcid:1234) \n" +
" DEFINED AS (int srcid) \n" +
" BEGIN \n" +
" String _NOW; \n" +
Expand Down Expand Up @@ -1162,7 +1162,7 @@ public void testRelevanceVarDeclError3() throws Exception
JSONObject json = _compiler.compile(
"SELECT * \n" +
"FROM cars \n" +
"USING RELEVANCE MODEL md1 ('srcid':1234) \n" +
"USING RELEVANCE MODEL md1 (srcid:1234) \n" +
" DEFINED AS (int srcid) \n" +
" BEGIN \n" +
" int x = 100; \n" +
Expand Down Expand Up @@ -1195,7 +1195,7 @@ public void testRelevanceModelParamError1() throws Exception
JSONObject json = _compiler.compile(
"SELECT * \n" +
"FROM cars \n" +
"USING RELEVANCE MODEL md1 ('srcid':1234) \n" +
"USING RELEVANCE MODEL md1 (srcid:1234) \n" +
" DEFINED AS (int srcid, float price) \n" +
" BEGIN \n" +
" return 0.5; \n" +
Expand Down Expand Up @@ -1226,7 +1226,7 @@ public void testRelevanceModelParamError2() throws Exception
JSONObject json = _compiler.compile(
"SELECT * \n" +
"FROM cars \n" +
"USING RELEVANCE MODEL md1 ('srcid':1234) \n" +
"USING RELEVANCE MODEL md1 (srcid:1234) \n" +
" DEFINED AS (int srcid, String srcid) \n" +
" BEGIN \n" +
" return 0.5; \n" +
Expand Down Expand Up @@ -1257,7 +1257,7 @@ public void testRelevanceModelParamError3() throws Exception
JSONObject json = _compiler.compile(
"SELECT * \n" +
"FROM cars \n" +
"USING RELEVANCE MODEL md1 ('srcid':1234) \n" +
"USING RELEVANCE MODEL md1 (srcid:1234) \n" +
" DEFINED AS (int srcid, long _NOW) \n" +
" BEGIN \n" +
" return 0.5; \n" +
Expand Down

0 comments on commit 5a08a3a

Please sign in to comment.