Skip to content

Commit

Permalink
fix(query): fix binary default string (#16345)
Browse files Browse the repository at this point in the history
* fix(query): fix binary default string

* fix(query): fix inf display

* fix(query): fix inf display

* fix(query): fix try cast

* fix(query): fix

* fix(query): fix
  • Loading branch information
sundy-li committed Aug 29, 2024
1 parent 9d706f4 commit a5c4129
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/query/expression/src/utils/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,30 @@ impl<Index: ColumnIndex> Expr<Index> {
#[recursive::recursive]
fn write_expr<Index: ColumnIndex>(expr: &Expr<Index>, min_precedence: usize) -> String {
match expr {
Expr::Constant { scalar, .. } => scalar.as_ref().to_string(),
Expr::Constant { scalar, .. } => match scalar {
s @ Scalar::Binary(_) => format!("from_hex('{s}')::string"),
Scalar::Number(NumberScalar::Float32(f)) if f.is_nan() => {
"'nan'::Float32".to_string()
}
Scalar::Number(NumberScalar::Float64(f)) if f.is_nan() => {
"'nan'::Float64".to_string()
}
Scalar::Number(NumberScalar::Float32(f)) if f.is_infinite() => {
if *f != f32::NEG_INFINITY {
"'inf'::Float32".to_string()
} else {
"'-inf'::Float32".to_string()
}
}
Scalar::Number(NumberScalar::Float64(f)) if f.is_infinite() => {
if *f != f64::NEG_INFINITY {
"'inf'::Float64".to_string()
} else {
"'-inf'::Float64".to_string()
}
}
other => other.as_ref().to_string(),
},
Expr::ColumnRef { display_name, .. } => display_name.clone(),
Expr::Cast {
is_try,
Expand Down
1 change: 1 addition & 0 deletions src/query/sql/src/planner/expression_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pub fn parse_default_expr_to_string(
} else {
(expr, false)
};

Ok((expr.sql_display(), is_deterministic))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,16 @@ SELECT id, v FROM t1 order by id
3 616161
4 616161

statement ok
create table t2(a int, b binary NOT NULL DEFAULT 'abc', c double default 'inf', e float default 'nan' );

statement ok
insert into t2 (a) values (3);

query ITTT
select a, b::string, c, e from t2;
----
3 abc Infinity NaN

statement ok
DROP DATABASE db_binary
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Error: APIError: ResponseError with 1006: invalid utf8 sequence while evaluating function `to_string(D00C)` in expr `to_string()`, during run expr: `CAST(D00C AS String NULL)`
Error: APIError: ResponseError with 1006: invalid utf8 sequence while evaluating function `to_string(D00C)` in expr `to_string()`, during run expr: `CAST(from_hex('D00C')::string AS String NULL)`
null

0 comments on commit a5c4129

Please sign in to comment.