Skip to content

Commit

Permalink
fix function bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Russell committed Apr 9, 2024
1 parent 3525fb6 commit 0a91ebb
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 156 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,12 @@ more results? (y/n): n

# Change Log

## [1.22](https://github.com/vincentrussell/sql-to-mongo-db-query-converter/tree/sql-to-mongo-db-query-converter-1.21) (2024-04-09)

**Bugs:**

- Custom functions weren't being parsed correctly


## [1.21](https://github.com/vincentrussell/sql-to-mongo-db-query-converter/tree/sql-to-mongo-db-query-converter-1.21) (2022-03-21)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ private void parseComparativeExpr(final Document query, final Expression leftExp
Document doc = new Document();
Object leftParse = parseExpression(new Document(), leftExpression, rightExpression);
Object rightParse = parseExpression(new Document(), rightExpression, leftExpression);
doc.put(operator, Arrays.asList(leftParse, (SqlUtils.isColumn(rightExpression)
&& !rightExpression.toString().startsWith("$") ? "$" + rightParse : rightParse)));
doc.put(operator, Arrays.asList(leftParse, ((SqlUtils.isColumn(rightExpression)
&& !rightExpression.toString().startsWith("$")
&& !Document.class.isInstance(leftParse)) ? "$" + rightParse : rightParse)));
if (requiresMultistepAggregation) {
query.put("$expr", doc);
} else {
Expand All @@ -117,10 +118,11 @@ private void parseComparativeExpr(final Document query, final Expression leftExp
}
} else if (Function.class.isInstance(rightExpression)) {
Document doc = new Document();
Object leftParse = parseExpression(new Document(), rightExpression, leftExpression);
Object rightParse = parseExpression(new Document(), leftExpression, rightExpression);
doc.put(operator, Arrays.asList(leftParse, (SqlUtils.isColumn(leftExpression)
&& !leftExpression.toString().startsWith("$") ? "$" + rightParse : rightParse)));
Object leftParse = parseExpression(new Document(), leftExpression, rightExpression);
Object rightParse = parseExpression(new Document(), rightExpression, leftExpression);
doc.put(operator, Arrays.asList(leftParse, ((SqlUtils.isColumn(leftExpression)
&& !leftExpression.toString().startsWith("$")
&& !Document.class.isInstance(rightParse)) ? "$" + rightParse : rightParse)));
if (requiresMultistepAggregation) {
query.put("$expr", doc);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,163 +895,163 @@ public void writeJoinWithAllCount() throws ParseException, IOException {
QueryConverter queryConverter = new QueryConverter.Builder().sqlString("select count(*) from Table1 as t join Table2 as e on e._id = OID(t.Table1.eventId) where 1 = 1 AND t.Table1.eventId='111111' AND e.Table2.tid='2222222'").build();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
queryConverter.write(byteArrayOutputStream);
assertEquals("db.Table1.aggregate([{\n" +
" \"$match\": {\n" +
" \"$and\": [\n" +
" {\n" +
" \"$expr\": {\n" +
" \"$eq\": [\n" +
" 1,\n" +
" 1\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"Table1.eventId\": \"111111\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"},{\n" +
" \"$lookup\": {\n" +
" \"from\": \"Table2\",\n" +
" \"let\": {\n" +
" \"table1_eventid\": \"$Table1.eventId\"\n" +
" },\n" +
" \"pipeline\": [\n" +
" {\n" +
" \"$match\": {\n" +
" \"$and\": [\n" +
" {\n" +
" \"$expr\": {\n" +
" \"$eq\": [\n" +
" {\n" +
" \"$toObjectId\": \"$$table1_eventid\"\n" +
" },\n" +
" \"$_id\"\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"$and\": [\n" +
" {\n" +
" \"$expr\": {\n" +
" \"$eq\": [\n" +
" 1,\n" +
" 1\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"Table2.tid\": \"2222222\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"as\": \"e\"\n" +
" }\n" +
"},{\n" +
" \"$unwind\": {\n" +
" \"path\": \"$e\",\n" +
" \"preserveNullAndEmptyArrays\": false\n" +
" }\n" +
"},{\n" +
" \"$group\": {\n" +
" \"_id\": {},\n" +
" \"count\": {\n" +
" \"$sum\": 1\n" +
" }\n" +
" }\n" +
"},{\n" +
" \"$project\": {\n" +
" \"count\": 1,\n" +
" \"_id\": 0\n" +
" }\n" +
"}])",byteArrayOutputStream.toString("UTF-8"));
assertEquals("db.Table1.aggregate([{\n"
+ " \"$match\": {\n"
+ " \"$and\": [\n"
+ " {\n"
+ " \"$expr\": {\n"
+ " \"$eq\": [\n"
+ " 1,\n"
+ " 1\n"
+ " ]\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"Table1.eventId\": \"111111\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "},{\n"
+ " \"$lookup\": {\n"
+ " \"from\": \"Table2\",\n"
+ " \"let\": {\n"
+ " \"table1_eventid\": \"$Table1.eventId\"\n"
+ " },\n"
+ " \"pipeline\": [\n"
+ " {\n"
+ " \"$match\": {\n"
+ " \"$and\": [\n"
+ " {\n"
+ " \"$expr\": {\n"
+ " \"$eq\": [\n"
+ " \"$_id\",\n"
+ " {\n"
+ " \"$toObjectId\": \"$$table1_eventid\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"$and\": [\n"
+ " {\n"
+ " \"$expr\": {\n"
+ " \"$eq\": [\n"
+ " 1,\n"
+ " 1\n"
+ " ]\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"Table2.tid\": \"2222222\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " }\n"
+ " ],\n"
+ " \"as\": \"e\"\n"
+ " }\n"
+ "},{\n"
+ " \"$unwind\": {\n"
+ " \"path\": \"$e\",\n"
+ " \"preserveNullAndEmptyArrays\": false\n"
+ " }\n"
+ "},{\n"
+ " \"$group\": {\n"
+ " \"_id\": {},\n"
+ " \"count\": {\n"
+ " \"$sum\": 1\n"
+ " }\n"
+ " }\n"
+ "},{\n"
+ " \"$project\": {\n"
+ " \"count\": 1,\n"
+ " \"_id\": 0\n"
+ " }\n"
+ "}])",byteArrayOutputStream.toString("UTF-8"));
}

@Test
public void writeJoinWithAllCountNestedDif() throws ParseException, IOException {
QueryConverter queryConverter = new QueryConverter.Builder().sqlString("select count(*) from Table1 as t join Table2 as e on e._id = OID(t.root1.eventId) where 1 = 1 AND t.root1.eventId='111111' AND e.root2.tid='2222222'").build();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
queryConverter.write(byteArrayOutputStream);
assertEquals("db.Table1.aggregate([{\n" +
" \"$match\": {\n" +
" \"$and\": [\n" +
" {\n" +
" \"$expr\": {\n" +
" \"$eq\": [\n" +
" 1,\n" +
" 1\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"root1.eventId\": \"111111\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"},{\n" +
" \"$lookup\": {\n" +
" \"from\": \"Table2\",\n" +
" \"let\": {\n" +
" \"root1_eventid\": \"$root1.eventId\"\n" +
" },\n" +
" \"pipeline\": [\n" +
" {\n" +
" \"$match\": {\n" +
" \"$and\": [\n" +
" {\n" +
" \"$expr\": {\n" +
" \"$eq\": [\n" +
" {\n" +
" \"$toObjectId\": \"$$root1_eventid\"\n" +
" },\n" +
" \"$_id\"\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"$and\": [\n" +
" {\n" +
" \"$expr\": {\n" +
" \"$eq\": [\n" +
" 1,\n" +
" 1\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"root2.tid\": \"2222222\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"as\": \"e\"\n" +
" }\n" +
"},{\n" +
" \"$unwind\": {\n" +
" \"path\": \"$e\",\n" +
" \"preserveNullAndEmptyArrays\": false\n" +
" }\n" +
"},{\n" +
" \"$group\": {\n" +
" \"_id\": {},\n" +
" \"count\": {\n" +
" \"$sum\": 1\n" +
" }\n" +
" }\n" +
"},{\n" +
" \"$project\": {\n" +
" \"count\": 1,\n" +
" \"_id\": 0\n" +
" }\n" +
"}])",byteArrayOutputStream.toString("UTF-8"));
assertEquals("db.Table1.aggregate([{\n"
+ " \"$match\": {\n"
+ " \"$and\": [\n"
+ " {\n"
+ " \"$expr\": {\n"
+ " \"$eq\": [\n"
+ " 1,\n"
+ " 1\n"
+ " ]\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"root1.eventId\": \"111111\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "},{\n"
+ " \"$lookup\": {\n"
+ " \"from\": \"Table2\",\n"
+ " \"let\": {\n"
+ " \"root1_eventid\": \"$root1.eventId\"\n"
+ " },\n"
+ " \"pipeline\": [\n"
+ " {\n"
+ " \"$match\": {\n"
+ " \"$and\": [\n"
+ " {\n"
+ " \"$expr\": {\n"
+ " \"$eq\": [\n"
+ " \"$_id\",\n"
+ " {\n"
+ " \"$toObjectId\": \"$$root1_eventid\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"$and\": [\n"
+ " {\n"
+ " \"$expr\": {\n"
+ " \"$eq\": [\n"
+ " 1,\n"
+ " 1\n"
+ " ]\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"root2.tid\": \"2222222\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " }\n"
+ " ],\n"
+ " \"as\": \"e\"\n"
+ " }\n"
+ "},{\n"
+ " \"$unwind\": {\n"
+ " \"path\": \"$e\",\n"
+ " \"preserveNullAndEmptyArrays\": false\n"
+ " }\n"
+ "},{\n"
+ " \"$group\": {\n"
+ " \"_id\": {},\n"
+ " \"count\": {\n"
+ " \"$sum\": 1\n"
+ " }\n"
+ " }\n"
+ "},{\n"
+ " \"$project\": {\n"
+ " \"count\": 1,\n"
+ " \"_id\": 0\n"
+ " }\n"
+ "}])",byteArrayOutputStream.toString("UTF-8"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ public void betweenWithDates() throws ParseException, java.text.ParseException {
assertEquals(documentValuesArray("$and", document("date", document("$gte", toDate("yyyy-MM-dd", "2012-12-01"))),
document("date", document("$lte", toDate("yyyy-MM-dd", "2012-12-02")))), mongoDBQueryHolder.getQuery());
}

@Test
public void functionsStayOnSameSideOfOperandLeftSide() throws ParseException {
QueryConverter queryConverter = new QueryConverter.Builder().sqlString("select * from table where beginningOfMonth(-1) < eventTime").build();
MongoDBQueryHolder mongoDBQueryHolder = queryConverter.getMongoQuery();
assertEquals(document("$lt", objsToList(document("$beginningOfMonth", Long.parseLong("-1")), "eventTime")), mongoDBQueryHolder.getQuery());
}

@Test
public void functionsStayOnSameSideOfOperandRightSide() throws ParseException {
QueryConverter queryConverter = new QueryConverter.Builder().sqlString("select * from table where eventTime > beginningOfMonth(-1)").build();
MongoDBQueryHolder mongoDBQueryHolder = queryConverter.getMongoQuery();
assertEquals(document("$gt", objsToList("eventTime", document("$beginningOfMonth", Long.parseLong("-1")))), mongoDBQueryHolder.getQuery());
}

@Test
public void functionsOnBothSide() throws ParseException {
QueryConverter queryConverter = new QueryConverter.Builder().sqlString("select * from table where beginningOfMonth(-1) > beginningOfMonth(-1)").build();
MongoDBQueryHolder mongoDBQueryHolder = queryConverter.getMongoQuery();
assertEquals(document("$gt", objsToList(document("$beginningOfMonth", Long.parseLong("-1")), document("$beginningOfMonth", Long.parseLong("-1")))), mongoDBQueryHolder.getQuery());
}

@Test
public void selectFieldSurroundedInQuotesInFunctions() throws ParseException {
Expand Down

0 comments on commit 0a91ebb

Please sign in to comment.