-
Notifications
You must be signed in to change notification settings - Fork 117
Add operator documentation (BETWEEN, LIKE, IN, IS DISTINCT FROM) #3762
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
Open
arnaud-lacurie
wants to merge
8
commits into
FoundationDB:main
Choose a base branch
from
arnaud-lacurie:docs/operators-documentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8b6e516
Add BETWEEN, LIKE, IN, and IS DISTINCT FROM operator documentation
arnaud-lacurie 4b6956c
Add name index to operators documentation tests
arnaud-lacurie 7d9755a
Polish documentation
arnaud-lacurie dbaeca7
Split tests and add doc for is distinct
arnaud-lacurie 98f38c5
Add tests
arnaud-lacurie 7be4e62
Address PR feedback
arnaud-lacurie 8e2c330
Merge branch 'main' into docs/operators-documentation
arnaud-lacurie 7825070
Delete yaml-tests/src/test/resources/documentation-queries/operators-…
arnaud-lacurie 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 hidden or 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
8 changes: 8 additions & 0 deletions
8
docs/sphinx/source/reference/sql_commands/DQL/Operators/BETWEEN.diagram
This file contains hidden or 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,8 @@ | ||
| Diagram( | ||
| NonTerminal('expression'), | ||
| Optional(Terminal('NOT'), 1), | ||
| Terminal('BETWEEN'), | ||
| NonTerminal('lower_bound'), | ||
| Terminal('AND'), | ||
| NonTerminal('upper_bound') | ||
| ) |
225 changes: 225 additions & 0 deletions
225
docs/sphinx/source/reference/sql_commands/DQL/Operators/BETWEEN.rst
This file contains hidden or 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,225 @@ | ||
| ======= | ||
| BETWEEN | ||
| ======= | ||
|
|
||
| .. _between: | ||
|
|
||
| Tests whether a value falls within a specified range (inclusive). | ||
|
|
||
| Syntax | ||
| ====== | ||
|
|
||
| .. raw:: html | ||
| :file: BETWEEN.diagram.svg | ||
|
|
||
| The BETWEEN operator is used in WHERE clauses: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| SELECT column1, column2 | ||
| FROM table_name | ||
| WHERE column1 BETWEEN lower_value AND upper_value | ||
|
|
||
| Parameters | ||
| ========== | ||
|
|
||
| ``expression`` | ||
| The value to test. Can be a column name, calculation, or any valid expression. | ||
|
|
||
| ``lower_bound`` | ||
| The lower bound of the range (inclusive). | ||
|
|
||
| ``upper_bound`` | ||
| The upper bound of the range (inclusive). | ||
|
|
||
| ``NOT`` (optional) | ||
| Negates the result - returns true if the value is **outside** the range. | ||
|
|
||
| Returns | ||
| ======= | ||
|
|
||
| Returns: | ||
| - ``TRUE`` if ``expression >= lower_bound AND expression <= upper_bound`` | ||
| - ``FALSE`` otherwise | ||
| - ``NULL`` if any operand is NULL | ||
|
|
||
| With ``NOT BETWEEN``: | ||
| - ``TRUE`` if ``expression < lower_bound OR expression > upper_bound`` | ||
| - ``FALSE`` otherwise | ||
| - ``NULL`` if any operand is NULL | ||
|
|
||
| **Important**: If ``lower_bound > upper_bound``, the range is empty and ``BETWEEN`` always returns ``FALSE``. | ||
|
|
||
| Examples | ||
| ======== | ||
|
|
||
| Setup | ||
| ----- | ||
|
|
||
| For these examples, assume we have a ``products`` table: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| CREATE TABLE products( | ||
| id BIGINT, | ||
| name STRING, | ||
| price BIGINT, | ||
| stock INTEGER, | ||
| PRIMARY KEY(id)) | ||
|
|
||
| INSERT INTO products VALUES | ||
| (1, 'Widget A', 100, 50), | ||
| (2, 'Widget B', 150, 30), | ||
| (3, 'Gadget X', 200, 20), | ||
| (4, 'Tool A', 80, 100), | ||
| (5, 'Tool B', 120, 15) | ||
|
|
||
| BETWEEN with Numbers | ||
| -------------------- | ||
|
|
||
| Find products with prices between 100 and 150 (inclusive): | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| SELECT name, price | ||
| FROM products | ||
| WHERE price BETWEEN 100 AND 150 | ||
|
|
||
| .. list-table:: | ||
| :header-rows: 1 | ||
|
|
||
| * - :sql:`name` | ||
| - :sql:`price` | ||
| * - :json:`"Widget A"` | ||
| - :json:`100` | ||
| * - :json:`"Widget B"` | ||
| - :json:`150` | ||
| * - :json:`"Tool B"` | ||
| - :json:`120` | ||
|
|
||
| NOT BETWEEN | ||
| ----------- | ||
|
|
||
| Find products with prices outside the range 100-150: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| SELECT name, price | ||
| FROM products | ||
| WHERE price NOT BETWEEN 100 AND 150 | ||
|
|
||
| .. list-table:: | ||
| :header-rows: 1 | ||
|
|
||
| * - :sql:`name` | ||
| - :sql:`price` | ||
| * - :json:`"Tool A"` | ||
| - :json:`80` | ||
| * - :json:`"Gadget X"` | ||
| - :json:`200` | ||
|
|
||
| BETWEEN with Equal Bounds | ||
| -------------------------- | ||
|
|
||
| Test for exact value using BETWEEN: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| SELECT name, price | ||
| FROM products | ||
| WHERE price BETWEEN 100 AND 100 | ||
|
|
||
| .. list-table:: | ||
| :header-rows: 1 | ||
|
|
||
| * - :sql:`name` | ||
| - :sql:`price` | ||
| * - :json:`"Widget A"` | ||
| - :json:`100` | ||
|
|
||
| This is equivalent to ``WHERE price = 100``. | ||
|
|
||
| Empty Range | ||
| ----------- | ||
|
|
||
| If lower bound > upper bound, no rows match: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| SELECT name, price | ||
| FROM products | ||
| WHERE price BETWEEN 150 AND 100 | ||
|
|
||
| Returns empty result set (no rows). | ||
|
|
||
| Combined with OR | ||
| ---------------- | ||
|
|
||
| Use multiple BETWEEN clauses with OR: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| SELECT name, price | ||
| FROM products | ||
| WHERE price BETWEEN 80 AND 100 OR price BETWEEN 180 AND 220 | ||
|
|
||
| .. list-table:: | ||
| :header-rows: 1 | ||
|
|
||
| * - :sql:`name` | ||
| - :sql:`price` | ||
| * - :json:`"Tool A"` | ||
| - :json:`80` | ||
| * - :json:`"Widget A"` | ||
| - :json:`100` | ||
| * - :json:`"Gadget X"` | ||
| - :json:`200` | ||
|
|
||
| Important Notes | ||
| =============== | ||
|
|
||
| Inclusive Range | ||
| --------------- | ||
|
|
||
| ``BETWEEN`` uses **inclusive** bounds. Both ``lower_bound`` and ``upper_bound`` are included in the matching range. | ||
|
|
||
| NULL Handling | ||
| ------------- | ||
|
|
||
| If any operand (expression, lower_bound, or upper_bound) is NULL, the result is NULL: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| -- Returns NULL | ||
| WHERE NULL BETWEEN 1 AND 10 | ||
|
|
||
| -- Returns NULL | ||
| WHERE price BETWEEN NULL AND 100 | ||
|
|
||
| -- Returns NULL | ||
| WHERE price BETWEEN 100 AND NULL | ||
|
|
||
| Equivalence | ||
| ----------- | ||
|
|
||
| ``BETWEEN`` is shorthand for a range check: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| -- These are equivalent: | ||
| WHERE x BETWEEN a AND b | ||
| WHERE x >= a AND x <= b | ||
|
|
||
| -- These are equivalent: | ||
| WHERE x NOT BETWEEN a AND b | ||
| WHERE x < a OR x > b | ||
|
|
||
| Type Compatibility | ||
| ------------------ | ||
|
|
||
| The expression, lower_bound, and upper_bound must be of compatible types. The comparison follows SQL type coercion rules. | ||
|
|
||
| See Also | ||
| ======== | ||
|
|
||
| * :ref:`Comparison Operators <comparison-operators>` - Other comparison operations |
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
docs/sphinx/source/reference/sql_commands/DQL/Operators/IN.diagram
This file contains hidden or 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,11 @@ | ||
| Diagram( | ||
| NonTerminal('expression'), | ||
| Optional(Terminal('NOT'), 1), | ||
| Terminal('IN'), | ||
| Terminal('('), | ||
| OneOrMore( | ||
| NonTerminal('value'), | ||
| Terminal(',') | ||
| ), | ||
| Terminal(')') | ||
| ) |
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.
I see that the
IS DISTINCT FROMoperator is currently placed underComparison Operatorsas well as described in its own page. However, the latter is more elaborate. Is this intentional in the sense that there should be a small summary of the operator in theComparison Operatorspage?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.
This is an earlier attempt at documenting IS DISTINCT FROM that I forgot about when writing a proper page. I think this can go away, or link to the page.
Thanks for catching that!