xumingming opened a new pull request, #60178:
URL: https://github.com/apache/spark/pull/60178

   ### What changes were proposed in this pull request?
   
   Generated-IV `aes_encrypt` and seedless `TABLESAMPLE` draw fresh randomness, 
but both reported `deterministic = true`. Rules that trust the flag then 
duplicated them.
   
   - `Sample.deterministic` is `seed.isDefined && child.deterministic`. 
`TABLESAMPLE` without `REPEATABLE` is nondeterministic. `REPEATABLE` and a 
programmatic seed stay deterministic.
   - `AesEncrypt` is nondeterministic when the mode may draw an IV and the IV 
may be missing. CBC and GCM draw; ECB and any other literal mode do not. A 
non-literal mode is treated as drawing. An empty string or empty binary IV is 
missing. A null IV is not: `StaticInvoke` returns null before `aesEncrypt`. A 
non-literal IV may be empty on some rows, so it stays nondeterministic. `Cast`, 
`Collate`, and a foldable deterministic child such as `unhex(...)` or 
`upper('ecb')` are reduced to the literal they produce, because `CheckAnalysis` 
reads the flag before `ConstantFolding`.
   - The same flag is set on the `StaticInvoke` replacement. 
`ReplaceExpressions` substitutes that call before `InlineCTE`.
   - `RewriteWithExpression` treats a deterministic `StaticInvoke` of literals 
as safe to duplicate. A generated-IV `aes_encrypt` keeps its `With`.
   
   ### Why are the changes needed?
   
   https://issues.apache.org/jira/browse/SPARK-60934
   
   `InlineCTE` inlines a multi-reference CTE when the definition is 
deterministic, and `RewriteWithExpression` duplicates a foldable definition at 
each reference. A seedless sample and `aes_encrypt` with no IV were both 
deterministic, so one CTE name became two draws, and a literal `aes_encrypt` 
could be folded once per copy. `rand()` does not have this hole. Marking these 
two nondeterministic makes a multi-reference CTE evaluate once. An explicit IV 
and a seeded sample stay shareable.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, compared with released Spark and current master.
   
   A multi-reference CTE that uses seedless `TABLESAMPLE` or generated-IV 
`aes_encrypt` is evaluated once. Two separate calls still differ.
   
   ```sql
   -- one ciphertext
   WITH c AS (SELECT aes_encrypt('abc', '1234567890123456') AS enc)
   SELECT a.enc = b.enc FROM c a JOIN c b;
   
   -- two ciphertexts
   SELECT aes_encrypt('abc', '1234567890123456') =
          aes_encrypt('abc', '1234567890123456');
   
   -- one sample, so EXCEPT is empty
   WITH src AS (SELECT id FROM range(10000)),
        s AS (SELECT id FROM src TABLESAMPLE (50 PERCENT))
   SELECT * FROM s EXCEPT SELECT * FROM s;
   ```
   
   `JOIN`, `ORDER BY`, and `GROUP BY` reject a generated IV or an empty IV 
(`INVALID_NON_DETERMINISTIC_EXPRESSIONS`). A null IV returns null and is 
allowed there. `unhex(...)` as the IV and `upper('ecb')` as the mode stay 
deterministic. `TABLESAMPLE ... REPEATABLE (seed)` stays deterministic and can 
still be inlined.
   
   ### How was this patch tested?
   
   New and updated tests:
   
   - `InlineCTESuite`: a multi-reference CTE with seedless `TABLESAMPLE` or 
generated-IV `aes_encrypt` is not inlined. `REPEATABLE` and an explicit IV are 
inlined.
   - `RewriteWithExpressionSuite`: a generated IV is not foldable. An explicit 
IV, ECB, a null IV, an empty binary IV, `unhex` / `upper('ecb')`, CBC, and 
lowercase `gcm` take the flag their inputs imply.
   - `SampleSuite`: `None` is nondeterministic, a seed is deterministic, and an 
outer seed does not clear a seedless child.
   - `MiscFunctionsSuite`: a null IV projects null and is accepted in a join. A 
generated-IV CTE referenced twice compares equal, and two separate calls 
compare unequal. Foldable `unhex` and `upper('ecb')` joins are accepted. An 
empty `X''` IV join is rejected.
   - `SQLQuerySuite`: a seedless `TABLESAMPLE` CTE referenced twice keeps both 
copies under the CTE repartition, and `EXCEPT` against itself is empty.
   
   ```
   build/sbt 'catalyst/testOnly 
org.apache.spark.sql.catalyst.optimizer.RewriteWithExpressionSuite 
org.apache.spark.sql.catalyst.plans.logical.SampleSuite 
org.apache.spark.sql.catalyst.optimizer.InlineCTESuite -- -z SPARK-60934'
   build/sbt 'sql/testOnly org.apache.spark.sql.MiscFunctionsSuite -- -z 
SPARK-60934'
   build/sbt 'sql/testOnly org.apache.spark.sql.SQLQuerySuite -- -z SPARK-60934'
   build/sbt 'catalyst/scalastyle' 'catalyst/Test/scalastyle' 
'sql/Test/scalastyle'
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Pi


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