Skip to content
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

sql: support preferred binary operators #70066

Merged

Conversation

nvanbenschoten
Copy link
Member

Needed for #69010.

This commit adds support for "preferred" overloads of binary operators. It then uses this new support to prefer the (jsonb, text) overload over the (jsonb, int) overload of the -> (JSONFetchVal) and ->> (JSONFetchText) operators. This allows the type system to properly type (according to Postgres) the following statements:

PREPARE x AS SELECT '{"field": 12}'->$1

PREPARE x AS SELECT '{"field": 12}'->>$1

Before this commit, each would return:

ERROR: unsupported binary operator: <jsonb> -> <jsonb>

This commit is helpful for PostgREST support. While this isn't a hard blocker, it is for use of JSON/JSONB data types with PostgREST. With this commit, and including the rest of the prototype in #69010, we can now handle requests like:

➜ curl -s 'http://localhost:3000/vehicles?status=eq.in_use&select=city,ext->brand&ext->>color=eq.blue' | jq
[
  {
    "brand": null,
    "city": "boston"
  },
  {
    "brand": "Pinarello",
    "city": "rome"
  },
  {
    "brand": null,
    "city": "san francisco"
  }
]

One of the more subtle parts of this change is the adjustment in typeCheckOverloadedExprs to consider overload preference before argument homogeneity. This was necessary to avoid type checking getting tricked into wanting a (jsonb, jsonb) overload (see above). I confirmed that no other overloaded function that has a PreferredOverload has more than one argument, so these two heuristic were operating independently to this point. This means that this change shouldn't be able to cause a regression in type checking. I think the change also makes sense, as an explicit preference should overrule what is effectively a guess.

Release note (sql change): Placeholder values can now be used as the right-hand operand of the JSONFetchVal (->) and JSONFetchText (->>) operators without ambiguity. This argument will be given the text type and the "object field lookup" variant of the operator will be used.

Release justification: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

Needed for cockroachdb#69010.

This commit adds support for "preferred" overloads of binary operators. It then
uses this new support to prefer the `(jsonb, text)` overload over the `(jsonb,
int)` overload of the `->` (JSONFetchVal) and `->>` (JSONFetchText) operators.
This allows the type system to properly type (according to Postgres) the
following statements:
```sql
PREPARE x AS SELECT '{"field": 12}'->$1

PREPARE x AS SELECT '{"field": 12}'->>$1
```

Before this commit, each would return:
```sql
ERROR: unsupported binary operator: <jsonb> -> <jsonb>
```

This commit is helpful for PostgREST support. While this isn't a hard blocker,
it is for use of JSON/JSONB data types with PostgREST. With this commit, and
including the rest of the prototype in cockroachdb#69010, we can now handle requests like:
```
➜ curl -s 'http://localhost:3000/vehicles?status=eq.in_use&select=city,ext->brand&ext->>color=eq.blue' | jq
[
  {
    "brand": null,
    "city": "boston"
  },
  {
    "brand": "Pinarello",
    "city": "rome"
  },
  {
    "brand": null,
    "city": "san francisco"
  }
]
```

One of the more subtle parts of this change is the adjustment in
`typeCheckOverloadedExprs` to consider overload preference before argument
homogeneity. This was necessary to avoid type checking getting tricked into
wanting a `(jsonb, jsonb)` overload (see above). I confirmed that no other
overloaded function that has a `PreferredOverload` has more than one argument,
so these two heuristic were operating independently to this point. This means
that this change shouldn't be able to cause a regression in type checking. I
think the change also makes sense, as an explicit preference should overrule
what is effectively a guess.

Release note (sql change): Placeholder values can now be used as the right-hand
operand of the JSONFetchVal (->) and JSONFetchText (->>) operators without
ambiguity. This argument will be given the text type and the "object field
lookup" variant of the operator will be used.

Release justification: None
Copy link
Contributor

@otan otan left a comment

Choose a reason for hiding this comment

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

one day we'll match postgres, one day

@nvanbenschoten
Copy link
Member Author

TFTR!

bors r=otan

@craig
Copy link
Contributor

craig bot commented Sep 13, 2021

Build succeeded:

@craig craig bot merged commit 71d0b36 into cockroachdb:master Sep 13, 2021
@nvanbenschoten nvanbenschoten deleted the nvanbenschoten/binOpPreferred branch September 14, 2021 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants