@@ -17,6 +17,7 @@ pub(crate) struct EtcdFdw {
1717 client : Client ,
1818 rt : Runtime ,
1919 fetch_results : Vec < KeyValue > ,
20+ tgt_cols : Vec < Column > ,
2021 fetch_key : bool ,
2122 fetch_value : bool ,
2223}
@@ -231,6 +232,7 @@ impl ForeignDataWrapper<EtcdFdwError> for EtcdFdw {
231232 client,
232233 rt,
233234 fetch_results,
235+ tgt_cols : Vec :: new ( ) ,
234236 fetch_key : false ,
235237 fetch_value : false ,
236238 } )
@@ -434,6 +436,7 @@ impl ForeignDataWrapper<EtcdFdwError> for EtcdFdw {
434436 } ;
435437 let result_vec = result_unwrapped. take_kvs ( ) ;
436438 self . fetch_results = result_vec;
439+ self . tgt_cols = columns. to_vec ( ) ;
437440 Ok ( ( ) )
438441 }
439442
@@ -448,11 +451,13 @@ impl ForeignDataWrapper<EtcdFdwError> for EtcdFdw {
448451 let value = x
449452 . value_str ( )
450453 . expect ( "Expected a value, but the value was empty" ) ;
451- if self . fetch_key {
452- row. push ( "key" , Some ( Cell :: String ( key. to_string ( ) ) ) ) ;
453- }
454- if self . fetch_value {
455- row. push ( "value" , Some ( Cell :: String ( value. to_string ( ) ) ) ) ;
454+ for tgt_col in & self . tgt_cols {
455+ if tgt_col. name == "key" {
456+ row. push ( & tgt_col. name , Some ( Cell :: String ( key. to_string ( ) ) ) ) ;
457+ }
458+ if tgt_col. name == "value" {
459+ row. push ( & tgt_col. name , Some ( Cell :: String ( value. to_string ( ) ) ) ) ;
460+ }
456461 }
457462 } ) )
458463 }
@@ -766,4 +771,62 @@ mod tests {
766771
767772 assert_eq ! ( Err ( spi:: SpiError :: InvalidPosition ) , query_result) ;
768773 }
774+
775+ #[ pg_test]
776+ fn test_select_value_only_with_key_filter ( ) {
777+ // Test for issue #26: selecting only value column with WHERE clause on key
778+ let ( _container, url) = create_container ( ) ;
779+
780+ create_fdt ( url) ;
781+
782+ // Insert test data
783+ Spi :: run ( "INSERT INTO test (key, value) VALUES ('key1','value1'),('key2','value2'),('key3','value3')" )
784+ . expect ( "INSERT should work" ) ;
785+
786+ // Test 1: SELECT value WHERE key = 'key2' should return the value
787+ let query_result = Spi :: get_one :: < String > ( "SELECT value FROM test WHERE key = 'key2'" )
788+ . expect ( "SELECT value with key filter should work" ) ;
789+
790+ assert_eq ! ( Some ( format!( "value2" ) ) , query_result) ;
791+
792+ // Test 2: SELECT key WHERE key = 'key1' should also work
793+ let query_result = Spi :: get_one :: < String > ( "SELECT key FROM test WHERE key = 'key1'" )
794+ . expect ( "SELECT key with key filter should work" ) ;
795+
796+ assert_eq ! ( Some ( format!( "key1" ) ) , query_result) ;
797+
798+ // Test 3: SELECT * WHERE key = 'key3' should work (baseline)
799+ let query_result = Spi :: get_two :: < String , String > ( "SELECT * FROM test WHERE key = 'key3'" )
800+ . expect ( "SELECT * with key filter should work" ) ;
801+
802+ assert_eq ! ( ( Some ( format!( "key3" ) ) , Some ( format!( "value3" ) ) ) , query_result) ;
803+ }
804+
805+ #[ pg_test]
806+ fn test_update_value_only_with_key_filter ( ) {
807+ // Test for issue #26: UPDATE with only value column when key is in WHERE clause
808+ let ( _container, url) = create_container ( ) ;
809+
810+ create_fdt ( url) ;
811+
812+ // Insert test data
813+ Spi :: run ( "INSERT INTO test (key, value) VALUES ('gather/78','original_value'),('gather/84','other_value')" )
814+ . expect ( "INSERT should work" ) ;
815+
816+ // Update without including key column in SET clause
817+ Spi :: run ( "UPDATE test SET value = 'updated_value' WHERE key = 'gather/84'" )
818+ . expect ( "UPDATE with key filter should work" ) ;
819+
820+ // Verify the update worked
821+ let query_result = Spi :: get_one :: < String > ( "SELECT value FROM test WHERE key = 'gather/84'" )
822+ . expect ( "SELECT after UPDATE should work" ) ;
823+
824+ assert_eq ! ( Some ( format!( "updated_value" ) ) , query_result) ;
825+
826+ // Verify other row was not affected
827+ let query_result = Spi :: get_one :: < String > ( "SELECT value FROM test WHERE key = 'gather/78'" )
828+ . expect ( "SELECT other row should work" ) ;
829+
830+ assert_eq ! ( Some ( format!( "original_value" ) ) , query_result) ;
831+ }
769832}
0 commit comments