peter-toth commented on code in PR #60738:
URL: https://github.com/apache/spark/pull/60738#discussion_r4645165578


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala:
##########
@@ -162,6 +163,32 @@ object CharVarcharUtils extends Logging with 
SparkCharVarcharUtils {
       .build()
   }
 
+  /**
+   * Adds the CHAR/VARCHAR types of the V2 `columns` to the attributes of 
`output` with the same
+   * names. The query of a write does not have them, see 
`TableOutputResolver.applyColumnMetadata`.
+   * An expression stored with the table, such as a CHECK condition or a 
generation expression,
+   * resolved against the result pads a string literal compared with a CHAR 
column, as the write
+   * pads the stored values. A write that treats CHAR as STRING does not pad 
the values, so `output`
+   * is kept as it is: `ApplyCharTypePadding` skips such a write, but not an 
expression analyzed
+   * under the fixed settings of `StoredExpressionText`.
+   */
+  def withCharVarcharTypes(output: Seq[Attribute], columns: Array[Column]): 
Seq[Attribute] = {
+    val conf = SQLConf.get
+    // standardSemantics takes precedence over legacy charVarcharAsString.
+    if (conf.charVarcharAsString && !conf.charVarcharStandardSemantics) {
+      output

Review Comment:
   **Finding 1.** Under `spark.sql.legacy.charVarcharAsString`, an attribute 
read from a CHAR column still carries its CHAR type in the metadata, and the 
query of the write keeps it. The generation expression is analyzed under the 
fixed settings of `StoredExpressionText`, so `ApplyCharTypePadding` pads 
`'OPEN'` against it. The stored values are not padded, so the check fails for a 
row that Spark itself filled:
   
   ```sql
   SET spark.sql.legacy.charVarcharAsString=true;
   CREATE TABLE t (id INT, status CHAR(10), is_open BOOLEAN GENERATED ALWAYS AS 
(status = 'OPEN'));
   INSERT INTO t (id, status) VALUES (1, 'OPEN');
   -- Fails with CHECK_CONSTRAINT_VIOLATION on master and on this head.
   INSERT INTO t SELECT * FROM t;
   ```
   
   `SELECT id, CAST(status AS STRING), is_open` instead of `*` passes. Removing 
the types here keeps the output of such a write as unpadded as its values:
   
   ```suggestion
         output.map(a => a.withMetadata(cleanMetadata(a.metadata)))
   ```
   
   With it, the copy passes, and `GeneratedColumnWriteSuite` and 
`v2.CheckConstraintSuite` pass. An `INSERT INTO ... SELECT * FROM` the table in 
the new test, after the two inserts, fails without it in the legacy loop. The 
doc comment above would then say that the types are removed rather than kept.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/GeneratedColumnWriteSuite.scala:
##########
@@ -448,6 +448,25 @@ class GeneratedColumnWriteSuite extends QueryTest with 
DatasourceV2SQLBase {
     }
   }
 
+  test("SPARK-62110: generation expression comparing a CHAR column with a 
string literal") {
+    val tblName = "my_tab"
+    // A write pads 'OPEN' to the length of `status`, as it pads the stored 
value, unless the
+    // legacy config makes it store the value without padding.
+    Seq(false, true).foreach { legacy =>
+      withTable(s"testcat.$tblName") {
+        sql(s"""CREATE TABLE testcat.$tblName(
+               |  id INT, status CHAR(10), is_open BOOLEAN GENERATED ALWAYS AS 
(status = 'OPEN')
+               |) USING foo""".stripMargin)
+        withSQLConf(SQLConf.LEGACY_CHAR_VARCHAR_AS_STRING.key -> 
legacy.toString) {
+          sql(s"INSERT INTO testcat.$tblName(id, status) VALUES (1, 'OPEN')")
+          sql(s"INSERT INTO testcat.$tblName VALUES (2, 'OPEN', true)")

Review Comment:
   **Finding 2.** Issue 3 of the description is not checked here. Master 
accepts the `false` row below, so this would pin it:
   
   ```suggestion
             sql(s"INSERT INTO testcat.$tblName VALUES (2, 'OPEN', true)")
             assertWriteFails("CHECK_CONSTRAINT_VIOLATION") {
               sql(s"INSERT INTO testcat.$tblName VALUES (3, 'OPEN', false)")
             }
   ```
   
   It passes on this head in both loops.
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to