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

feat(expr): support int8send and int8recv #13077

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions e2e_test/batch/functions/pgwire_send_recv.slt.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
query TT
select int8send(2147483647);
----
\x000000007fffffff

query I
select int8recv(int8send(76));
----
76

query I
select int8recv(' a'::bytea);
----
2314885530818453601

statement error could not convert slice to array
select int8recv('a'::bytea);

query I
select int8recv(decode(substr(md5(''), 3, 16), 'hex'));
----
2129315932054619369

query I
select int8recv(substr(decode(md5(''), 'hex'), 2, 8));
----
2129315932054619369

query I
select int8recv(substr(sha256(''), 2, 8));
----
-5709365202766785382

statement error decode
select int8recv(to_hex(2129315932054619369));

query I
select int8recv(decode(to_hex(2129315932054619369), 'hex'));
----
2129315932054619369
2 changes: 2 additions & 0 deletions proto/expr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ message ExprNode {
LEFT = 317;
RIGHT = 318;
FORMAT = 319;
PGWIRE_SEND = 320;
PGWIRE_RECV = 321;

// Unary operators
NEG = 401;
Expand Down
14 changes: 14 additions & 0 deletions src/expr/impl/src/scalar/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ where
.map_err(|err: <T as FromStr>::Err| ExprError::Parse(err.to_string().into()))
}

// TODO: introduce `FromBinary` and support all types
#[function("pgwire_recv(bytea) -> int8")]
pub fn pgwire_recv(elem: &[u8]) -> Result<i64> {
let fixed_length =
<[u8; 8]>::try_from(elem).map_err(|e| ExprError::Parse(e.to_string().into()))?;
Ok(i64::from_be_bytes(fixed_length))
}

#[function("cast(int2) -> int256")]
#[function("cast(int4) -> int256")]
#[function("cast(int8) -> int256")]
Expand Down Expand Up @@ -156,6 +164,12 @@ pub fn general_to_text(elem: impl ToText, mut writer: &mut impl Write) {
elem.write(&mut writer).unwrap();
}

// TODO: use `ToBinary` and support all types
#[function("pgwire_send(int8) -> bytea")]
fn pgwire_send(elem: i64) -> Box<[u8]> {
elem.to_be_bytes().into()
}

#[function("cast(boolean) -> varchar")]
pub fn bool_to_varchar(input: bool, writer: &mut impl Write) {
writer
Expand Down
13 changes: 13 additions & 0 deletions src/frontend/src/binder/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,19 @@ impl Binder {
("sha512", raw_call(ExprType::Sha512)),
("left", raw_call(ExprType::Left)),
("right", raw_call(ExprType::Right)),
("int8send", raw_call(ExprType::PgwireSend)),
("int8recv", guard_by_len(1, raw(|_binder, mut inputs| {
// Similar to `cast` from string, return type is set explicitly rather than inferred.
let hint = if !inputs[0].is_untyped() && inputs[0].return_type() == DataType::Varchar {
" Consider `decode` or cast."
} else {
""
};
inputs[0].cast_implicit_mut(DataType::Bytea).map_err(|e| {
ErrorCode::BindError(format!("{e} in `recv`.{hint}"))
})?;
Ok(FunctionCall::new_unchecked(ExprType::PgwireRecv, inputs, DataType::Int64).into())
}))),
// array
("array_cat", raw_call(ExprType::ArrayCat)),
("array_append", raw_call(ExprType::ArrayAppend)),
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/expr/pure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ impl ExprVisitor for ImpureAnalyzer {
| expr_node::Type::ArrayPositions
| expr_node::Type::StringToArray
| expr_node::Type::Format
| expr_node::Type::PgwireSend
| expr_node::Type::PgwireRecv
| expr_node::Type::ArrayTransform
| expr_node::Type::Greatest
| expr_node::Type::Least =>
Expand Down
5 changes: 5 additions & 0 deletions src/tests/sqlsmith/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ fn is_numeric_out_of_range_err(db_error: &str) -> bool {
|| db_error.contains("Casting to u32 out of range")
}

fn is_parse_err(db_error: &str) -> bool {
db_error.contains("Parse error")
}

/// Skip queries with unimplemented features
fn is_unimplemented_error(db_error: &str) -> bool {
db_error.contains("not yet implemented")
Expand Down Expand Up @@ -106,6 +110,7 @@ pub fn is_neg_exp_error(db_error: &str) -> bool {
pub fn is_permissible_error(db_error: &str) -> bool {
is_numeric_out_of_range_err(db_error)
|| is_zero_err(db_error)
|| is_parse_err(db_error)
|| is_unimplemented_error(db_error)
|| not_unique_error(db_error)
|| is_window_error(db_error)
Expand Down
Loading