@@ -94,6 +94,24 @@ func TestColumn(t *testing.T) {
9494 require .Zero (t , timeVal .Compare (tm ))
9595 require .False (t , isNull )
9696 require .NoError (t , err )
97+
98+ t .Run ("resolve virtual expression prefers exact column ID" , func (t * testing.T ) {
99+ strTp := types .NewFieldType (mysql .TypeVarchar )
100+ baseCol := & Column {UniqueID : 10 , RetType : strTp }
101+ virtualExpr := NewFunctionInternal (ctx , ast .Lower , strTp , baseCol )
102+ exprOnlyMatchedCol := & Column {UniqueID : 12 , RetType : strTp , VirtualExpr : virtualExpr .Clone ()}
103+ exactMatchedCol := & Column {UniqueID : 11 , RetType : strTp , VirtualExpr : virtualExpr .Clone ()}
104+ targetCol := & Column {UniqueID : exactMatchedCol .UniqueID , RetType : strTp , VirtualExpr : virtualExpr .Clone ()}
105+
106+ resolvedExpr , ok := targetCol .ResolveIndicesByVirtualExpr (ctx .GetEvalCtx (), NewSchema (exprOnlyMatchedCol , exactMatchedCol ))
107+ require .True (t , ok )
108+ require .Equal (t , 1 , resolvedExpr .(* Column ).Index )
109+
110+ ambiguousTargetCol := & Column {UniqueID : 13 , RetType : strTp , VirtualExpr : virtualExpr .Clone ()}
111+ resolvedExpr , ok = ambiguousTargetCol .ResolveIndicesByVirtualExpr (ctx .GetEvalCtx (), NewSchema (exprOnlyMatchedCol , exactMatchedCol ))
112+ require .True (t , ok )
113+ require .Equal (t , 0 , resolvedExpr .(* Column ).Index )
114+ })
97115}
98116
99117func TestColumnHashCode (t * testing.T ) {
0 commit comments