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] Add functions for binary strings #1264

Merged
merged 3 commits into from
Jan 17, 2024
Merged

[SQL] Add functions for binary strings #1264

merged 3 commits into from
Jan 17, 2024

Conversation

abhizer
Copy link
Contributor

@abhizer abhizer commented Jan 16, 2024

  • substring, overlay, octet_length, position

Is this a user-visible change (yes/no): yes (updates the docs)

#1262

* substring, overlay, octet_length, position

Signed-off-by: Abhinav Gyawali <22275402+abhizer@users.noreply.github.com>
@abhizer abhizer added documentation Improvements or additions to documentation SQL compiler Related to the SQL compiler labels Jan 16, 2024
docs/sql/binary.md Outdated Show resolved Hide resolved
</tr>
<tr>
<td><code>SUBSTRING</code>(binary FROM integer)</td>
<td>Generate a substring of binary starting at the given point. If the start position integer is negative, the substring starts at the position 0</td>
Copy link
Collaborator

Choose a reason for hiding this comment

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

point = offset in bytes.
Are you sure about 0? Usually negative means that it's interpreted from the end.
Moverover, SQL offsets usually start at 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Postgres doesn't allow indexing with negative numbers, so that's what we do here.
And yeah, it should be 1 instead of 0 in terms of SQL.

</tr>
<tr>
<td><code>SUBSTRING</code>(binary FROM integer1 FOR integer2)</td>
<td>Generate a substring of binary starting at the given point with a given length. If the start position integer is negative, the substring starts at the position 0</td>
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment

</tr>
<tr>
<td><code>OVERLAY</code>(binary1 PLACING binary2 FROM integer [ FOR integer2 ])</td>
<td>Generate a binary string that replaces substring of binary1 with binary2. If the start position integer is negative, the new string will contain binary2 in position 0</td>
Copy link
Collaborator

Choose a reason for hiding this comment

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

what if the FOR size is smaller than the binary2 size?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If FOR size is smaller than binary2 size, it still gets replaced by the entire binary2.

}

@Test
public void testOctetLength() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

are there postgres tests with these functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we steal these too?
They can be part of a separate PR.

public void testSubstring() {
this.qs(
"""
SELECT substring(x'123456', 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suspect that substring(..., 1) will generate the same result.
Then it means that values below 1 are treated as 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

sql-to-dbsp-compiler/lib/sqllib/src/binary.rs Outdated Show resolved Hide resolved
Signed-off-by: Abhinav Gyawali <22275402+abhizer@users.noreply.github.com>
Signed-off-by: Mihai Budiu <mbudiu@gmail.com>
@mihaibudiu mihaibudiu merged commit 0336134 into main Jan 17, 2024
4 of 5 checks passed
@mihaibudiu mihaibudiu deleted the binary_overlay branch January 17, 2024 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation SQL compiler Related to the SQL compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants