2626import org .apache .calcite .rex .RexNode ;
2727import org .apache .calcite .runtime .SqlFunctions ;
2828import org .apache .calcite .sql .SqlKind ;
29+ import org .apache .calcite .sql .type .SqlTypeName ;
2930import org .apache .calcite .util .DateString ;
3031import org .apache .calcite .util .NlsString ;
3132import org .apache .wayang .basic .data .Record ;
3738
3839public class FilterPredicateImpl implements FunctionDescriptor .SerializablePredicate <Record > {
3940 class FilterCallTreeFactory implements CallTreeFactory {
40- public SerializableFunction <List <Object >, Object > deriveOperation (final SqlKind kind ) {
41+ public SerializableFunction <List <Object >, Object > deriveOperation (final SqlKind kind , final SqlTypeName returnType ) {
4142 return input -> switch (kind ) {
4243 case NOT -> !(boolean ) input .get (0 );
4344 case IS_NOT_NULL -> !isEqualTo (input .get (0 ), null );
@@ -55,6 +56,8 @@ public SerializableFunction<List<Object>, Object> deriveOperation(final SqlKind
5556 case OR -> input .stream ().anyMatch (obj -> Boolean .class .cast (obj ).booleanValue ());
5657 case MINUS -> widenToDouble .apply (input .get (0 )) - widenToDouble .apply (input .get (1 ));
5758 case PLUS -> widenToDouble .apply (input .get (0 )) + widenToDouble .apply (input .get (1 ));
59+ // TODO: may need better support for CASTing in the future. See sqlCast() in this file.
60+ case CAST -> input .get (0 ) instanceof Number ? widenToDouble .apply (input .get (0 )) : ensureComparable .apply (input .get (0 ));
5861 case SEARCH -> {
5962 if (input .get (0 ) instanceof final ImmutableRangeSet range ) {
6063 assert input .get (1 ) instanceof Comparable
@@ -81,14 +84,24 @@ public SerializableFunction<List<Object>, Object> deriveOperation(final SqlKind
8184 };
8285 }
8386
87+ /**
88+ * Java implementation of SQL cast.
89+ * @param input input field
90+ * @param type the new return type of the field
91+ * @return Java-type equivalent to {@link SqlTypeName} counterpart.
92+ */
93+ private static Object sqlCast (Object input , SqlTypeName type ){
94+ throw new UnsupportedOperationException ("sqlCasting is not yet implemented." );
95+ }
96+
8497 /**
8598 * Java equivalent of SQL like clauses
8699 *
87100 * @param s1
88101 * @param s2
89102 * @return true if {@code s1} like {@code s2}
90103 */
91- private boolean like (final String s1 , final String s2 ) {
104+ private static boolean like (final String s1 , final String s2 ) {
92105 return new SqlFunctions .LikeFunction ().like (s1 , s2 );
93106 }
94107
@@ -99,7 +112,7 @@ private boolean like(final String s1, final String s2) {
99112 * @param o2
100113 * @return true if {@code o1 > o2}
101114 */
102- private boolean isGreaterThan (final Object o1 , final Object o2 ) {
115+ private static boolean isGreaterThan (final Object o1 , final Object o2 ) {
103116 return ensureComparable .apply (o1 ).compareTo (ensureComparable .apply (o2 )) > 0 ;
104117 }
105118
@@ -110,7 +123,7 @@ private boolean isGreaterThan(final Object o1, final Object o2) {
110123 * @param o2
111124 * @return true if {@code o1 < o2}
112125 */
113- private boolean isLessThan (final Object o1 , final Object o2 ) {
126+ private static boolean isLessThan (final Object o1 , final Object o2 ) {
114127 return ensureComparable .apply (o1 ).compareTo (ensureComparable .apply (o2 )) < 0 ;
115128 }
116129
@@ -121,7 +134,7 @@ private boolean isLessThan(final Object o1, final Object o2) {
121134 * @param o2
122135 * @return true if {@code o1 == o2}
123136 */
124- private boolean isEqualTo (final Object o1 , final Object o2 ) {
137+ private static boolean isEqualTo (final Object o1 , final Object o2 ) {
125138 return Objects .equals (ensureComparable .apply (o1 ), ensureComparable .apply (o2 ));
126139 }
127140 }
@@ -133,7 +146,7 @@ private boolean isEqualTo(final Object o1, final Object o2) {
133146 *
134147 * @throws UnsupportedOperationException if conversion was not possible
135148 */
136- final SerializableFunction <Object , Double > widenToDouble = field -> {
149+ final static SerializableFunction <Object , Double > widenToDouble = field -> {
137150 if (field instanceof final Number number ) {
138151 return number .doubleValue ();
139152 } else if (field instanceof final Date date ) {
@@ -148,7 +161,7 @@ private boolean isEqualTo(final Object o1, final Object o2) {
148161 /**
149162 * Widening conversions, all numbers to double
150163 */
151- final SerializableFunction <Object , Comparable > ensureComparable = field -> {
164+ final static SerializableFunction <Object , Comparable > ensureComparable = field -> {
152165 if (field instanceof final Number number ) {
153166 return number .doubleValue ();
154167 } else if (field instanceof final Date date ) {
0 commit comments