Skip to content

Commit 916f4dc

Browse files
committed
chore: extract protocol error to a separate file
1 parent 9641cf3 commit 916f4dc

File tree

10 files changed

+195
-100
lines changed

10 files changed

+195
-100
lines changed

packages/cubejs-backend-native/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubenativeutils/Cargo.lock

Lines changed: 25 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubesql/cubesql/src/sql/dataframe.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ use datafusion::arrow::{
1111
datatypes::{DataType, IntervalUnit, Schema, TimeUnit},
1212
record_batch::RecordBatch,
1313
};
14-
// Type aliases for compatibility - actual implementations are in pg-srv
15-
pub type IntervalValue = pg_srv::IntervalValue;
16-
pub type TimestampValue = pg_srv::TimestampValue;
1714
use rust_decimal::prelude::*;
1815
use serde::{Serialize, Serializer};
1916
use std::fmt::Debug;
@@ -78,6 +75,10 @@ impl Row {
7875
}
7976
}
8077

78+
// Type aliases for compatibility - actual implementations are in pg-srv
79+
pub type IntervalValue = pg_srv::IntervalValue;
80+
pub type TimestampValue = pg_srv::TimestampValue;
81+
8182
#[derive(Debug)]
8283
pub enum TableValue {
8384
Null,

rust/cubesql/pg-srv/src/encoding.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,36 @@ mod tests {
163163
assert_text_encode(true, &[0, 0, 0, 1, 116]);
164164
assert_text_encode(false, &[0, 0, 0, 1, 102]);
165165
assert_text_encode("str".to_string(), &[0, 0, 0, 3, 115, 116, 114]);
166+
assert_text_encode(
167+
IntervalValue::new(0, 0, 0, 0, 0, 0),
168+
&[
169+
0, 0, 0, 46, 48, 32, 121, 101, 97, 114, 115, 32, 48, 32, 109, 111, 110, 115, 32,
170+
48, 32, 100, 97, 121, 115, 32, 48, 32, 104, 111, 117, 114, 115, 32, 48, 32, 109,
171+
105, 110, 115, 32, 48, 46, 48, 48, 32, 115, 101, 99, 115,
172+
],
173+
);
174+
assert_text_encode(
175+
IntervalValue::new(1, 2, 3, 4, 5, 6),
176+
&[
177+
0, 0, 0, 50, 48, 32, 121, 101, 97, 114, 115, 32, 49, 32, 109, 111, 110, 115, 32,
178+
50, 32, 100, 97, 121, 115, 32, 51, 32, 104, 111, 117, 114, 115, 32, 52, 32, 109,
179+
105, 110, 115, 32, 53, 46, 48, 48, 48, 48, 48, 54, 32, 115, 101, 99, 115,
180+
],
181+
);
182+
assert_text_encode(
183+
TimestampValue::new(0, None),
184+
&[
185+
0, 0, 0, 26, 49, 57, 55, 48, 45, 48, 49, 45, 48, 49, 32, 48, 48, 58, 48, 48, 58,
186+
48, 48, 46, 48, 48, 48, 48, 48, 48,
187+
],
188+
);
189+
assert_text_encode(
190+
TimestampValue::new(1650890322000000000, None),
191+
&[
192+
0, 0, 0, 26, 50, 48, 50, 50, 45, 48, 52, 45, 50, 53, 32, 49, 50, 58, 51, 56, 58,
193+
52, 50, 46, 48, 48, 48, 48, 48, 48,
194+
],
195+
);
166196

167197
Ok(())
168198
}
@@ -178,6 +208,24 @@ mod tests {
178208
fn test_binary_encoders() -> Result<(), ProtocolError> {
179209
assert_bind_encode(true, &[0, 0, 0, 1, 1]);
180210
assert_bind_encode(false, &[0, 0, 0, 1, 0]);
211+
assert_bind_encode(
212+
IntervalValue::new(0, 0, 0, 0, 0, 0),
213+
&[0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
214+
);
215+
assert_bind_encode(
216+
IntervalValue::new(1, 2, 3, 4, 5, 6),
217+
&[
218+
0, 0, 0, 16, 0, 0, 0, 2, 146, 85, 83, 70, 0, 0, 0, 2, 0, 0, 0, 1,
219+
],
220+
);
221+
assert_bind_encode(
222+
TimestampValue::new(0, None),
223+
&[0, 0, 0, 8, 255, 252, 162, 254, 196, 200, 32, 0],
224+
);
225+
assert_bind_encode(
226+
TimestampValue::new(1650890322000000000, None),
227+
&[0, 0, 0, 8, 0, 2, 128, 120, 159, 252, 216, 128],
228+
);
181229

182230
Ok(())
183231
}

rust/cubesql/pg-srv/src/error.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! Protocol error types for PostgreSQL wire protocol
2+
3+
use crate::protocol;
4+
use std::{backtrace::Backtrace, fmt::Formatter};
5+
6+
/// Protocol error abstract of handled/unhandled errors, it should not handle any kind of business logic errors
7+
/// TODO: Migrate back to thiserror crate, when Rust will stabilize feature(error_generic_member_access)
8+
#[derive(Debug)]
9+
pub enum ProtocolError {
10+
IO {
11+
source: std::io::Error,
12+
backtrace: Backtrace,
13+
},
14+
ErrorResponse {
15+
source: protocol::ErrorResponse,
16+
backtrace: Backtrace,
17+
},
18+
}
19+
20+
impl std::fmt::Display for ProtocolError {
21+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22+
match self {
23+
ProtocolError::IO { source, .. } => f.write_fmt(format_args!("IO error: {}", source)),
24+
ProtocolError::ErrorResponse { source, .. } => {
25+
f.write_fmt(format_args!("Error: {}", source.message))
26+
}
27+
}
28+
}
29+
}
30+
31+
impl From<std::io::Error> for ProtocolError {
32+
fn from(source: std::io::Error) -> Self {
33+
ProtocolError::IO {
34+
source,
35+
backtrace: Backtrace::capture(),
36+
}
37+
}
38+
}
39+
40+
impl From<protocol::ErrorResponse> for ProtocolError {
41+
fn from(source: protocol::ErrorResponse) -> Self {
42+
ProtocolError::ErrorResponse {
43+
source,
44+
backtrace: Backtrace::capture(),
45+
}
46+
}
47+
}
48+
49+
impl ProtocolError {
50+
/// Return Backtrace from any variant of Enum
51+
pub fn backtrace(&self) -> Option<&Backtrace> {
52+
match &self {
53+
ProtocolError::IO { backtrace, .. } => Some(backtrace),
54+
ProtocolError::ErrorResponse { backtrace, .. } => Some(backtrace),
55+
}
56+
}
57+
58+
/// Converts Error to protocol::ErrorResponse which is usefully for writing response to the client
59+
pub fn to_error_response(self) -> protocol::ErrorResponse {
60+
match self {
61+
ProtocolError::IO { source, .. } => protocol::ErrorResponse::error(
62+
protocol::ErrorCode::InternalError,
63+
source.to_string(),
64+
),
65+
ProtocolError::ErrorResponse { source, .. } => source,
66+
}
67+
}
68+
}

rust/cubesql/pg-srv/src/lib.rs

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,82 +6,16 @@ mod decoding;
66
mod encoding;
77

88
pub mod buffer;
9+
pub mod error;
910
pub mod extended;
10-
pub mod interval;
1111
pub mod pg_type;
1212
pub mod protocol;
13-
pub mod timestamp;
13+
pub mod values;
1414

1515
pub use buffer::*;
1616
pub use decoding::*;
1717
pub use encoding::*;
18+
pub use error::*;
1819
pub use extended::*;
19-
pub use interval::*;
2020
pub use pg_type::*;
21-
pub use timestamp::*;
22-
23-
use std::{backtrace::Backtrace, fmt::Formatter};
24-
25-
/// Protocol error abstract of handled/unhandled errors, it should not handle any kind of business logic errors
26-
/// TODO: Migrate back to thiserror crate, when Rust will stabilize feature(error_generic_member_access)
27-
#[derive(Debug)]
28-
pub enum ProtocolError {
29-
IO {
30-
source: std::io::Error,
31-
backtrace: Backtrace,
32-
},
33-
ErrorResponse {
34-
source: protocol::ErrorResponse,
35-
backtrace: Backtrace,
36-
},
37-
}
38-
39-
impl std::fmt::Display for ProtocolError {
40-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41-
match self {
42-
ProtocolError::IO { source, .. } => f.write_fmt(format_args!("IO error: {}", source)),
43-
ProtocolError::ErrorResponse { source, .. } => {
44-
f.write_fmt(format_args!("Error: {}", source.message))
45-
}
46-
}
47-
}
48-
}
49-
50-
impl From<std::io::Error> for ProtocolError {
51-
fn from(source: std::io::Error) -> Self {
52-
ProtocolError::IO {
53-
source,
54-
backtrace: Backtrace::capture(),
55-
}
56-
}
57-
}
58-
59-
impl From<protocol::ErrorResponse> for ProtocolError {
60-
fn from(source: protocol::ErrorResponse) -> Self {
61-
ProtocolError::ErrorResponse {
62-
source,
63-
backtrace: Backtrace::capture(),
64-
}
65-
}
66-
}
67-
68-
impl ProtocolError {
69-
/// Return Backtrace from any variant of Enum
70-
pub fn backtrace(&self) -> Option<&Backtrace> {
71-
match &self {
72-
ProtocolError::IO { backtrace, .. } => Some(backtrace),
73-
ProtocolError::ErrorResponse { backtrace, .. } => Some(backtrace),
74-
}
75-
}
76-
77-
/// Converts Error to protocol::ErrorResponse which is usefully for writing response to the client
78-
pub fn to_error_response(self) -> protocol::ErrorResponse {
79-
match self {
80-
ProtocolError::IO { source, .. } => protocol::ErrorResponse::error(
81-
protocol::ErrorCode::InternalError,
82-
source.to_string(),
83-
),
84-
ProtocolError::ErrorResponse { source, .. } => source,
85-
}
86-
}
87-
}
21+
pub use values::*;
File renamed without changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! PostgreSQL value types for wire protocol
2+
3+
pub mod interval;
4+
#[cfg(feature = "with-chrono")]
5+
pub mod timestamp;
6+
7+
pub use interval::*;
8+
#[cfg(feature = "with-chrono")]
9+
pub use timestamp::*;

0 commit comments

Comments
 (0)