-
Notifications
You must be signed in to change notification settings - Fork 0
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: support generating shell syntax for $group INTELLIJ-197 #134
Merged
himanshusinghs
merged 4 commits into
main
from
INTELLIJ-197-generate-shell-syntax-group-stage
Jan 30, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
176 changes: 176 additions & 0 deletions
176
...godb-dialects/mongosh/src/main/kotlin/com/mongodb/jbplugin/dialects/mongosh/aggr/Group.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,176 @@ | ||
package com.mongodb.jbplugin.dialects.mongosh.aggr | ||
|
||
import com.mongodb.jbplugin.dialects.mongosh.backend.MongoshBackend | ||
import com.mongodb.jbplugin.dialects.mongosh.query.resolveFieldReference | ||
import com.mongodb.jbplugin.dialects.mongosh.query.resolveValueReference | ||
import com.mongodb.jbplugin.mql.Node | ||
import com.mongodb.jbplugin.mql.components.HasAccumulatedFields | ||
import com.mongodb.jbplugin.mql.components.HasFieldReference | ||
import com.mongodb.jbplugin.mql.components.HasLimit | ||
import com.mongodb.jbplugin.mql.components.HasSorts | ||
import com.mongodb.jbplugin.mql.components.HasValueReference | ||
import com.mongodb.jbplugin.mql.components.Name | ||
import com.mongodb.jbplugin.mql.components.Named | ||
|
||
internal fun <S>MongoshBackend.emitGroupStage(node: Node<S>): MongoshBackend { | ||
val idFieldReference = node.component<HasFieldReference<S>>() | ||
val idValueReference = node.component<HasValueReference<S>>() | ||
|
||
val accumulatedFields = node.component<HasAccumulatedFields<S>>()?.children ?: emptyList() | ||
val emitLongQuery = idValueReference.isLongIdValueReference() || accumulatedFields.size > 2 | ||
|
||
// "{" | ||
emitObjectStart(long = emitLongQuery) | ||
// "{ $group : " | ||
emitObjectKey(registerConstant('$' + "group")) | ||
// "{ $group : { " | ||
emitObjectStart(long = emitLongQuery) | ||
if (idFieldReference != null && idValueReference != null) { | ||
// "{ $group : { _id : null, " | ||
emitAsFieldValueDocument(listOf(node), isLong = emitLongQuery) | ||
// "{ $group : { _id : null, totalCount: { $sum : 1 }, " | ||
emitAccumulatedFields(accumulatedFields, emitLongQuery) | ||
} | ||
// "{ $group : { _id : null, totalCount: { $sum : 1 }, }" | ||
emitObjectEnd(long = emitLongQuery) | ||
// "{ $group : { _id : null, totalCount: { $sum : 1 }, } }" | ||
emitObjectEnd(long = emitLongQuery) | ||
|
||
return this | ||
} | ||
|
||
private fun <S>HasValueReference<S>?.isLongIdValueReference(): Boolean { | ||
val computedReference = this?.reference as? HasValueReference.Computed<S> | ||
?: return false | ||
val fieldReferences = computedReference.type.expression.components<HasFieldReference<S>>() | ||
return fieldReferences.size >= 3 | ||
} | ||
|
||
private fun <S>MongoshBackend.emitAccumulatedFields( | ||
accumulatedFields: List<Node<S>>, | ||
emitLongQuery: Boolean | ||
): MongoshBackend { | ||
for (accumulatedField in accumulatedFields) { | ||
val accumulator = accumulatedField.component<Named>() ?: continue | ||
when (accumulator.name) { | ||
Name.SUM, | ||
Name.AVG, | ||
Name.MIN, | ||
Name.MAX, | ||
Name.FIRST, | ||
Name.LAST, | ||
Name.PUSH, | ||
Name.ADD_TO_SET -> { | ||
emitKeyValueAccumulator( | ||
accumulator, | ||
accumulatedField, | ||
emitLongQuery | ||
) | ||
// "{ $group : { _id : null, totalCount : { $sum : 1 } | ||
emitObjectValueEnd(long = emitLongQuery) | ||
} | ||
Name.TOP, | ||
Name.TOP_N, | ||
Name.BOTTOM, | ||
Name.BOTTOM_N -> { | ||
emitTopBottomAccumulator( | ||
accumulator, | ||
accumulatedField, | ||
emitLongQuery | ||
) | ||
// "{ $group : { _id : null, totalCount : { $top : { sortBy: { year: -1 }, "title" } } | ||
emitObjectValueEnd(long = emitLongQuery) | ||
} | ||
else -> continue | ||
} | ||
} | ||
return this | ||
} | ||
|
||
/** | ||
* Emits for the following accumulators | ||
* sum, avg, first, last, max, min, push, addToSet | ||
*/ | ||
private fun <S>MongoshBackend.emitKeyValueAccumulator( | ||
accumulator: Named, | ||
accumulatedField: Node<S>, | ||
emitLongQuery: Boolean, | ||
): MongoshBackend { | ||
val fieldRef = accumulatedField.component<HasFieldReference<S>>() ?: return this | ||
val valueRef = accumulatedField.component<HasValueReference<S>>() ?: return this | ||
|
||
// "{ $group : { _id : null, totalCount : | ||
emitObjectKey( | ||
resolveFieldReference( | ||
fieldRef = fieldRef, | ||
fieldUsedAsValue = false, | ||
) | ||
) | ||
// "{ $group : { _id : null, totalCount : { | ||
emitObjectStart(long = emitLongQuery) | ||
// "{ $group : { _id : null, totalCount : { $sum : | ||
emitObjectKey(registerConstant('$' + accumulator.name.canonical)) | ||
// "{ $group : { _id : null, totalCount : { $sum : 1 | ||
emitContextValue(resolveValueReference(valueRef, fieldRef)) | ||
// "{ $group : { _id : null, totalCount : { $sum : 1 } | ||
emitObjectEnd(long = emitLongQuery) | ||
return this | ||
} | ||
|
||
private fun <S>MongoshBackend.emitTopBottomAccumulator( | ||
accumulator: Named, | ||
accumulatedField: Node<S>, | ||
emitLongQuery: Boolean, | ||
): MongoshBackend { | ||
val fieldRef = accumulatedField.component<HasFieldReference<S>>() ?: return this | ||
val valueRef = accumulatedField.component<HasValueReference<S>>() ?: return this | ||
val sorts = accumulatedField.component<HasSorts<S>>()?.children ?: emptyList() | ||
val limit = accumulatedField.component<HasLimit>()?.limit | ||
emitObjectKey( | ||
resolveFieldReference( | ||
fieldRef = fieldRef, | ||
fieldUsedAsValue = false, | ||
) | ||
) | ||
// "{" | ||
emitObjectStart(long = emitLongQuery) | ||
// "{ $top : " | ||
emitObjectKey(registerConstant('$' + accumulator.name.canonical)) | ||
// "{ $top : {" | ||
emitObjectStart(long = emitLongQuery) | ||
// "{ $top : { "sortBy" : " | ||
emitObjectKey(registerConstant("sortBy")) | ||
// "{ $top : { "sortBy" : { " | ||
emitObjectStart(long = emitLongQuery) | ||
// "{ $top : { "sortBy" : { "field" : 1," | ||
emitAsFieldValueDocument(sorts, emitLongQuery) | ||
// "{ $top : { "sortBy" : { "field" : 1, }" | ||
emitObjectEnd(long = emitLongQuery) | ||
// "{ $top : { "sortBy" : { "field" : 1, }, " | ||
emitObjectValueEnd(long = emitLongQuery) | ||
|
||
// "{ $top : { "sortBy" : { "field" : 1, }, output : " | ||
emitObjectKey(registerConstant("output")) | ||
// "{ $top : { "sortBy" : { "field" : 1, }, output : "$someField"" | ||
emitContextValue(resolveValueReference(valueRef, fieldRef)) | ||
// "{ $top : { "sortBy" : { "field" : 1, }, output : "$someField", " | ||
emitObjectValueEnd(long = emitLongQuery) | ||
|
||
if (limit != null) { | ||
// "{ $top : { "sortBy" : { "field" : 1, }, output : "$someField", n : " | ||
emitObjectKey(registerConstant("n")) | ||
// "{ $top : { "sortBy" : { "field" : 1, }, output : "$someField", n : 3" | ||
emitContextValue(registerConstant(limit)) | ||
// "{ $top : { "sortBy" : { "field" : 1, }, output : "$someField", n : 3, " | ||
emitObjectValueEnd(long = emitLongQuery) | ||
} | ||
|
||
// "{ $top : { "sortBy" : { "field" : 1, }, output : "$someField", } " or | ||
// "{ $top : { "sortBy" : { "field" : 1, }, output : "$someField", n : 3, }" | ||
emitObjectEnd(long = emitLongQuery) | ||
|
||
// "{ $top : { "sortBy" : { "field" : 1, }, output : "$someField", } }" or | ||
// "{ $top : { "sortBy" : { "field" : 1, }, output : "$someField", n : 3, } }" | ||
emitObjectEnd(long = emitLongQuery) | ||
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
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
Oops, something went wrong.
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.
@kmruiz I adopted this approach to make MongoshBackend understand when to prefix the field name with a
$
and when not to. The idea is if a field reference is used in the value place then it is supposed to be true and the emitted field will be prefixed with$
otherwise not.