danny0405 commented on a change in pull request #2124:
URL: https://github.com/apache/calcite/pull/2124#discussion_r479916462



##########
File path: core/src/main/java/org/apache/calcite/rex/RexLiteral.java
##########
@@ -568,133 +592,149 @@ public void printAsJava(PrintWriter pw) {
    * <li>1234ABCD</li>
    * </ul>
    *
-   * <p>The destination where the value is appended must not incur I/O 
operations. This method is
-   * not meant to be used for writing the values to permanent storage.</p>
-   *
-   * @param value a value to be appended to the provided destination as a Java 
string
-   * @param destination a destination where to append the specified value
-   * @param typeName a type name to be used for the transformation of the 
value to a Java string
-   * @param includeType an indicator whether to include the data type in the 
Java representation
-   * @throws IllegalStateException if the appending to the destination 
<code>Appendable</code> fails
-   *         due to I/O
+   * @param value    Value to be appended to the provided destination as a 
Java string
+   * @param sb       Destination to which to append the specified value
+   * @param typeName Type name to be used for the transformation of the value 
to a Java string
+   * @param type Type to be used for the transformation of the value to a Java 
string
+   * @param includeType Whether to include the data type in the Java 
representation
    */
-  private static void appendAsJava(
-      Comparable value,
-      Appendable destination,
-      SqlTypeName typeName,
-      boolean java, RexDigestIncludeType includeType) {
-    try {
-      switch (typeName) {
-      case CHAR:
-        NlsString nlsString = (NlsString) value;
-        if (java) {
-          Util.printJavaString(
-              destination,
-              nlsString.getValue(),
-              true);
-        } else {
-          boolean includeCharset =
-              (nlsString.getCharsetName() != null)
-                  && !nlsString.getCharsetName().equals(
-                  CalciteSystemProperty.DEFAULT_CHARSET.value());
-          destination.append(nlsString.asSql(includeCharset, false));
-        }
-        break;
-      case BOOLEAN:
-        assert value instanceof Boolean;
-        destination.append(value.toString());
-        break;
-      case DECIMAL:
-        assert value instanceof BigDecimal;
-        destination.append(value.toString());
-        break;
-      case DOUBLE:
-        assert value instanceof BigDecimal;
-        destination.append(Util.toScientificNotation((BigDecimal) value));
-        break;
-      case BIGINT:
-        assert value instanceof BigDecimal;
-        long narrowLong = ((BigDecimal) value).longValue();
-        destination.append(String.valueOf(narrowLong));
-        destination.append('L');
-        break;
-      case BINARY:
-        assert value instanceof ByteString;
-        destination.append("X'");
-        destination.append(((ByteString) value).toString(16));
-        destination.append("'");
-        break;
-      case NULL:
-        assert value == null;
-        destination.append("null");
-        break;
-      case SYMBOL:
-        assert value instanceof Enum;
-        destination.append("FLAG(");
-        destination.append(value.toString());
-        destination.append(")");
-        break;
-      case DATE:
-        assert value instanceof DateString;
-        destination.append(value.toString());
-        break;
-      case TIME:
-        assert value instanceof TimeString;
-        destination.append(value.toString());
-        break;
-      case TIME_WITH_LOCAL_TIME_ZONE:
-        assert value instanceof TimeString;
-        destination.append(value.toString());
-        break;
-      case TIMESTAMP:
-        assert value instanceof TimestampString;
-        destination.append(value.toString());
-        break;
-      case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
-        assert value instanceof TimestampString;
-        destination.append(value.toString());
-        break;
-      case INTERVAL_YEAR:
-      case INTERVAL_YEAR_MONTH:
-      case INTERVAL_MONTH:
-      case INTERVAL_DAY:
-      case INTERVAL_DAY_HOUR:
-      case INTERVAL_DAY_MINUTE:
-      case INTERVAL_DAY_SECOND:
-      case INTERVAL_HOUR:
-      case INTERVAL_HOUR_MINUTE:
-      case INTERVAL_HOUR_SECOND:
-      case INTERVAL_MINUTE:
-      case INTERVAL_MINUTE_SECOND:
-      case INTERVAL_SECOND:
-        assert value instanceof BigDecimal;
-        destination.append(value.toString());
-        break;
-      case MULTISET:
-      case ROW:
-        @SuppressWarnings("unchecked")
-        final List<RexLiteral> list = (List) value;
-        destination.append(
-            (new AbstractList<String>() {
-              public String get(int index) {
-                return list.get(index).computeDigest(includeType);
-              }
-
-              public int size() {
-                return list.size();
-              }
-            }).toString());
-        break;
-      case GEOMETRY:
-        final String wkt = GeoFunctions.ST_AsWKT((Geometries.Geom) value);
-        destination.append(wkt);
-        break;
-      default:
-        assert valueMatchesType(value, typeName, true);
-        throw Util.needToImplement(typeName);
+  private static void appendAsJava(Comparable value, StringBuilder sb,
+      SqlTypeName typeName, RelDataType type, boolean java,
+      RexDigestIncludeType includeType) {
+    switch (typeName) {
+    case CHAR:
+      NlsString nlsString = (NlsString) value;
+      if (java) {
+        Util.printJavaString(
+            sb,
+            nlsString.getValue(),
+            true);
+      } else {
+        boolean includeCharset =
+            (nlsString.getCharsetName() != null)
+                && !nlsString.getCharsetName().equals(
+                CalciteSystemProperty.DEFAULT_CHARSET.value());
+        sb.append(nlsString.asSql(includeCharset, false));
       }
-    } catch (IOException e) {
-      throw new IllegalStateException("The destination Appendable should not 
incur I/O.", e);
+      break;
+    case BOOLEAN:
+      assert value instanceof Boolean;
+      sb.append(value.toString());
+      break;
+    case DECIMAL:
+      assert value instanceof BigDecimal;
+      sb.append(value.toString());
+      break;
+    case DOUBLE:
+      assert value instanceof BigDecimal;
+      sb.append(Util.toScientificNotation((BigDecimal) value));
+      break;
+    case BIGINT:
+      assert value instanceof BigDecimal;
+      long narrowLong = ((BigDecimal) value).longValue();
+      sb.append(String.valueOf(narrowLong));
+      sb.append('L');
+      break;
+    case BINARY:
+      assert value instanceof ByteString;
+      sb.append("X'");
+      sb.append(((ByteString) value).toString(16));
+      sb.append("'");
+      break;
+    case NULL:
+      assert value == null;
+      sb.append("null");
+      break;
+    case SARG:
+      assert value instanceof Sarg;
+      //noinspection unchecked,rawtypes
+      Util.asStringBuilder(sb, sb2 ->
+          printSarg(sb2, (Sarg) value, type));
+      break;
+    case SYMBOL:
+      assert value instanceof Enum;
+      sb.append("FLAG(");
+      sb.append(value.toString());
+      sb.append(")");
+      break;
+    case DATE:
+      assert value instanceof DateString;
+      sb.append(value.toString());
+      break;
+    case TIME:
+    case TIME_WITH_LOCAL_TIME_ZONE:
+      assert value instanceof TimeString;
+      sb.append(value.toString());
+      break;
+    case TIMESTAMP:
+    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+      assert value instanceof TimestampString;
+      sb.append(value.toString());
+      break;
+    case INTERVAL_YEAR:
+    case INTERVAL_YEAR_MONTH:
+    case INTERVAL_MONTH:
+    case INTERVAL_DAY:
+    case INTERVAL_DAY_HOUR:
+    case INTERVAL_DAY_MINUTE:
+    case INTERVAL_DAY_SECOND:
+    case INTERVAL_HOUR:
+    case INTERVAL_HOUR_MINUTE:
+    case INTERVAL_HOUR_SECOND:
+    case INTERVAL_MINUTE:
+    case INTERVAL_MINUTE_SECOND:
+    case INTERVAL_SECOND:
+      assert value instanceof BigDecimal;
+      sb.append(value.toString());
+      break;
+    case MULTISET:
+    case ROW:
+      final List<RexLiteral> list = (List) value;
+      Util.asStringBuilder(sb, sb2 ->
+          Util.printList(sb, list.size(), (sb3, i) ->
+              sb3.append(list.get(i).computeDigest(includeType))));
+      break;
+    case GEOMETRY:
+      final String wkt = GeoFunctions.ST_AsWKT((Geometries.Geom) value);
+      sb.append(wkt);
+      break;
+    default:
+      assert valueMatchesType(value, typeName, true);
+      throw Util.needToImplement(typeName);
+    }
+  }
+
+  private static <C extends Comparable<C>> void printSarg(StringBuilder sb,
+      Sarg<C> sarg, RelDataType type) {
+    sarg.printTo(sb, (sb2, value) ->
+        sb2.append(toLiteral(type, value)));
+  }
+
+  /** Converts a value to a temporary literal, for the purposes of generating a
+   * digest. Literals of type ROW and MULTISET require that their components 
are
+   * also literals. */
+  private static RexLiteral toLiteral(RelDataType type, Comparable<?> value) {
+    final SqlTypeName typeName = strictTypeName(type);
+    switch (typeName) {

Review comment:
       Seems an useless call of `strictTypeName(type)`.

##########
File path: core/src/main/java/org/apache/calcite/rex/RexLiteral.java
##########
@@ -568,133 +592,149 @@ public void printAsJava(PrintWriter pw) {
    * <li>1234ABCD</li>
    * </ul>
    *
-   * <p>The destination where the value is appended must not incur I/O 
operations. This method is
-   * not meant to be used for writing the values to permanent storage.</p>
-   *
-   * @param value a value to be appended to the provided destination as a Java 
string
-   * @param destination a destination where to append the specified value
-   * @param typeName a type name to be used for the transformation of the 
value to a Java string
-   * @param includeType an indicator whether to include the data type in the 
Java representation
-   * @throws IllegalStateException if the appending to the destination 
<code>Appendable</code> fails
-   *         due to I/O
+   * @param value    Value to be appended to the provided destination as a 
Java string
+   * @param sb       Destination to which to append the specified value
+   * @param typeName Type name to be used for the transformation of the value 
to a Java string
+   * @param type Type to be used for the transformation of the value to a Java 
string
+   * @param includeType Whether to include the data type in the Java 
representation
    */
-  private static void appendAsJava(
-      Comparable value,
-      Appendable destination,
-      SqlTypeName typeName,
-      boolean java, RexDigestIncludeType includeType) {
-    try {
-      switch (typeName) {
-      case CHAR:
-        NlsString nlsString = (NlsString) value;
-        if (java) {
-          Util.printJavaString(
-              destination,
-              nlsString.getValue(),
-              true);
-        } else {
-          boolean includeCharset =
-              (nlsString.getCharsetName() != null)
-                  && !nlsString.getCharsetName().equals(
-                  CalciteSystemProperty.DEFAULT_CHARSET.value());
-          destination.append(nlsString.asSql(includeCharset, false));
-        }
-        break;
-      case BOOLEAN:
-        assert value instanceof Boolean;
-        destination.append(value.toString());
-        break;
-      case DECIMAL:
-        assert value instanceof BigDecimal;
-        destination.append(value.toString());
-        break;
-      case DOUBLE:
-        assert value instanceof BigDecimal;
-        destination.append(Util.toScientificNotation((BigDecimal) value));
-        break;
-      case BIGINT:
-        assert value instanceof BigDecimal;
-        long narrowLong = ((BigDecimal) value).longValue();
-        destination.append(String.valueOf(narrowLong));
-        destination.append('L');
-        break;
-      case BINARY:
-        assert value instanceof ByteString;
-        destination.append("X'");
-        destination.append(((ByteString) value).toString(16));
-        destination.append("'");
-        break;
-      case NULL:
-        assert value == null;
-        destination.append("null");
-        break;
-      case SYMBOL:
-        assert value instanceof Enum;
-        destination.append("FLAG(");
-        destination.append(value.toString());
-        destination.append(")");
-        break;
-      case DATE:
-        assert value instanceof DateString;
-        destination.append(value.toString());
-        break;
-      case TIME:
-        assert value instanceof TimeString;
-        destination.append(value.toString());
-        break;
-      case TIME_WITH_LOCAL_TIME_ZONE:
-        assert value instanceof TimeString;
-        destination.append(value.toString());
-        break;
-      case TIMESTAMP:
-        assert value instanceof TimestampString;
-        destination.append(value.toString());
-        break;
-      case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
-        assert value instanceof TimestampString;
-        destination.append(value.toString());
-        break;
-      case INTERVAL_YEAR:
-      case INTERVAL_YEAR_MONTH:
-      case INTERVAL_MONTH:
-      case INTERVAL_DAY:
-      case INTERVAL_DAY_HOUR:
-      case INTERVAL_DAY_MINUTE:
-      case INTERVAL_DAY_SECOND:
-      case INTERVAL_HOUR:
-      case INTERVAL_HOUR_MINUTE:
-      case INTERVAL_HOUR_SECOND:
-      case INTERVAL_MINUTE:
-      case INTERVAL_MINUTE_SECOND:
-      case INTERVAL_SECOND:
-        assert value instanceof BigDecimal;
-        destination.append(value.toString());
-        break;
-      case MULTISET:
-      case ROW:
-        @SuppressWarnings("unchecked")
-        final List<RexLiteral> list = (List) value;
-        destination.append(
-            (new AbstractList<String>() {
-              public String get(int index) {
-                return list.get(index).computeDigest(includeType);
-              }
-
-              public int size() {
-                return list.size();
-              }
-            }).toString());
-        break;
-      case GEOMETRY:
-        final String wkt = GeoFunctions.ST_AsWKT((Geometries.Geom) value);
-        destination.append(wkt);
-        break;
-      default:
-        assert valueMatchesType(value, typeName, true);
-        throw Util.needToImplement(typeName);
+  private static void appendAsJava(Comparable value, StringBuilder sb,
+      SqlTypeName typeName, RelDataType type, boolean java,
+      RexDigestIncludeType includeType) {
+    switch (typeName) {
+    case CHAR:
+      NlsString nlsString = (NlsString) value;
+      if (java) {
+        Util.printJavaString(
+            sb,
+            nlsString.getValue(),
+            true);
+      } else {
+        boolean includeCharset =
+            (nlsString.getCharsetName() != null)
+                && !nlsString.getCharsetName().equals(
+                CalciteSystemProperty.DEFAULT_CHARSET.value());
+        sb.append(nlsString.asSql(includeCharset, false));
       }
-    } catch (IOException e) {
-      throw new IllegalStateException("The destination Appendable should not 
incur I/O.", e);
+      break;
+    case BOOLEAN:
+      assert value instanceof Boolean;
+      sb.append(value.toString());
+      break;
+    case DECIMAL:
+      assert value instanceof BigDecimal;
+      sb.append(value.toString());
+      break;
+    case DOUBLE:
+      assert value instanceof BigDecimal;
+      sb.append(Util.toScientificNotation((BigDecimal) value));
+      break;
+    case BIGINT:
+      assert value instanceof BigDecimal;
+      long narrowLong = ((BigDecimal) value).longValue();
+      sb.append(String.valueOf(narrowLong));
+      sb.append('L');
+      break;
+    case BINARY:
+      assert value instanceof ByteString;
+      sb.append("X'");
+      sb.append(((ByteString) value).toString(16));
+      sb.append("'");
+      break;
+    case NULL:
+      assert value == null;
+      sb.append("null");
+      break;
+    case SARG:
+      assert value instanceof Sarg;
+      //noinspection unchecked,rawtypes
+      Util.asStringBuilder(sb, sb2 ->
+          printSarg(sb2, (Sarg) value, type));
+      break;
+    case SYMBOL:
+      assert value instanceof Enum;
+      sb.append("FLAG(");
+      sb.append(value.toString());
+      sb.append(")");
+      break;
+    case DATE:
+      assert value instanceof DateString;
+      sb.append(value.toString());
+      break;
+    case TIME:
+    case TIME_WITH_LOCAL_TIME_ZONE:
+      assert value instanceof TimeString;
+      sb.append(value.toString());
+      break;
+    case TIMESTAMP:
+    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+      assert value instanceof TimestampString;
+      sb.append(value.toString());
+      break;
+    case INTERVAL_YEAR:
+    case INTERVAL_YEAR_MONTH:
+    case INTERVAL_MONTH:
+    case INTERVAL_DAY:
+    case INTERVAL_DAY_HOUR:
+    case INTERVAL_DAY_MINUTE:
+    case INTERVAL_DAY_SECOND:
+    case INTERVAL_HOUR:
+    case INTERVAL_HOUR_MINUTE:
+    case INTERVAL_HOUR_SECOND:
+    case INTERVAL_MINUTE:
+    case INTERVAL_MINUTE_SECOND:
+    case INTERVAL_SECOND:
+      assert value instanceof BigDecimal;
+      sb.append(value.toString());
+      break;
+    case MULTISET:
+    case ROW:
+      final List<RexLiteral> list = (List) value;
+      Util.asStringBuilder(sb, sb2 ->
+          Util.printList(sb, list.size(), (sb3, i) ->
+              sb3.append(list.get(i).computeDigest(includeType))));
+      break;
+    case GEOMETRY:
+      final String wkt = GeoFunctions.ST_AsWKT((Geometries.Geom) value);
+      sb.append(wkt);
+      break;
+    default:
+      assert valueMatchesType(value, typeName, true);
+      throw Util.needToImplement(typeName);
+    }
+  }
+
+  private static <C extends Comparable<C>> void printSarg(StringBuilder sb,
+      Sarg<C> sarg, RelDataType type) {
+    sarg.printTo(sb, (sb2, value) ->
+        sb2.append(toLiteral(type, value)));
+  }
+
+  /** Converts a value to a temporary literal, for the purposes of generating a
+   * digest. Literals of type ROW and MULTISET require that their components 
are
+   * also literals. */
+  private static RexLiteral toLiteral(RelDataType type, Comparable<?> value) {
+    final SqlTypeName typeName = strictTypeName(type);
+    switch (typeName) {

Review comment:
       Seems a useless call of `strictTypeName(type)`.




----------------------------------------------------------------
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.

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


Reply via email to