Skip to content

Commit 7c8b98a

Browse files
authored
implement cast from bool to number (#6274)
The cast() documentation specifies that a bool value can be cast to a number type where false is 0 and true is 1.
1 parent 4586c71 commit 7c8b98a

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

runtime/sam/expr/coerce/cast.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ func ToUint(val super.Value, typUint super.Type) (uint64, bool) {
2323
v := val.Float()
2424
min, max, check := FromFloatOverflowCheck(val.Type(), typUint)
2525
return uint64(v), !check || v >= min && v <= max
26+
case id == super.IDBool:
27+
var v uint64
28+
if val.Bool() {
29+
v = 1
30+
}
31+
return v, true
2632
case id == super.IDString:
2733
v, err := strconv.ParseUint(val.AsString(), 10, UintBits(typUint))
2834
return v, err == nil
@@ -45,6 +51,12 @@ func ToInt(val super.Value, typInt super.Type) (int64, bool) {
4551
v := val.Float()
4652
min, max, check := FromFloatOverflowCheck(val.Type(), typInt)
4753
return int64(v), !check || v >= min && v <= max
54+
case id == super.IDBool:
55+
var v int64
56+
if val.Bool() {
57+
v = 1
58+
}
59+
return v, true
4860
case id == super.IDString:
4961
v, err := strconv.ParseInt(val.AsString(), 10, IntBits(typInt))
5062
return v, err == nil
@@ -63,6 +75,10 @@ func ToFloat(val super.Value, typ super.Type) (float64, bool) {
6375
v = float64(val.Int())
6476
case super.IsFloat(fromId):
6577
v = val.Float()
78+
case fromId == super.IDBool:
79+
if val.Bool() {
80+
v = 1
81+
}
6682
case fromId == super.IDString:
6783
var err error
6884
if v, err = byteconv.ParseFloat64(val.Bytes()); err != nil {

runtime/vam/expr/cast/number.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func toNumeric[T numeric](vec vector.Any, typ super.Type, index []uint32) ([]T,
7979
return checkAndCastNumbers[float64, T](vec.Values, min, max, index)
8080
}
8181
return castNumbers[float64, T](vec.Values, index), nil
82+
case *vector.Bool:
83+
return boolToNumeric[T](vec, index), nil
8284
default:
8385
panic(vec)
8486
}
@@ -123,6 +125,21 @@ func castNumbers[E numeric, T numeric](s []E, index []uint32) []T {
123125
return out
124126
}
125127

128+
func boolToNumeric[T numeric](vec *vector.Bool, index []uint32) []T {
129+
n := lengthOf(vec, index)
130+
out := make([]T, n)
131+
for i := range n {
132+
idx := i
133+
if index != nil {
134+
idx = index[i]
135+
}
136+
if vec.Bits.IsSet(idx) {
137+
out[i] = 1
138+
}
139+
}
140+
return out
141+
}
142+
126143
func castStringToNumber(vec vector.Any, typ super.Type, index []uint32) (vector.Any, []uint32) {
127144
svec := vec.(*vector.String)
128145
switch id := typ.ID(); {

runtime/ztests/expr/cast/float.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ input: |
99
1::uint64
1010
1000000000::uint64
1111
2::=named
12+
false
13+
true
1214
1315
output: |
1416
1.5::float16
@@ -29,3 +31,9 @@ output: |
2931
2.::float16
3032
2.::float32
3133
2.
34+
0.::float16
35+
0.::float32
36+
0.
37+
1.::float16
38+
1.::float32
39+
1.

runtime/ztests/expr/cast/int.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ input: |
1212
-1.
1313
1.5::=named
1414
1e8
15-
15+
false
16+
true
1617
1718
output: |
1819
-1::int8
@@ -47,3 +48,11 @@ output: |
4748
error({message:"cannot cast to int16",on:100000000.})
4849
100000000::int32
4950
100000000
51+
0::int8
52+
0::int16
53+
0::int32
54+
0
55+
1::int8
56+
1::int16
57+
1::int32
58+
1

runtime/ztests/expr/cast/uint.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
spq: |
2+
values this::uint8, this::uint16, this::uint32, this::uint64
3+
4+
vector: true
5+
6+
input: |
7+
false
8+
true
9+
10+
output: |
11+
0::uint8
12+
0::uint16
13+
0::uint32
14+
0::uint64
15+
1::uint8
16+
1::uint16
17+
1::uint32
18+
1::uint64

0 commit comments

Comments
 (0)