vlsi commented on code in PR #5281:
URL: https://github.com/apache/calcite/pull/5281#discussion_r4348505488


##########
core/src/test/java/org/apache/calcite/sql/type/SqlTypeFactoryTest.java:
##########
@@ -36,16 +40,57 @@
 import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.hasToString;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
+import static java.util.Objects.requireNonNull;
+
 /**
  * Test for {@link SqlTypeFactoryImpl}.
  */
 class SqlTypeFactoryTest {
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7807";>[CALCITE-7807]
+   * Reuse unchanged SQL types during least-restrictive inference</a>. */
+  @Test void testUnchangedCharsetAndCollationReturnsSameType() {
+    final BasicSqlType type = (BasicSqlType) new SqlTypeFixture().sqlVarchar;
+    final Charset charset = requireNonNull(type.getCharset());
+    final SqlCollation collation = requireNonNull(type.getCollation());
+
+    assertSame(type,
+        type.createWithCharsetAndCollation(charset, collation),
+        "createWithCharsetAndCollation(" + charset + ", " + collation + ")");
+  }
+
+  @Test void testDifferentCharsetIsAppliedToNewType() {
+    final BasicSqlType type = (BasicSqlType) new SqlTypeFixture().sqlVarchar;
+    final BasicSqlType utf16Type =
+        type.createWithCharsetAndCollation(StandardCharsets.UTF_16, 
SqlCollation.IMPLICIT);
+
+    assertEquals(StandardCharsets.UTF_16, utf16Type.getCharset());
+    assertSame(SqlCollation.IMPLICIT, utf16Type.getCollation());
+  }
+
+  /** {@link SqlCollation#equals} ignores coercibility, so COERCIBLE equals
+   * IMPLICIT; the type must still be rebuilt. */
+  @Test void testCollationDifferingOnlyInCoercibilityIsNotReused() {
+    final BasicSqlType type =
+        new BasicSqlType(RelDataTypeSystem.DEFAULT, SqlTypeName.VARCHAR, 10)
+            .createWithCharsetAndCollation(
+                StandardCharsets.ISO_8859_1, SqlCollation.IMPLICIT);
+    final Charset charset = requireNonNull(type.getCharset());
+    final BasicSqlType coercible =
+        type.createWithCharsetAndCollation(charset, SqlCollation.COERCIBLE);
+
+    assertSame(SqlCollation.COERCIBLE, coercible.getCollation());

Review Comment:
   `SqlCollation.toString()` prints only `"COLLATE " + collationName`, and 
`IMPLICIT` and `COERCIBLE` have the same name. If this fails, both sides print 
as the same `COLLATE ...` text, and the failure does not show the coercibility. 
This amends my earlier suggestion: assert the coercibility instead, so a 
failure prints `expected: <COERCIBLE> but was: <IMPLICIT>`:
   
   ```java
       assertEquals(SqlCollation.Coercibility.COERCIBLE,
           requireNonNull(coercible.getCollation()).getCoercibility());
   ```



##########
core/src/test/java/org/apache/calcite/sql/type/SqlTypeFactoryTest.java:
##########
@@ -36,16 +40,57 @@
 import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.hasToString;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
+import static java.util.Objects.requireNonNull;
+
 /**
  * Test for {@link SqlTypeFactoryImpl}.
  */
 class SqlTypeFactoryTest {
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7807";>[CALCITE-7807]
+   * Reuse unchanged SQL types during least-restrictive inference</a>. */
+  @Test void testUnchangedCharsetAndCollationReturnsSameType() {
+    final BasicSqlType type = (BasicSqlType) new SqlTypeFixture().sqlVarchar;
+    final Charset charset = requireNonNull(type.getCharset());
+    final SqlCollation collation = requireNonNull(type.getCollation());
+
+    assertSame(type,
+        type.createWithCharsetAndCollation(charset, collation),
+        "createWithCharsetAndCollation(" + charset + ", " + collation + ")");
+  }
+
+  @Test void testDifferentCharsetIsAppliedToNewType() {
+    final BasicSqlType type = (BasicSqlType) new SqlTypeFixture().sqlVarchar;

Review Comment:
   This base type has the same problem the coercibility test had: `sqlVarchar` 
comes from `DATATYPE_CACHE`, so it may be a `COERCIBLE` `VARCHAR NOT NULL` 
interned by an earlier test. Then `collation == this.collation` is false for 
the `IMPLICIT` argument, the slow path runs, and the test passes even if the 
fast path stops checking the charset. Of the new tests, this is the only one 
that catches a dropped charset check. Please build the base type outside the 
cache, as in `testCollationDifferingOnlyInCoercibilityIsNotReused`.



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