-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(query-generation): add support for $sort INTELLIJ-196 #131
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...ngodb-dialects/mongosh/src/main/kotlin/com/mongodb/jbplugin/dialects/mongosh/aggr/Sort.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.mongodb.jbplugin.dialects.mongosh.aggr | ||
|
||
import com.mongodb.jbplugin.dialects.mongosh.backend.MongoshBackend | ||
import com.mongodb.jbplugin.mql.Node | ||
import com.mongodb.jbplugin.mql.components.HasSorts | ||
|
||
internal fun <S> MongoshBackend.emitSortStage(node: Node<S>): MongoshBackend { | ||
val sorts = node.component<HasSorts<S>>()?.children ?: emptyList() | ||
val isLongSortChain = sorts.size > 3 | ||
|
||
emitObjectStart(long = isLongSortChain) | ||
emitObjectKey(registerConstant('$' + "sort")) | ||
emitObjectStart(long = isLongSortChain) | ||
emitAsFieldValueDocument(sorts, isLongSortChain) | ||
emitObjectEnd(long = isLongSortChain) | ||
emitObjectEnd(long = isLongSortChain) | ||
|
||
return this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...b-dialects/mongosh/src/test/kotlin/com/mongodb/jbplugin/dialects/mongosh/aggr/SortTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.mongodb.jbplugin.dialects.mongosh.aggr | ||
|
||
import com.mongodb.jbplugin.dialects.mongosh.assertGeneratedQuery | ||
import com.mongodb.jbplugin.mql.BsonInt32 | ||
import com.mongodb.jbplugin.mql.Node | ||
import com.mongodb.jbplugin.mql.components.HasAggregation | ||
import com.mongodb.jbplugin.mql.components.HasFieldReference | ||
import com.mongodb.jbplugin.mql.components.HasSorts | ||
import com.mongodb.jbplugin.mql.components.HasValueReference | ||
import com.mongodb.jbplugin.mql.components.IsCommand | ||
import com.mongodb.jbplugin.mql.components.Name | ||
import com.mongodb.jbplugin.mql.components.Named | ||
import org.junit.jupiter.api.Test | ||
|
||
class SortTest { | ||
@Test | ||
fun `can format a sort stage`() { | ||
assertGeneratedQuery( | ||
""" | ||
var collection = "" | ||
var database = "" | ||
|
||
db.getSiblingDB(database).getCollection(collection).aggregate([{"${"$"}sort": {"myField": 1, "myOtherField": -1, }}, ]) | ||
""".trimIndent() | ||
) { | ||
Node( | ||
Unit, | ||
listOf( | ||
IsCommand(IsCommand.CommandType.AGGREGATE), | ||
HasAggregation( | ||
listOf( | ||
Node( | ||
Unit, | ||
listOf( | ||
Named(Name.SORT), | ||
HasSorts( | ||
listOf( | ||
Node( | ||
Unit, | ||
listOf( | ||
HasFieldReference( | ||
HasFieldReference.FromSchema( | ||
Unit, | ||
"myField" | ||
) | ||
), | ||
HasValueReference( | ||
HasValueReference.Inferred( | ||
Unit, | ||
1, | ||
BsonInt32 | ||
) | ||
) | ||
) | ||
), | ||
Node( | ||
Unit, | ||
listOf( | ||
HasFieldReference( | ||
HasFieldReference.FromSchema( | ||
Unit, | ||
"myOtherField" | ||
) | ||
), | ||
HasValueReference( | ||
HasValueReference.Inferred( | ||
Unit, | ||
-1, | ||
BsonInt32 | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this because I realised that we weren't supporting Computed values. This is something that we will need to improve whenever we support Computed values that are more than just a field reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to be interesting because now we can finally see if the root bsonType will be helpful in generating these queries or not. Right now we only take one HasFieldReference component but there could be more :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we leave a todo with a ticket or were you planning to tackle this as part of group?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK for now we are only going to support field names as references, no arbitrary expressions, so I think this is kinda of enough for our use cases. I was thinking that we could rework this method in the future whenever we support more types of expressions. I can create a ticket for this, I think it's good to track it.