1+ use std:: sync:: Arc ;
2+
3+ use arrow_schema:: { DataType , IntervalUnit } ;
14use serde:: { Deserialize , Serialize } ;
25
6+ use crate :: common:: {
7+ nodes:: { ArcPredicateNode , PredicateNode , PredicateType , ReprPredicateNode } ,
8+ values:: { SerializableOrderedF64 , Value } ,
9+ } ;
10+
311/// TODO: documentation
412#[ derive( Copy , Clone , PartialEq , Eq , Hash , Debug , Serialize , Deserialize ) ]
513pub enum ConstantType {
@@ -19,3 +27,184 @@ pub enum ConstantType {
1927 Decimal ,
2028 Binary ,
2129}
30+
31+ impl ConstantType {
32+ pub fn get_data_type_from_value ( value : & Value ) -> Self {
33+ match value {
34+ Value :: Bool ( _) => ConstantType :: Bool ,
35+ Value :: String ( _) => ConstantType :: Utf8String ,
36+ Value :: UInt8 ( _) => ConstantType :: UInt8 ,
37+ Value :: UInt16 ( _) => ConstantType :: UInt16 ,
38+ Value :: UInt32 ( _) => ConstantType :: UInt32 ,
39+ Value :: UInt64 ( _) => ConstantType :: UInt64 ,
40+ Value :: Int8 ( _) => ConstantType :: Int8 ,
41+ Value :: Int16 ( _) => ConstantType :: Int16 ,
42+ Value :: Int32 ( _) => ConstantType :: Int32 ,
43+ Value :: Int64 ( _) => ConstantType :: Int64 ,
44+ Value :: Float ( _) => ConstantType :: Float64 ,
45+ Value :: Date32 ( _) => ConstantType :: Date ,
46+ _ => unimplemented ! ( "get_data_type_from_value() not implemented for value {value}" ) ,
47+ }
48+ }
49+
50+ // TODO: current DataType and ConstantType are not 1 to 1 mapping
51+ // optd schema stores constantType from data type in catalog.get
52+ // for decimal128, the precision is lost
53+ pub fn from_data_type ( data_type : DataType ) -> Self {
54+ match data_type {
55+ DataType :: Binary => ConstantType :: Binary ,
56+ DataType :: Boolean => ConstantType :: Bool ,
57+ DataType :: UInt8 => ConstantType :: UInt8 ,
58+ DataType :: UInt16 => ConstantType :: UInt16 ,
59+ DataType :: UInt32 => ConstantType :: UInt32 ,
60+ DataType :: UInt64 => ConstantType :: UInt64 ,
61+ DataType :: Int8 => ConstantType :: Int8 ,
62+ DataType :: Int16 => ConstantType :: Int16 ,
63+ DataType :: Int32 => ConstantType :: Int32 ,
64+ DataType :: Int64 => ConstantType :: Int64 ,
65+ DataType :: Float64 => ConstantType :: Float64 ,
66+ DataType :: Date32 => ConstantType :: Date ,
67+ DataType :: Interval ( IntervalUnit :: MonthDayNano ) => ConstantType :: IntervalMonthDateNano ,
68+ DataType :: Utf8 => ConstantType :: Utf8String ,
69+ DataType :: Decimal128 ( _, _) => ConstantType :: Decimal ,
70+ _ => unimplemented ! ( "no conversion to ConstantType for DataType {data_type}" ) ,
71+ }
72+ }
73+
74+ pub fn into_data_type ( & self ) -> DataType {
75+ match self {
76+ ConstantType :: Binary => DataType :: Binary ,
77+ ConstantType :: Bool => DataType :: Boolean ,
78+ ConstantType :: UInt8 => DataType :: UInt8 ,
79+ ConstantType :: UInt16 => DataType :: UInt16 ,
80+ ConstantType :: UInt32 => DataType :: UInt32 ,
81+ ConstantType :: UInt64 => DataType :: UInt64 ,
82+ ConstantType :: Int8 => DataType :: Int8 ,
83+ ConstantType :: Int16 => DataType :: Int16 ,
84+ ConstantType :: Int32 => DataType :: Int32 ,
85+ ConstantType :: Int64 => DataType :: Int64 ,
86+ ConstantType :: Float64 => DataType :: Float64 ,
87+ ConstantType :: Date => DataType :: Date32 ,
88+ ConstantType :: IntervalMonthDateNano => DataType :: Interval ( IntervalUnit :: MonthDayNano ) ,
89+ ConstantType :: Decimal => DataType :: Float64 ,
90+ ConstantType :: Utf8String => DataType :: Utf8 ,
91+ }
92+ }
93+ }
94+
95+ #[ derive( Clone , Debug ) ]
96+ pub struct ConstantPred ( pub ArcPredicateNode ) ;
97+
98+ impl ConstantPred {
99+ pub fn new ( value : Value ) -> Self {
100+ let typ = ConstantType :: get_data_type_from_value ( & value) ;
101+ Self :: new_with_type ( value, typ)
102+ }
103+
104+ pub fn new_with_type ( value : Value , typ : ConstantType ) -> Self {
105+ ConstantPred (
106+ PredicateNode {
107+ typ : PredicateType :: Constant ( typ) ,
108+ children : vec ! [ ] ,
109+ data : Some ( value) ,
110+ }
111+ . into ( ) ,
112+ )
113+ }
114+
115+ pub fn bool ( value : bool ) -> Self {
116+ Self :: new_with_type ( Value :: Bool ( value) , ConstantType :: Bool )
117+ }
118+
119+ pub fn string ( value : impl AsRef < str > ) -> Self {
120+ Self :: new_with_type (
121+ Value :: String ( value. as_ref ( ) . into ( ) ) ,
122+ ConstantType :: Utf8String ,
123+ )
124+ }
125+
126+ pub fn uint8 ( value : u8 ) -> Self {
127+ Self :: new_with_type ( Value :: UInt8 ( value) , ConstantType :: UInt8 )
128+ }
129+
130+ pub fn uint16 ( value : u16 ) -> Self {
131+ Self :: new_with_type ( Value :: UInt16 ( value) , ConstantType :: UInt16 )
132+ }
133+
134+ pub fn uint32 ( value : u32 ) -> Self {
135+ Self :: new_with_type ( Value :: UInt32 ( value) , ConstantType :: UInt32 )
136+ }
137+
138+ pub fn uint64 ( value : u64 ) -> Self {
139+ Self :: new_with_type ( Value :: UInt64 ( value) , ConstantType :: UInt64 )
140+ }
141+
142+ pub fn int8 ( value : i8 ) -> Self {
143+ Self :: new_with_type ( Value :: Int8 ( value) , ConstantType :: Int8 )
144+ }
145+
146+ pub fn int16 ( value : i16 ) -> Self {
147+ Self :: new_with_type ( Value :: Int16 ( value) , ConstantType :: Int16 )
148+ }
149+
150+ pub fn int32 ( value : i32 ) -> Self {
151+ Self :: new_with_type ( Value :: Int32 ( value) , ConstantType :: Int32 )
152+ }
153+
154+ pub fn int64 ( value : i64 ) -> Self {
155+ Self :: new_with_type ( Value :: Int64 ( value) , ConstantType :: Int64 )
156+ }
157+
158+ pub fn interval_month_day_nano ( value : i128 ) -> Self {
159+ Self :: new_with_type ( Value :: Int128 ( value) , ConstantType :: IntervalMonthDateNano )
160+ }
161+
162+ pub fn float64 ( value : f64 ) -> Self {
163+ Self :: new_with_type (
164+ Value :: Float ( SerializableOrderedF64 ( value. into ( ) ) ) ,
165+ ConstantType :: Float64 ,
166+ )
167+ }
168+
169+ pub fn date ( value : i64 ) -> Self {
170+ Self :: new_with_type ( Value :: Int64 ( value) , ConstantType :: Date )
171+ }
172+
173+ pub fn decimal ( value : f64 ) -> Self {
174+ Self :: new_with_type (
175+ Value :: Float ( SerializableOrderedF64 ( value. into ( ) ) ) ,
176+ ConstantType :: Decimal ,
177+ )
178+ }
179+
180+ pub fn serialized ( value : Arc < [ u8 ] > ) -> Self {
181+ Self :: new_with_type ( Value :: Serialized ( value) , ConstantType :: Binary )
182+ }
183+
184+ /// Gets the constant value.
185+ pub fn value ( & self ) -> Value {
186+ self . 0 . data . clone ( ) . unwrap ( )
187+ }
188+
189+ pub fn constant_type ( & self ) -> ConstantType {
190+ if let PredicateType :: Constant ( typ) = self . 0 . typ {
191+ typ
192+ } else {
193+ panic ! ( "not a constant" )
194+ }
195+ }
196+ }
197+
198+ impl ReprPredicateNode for ConstantPred {
199+ fn into_pred_node ( self ) -> ArcPredicateNode {
200+ self . 0
201+ }
202+
203+ fn from_pred_node ( rel_node : ArcPredicateNode ) -> Option < Self > {
204+ if let PredicateType :: Constant ( _) = rel_node. typ {
205+ Some ( Self ( rel_node) )
206+ } else {
207+ None
208+ }
209+ }
210+ }
0 commit comments