LENGTHC Implementation

1. Purpose

ivorysql_ora provides Oracle-compatible LENGTHC to count Unicode code points after NFC normalization in UTF8 databases. For examples, see LENGTHC Function.

2. Implementation

2.1. Character Inputs and Encoding Paths

sys.lengthc(text) and the byte- and character-length variants of Oracle-compatible CHAR and VARCHAR2 call ora_lengthc() directly. The C function reads the variable-length string data and preserves fixed-length padding. Casting a CHAR value to text would trim that padding, so these overloads operate on the original value.

The entry point is ora_lengthc(PG_FUNCTION_ARGS) in contrib/ivorysql_ora/src/builtin_functions/character_datatype_functions.c. For UTF8 input it uses PostgreSQL’s src/common/unicode_norm.c to apply Unicode NFC rules for canonical decomposition, ordering, and composition. Other database encodings use direct character counting.

  1. For single-byte database encodings, return the byte count.

  2. For multibyte encodings other than UTF8, count characters with pg_mbstrlen_with_len().

  3. For ASCII-only UTF8 input, return the byte count. For other UTF8 input, count code points, check the allocation limit, and decode into a char32_t array.

  4. Call unicode_is_normalized_quickcheck(UNICODE_NFC, …​) and return the existing count when the check confirms NFC.

  5. Otherwise, call unicode_normalize(UNICODE_NFC, …​) and return the normalized code point count.

NFC can also increase the code point count. For example, U+0958 normalizes to U+0915 followed by U+093C, so LENGTHC returns 2.

2.2. Overloads, Type Conversion, and Indexes

The installation script declares 18 exact overloads. Fifteen count characters and three reject large-object types. For supported non-string inputs, the SQL overloads explicitly cast the value to sys.oravarcharchar before calling the character-counting function.

Input SQL overload argument types Implementation

Character

text, sys.oracharchar, sys.oracharbyte, sys.oravarcharchar, sys.oravarcharbyte

Character overloads bound to ora_lengthc() are STRICT, IMMUTABLE, and PARALLEL SAFE. They support expression indexes on character columns. LONG resolves through its base type, text.

Numeric

integer (int4), numeric, sys.number, sys.binary_double

Count the characters of the converted numeric text.

Datetime

sys.oradate, sys.oratimestamp, sys.oratimestamptz

Text formatting depends on session NLS settings, so these overloads are STABLE. Expression indexes reject them.

Binary

bytea, sys.raw, sys.long_raw

Separate overloads avoid ambiguity with BLOB. The count uses the text generated with bytea_output.

Large objects

sys.clob, sys.nclob, sys.blob

Call ora_lengthc_unsupported() to raise a type error on an exact match, including a NULL of one of these types.

Paths are relative to contrib/ivorysql_ora/.

File Role

src/builtin_functions/character_datatype_functions.c

Character counting, NFC normalization, and large-object type errors.

src/builtin_functions/builtin_functions—​1.0.sql

SQL overloads, type conversions, and volatility declarations.

sql/ora_character_datatype_functions.sql, expected/ora_character_datatype_functions.out

Regression coverage for normalization, padding, null handling, type conversion, large-object rejection, and expression indexes. The counting cases also compare against length(normalize(value, 'NFC')).