@@ -2,6 +2,7 @@ use anyhow::{anyhow, bail, Result};
22use parking_lot:: Mutex ;
33use std:: collections:: HashMap ;
44use std:: fmt:: { Display , Error , Formatter } ;
5+ use std:: num:: NonZeroUsize ;
56use std:: str:: FromStr ;
67use std:: sync:: Arc ;
78use std:: time:: Instant ;
@@ -962,16 +963,13 @@ impl Model {
962963 pub fn numeric_encode_features ( & self , rows : & [ pgrx:: datum:: AnyElement ] ) -> Vec < f32 > {
963964 // TODO handle FLOAT4[] as if it were pgrx::datum::AnyElement, skipping all this, and going straight to predict
964965 let mut features = Vec :: new ( ) ; // TODO pre-allocate space
965- let columns = & self . snapshot . columns ;
966966 for row in rows {
967967 match row. oid ( ) {
968968 pgrx_pg_sys:: RECORDOID => {
969969 let tuple = unsafe { PgHeapTuple :: from_composite_datum ( row. datum ( ) ) } ;
970- for index in 1 ..tuple. len ( ) + 1 {
971- let column = & columns[ index - 1 ] ;
972- let attribute = tuple
973- . get_attribute_by_index ( index. try_into ( ) . unwrap ( ) )
974- . unwrap ( ) ;
970+ for ( i, column) in self . snapshot . features ( ) . enumerate ( ) {
971+ let index = NonZeroUsize :: new ( i + 1 ) . unwrap ( ) ;
972+ let attribute = tuple. get_attribute_by_index ( index) . unwrap ( ) ;
975973 match & column. statistics . categories {
976974 Some ( _categories) => {
977975 let key = match attribute. atttypid {
@@ -982,14 +980,14 @@ impl Model {
982980 | pgrx_pg_sys:: VARCHAROID
983981 | pgrx_pg_sys:: BPCHAROID => {
984982 let element: Result < Option < String > , TryFromDatumError > =
985- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
983+ tuple. get_by_index ( index) ;
986984 element
987985 . unwrap ( )
988986 . unwrap_or ( snapshot:: NULL_CATEGORY_KEY . to_string ( ) )
989987 }
990988 pgrx_pg_sys:: BOOLOID => {
991989 let element: Result < Option < bool > , TryFromDatumError > =
992- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
990+ tuple. get_by_index ( index) ;
993991 element
994992 . unwrap ( )
995993 . map_or ( snapshot:: NULL_CATEGORY_KEY . to_string ( ) , |k| {
@@ -998,7 +996,7 @@ impl Model {
998996 }
999997 pgrx_pg_sys:: INT2OID => {
1000998 let element: Result < Option < i16 > , TryFromDatumError > =
1001- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
999+ tuple. get_by_index ( index) ;
10021000 element
10031001 . unwrap ( )
10041002 . map_or ( snapshot:: NULL_CATEGORY_KEY . to_string ( ) , |k| {
@@ -1007,7 +1005,7 @@ impl Model {
10071005 }
10081006 pgrx_pg_sys:: INT4OID => {
10091007 let element: Result < Option < i32 > , TryFromDatumError > =
1010- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1008+ tuple. get_by_index ( index) ;
10111009 element
10121010 . unwrap ( )
10131011 . map_or ( snapshot:: NULL_CATEGORY_KEY . to_string ( ) , |k| {
@@ -1016,7 +1014,7 @@ impl Model {
10161014 }
10171015 pgrx_pg_sys:: INT8OID => {
10181016 let element: Result < Option < i64 > , TryFromDatumError > =
1019- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1017+ tuple. get_by_index ( index) ;
10201018 element
10211019 . unwrap ( )
10221020 . map_or ( snapshot:: NULL_CATEGORY_KEY . to_string ( ) , |k| {
@@ -1025,7 +1023,7 @@ impl Model {
10251023 }
10261024 pgrx_pg_sys:: FLOAT4OID => {
10271025 let element: Result < Option < f32 > , TryFromDatumError > =
1028- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1026+ tuple. get_by_index ( index) ;
10291027 element
10301028 . unwrap ( )
10311029 . map_or ( snapshot:: NULL_CATEGORY_KEY . to_string ( ) , |k| {
@@ -1034,7 +1032,7 @@ impl Model {
10341032 }
10351033 pgrx_pg_sys:: FLOAT8OID => {
10361034 let element: Result < Option < f64 > , TryFromDatumError > =
1037- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1035+ tuple. get_by_index ( index) ;
10381036 element
10391037 . unwrap ( )
10401038 . map_or ( snapshot:: NULL_CATEGORY_KEY . to_string ( ) , |k| {
@@ -1056,79 +1054,79 @@ impl Model {
10561054 }
10571055 pgrx_pg_sys:: BOOLOID => {
10581056 let element: Result < Option < bool > , TryFromDatumError > =
1059- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1057+ tuple. get_by_index ( index) ;
10601058 features. push (
10611059 element. unwrap ( ) . map_or ( f32:: NAN , |v| v as u8 as f32 ) ,
10621060 ) ;
10631061 }
10641062 pgrx_pg_sys:: INT2OID => {
10651063 let element: Result < Option < i16 > , TryFromDatumError > =
1066- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1064+ tuple. get_by_index ( index) ;
10671065 features
10681066 . push ( element. unwrap ( ) . map_or ( f32:: NAN , |v| v as f32 ) ) ;
10691067 }
10701068 pgrx_pg_sys:: INT4OID => {
10711069 let element: Result < Option < i32 > , TryFromDatumError > =
1072- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1070+ tuple. get_by_index ( index) ;
10731071 features
10741072 . push ( element. unwrap ( ) . map_or ( f32:: NAN , |v| v as f32 ) ) ;
10751073 }
10761074 pgrx_pg_sys:: INT8OID => {
10771075 let element: Result < Option < i64 > , TryFromDatumError > =
1078- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1076+ tuple. get_by_index ( index) ;
10791077 features
10801078 . push ( element. unwrap ( ) . map_or ( f32:: NAN , |v| v as f32 ) ) ;
10811079 }
10821080 pgrx_pg_sys:: FLOAT4OID => {
10831081 let element: Result < Option < f32 > , TryFromDatumError > =
1084- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1082+ tuple. get_by_index ( index) ;
10851083 features. push ( element. unwrap ( ) . map_or ( f32:: NAN , |v| v) ) ;
10861084 }
10871085 pgrx_pg_sys:: FLOAT8OID => {
10881086 let element: Result < Option < f64 > , TryFromDatumError > =
1089- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1087+ tuple. get_by_index ( index) ;
10901088 features
10911089 . push ( element. unwrap ( ) . map_or ( f32:: NAN , |v| v as f32 ) ) ;
10921090 }
10931091 // TODO handle NULL to NaN for arrays
10941092 pgrx_pg_sys:: BOOLARRAYOID => {
10951093 let element: Result < Option < Vec < bool > > , TryFromDatumError > =
1096- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1094+ tuple. get_by_index ( index) ;
10971095 for j in element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
10981096 features. push ( * j as i8 as f32 ) ;
10991097 }
11001098 }
11011099 pgrx_pg_sys:: INT2ARRAYOID => {
11021100 let element: Result < Option < Vec < i16 > > , TryFromDatumError > =
1103- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1101+ tuple. get_by_index ( index) ;
11041102 for j in element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
11051103 features. push ( * j as f32 ) ;
11061104 }
11071105 }
11081106 pgrx_pg_sys:: INT4ARRAYOID => {
11091107 let element: Result < Option < Vec < i32 > > , TryFromDatumError > =
1110- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1108+ tuple. get_by_index ( index) ;
11111109 for j in element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
11121110 features. push ( * j as f32 ) ;
11131111 }
11141112 }
11151113 pgrx_pg_sys:: INT8ARRAYOID => {
11161114 let element: Result < Option < Vec < i64 > > , TryFromDatumError > =
1117- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1115+ tuple. get_by_index ( index) ;
11181116 for j in element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
11191117 features. push ( * j as f32 ) ;
11201118 }
11211119 }
11221120 pgrx_pg_sys:: FLOAT4ARRAYOID => {
11231121 let element: Result < Option < Vec < f32 > > , TryFromDatumError > =
1124- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1122+ tuple. get_by_index ( index) ;
11251123 for j in element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
11261124 features. push ( * j) ;
11271125 }
11281126 }
11291127 pgrx_pg_sys:: FLOAT8ARRAYOID => {
11301128 let element: Result < Option < Vec < f64 > > , TryFromDatumError > =
1131- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1129+ tuple. get_by_index ( index) ;
11321130 for j in element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
11331131 features. push ( * j as f32 ) ;
11341132 }
0 commit comments