=== modified file 'rules/annotations/where_clause_prep.rules'
--- rules/annotations/where_clause_prep.rules	2021-10-05 16:36:15 +0000
+++ rules/annotations/where_clause_prep.rules	2023-02-20 14:37:08 +0000
@@ -4,7 +4,7 @@
 ** Module   : where_clause_prep.rules
 ** Abstract : prepare for where clause conversion 
 **
-** Copyright (c) 2005-2021, Golden Code Development Corporation.
+** Copyright (c) 2005-2023, Golden Code Development Corporation.
 **
 ** _#_ _I_ __Date__ __JPRM__ __________________________________Description___________________________________
 ** 001 GES 20051024   @23118 Prepare for where clause conversion.
@@ -81,6 +81,7 @@
 ** 041 IAS 20210208          Do not convert CONTAINS arguments to upper case at conversion time.
 **     ECF 20210317          Ignore subscript on an extent field lvalue of a CONTAINS operation.
 **     IAS 20211004          Fixed conversion of INDEX and LOOKUP for case-sensitive arguments
+** 042 IAS 20230220          Fixed nested CAN-FIND conversion
 */
 -->
 
@@ -175,12 +176,30 @@
    <variable name="litDate"      type="com.goldencode.p2j.util.date" />
    <variable name="litDatetime"  type="com.goldencode.p2j.util.datetime" />
    <variable name="litDatetimetz" type="com.goldencode.p2j.util.datetimetz" />
+   <variable name="canFind"      type="java.util.Set" />
    
    <!-- expression libraries -->
    <include name="common-progress" />
    
    <func-library access="private">
    
+       <!-- detect whether node is part of a nested CAN-FIND expression -->
+      <function name="is_nested_can_find">
+      
+         <variable name="ref"    type="com.goldencode.ast.Aast" />
+         <return   name="res"    type="java.lang.Boolean" />
+         
+         <expression>
+            ref = this.getAncestor(-1, prog.record_phrase)
+         </expression>
+         <expression>
+            res = ref != null                         and
+                  ref.isAnnotation("nested_can_find") and
+                  #(boolean) ref.getAnnotation("nested_can_find")
+         </expression>
+         
+      </function>
+ 
       <!-- test if this is the special case where a function gets
            translated into the id field reference in FQL -->
       <function name="test_for_id_function">
@@ -272,6 +291,8 @@
       <rule>bcCutoff    = create("com.goldencode.p2j.util.date", 12, 31, -1)</rule>
       <rule>bcDtCutoff  = create("com.goldencode.p2j.util.datetime", 12, 31, -1, 0, 0)</rule>
       <rule>bcDtzCutoff = create("com.goldencode.p2j.util.datetimetz", 12, 31, -1, 0, 0)</rule>
+      <rule>canFind = create("java.util.HashSet") </rule>
+      
       
    </init-rules>
    
@@ -325,7 +346,15 @@
             </while>
             <action>colonRef.putAnnotation("sub_expression", true)</action>
          </rule>
+
+         <rule>type == prog.table and getNoteBoolean("canfind")
+            <action>canFind.add(getNoteString("bufname"))</action>
+         </rule>       
          
+         <rule>type == prog.record_phrase and !evalLib("is_nested_can_find")
+             <action>canFind.clear()</action>
+         </rule>       
+
          <rule>ignoreId == -1
          
             <!-- this node can't be represented in FQL -->
@@ -413,11 +442,13 @@
                 type == prog.kw_not                                        or
                 validop                                                    or
                 evalLib("literals", type)                                  or
-                (evalLib("fields") and getNoteBoolean("current_buffer"))   or
+                (evalLib("fields") and 
+                    (getNoteBoolean("current_buffer") or getNoteBoolean("canfind"))
+                )                                                          or
                 evalLib("subscript")                                       or
                 idfunc                                                     or
                 evalLib("is_type_pair", prog.func_poly, prog.kw_if))
-               
+
                <!-- this is an FQL node (for AND, OR, NOT, and ternary IF this is
                     provisional only, until children can be checked) -->
                <action>putNote("hql", true)</action>
@@ -645,10 +676,10 @@
                   <!-- remember that this is the root node of a sub-expression
                        that must be evaluated on the client (and substituted
                        into the query) -->
+
                   <rule>!evalLib("is_type_pair", prog.func_logical, prog.kw_can_find)
                      <action>putNote("sub_expression", true)</action>
                   </rule>
-                  
                   <!-- sub-expressions rooted at a field reference to a related buffer in a
                        multi-table query need to be emitted with a field name -->
                   <rule>evalLib("fields") and getNoteBoolean("related_buffer")
@@ -712,9 +743,13 @@
                      <action>copy.parent.putAnnotation("casesens", true)</action>
                   </rule>
                </rule>
-               
+        
+               <action>test=false</action>
+               <rule>evalLib("fields")
+                    <action>test = canFind.contains(getNoteString("bufname"))</action>
+               </rule>
                <!-- this node can't be represented in FQL -->
-               <action on="false">putNote("hql", false)</action>
+               <action on="false">putNote("hql", test)</action>
                
             </rule>
          </rule>

