@@ -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 {
962963pub 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
964965let mut features =Vec :: new ( ) ; // TODO pre-allocate space
965- let columns =& self . snapshot . columns ;
966966for rowin rows{
967967match row. oid ( ) {
968968 pgrx_pg_sys:: RECORDOID =>{
969969let tuple =unsafe { PgHeapTuple :: from_composite_datum ( row. datum ( ) ) } ;
970- for indexin 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 ( ) ;
975973match & column. statistics . categories {
976974Some ( _categories) =>{
977975let key =match attribute. atttypid {
@@ -982,14 +980,14 @@ impl Model {
982980 | pgrx_pg_sys:: VARCHAROID
983981 | pgrx_pg_sys:: BPCHAROID =>{
984982let 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 =>{
991989let 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 =>{
1000998let 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 =>{
10091007let 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 =>{
10181016let 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 =>{
10271025let 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 =>{
10361034let 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 =>{
10581056let 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| vas u8 as f32 ) ,
10621060) ;
10631061}
10641062 pgrx_pg_sys:: INT2OID =>{
10651063let 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| vas f32 ) ) ;
10691067}
10701068 pgrx_pg_sys:: INT4OID =>{
10711069let 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| vas f32 ) ) ;
10751073}
10761074 pgrx_pg_sys:: INT8OID =>{
10771075let 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| vas f32 ) ) ;
10811079}
10821080 pgrx_pg_sys:: FLOAT4OID =>{
10831081let 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 =>{
10881086let 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| vas f32 ) ) ;
10921090}
10931091// TODO handle NULL to NaN for arrays
10941092 pgrx_pg_sys:: BOOLARRAYOID =>{
10951093let element: Result < Option < Vec < bool > > , TryFromDatumError > =
1096- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1094+ tuple. get_by_index ( index) ;
10971095for jin element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
10981096 features. push ( * jas i8 as f32 ) ;
10991097}
11001098}
11011099 pgrx_pg_sys:: INT2ARRAYOID =>{
11021100let element: Result < Option < Vec < i16 > > , TryFromDatumError > =
1103- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1101+ tuple. get_by_index ( index) ;
11041102for jin element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
11051103 features. push ( * jas f32 ) ;
11061104}
11071105}
11081106 pgrx_pg_sys:: INT4ARRAYOID =>{
11091107let element: Result < Option < Vec < i32 > > , TryFromDatumError > =
1110- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1108+ tuple. get_by_index ( index) ;
11111109for jin element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
11121110 features. push ( * jas f32 ) ;
11131111}
11141112}
11151113 pgrx_pg_sys:: INT8ARRAYOID =>{
11161114let element: Result < Option < Vec < i64 > > , TryFromDatumError > =
1117- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1115+ tuple. get_by_index ( index) ;
11181116for jin element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
11191117 features. push ( * jas f32 ) ;
11201118}
11211119}
11221120 pgrx_pg_sys:: FLOAT4ARRAYOID =>{
11231121let element: Result < Option < Vec < f32 > > , TryFromDatumError > =
1124- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1122+ tuple. get_by_index ( index) ;
11251123for jin element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
11261124 features. push ( * j) ;
11271125}
11281126}
11291127 pgrx_pg_sys:: FLOAT8ARRAYOID =>{
11301128let element: Result < Option < Vec < f64 > > , TryFromDatumError > =
1131- tuple. get_by_index ( index. try_into ( ) . unwrap ( ) ) ;
1129+ tuple. get_by_index ( index) ;
11321130for jin element. as_ref ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) {
11331131 features. push ( * jas f32 ) ;
11341132}