This behaves identically to the mysql implementation, namely: - 1-indexed positions - supported negative positions (count from the end of the string) - [optional] len. No len indicates longest substr possible Marks as IR_ALWAYS_INLINE since this is called in other overloads. So the overloads can also replace GetConstFnAttr() calls in codegen.
| 60 | // Marks as IR_ALWAYS_INLINE since this is called in other overloads. |
| 61 | // So the overloads can also replace GetConstFnAttr() calls in codegen. |
| 62 | IR_ALWAYS_INLINE StringVal StringFunctions::Substring(FunctionContext* context, |
| 63 | const StringVal& str, const BigIntVal& pos, const BigIntVal& len) { |
| 64 | if (str.is_null || pos.is_null || len.is_null) return StringVal::null(); |
| 65 | if (context->impl()->GetConstFnAttr(FunctionContextImpl::UTF8_MODE)) { |
| 66 | return Utf8Substring(context, str, pos, len); |
| 67 | } |
| 68 | int fixed_pos = pos.val; |
| 69 | if (fixed_pos < 0) fixed_pos = str.len + fixed_pos + 1; |
| 70 | int max_len = str.len - fixed_pos + 1; |
| 71 | int fixed_len = ::min(static_cast<int>(len.val), max_len); |
| 72 | if (fixed_pos > 0 && fixed_pos <= str.len && fixed_len > 0) { |
| 73 | return StringVal(str.ptr + fixed_pos - 1, fixed_len); |
| 74 | } else { |
| 75 | return StringVal(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | StringVal StringFunctions::Substring(FunctionContext* context, |
| 80 | const StringVal& str, const BigIntVal& pos) { |
no test coverage detected