Skip to content

Commit

Permalink
[Fix](Json) fix some cast issue (#38683)
Browse files Browse the repository at this point in the history
1. casting to boolean/double type should consider all numeric types
including int , double and boolean
2. casting to floating point type should consisder boolean types and int types
  • Loading branch information
eldenmoon authored and dataroaring committed Aug 11, 2024
1 parent 2e5493d commit b8f35a7
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 284 deletions.
27 changes: 25 additions & 2 deletions be/src/vec/functions/function_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -856,12 +856,19 @@ struct ConvertImplFromJsonb {
res[i] = 0;
continue;
}

if constexpr (type_index == TypeIndex::UInt8) {
// cast from json value to boolean type
if (value->isTrue()) {
res[i] = 1;
} else if (value->isFalse()) {
res[i] = 0;
} else if (value->isInt()) {
res[i] = ((const JsonbIntVal*)value)->val() == 0 ? 0 : 1;
} else if (value->isDouble()) {
res[i] = static_cast<ColumnType::value_type>(
((const JsonbDoubleVal*)value)->val()) == 0
? 0
: 1;
} else {
null_map[i] = 1;
res[i] = 0;
Expand All @@ -871,15 +878,31 @@ struct ConvertImplFromJsonb {
type_index == TypeIndex::Int32 ||
type_index == TypeIndex::Int64 ||
type_index == TypeIndex::Int128) {
// cast from json value to integer types
if (value->isInt()) {
res[i] = ((const JsonbIntVal*)value)->val();
} else if (value->isDouble()) {
res[i] = static_cast<ColumnType::value_type>(
((const JsonbDoubleVal*)value)->val());
} else if (value->isTrue()) {
res[i] = 1;
} else if (value->isFalse()) {
res[i] = 0;
} else {
null_map[i] = 1;
res[i] = 0;
}
} else if constexpr (type_index == TypeIndex::Float64) {
} else if constexpr (type_index == TypeIndex::Float64 ||
type_index == TypeIndex::Float32) {
// cast from json value to floating point types
if (value->isDouble()) {
res[i] = ((const JsonbDoubleVal*)value)->val();
} else if (value->isFloat()) {
res[i] = ((const JsonbFloatVal*)value)->val();
} else if (value->isTrue()) {
res[i] = 1;
} else if (value->isFalse()) {
res[i] = 0;
} else if (value->isInt()) {
res[i] = ((const JsonbIntVal*)value)->val();
} else {
Expand Down
37 changes: 35 additions & 2 deletions regression-test/data/datatype_p0/json/json_cast.out
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
true

-- !sql8 --
\N
1000

-- !sql9 --
1000.1111

-- !sql10 --
\N
true

-- !sql11 --
["CXO0N: 1045901740","HMkTa: 1348450505","44 HHD: 915015173","j9WoJ: -1517316688"]
Expand All @@ -53,3 +53,36 @@ true
-- !sql18 --
\N

-- !sql19 --
1

-- !sql19 --
0

-- !sql20 --
1.0

-- !sql20 --
0.0

-- !sql21 --
true

-- !sql22 --
1024.0

-- !sql23 --
1024.0

-- !sql24 --
1024.0

-- !sql24 --
[1,2,3]

-- !sql25 --
[1,2,3]

-- !sql26 --
["2020-01-01"]

76 changes: 38 additions & 38 deletions regression-test/data/json_p0/test_json_load_and_function.out
Original file line number Diff line number Diff line change
Expand Up @@ -5716,11 +5716,11 @@
2 null \N
3 true true
4 false false
5 100 \N
6 10000 \N
7 1000000000 \N
8 1152921504606846976 \N
9 6.18 \N
5 100 true
6 10000 true
7 1000000000 true
8 1152921504606846976 true
9 6.18 true
10 "abcd" \N
11 {} \N
12 {"k1":"v31","k2":300} \N
Expand All @@ -5733,23 +5733,23 @@
26 \N \N
27 {"k1":"v1","k2":200} \N
28 {"a.b.c":{"k1.a1":"v31","k2":300},"a":"niu"} \N
29 12524337771678448270 \N
30 -9223372036854775808 \N
31 18446744073709551615 \N
29 12524337771678448270 true
30 -9223372036854775808 true
31 18446744073709551615 true
32 {"":"v1"} \N
33 {"":1,"":"v1"} \N
34 {"":1,"ab":"v1","":"v1","":2} \N

-- !select --
1 \N \N
2 null \N
3 true \N
4 false \N
3 true 1
4 false 0
5 100 100
6 10000 10000
7 1000000000 -13824
8 1152921504606846976 0
9 6.18 \N
9 6.18 6
10 "abcd" \N
11 {} \N
12 {"k1":"v31","k2":300} \N
Expand All @@ -5772,13 +5772,13 @@
-- !select --
1 \N \N
2 null \N
3 true \N
4 false \N
3 true 1
4 false 0
5 100 100
6 10000 10000
7 1000000000 1000000000
8 1152921504606846976 0
9 6.18 \N
9 6.18 6
10 "abcd" \N
11 {} \N
12 {"k1":"v31","k2":300} \N
Expand All @@ -5801,13 +5801,13 @@
-- !select --
1 \N \N
2 null \N
3 true \N
4 false \N
3 true 1
4 false 0
5 100 100
6 10000 10000
7 1000000000 1000000000
8 1152921504606846976 1152921504606846976
9 6.18 \N
9 6.18 6
10 "abcd" \N
11 {} \N
12 {"k1":"v31","k2":300} \N
Expand All @@ -5830,8 +5830,8 @@
-- !select --
1 \N \N
2 null \N
3 true \N
4 false \N
3 true 1.0
4 false 0.0
5 100 100.0
6 10000 10000.0
7 1000000000 1.0E9
Expand Down Expand Up @@ -5890,11 +5890,11 @@
2 null \N
3 true true
4 false false
5 100 \N
6 10000 \N
7 1000000000 \N
8 1152921504606846976 \N
9 6.18 \N
5 100 true
6 10000 true
7 1000000000 true
8 1152921504606846976 true
9 6.18 true
10 "abcd" \N
11 {} \N
12 {"k1":"v31","k2":300} \N
Expand All @@ -5907,23 +5907,23 @@
26 \N \N
27 {"k1":"v1","k2":200} \N
28 {"a.b.c":{"k1.a1":"v31","k2":300},"a":"niu"} \N
29 12524337771678448270 \N
30 -9223372036854775808 \N
31 18446744073709551615 \N
29 12524337771678448270 true
30 -9223372036854775808 true
31 18446744073709551615 true
32 {"":"v1"} \N
33 {"":1,"":"v1"} \N
34 {"":1,"ab":"v1","":"v1","":2} \N

-- !select --
1 \N \N
2 null \N
3 true \N
4 false \N
3 true 1
4 false 0
5 100 100
6 10000 10000
7 1000000000 -13824
8 1152921504606846976 0
9 6.18 \N
9 6.18 6
10 "abcd" \N
11 {} \N
12 {"k1":"v31","k2":300} \N
Expand All @@ -5946,13 +5946,13 @@
-- !select --
1 \N \N
2 null \N
3 true \N
4 false \N
3 true 1
4 false 0
5 100 100
6 10000 10000
7 1000000000 1000000000
8 1152921504606846976 0
9 6.18 \N
9 6.18 6
10 "abcd" \N
11 {} \N
12 {"k1":"v31","k2":300} \N
Expand All @@ -5975,13 +5975,13 @@
-- !select --
1 \N \N
2 null \N
3 true \N
4 false \N
3 true 1
4 false 0
5 100 100
6 10000 10000
7 1000000000 1000000000
8 1152921504606846976 1152921504606846976
9 6.18 \N
9 6.18 6
10 "abcd" \N
11 {} \N
12 {"k1":"v31","k2":300} \N
Expand All @@ -6004,8 +6004,8 @@
-- !select --
1 \N \N
2 null \N
3 true \N
4 false \N
3 true 1
4 false 0
5 100 100
6 10000 10000
7 1000000000 1000000000
Expand Down
Loading

0 comments on commit b8f35a7

Please sign in to comment.