dongjoon-hyun commented on code in PR #60738:
URL: https://github.com/apache/spark/pull/60738#discussion_r4645347961


##########
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:
   Thank you, confirmed. `INSERT INTO t SELECT * FROM t` skips 
`ResolveOutputRelation`, because the query output already matches the table, 
CHAR type included, so its attributes keep the CHAR type of the read. I removed 
the types in the legacy branch as suggested and updated the doc comment. The 
new test now copies the rows of the table at the end, which fails without it in 
the legacy loop.
   
   This also changes a CHECK read from `predicateSql` under 
`charVarcharAsString`. It now compares without padding, as a CHECK converted 
back from `predicate()` already does in that session, so I mentioned it in the 
PR description. In turn, a row that a session without the legacy config stored 
padded, such as `('OPEN      ', true)`, fails when a legacy session copies it, 
as `SELECT status = 'OPEN'` returns `false` in that session.
   



##########
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:
   Added. It passes in both loops, and master accepts the row.
   



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