Skip to content

Conversation

prestonvasquez
Copy link
Contributor

@prestonvasquez prestonvasquez commented Apr 13, 2022

Description

GODRIVER-2268

This adds the Go language as an output for the language transpiler, enabling exports for find and aggregation pipelines.

For the dbx-go team, to test on compass locally you can read the instructions for installing here:

  1. Install Node ^14.17.5 brew install node@^14.17.5
  2. Install npm 7 npm install -g npm7
  3. Clone/Fork the compas repository,
  4. check out this branch git checkout prestonvasquez:GODRIVER-2268.compassGoExport
  5. npm run bootstrap
  6. npm run start

Checklist

  • New tests and/or benchmarks are included
  • Documentation is changed or added

Motivation and Context

  • Bugfix
  • New feature
  • Dependency update
  • Misc

Open Questions

There are a few open questions from the dbx-go team:

Q1. Is it possible to do the string parsing within the transpiler code?. This is in reference to how data is transpiled when trying to convert a string to a long type. In its current state the code will generate a function to handle this:

Screen Shot 2022-04-13 at 11 41 22 AM

But perhaps it would be better to just cast the literal directly:

bson.D{{ "x": int64("1") }}

Two considerations for casting directly:

  1. This code will fail at compile time
  2. Including the function adds consistency across other drivers as well as other symbols transpilers within the go driver.

Q2. Are there any cases where the query might contain NumberLong(variable)? This is a follow-up to Q1.

Q3. Also, is it possible to just print an error message, like "unsupported conversion"? This is also a follow-up to Q1. If we are not wanting to include the function in the raw output, should we just not support any raw output that require declarations?

Q4. If we decide to use declarations, do we want them to be functions or variables?

Dependents

This ticket depends on #2964 , which adds the capability of prepending statements with declarations.

Types of changes

  • Backport Needed
  • Patch (non-breaking change which fixes an issue)
  • Minor (non-breaking change which adds functionality)
  • Major (fix or feature that would cause existing functionality to change)

prestonvasquez and others added 30 commits April 4, 2022 15:05
…f github.com:prestonvasquez/compass into COMPASS-5685.addVariableDeclarationsToBSONTranspiler
Co-authored-by: Anna Henningsen <github@addaleax.net>
Co-authored-by: Anna Henningsen <github@addaleax.net>
…f github.com:prestonvasquez/compass into COMPASS-5685.addVariableDeclarationsToBSONTranspiler
…f github.com:prestonvasquez/compass into COMPASS-5685.addVariableDeclarationsToBSONTranspiler
prestonvasquez and others added 6 commits April 15, 2022 09:11
Co-authored-by: Benjamin Rewis <32186188+benjirewis@users.noreply.github.com>
Co-authored-by: Benjamin Rewis <32186188+benjirewis@users.noreply.github.com>
Co-authored-by: Benjamin Rewis <32186188+benjirewis@users.noreply.github.com>
…quez/compass into GODRIVER-2268.compassGoExport
if (literal === '')
return 'bson.A{}';
return `bson.A{${literal}${closingIndent}}`;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed the generated code for aggregation pipelines uses a top-level bson.A instead of a mongo.Pipeline. Most of the Go driver documentation shows using a mongo.Pipeline for Aggregate() if all of the pipeline stages are bson.D values (see an example here).

Screen Shot 2022-04-15 at 10 50 53 AM

Is it possible to use a mongo.Pipeline instead of a bson.A for the top-level pipeline value in Aggregate() calls?

E.g.

mongo.Pipeline{
    bson.D{{"$sort", bson.D{{"x", 1}}}},
}

Copy link
Contributor Author

@prestonvasquez prestonvasquez Apr 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's almost certainly possible, but at the moment there is no wrapper specifically for aggregations, it just treats [] as whatever our transpiler defines for the ArrayTypeTemplate. Looking at the go-driver aggregation code it seems that bson.A will work in a pinch, but I've created COMPASS-5742 to investigate adding a method to propagate aggregation/filter metadata to the compile methods to create a switch condition to help with this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to defer to COMPASS-5742.

Copy link
Contributor

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Go code looks good to me.

In some examples you ignore the result of a Find or Aggregate operation (I'm assuming because it introduces an unused variable), but it should be pretty obvious to users that they should replace _ with their own variable and continue logic with that variable.

Copy link

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a few bugs, but everything else looks good!

Copy link

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks awesome! 👍

#############################################

DriverTemplate: &DriverTemplate !!js/function >
(spec) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to wrap all of this in a main() function so that it can be copied and pasted to run without needing to add overhead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other languages don't seem to do that, cc: @mmarcon

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcasimir and I chatted about this. The other languages don't do this but they probably should. A good UX compromise could be to wrap the code inside a function RunAggregation or something like that which then a user can call from their main(). Maybe let's do that as a follow-up PR.

@mcasimir mcasimir merged commit dd6f188 into mongodb-js:main May 16, 2022
@prestonvasquez prestonvasquez deleted the GODRIVER-2268.compassGoExport branch June 11, 2022 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants