@@ -3,7 +3,7 @@ use sqlite3_parser::ast::{self, Expr, UnaryOperator};
3
3
4
4
use crate :: {
5
5
function:: { Func , SingleRowFunc } ,
6
- schema:: { Column , Schema , Table } ,
6
+ schema:: { Schema , Table , Type } ,
7
7
select:: { ColumnInfo , Select , SrcTable } ,
8
8
util:: normalize_ident,
9
9
vdbe:: { BranchOffset , Insn , ProgramBuilder } ,
@@ -286,9 +286,8 @@ pub fn translate_expr(
286
286
for arg in args {
287
287
let reg = program. alloc_register ( ) ;
288
288
let _ = translate_expr ( program, select, arg, reg, cursor_hint) ?;
289
- match arg {
290
- ast:: Expr :: Literal ( _) => program. mark_last_insn_constant ( ) ,
291
- _ => { }
289
+ if let ast:: Expr :: Literal ( _) = arg {
290
+ program. mark_last_insn_constant ( )
292
291
}
293
292
}
294
293
program. emit_insn ( Insn :: Function {
@@ -374,9 +373,9 @@ pub fn translate_expr(
374
373
ast:: Expr :: FunctionCallStar { .. } => todo ! ( ) ,
375
374
ast:: Expr :: Id ( ident) => {
376
375
// let (idx, col) = table.unwrap().get_column(&ident.0).unwrap();
377
- let ( idx, col , cursor_id) =
376
+ let ( idx, col_type , cursor_id, is_primary_key ) =
378
377
resolve_ident_table ( program, & ident. 0 , select, cursor_hint) ?;
379
- if col . primary_key {
378
+ if is_primary_key {
380
379
program. emit_insn ( Insn :: RowId {
381
380
cursor_id,
382
381
dest : target_register,
@@ -388,7 +387,7 @@ pub fn translate_expr(
388
387
cursor_id,
389
388
} ) ;
390
389
}
391
- maybe_apply_affinity ( col , target_register, program) ;
390
+ maybe_apply_affinity ( col_type , target_register, program) ;
392
391
Ok ( target_register)
393
392
}
394
393
ast:: Expr :: InList { .. } => todo ! ( ) ,
@@ -436,9 +435,9 @@ pub fn translate_expr(
436
435
ast:: Expr :: NotNull ( _) => todo ! ( ) ,
437
436
ast:: Expr :: Parenthesized ( _) => todo ! ( ) ,
438
437
ast:: Expr :: Qualified ( tbl, ident) => {
439
- let ( idx, col , cursor_id) =
438
+ let ( idx, col_type , cursor_id, is_primary_key ) =
440
439
resolve_ident_qualified ( program, & tbl. 0 , & ident. 0 , select, cursor_hint) ?;
441
- if col . primary_key {
440
+ if is_primary_key {
442
441
program. emit_insn ( Insn :: RowId {
443
442
cursor_id,
444
443
dest : target_register,
@@ -450,7 +449,7 @@ pub fn translate_expr(
450
449
cursor_id,
451
450
} ) ;
452
451
}
453
- maybe_apply_affinity ( col , target_register, program) ;
452
+ maybe_apply_affinity ( col_type , target_register, program) ;
454
453
Ok ( target_register)
455
454
}
456
455
ast:: Expr :: Raise ( _, _) => todo ! ( ) ,
@@ -562,7 +561,7 @@ pub fn resolve_ident_qualified<'a>(
562
561
ident : & String ,
563
562
select : & ' a Select ,
564
563
cursor_hint : Option < usize > ,
565
- ) -> Result < ( usize , & ' a Column , usize ) > {
564
+ ) -> Result < ( usize , Type , usize , bool ) > {
566
565
for join in & select. src_tables {
567
566
match join. table {
568
567
Table :: BTree ( ref table) => {
@@ -579,7 +578,7 @@ pub fn resolve_ident_qualified<'a>(
579
578
if res. is_some ( ) {
580
579
let ( idx, col) = res. unwrap ( ) ;
581
580
let cursor_id = program. resolve_cursor_id ( & table_identifier, cursor_hint) ;
582
- return Ok ( ( idx, col, cursor_id) ) ;
581
+ return Ok ( ( idx, col. ty , cursor_id, col . primary_key ) ) ;
583
582
}
584
583
}
585
584
}
@@ -598,7 +597,7 @@ pub fn resolve_ident_table<'a>(
598
597
ident : & String ,
599
598
select : & ' a Select ,
600
599
cursor_hint : Option < usize > ,
601
- ) -> Result < ( usize , & ' a Column , usize ) > {
600
+ ) -> Result < ( usize , Type , usize , bool ) > {
602
601
let mut found = Vec :: new ( ) ;
603
602
for join in & select. src_tables {
604
603
match join. table {
@@ -611,11 +610,29 @@ pub fn resolve_ident_table<'a>(
611
610
. columns
612
611
. iter ( )
613
612
. enumerate ( )
614
- . find ( |( _, col) | col. name == * ident) ;
613
+ . find ( |( _, col) | col. name == * ident)
614
+ . map ( |( idx, col) | ( idx, col. ty , col. primary_key ) ) ;
615
+ let mut idx;
616
+ let mut col_type;
617
+ let mut is_primary_key;
615
618
if res. is_some ( ) {
616
- let ( idx, col) = res. unwrap ( ) ;
619
+ ( idx, col_type, is_primary_key) = res. unwrap ( ) ;
620
+ // overwrite if cursor hint is provided
621
+ if let Some ( cursor_hint) = cursor_hint {
622
+ let cols = & program. cursor_ref [ cursor_hint] . 1 ;
623
+ if let Some ( res) = cols. as_ref ( ) . and_then ( |res| {
624
+ res. columns ( )
625
+ . iter ( )
626
+ . enumerate ( )
627
+ . find ( |x| x. 1 . name == * ident)
628
+ } ) {
629
+ idx = res. 0 ;
630
+ col_type = res. 1 . ty ;
631
+ is_primary_key = res. 1 . primary_key ;
632
+ }
633
+ }
617
634
let cursor_id = program. resolve_cursor_id ( & table_identifier, cursor_hint) ;
618
- found. push ( ( idx, col , cursor_id) ) ;
635
+ found. push ( ( idx, col_type , cursor_id, is_primary_key ) ) ;
619
636
}
620
637
}
621
638
Table :: Pseudo ( _) => todo ! ( ) ,
@@ -631,8 +648,8 @@ pub fn resolve_ident_table<'a>(
631
648
anyhow:: bail!( "Parse error: ambiguous column name {}" , ident. as_str( ) ) ;
632
649
}
633
650
634
- pub fn maybe_apply_affinity ( col : & Column , target_register : usize , program : & mut ProgramBuilder ) {
635
- if col . ty == crate :: schema:: Type :: Real {
651
+ pub fn maybe_apply_affinity ( col_type : Type , target_register : usize , program : & mut ProgramBuilder ) {
652
+ if col_type == crate :: schema:: Type :: Real {
636
653
program. emit_insn ( Insn :: RealAffinity {
637
654
register : target_register,
638
655
} )
0 commit comments