Skip to content

Use PSQLFormat when encoding and decoding #158

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

Merged
merged 4 commits into from
Jul 22, 2021

Conversation

fabianfett
Copy link
Collaborator

@fabianfett fabianfett commented May 10, 2021

Postgres supports two encodings on the wire text (0) and binary (1). Up until now the new PSQLDecodable only supported binary encoding.

  • This PR does not change any public API
  • Rename PSQLFormatCode to PSQLFormat
  • Add PSQLFormat as parameter to the decode function.
  • Add psqlFormat: PSQLFormat as protocol requirement for PSQLEncodable
  • All existing types encode into and decode from .binary format only (we can change this with follow up PRs)

@fabianfett fabianfett changed the title Use Format when decoding Use PSQLFormat when encoding and decoding May 10, 2021
@fabianfett fabianfett requested review from gwynne and 0xTim May 10, 2021 17:02
Copy link
Member

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

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

LGTM

@fabianfett fabianfett force-pushed the ff-use-format-when-decoding branch from 5a18b1c to 8029bcd Compare May 12, 2021 19:54
Copy link
Contributor

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

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

Looks good, I left some questions inline.

Comment on lines +17 to 21
case (.binary, .float8):
guard buffer.readableBytes == 8, let double = buffer.readDouble() else {
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
}
return Float(double)
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we okay with the float-to-double conversion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The implementation I inherited did this, so I guess it's okay for now. Maybe change with the next major release?

Comment on lines +24 to +26
self.parameters.forEach {
buffer.writeInteger($0.psqlFormat.rawValue)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should reserve capacity in the buffer to minimise intermediate allocs?

Copy link
Contributor

Choose a reason for hiding this comment

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

Although we have to encode the actual parameters as well which make it harder to know how much capacity to reserve.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe in another pr at a later point?

Copy link
Contributor

Choose a reason for hiding this comment

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

Fine by me.

Comment on lines 7 to 8
/// In a `RowDescription` returned from the statement variant of `Describe`,
/// the format is not yet known and will always be `.text`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this, does it mean that we assume it to be .text until we know otherwise?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed the sentence in question. this is explained in ExtendedQueryStateMachine much better. And is really only relevant there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@fabianfett fabianfett force-pushed the ff-use-format-when-decoding branch 2 times, most recently from 6df8e99 to 82d57b3 Compare May 14, 2021 15:22
Comment on lines 22 to 33
/// Decode an entity from the `byteBuffer` in postgres binary format
///
/// - Parameters:
/// - byteBuffer: A `ByteBuffer` to decode. The byteBuffer is sliced in such a way that it is expected
/// that the complete buffer is consumed for decoding
/// - type: The postgres data type. Depending on this type the `byteBuffer`'s bytes need to be interpreted
/// in different ways.
/// - format: The postgres wire format. Can be `.text` or `.binary`
/// - context: A `PSQLDecodingContext` providing context for decoding. This includes a `JSONDecoder`
/// to use when decoding json and metadata to create better erros.
/// - Returns: A decoded object
static func decode(from byteBuffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@glbrntt Does that make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly! Aren't these are in conflict?

    /// Decode an entity from the `byteBuffer` in postgres binary format
    ///   - format: The postgres wire format. Can be `.text` or `.binary`

/// in different ways.
/// - format: The postgres wire format. Can be `.text` or `.binary`
/// - context: A `PSQLDecodingContext` providing context for decoding. This includes a `JSONDecoder`
/// to use when decoding json and metadata to create better erros.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// to use when decoding json and metadata to create better erros.
/// to use when decoding json and metadata to create better errors.

Comment on lines 22 to 33
/// Decode an entity from the `byteBuffer` in postgres binary format
///
/// - Parameters:
/// - byteBuffer: A `ByteBuffer` to decode. The byteBuffer is sliced in such a way that it is expected
/// that the complete buffer is consumed for decoding
/// - type: The postgres data type. Depending on this type the `byteBuffer`'s bytes need to be interpreted
/// in different ways.
/// - format: The postgres wire format. Can be `.text` or `.binary`
/// - context: A `PSQLDecodingContext` providing context for decoding. This includes a `JSONDecoder`
/// to use when decoding json and metadata to create better erros.
/// - Returns: A decoded object
static func decode(from byteBuffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self
Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly! Aren't these are in conflict?

    /// Decode an entity from the `byteBuffer` in postgres binary format
    ///   - format: The postgres wire format. Can be `.text` or `.binary`

@fabianfett fabianfett force-pushed the ff-use-format-when-decoding branch from 82d57b3 to 21fb161 Compare July 21, 2021 16:08
@fabianfett fabianfett merged commit e18b7f8 into vapor:main Jul 22, 2021
@fabianfett fabianfett deleted the ff-use-format-when-decoding branch July 22, 2021 09:28
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.

3 participants