@@ -46,6 +46,7 @@ use crate::planner::semantic::NameResolutionContext;
4646use crate :: plans:: BoundColumnRef ;
4747use crate :: plans:: EvalScalar ;
4848use crate :: plans:: Filter ;
49+ use crate :: plans:: FunctionCall ;
4950use crate :: plans:: HashJoinBuildCacheInfo ;
5051use crate :: plans:: Join ;
5152use crate :: plans:: JoinEquiCondition ;
@@ -61,6 +62,8 @@ pub struct JoinConditions {
6162 pub ( crate ) other_conditions : Vec < ScalarExpr > ,
6263}
6364
65+ const ASOF_USING_RANGE_FUNC : & str = "gte" ;
66+
6467impl Binder {
6568 pub ( crate ) fn bind_join (
6669 & mut self ,
@@ -732,6 +735,7 @@ impl<'a> JoinConditionResolver<'a> {
732735 using_columns,
733736 left_join_conditions,
734737 right_join_conditions,
738+ non_equi_conditions,
735739 join_op,
736740 ) ?;
737741 }
@@ -746,6 +750,7 @@ impl<'a> JoinConditionResolver<'a> {
746750 using_columns,
747751 left_join_conditions,
748752 right_join_conditions,
753+ non_equi_conditions,
749754 join_op,
750755 ) ?
751756 }
@@ -907,6 +912,7 @@ impl<'a> JoinConditionResolver<'a> {
907912 using_columns : Vec < ( Span , String ) > ,
908913 left_join_conditions : & mut Vec < ScalarExpr > ,
909914 right_join_conditions : & mut Vec < ScalarExpr > ,
915+ non_equi_conditions : & mut Vec < ScalarExpr > ,
910916 join_op : & JoinOperator ,
911917 ) -> Result < ( ) > {
912918 bind_join_columns (
@@ -915,7 +921,18 @@ impl<'a> JoinConditionResolver<'a> {
915921 self . join_context ,
916922 ) ;
917923 let left_columns_len = self . left_column_bindings . len ( ) ;
918- for ( span, join_key) in using_columns. iter ( ) {
924+ let asof_range_column = if matches ! (
925+ join_op,
926+ JoinOperator :: Asof
927+ | JoinOperator :: LeftAsof
928+ | JoinOperator :: RightAsof
929+ | JoinOperator :: FullAsof
930+ ) {
931+ using_columns. len ( ) . checked_sub ( 1 )
932+ } else {
933+ None
934+ } ;
935+ for ( index, ( span, join_key) ) in using_columns. iter ( ) . enumerate ( ) {
919936 let join_key_name = join_key. as_str ( ) ;
920937 let left_scalar = if let Some ( col_binding) = self . join_context . columns
921938 [ 0 ..left_columns_len]
@@ -950,7 +967,8 @@ impl<'a> JoinConditionResolver<'a> {
950967 ) )
951968 . set_span ( * span) ) ;
952969 } ;
953- let idx = !matches ! ( join_op, JoinOperator :: RightOuter ) as usize ;
970+ let idx =
971+ !matches ! ( join_op, JoinOperator :: RightOuter | JoinOperator :: RightAsof ) as usize ;
954972 if let Some ( col_binding) = self
955973 . join_context
956974 . columns
@@ -961,16 +979,25 @@ impl<'a> JoinConditionResolver<'a> {
961979 } )
962980 . nth ( idx)
963981 {
964- // Always make the second using column in the join_context invisible in unqualified wildcard.
982+ // Make the duplicate USING column invisible in unqualified wildcard.
965983 col_binding. visibility = Visibility :: UnqualifiedWildcardInVisible ;
966984 }
967985
968- self . add_equi_conditions (
969- left_scalar,
970- right_scalar,
971- left_join_conditions,
972- right_join_conditions,
973- ) ?;
986+ if Some ( index) == asof_range_column {
987+ non_equi_conditions. push ( ScalarExpr :: FunctionCall ( FunctionCall {
988+ span : * span,
989+ func_name : ASOF_USING_RANGE_FUNC . to_string ( ) ,
990+ params : vec ! [ ] ,
991+ arguments : vec ! [ left_scalar, right_scalar] ,
992+ } ) ) ;
993+ } else {
994+ self . add_equi_conditions (
995+ left_scalar,
996+ right_scalar,
997+ left_join_conditions,
998+ right_join_conditions,
999+ ) ?;
1000+ }
9741001 }
9751002 Ok ( ( ) )
9761003 }
0 commit comments