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

Fix bit string literals over SQL adapter #8112

Merged
merged 1 commit into from
Dec 12, 2024
Merged

Fix bit string literals over SQL adapter #8112

merged 1 commit into from
Dec 12, 2024

Conversation

aljazerzen
Copy link
Contributor

@aljazerzen aljazerzen commented Dec 12, 2024

So, bytea and bitstring/bit varying are not the same thing in pg. In SQL, they are both represented by string literals (of type unknown). The difference is that bitstring can have sub-byte length.

Initially, I represented BitStringConstant with bytes + bitlength. It turned out that Python has no easy way to convert 10101010001 to bytes, so I replaced that with plain str, as it was passed from parser.

We do still need ByteaConstant, because in EdgeQL's bytes literal should land in bytea pg type, not bitstring.


Here is the now unused code for codegen of the old BitStringConstants:

def quote_bitstring_literal(data: bytes, length: Optional[int] = None) -> str:
    """Return valid SQL representation of a bit string value."""

    if data:
        
        b = binascii.b2a_hex(data).decode('ascii')
        if length:
            b = b[0:((length + 3) // 4)]
            if length % 4 != 0:
                return f"x'{b}'::bit({length})"
        return f"x'{b}'"
    else:
        return "x''"

@aljazerzen aljazerzen requested a review from msullivan December 12, 2024 12:35
@msullivan msullivan merged commit 94f6396 into master Dec 12, 2024
24 checks passed
@msullivan msullivan deleted the sql-bit-string branch December 12, 2024 23:09
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