The Wayback Machine - https://web.archive.org/web/20201201232748/https://github.com/h2database/h2database/pull/2670
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

`= ANY(array)` support #2670

Open
wants to merge 2 commits into
base: master
from
Open

Conversation

@auntyellow
Copy link
Contributor

@auntyellow auntyellow commented May 30, 2020

Mentioned in #2480 .

Convert value = ANY(array) toARRAY_CONTAINS(array, value), and this feature can allow some PostgreSQL clients to get PK/UK from pg_constraint, mentioned in #2450 .

The conversion is in optimize stage, but Select.setGroupQuery is in parse stage. If any ANY function detected, skip setGroupQuery. Then setGroupQuery for real aggregate ANY before execute query.

Copy link
Contributor

@katzyn katzyn left a comment

The SQL Standard has is a syntax conflict between quantified comparison predicates and aggregate functions. This conflict is resolved in H2 via requirement to add parentheses around these functions.

ANY(array) is not a part of the SQL Standard, I'm not sure that we want to have such feature at all. We can accept this feature, but only with sane and non-intrusive implementation.

First of all, you don't need to touch aggregate functions. They aren't related here, the mentioned syntax conflict is already resolved it H2 and there is nothing to change.

Take a look on ConditionIn, ConditionInQuery, ConditionInConstantSet, and ConditionInParameter. You need to create an additional condition. This condition should work like ConditionInQuery, with its compareType parameter, but shouldn't inherit the PredicateWithSubquery. It should extend Condition instead and use the passed expression with array.

The another possible approach is to use the ConditionInQuery() with UNNEST(array) as a query.

@auntyellow
Copy link
Contributor Author

@auntyellow auntyellow commented May 31, 2020

The ANY function here have different meanings, determined by its argument:

  • [A] a condition if arg is a subquery or a parameter;
  • [B] an aggregate if arg is a bool;
  • [C] comparison/condition if arg is an array.

[A] is clear in H2, but [B] and [C] is hard to distinct in parsing because column type is unknown. In most databases, however, ANY is never recognized as an aggregate. If [C] is only supported in PostgreSQL (I don't know whether Oracle and DB2 also support ANY(array)), things can be simpler: just skip [B] and convert [C] to ARRAY_CONTAINS or new Comparison/ConditionAnyArray.

I may close this and submit another pr.

@katzyn
Copy link
Contributor

@katzyn katzyn commented May 31, 2020

{<|>|<=|>=|=|<>} ANY(query) is a quantified comparison predicate.
{<|>|<=|>=|=|<>} ANY(non-query expression) is a simple comparison with aggregate function.
{<|>|<=|>=|=|<>} (ANY(anything, including subquery)) is a simple comparison with aggregate function.

if arg is a bool;

In H2 other data types may be used as boolean values.

All other constructions are PostgreSQL-specific. H2 supports only = ANY(?) for some limited compatibility with PostgreSQL, this specific construction is used often and was requested multiple times.

You can add {<|>|<=|>=|=|<>} ANY(array) too, but not in that way.

Actually this problem is really even more complicated than I thought initially, because we can't determine data type of expression early if it is not a constant expression. It looks like {<|>|<=|>=|=|<>} ANY(non-query expression) with non-constant expression should always be parsed not as required by the SQL Standard unconditionally, but I don't like “features” that work like a bugs. We need to design and discuss this feature first.

@katzyn katzyn mentioned this pull request May 31, 2020
@auntyellow auntyellow force-pushed the auntyellow:equals-any-array branch from 90b599b to 12988f8 Jun 6, 2020
@auntyellow auntyellow force-pushed the auntyellow:equals-any-array branch from 12988f8 to a9d208d Jul 11, 2020
@auntyellow auntyellow force-pushed the auntyellow:equals-any-array branch from a9d208d to 2f28164 Aug 17, 2020
auntyellow added 2 commits May 27, 2020
this can prevent HeidiSQL error on unique or composite keys
@auntyellow auntyellow force-pushed the auntyellow:equals-any-array branch from 2f28164 to e0a4e3e Sep 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.