6464
6565import static org .apache .paimon .data .Timestamp .fromLocalDateTime ;
6666
67- public class PaimonPredicateConverter extends ScalarOperatorVisitor <Predicate , Void > {
67+ public class PaimonPredicateConverter extends ScalarOperatorVisitor <Predicate , PaimonPredicateContext > {
6868 private static final Logger LOG = LogManager .getLogger (PaimonPredicateConverter .class );
6969 private final PredicateBuilder builder ;
7070 private final List <String > fieldNames ;
@@ -84,35 +84,48 @@ public Predicate convert(ScalarOperator operator) {
8484 }
8585
8686 @ Override
87- public Predicate visit (ScalarOperator scalarOperator , Void context ) {
87+ public Predicate visit (ScalarOperator scalarOperator , PaimonPredicateContext context ) {
8888 return null ;
8989 }
9090
9191 @ Override
92- public Predicate visitCompoundPredicate (CompoundPredicateOperator operator , Void context ) {
92+ public Predicate visitCompoundPredicate (CompoundPredicateOperator operator , PaimonPredicateContext context ) {
9393 CompoundPredicateOperator .CompoundType op = operator .getCompoundType ();
9494 if (op == CompoundPredicateOperator .CompoundType .NOT ) {
9595 if (operator .getChild (0 ) instanceof LikePredicateOperator ) {
9696 return null ;
9797 }
98- Predicate expression = operator .getChild (0 ).accept (this , null );
99-
98+ PaimonPredicateContext paimonPredicateContext = new PaimonPredicateContext ();
99+ paimonPredicateContext .setParentPredicateOperator (operator );
100+ Predicate expression = operator .getChild (0 ).accept (this , paimonPredicateContext );
100101 if (expression != null ) {
101102 return expression .negate ().orElse (null );
102103 }
103104 } else {
104- Predicate left = operator .getChild (0 ).accept (this , null );
105- Predicate right = operator .getChild (1 ).accept (this , null );
105+ Predicate left = operator .getChild (0 ).accept (this , context );
106+ Predicate right = operator .getChild (1 ).accept (this , context );
107+ CompoundPredicateOperator parentPredicateOperator = null ;
108+ if (context != null && context .getParentPredicateOperator () != null ) {
109+ parentPredicateOperator = context .getParentPredicateOperator ();
110+ }
106111 if (left != null && right != null ) {
107112 return (op == CompoundPredicateOperator .CompoundType .OR ) ? PredicateBuilder .or (left , right ) :
108113 PredicateBuilder .and (left , right );
114+ } else if (parentPredicateOperator != null &&
115+ parentPredicateOperator .getCompoundType () == CompoundPredicateOperator .CompoundType .NOT ) {
116+ return null ;
117+ } else if (left != null && op == CompoundPredicateOperator .CompoundType .AND ) {
118+ //if op=and, return the predicates that are not null.
119+ return left ;
120+ } else if (right != null && op == CompoundPredicateOperator .CompoundType .AND ) {
121+ return right ;
109122 }
110123 }
111124 return null ;
112125 }
113126
114127 @ Override
115- public Predicate visitIsNullPredicate (IsNullPredicateOperator operator , Void context ) {
128+ public Predicate visitIsNullPredicate (IsNullPredicateOperator operator , PaimonPredicateContext context ) {
116129 String columnName = getColumnName (operator .getChild (0 ));
117130 if (columnName == null ) {
118131 return null ;
@@ -126,7 +139,7 @@ public Predicate visitIsNullPredicate(IsNullPredicateOperator operator, Void con
126139 }
127140
128141 @ Override
129- public Predicate visitBinaryPredicate (BinaryPredicateOperator operator , Void context ) {
142+ public Predicate visitBinaryPredicate (BinaryPredicateOperator operator , PaimonPredicateContext context ) {
130143 String columnName = getColumnName (operator .getChild (0 ));
131144 if (columnName == null ) {
132145 return null ;
@@ -158,12 +171,12 @@ public Predicate visitBinaryPredicate(BinaryPredicateOperator operator, Void con
158171 }
159172
160173 @ Override
161- public Predicate visitLargeInPredicate (LargeInPredicateOperator operator , Void context ) {
174+ public Predicate visitLargeInPredicate (LargeInPredicateOperator operator , PaimonPredicateContext context ) {
162175 throw new UnsupportedOperationException ("not support large in predicate in the PaimonPredicateConverter" );
163176 }
164177
165178 @ Override
166- public Predicate visitInPredicate (InPredicateOperator operator , Void context ) {
179+ public Predicate visitInPredicate (InPredicateOperator operator , PaimonPredicateContext context ) {
167180 String columnName = getColumnName (operator .getChild (0 ));
168181 if (columnName == null ) {
169182 return null ;
@@ -190,7 +203,7 @@ public Predicate visitInPredicate(InPredicateOperator operator, Void context) {
190203 }
191204
192205 @ Override
193- public Predicate visitLikePredicateOperator (LikePredicateOperator operator , Void context ) {
206+ public Predicate visitLikePredicateOperator (LikePredicateOperator operator , PaimonPredicateContext context ) {
194207 String columnName = getColumnName (operator .getChild (0 ));
195208 if (columnName == null ) {
196209 return null ;
@@ -364,4 +377,4 @@ public String visitCastOperator(CastOperator operator, Void context) {
364377 return operator .getChild (0 ).accept (this , context );
365378 }
366379 }
367- }
380+ }
0 commit comments