solveme commented on code in PR #7532:
URL: https://github.com/apache/ignite-3/pull/7532#discussion_r2798515169


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/externalize/RelJson.java:
##########
@@ -923,6 +955,116 @@ RexNode toRex(RelInput relInput, Object o) {
         }
     }
 
+    private static <C extends Comparable<C>> Sarg<C> sargFromJson(Map<String, 
Object> map, RelDataType type) {
+        final String nullAs = requireNonNull((String) map.get("nullAs"), 
"nullAs");
+        final List<List<String>> rangeSet =
+                requireNonNull((List<List<String>>) map.get("rangeSet"), 
"rangeSet");
+        return Sarg.of(ENUM_BY_NAME.get(nullAs),
+                RelJson.<C>rangeSetFromJson(rangeSet, type));
+    }
+
+    /** Converts a JSON list to a {@link RangeSet} with supplied value typing. 
*/
+    private static <C extends Comparable<C>> RangeSet<C> rangeSetFromJson(
+            List<List<String>> rangeSetsJson, RelDataType type) {
+        final ImmutableRangeSet.Builder<C> builder = 
ImmutableRangeSet.builder();
+        try {
+            rangeSetsJson.forEach(list -> builder.add(rangeFromJson(list, 
type)));
+        } catch (Exception e) {
+            throw new RuntimeException("Error creating RangeSet from JSON: ", 
e);
+        }
+        return builder.build();
+    }
+
+    /**
+     * Creates a {@link Range} from a JSON object.
+     *
+     * <p>The JSON object is as serialized using {@link #toJson(Range)},
+     * e.g. {@code ["[", ")", 10, "-"]}.
+     */
+    private static <C extends Comparable<C>> Range<C> rangeFromJson(
+            List<String> list, RelDataType type) {
+        switch (list.get(0)) {
+            case "all":
+                return Range.all();
+            case "atLeast":
+                return Range.atLeast(rangeEndPointFromJson(list.get(1), type));
+            case "atMost":
+                return Range.atMost(rangeEndPointFromJson(list.get(1), type));
+            case "greaterThan":
+                return Range.greaterThan(rangeEndPointFromJson(list.get(1), 
type));
+            case "lessThan":
+                return Range.lessThan(rangeEndPointFromJson(list.get(1), 
type));
+            case "singleton":
+                return Range.singleton(rangeEndPointFromJson(list.get(1), 
type));
+            case "closed":
+                return Range.closed(rangeEndPointFromJson(list.get(1), type),
+                        rangeEndPointFromJson(list.get(2), type));
+            case "closedOpen":
+                return Range.closedOpen(rangeEndPointFromJson(list.get(1)),
+                        rangeEndPointFromJson(list.get(2), type));
+            case "openClosed":
+                return Range.openClosed(rangeEndPointFromJson(list.get(1)),
+                        rangeEndPointFromJson(list.get(2), type));
+            case "open":
+                return Range.open(rangeEndPointFromJson(list.get(1), type),
+                        rangeEndPointFromJson(list.get(2), type));
+            default:
+                throw new AssertionError("unknown range type " + list.get(0));
+        }
+    }
+
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    @Deprecated
+    private static <C extends Comparable<C>> C rangeEndPointFromJson(Object o) 
{
+        Exception e = null;
+        for (Class clsType : VALUE_CLASSES) {
+            try {
+                return (C) OBJECT_MAPPER.readValue((String) o, clsType);
+            } catch (JsonProcessingException ex) {
+                e = ex;
+            }
+        }
+        throw new RuntimeException(
+                "Error deserializing range endpoint (did not find compatible 
type): ",
+                e);
+    }
+
+    private static <C extends Comparable<C>> C rangeEndPointFromJson(Object o, 
RelDataType type) {
+        Exception e;
+        try {
+            Class clsType = determineRangeEndpointValueClass(type);
+            return (C) OBJECT_MAPPER.readValue((String) o, clsType);
+        } catch (JsonProcessingException ex) {
+            e = ex;
+        }
+        throw new RuntimeException(
+                "Error deserializing range endpoint (did not find compatible 
type): ",
+                e);
+    }
+
+    private static Class determineRangeEndpointValueClass(RelDataType type) {
+        SqlTypeName typeName = RexLiteral.strictTypeName(type);
+        switch (typeName) {
+            case DECIMAL:
+                return BigDecimal.class;
+            case DOUBLE:
+                return Double.class;
+            case CHAR:
+                return NlsString.class;
+            case BOOLEAN:
+                return Boolean.class;
+            case TIMESTAMP:
+                return TimestampString.class;
+            case DATE:
+                return DateString.class;
+            case TIME:
+                return TimeString.class;
+            default:
+                throw new RuntimeException(
+                        "Error deserializing range endpoint (did not find 
compatible type)");
+        }

Review Comment:
   This piece of code is copied as is from Calcite. If type set is not 
sufficient it should be evaluated and  extended in separate task



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to