r33s3n6 opened a new issue, #68716:
URL: https://github.com/apache/doris/issues/68716

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   ### Version
   
   4.1.4 (doris-4.1.4-rc04-ad35a140c7f), single FE + single BE
   
   ### What's Wrong?
   
   `substring_index('aaa', 'aa', -1)` returns `a` when FE folds the constants 
and an empty string when BE computes it from columns.
   
   FE folding: `a`. BE (column arguments, or `debug_skip_fold_constant = 
true`): empty string.
   
   ```
   SET enable_sql_cache = false
   
   SELECT substring_index('aaa', 'aa', -1) AS fe, substring_index(s1, s2, -1) 
AS be FROM t
   
   +------+------+
   | fe   | be   |
   +------+------+
   | a    |      |
   +------+------+
   
   SET debug_skip_fold_constant = true
   
   SELECT substring_index('aaa', 'aa', -1)
   
   +----------------------------------+
   | substring_index('aaa', 'aa', -1) |
   +----------------------------------+
   |                                  |
   +----------------------------------+
   ```
   
   - **FE constant folding**: all arguments are literals, so Nereids folds the 
call during planning. Check: EXPLAIN shows the literal in place of the call: 
`final projections: 'a', substring_index(s1[#1], s2[#2], -1)`
   - **BE execution**: the arguments are columns (or `SET 
debug_skip_fold_constant = true`), so BE computes the call. Check: EXPLAIN 
keeps the call.
   
   ### What You Expected?
   
   FE constant folding and BE give the same result for the same arguments.
   
   ### How to Reproduce?
   
   Deployment: single FE + single BE, default session variables.
   
   ```sql
   CREATE TABLE t(id INT, s1 STRING, s2 STRING)
     DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES('replication_num' = '1');
   INSERT INTO t VALUES (1, 'aaa', 'aa');
   
   SET enable_sql_cache = false;
   SELECT substring_index('aaa', 'aa', -1) AS fe, substring_index(s1, s2, -1) 
AS be FROM t;
   SET debug_skip_fold_constant = true;
   SELECT substring_index('aaa', 'aa', -1);
   ```
   
   ### Anything Else?
   
   FE splits the string with `String.split` from the left, without overlapping 
matches; BE looks for the delimiter with `rfind` from the right, so a delimiter 
that overlaps itself is cut at a different position.
   
   - FE: 
[fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java#L801-L806](https://github.com/apache/doris/blob/4.1.4/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java#L801-L806)
 — `substringIndex`: `split(Pattern.quote(delim), -1)`
   - BE: 
[be/src/exprs/function/function_string_search.cpp#L520-L532](https://github.com/apache/doris/blob/4.1.4/be/src/exprs/function/function_string_search.cpp#L520-L532)
 — negative count: `rfind` from the right
   
   Found with AI assistance.
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


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