这是indexloc提供的服务,不要输入任何密码
Skip to content

handle column references that involve variables #74

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

Merged
merged 11 commits into from
Jun 12, 2024

Conversation

hallettj
Copy link
Collaborator

@hallettj hallettj commented Jun 10, 2024

To support root column references we need to handle column references that involve variables. Currently we're using the built-in variable $$ROOT to reference the root collection (which is wrong, but we'll fix that in another PR, and the correct solution will also require a variable.) Before this PR our column references don't work with variable references like $$ROOT due to the two different kinds of expressions that are found in MongoDB aggregation pipelines. Specifically there are,

  • match queries, where the reference is a key in the document used in a $match aggregation stage
  • aggregation expressions which appear in a number of contexts

The code we had would create a match stage like this:

{
  "$match": {
    "$$ROOT.field": { "$eq": 1 }
  }
}

That doesn't work because "$$ROOT.field" is in a match query context which does not allow using $ to reference field names or variables. But there is a match query operator, $expr, that switches to an aggregation expression context. So the correct solution looks like this:

{
  "$match": {
    "$expr": {
      "$eq": ["$$ROOT.field", 1]
    }
  }
}

This PR updates the code for producing $match stages to use switch to aggregation expression context to handle cases like this. Specifically I introduced an enum, ColumnRef, which signals cases where the reference needs to be in an aggregation expression context.

Since we're now supporting both expression contexts it's now possible to handle binary and unary comparisons on field names that contain $ or . without erroring out. This PR does that by replacing uses of safe_name (which can throw errors) in $match stage building with ColumnRef::from_comparison_target (which does not throw errors).

Supporting aggregation expression contexts was also a blocker for column-to-column binary comparisons. So I added that support to this PR. But those comparisons don't support relationship paths for the time being.

MDB-154

@hallettj hallettj self-assigned this Jun 10, 2024
Copy link
Contributor

@dmoverton dmoverton left a comment

Choose a reason for hiding this comment

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

Looks good

@hallettj hallettj merged commit 3b58fb8 into main Jun 12, 2024
@hallettj hallettj deleted the jesse/variables-in-column-refs branch June 12, 2024 02:37
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.

2 participants