Skip to content

Comments

Support VARCHAR/VARBINARY without explicit length#8909

Merged
mrotteveel merged 2 commits intoFirebirdSQL:masterfrom
mrotteveel:varchar-opt-length
Feb 18, 2026
Merged

Support VARCHAR/VARBINARY without explicit length#8909
mrotteveel merged 2 commits intoFirebirdSQL:masterfrom
mrotteveel:varchar-opt-length

Conversation

@mrotteveel
Copy link
Member

@mrotteveel mrotteveel commented Feb 17, 2026

Currently, Firebird doesn't have a default length for VARCHAR/CHARACTER VARYING, { NCHAR | NATIONAL {CHAR|CHARACTER} } VARYING, and VARBINARY/BINARY VARYING.

This PR adds a default length of 255 for all these types.

Why 255:

  • Big enough for common use cases
  • Big enough to cast from other (non-string) scalar types without truncation
  • Small enough to not be excessive in storage use, and fits in an index on page size 8192 with UTF8, with some room to spare (e.g. for compound indices)
  • 0xFF ;)

Why at all:

  • Required by the SQL standard (see below)
  • Allows things like CAST(something as VARCHAR) (see also second point under "Why 255"), which can be a simplification for query generators
  • Can result in simpler code for ad-hoc queries and such

For reference, the SQL:2023 standard requires such a default, as 6.1 <data type> defines:

<character string type> ::=
    [...]
  | CHARACTER VARYING [ <left paren> <character maximum length> <right paren> ]
  | CHAR VARYING [ <left paren> <character maximum length> <right paren> ]
  | VARCHAR [ <left paren> <character maximum length> <right paren> ]
  | [...]

[...]

<binary string type> ::=
    [..]
  | BINARY VARYING [ <left paren> <maximum length> <right paren> ]
  | VARBINARY [ <left paren> <maximum length> <right paren> ]
  | [...]

[...]

<national character string type> ::=
    [...]
  | NATIONAL CHARACTER VARYING [ <left paren> <character maximum length> <right paren> ]
  | NATIONAL CHAR VARYING [ <left paren> <character maximum length> <right paren> ]
  | NCHAR VARYING [ <left paren> <character maximum length> <right paren> ]
  | [...]

With syntax rule 6 (for BINARY) and 7 (for VARCHAR/NCHAR VARYING) specifying:

6) If <maximum length> is omitted, then an implementation-defined (IL007) <maximum length> is implicit.

7) If <character maximum length> is omitted, then an implementation-defined (ID069) <character maximum length> is implicit.

See also thread Introducing a default VARCHAR length on firebird-devel.

@aafemt
Copy link
Contributor

aafemt commented Feb 17, 2026

Instead of doubling branches I would introduce opt_char_length clause that returns 255 on absence and replace with it '(' pos_short_integer ')' in existing code. Did you try it?

@mrotteveel
Copy link
Member Author

Instead of doubling branches I would introduce opt_char_length clause that returns 255 on absence and replace with it '(' pos_short_integer ')' in existing code. Did you try it?

I also have to track FLD_has_len, which is needed for SET BIND (and maybe other things), and this follows the pattern that is also applied for BINARY, CHAR and NCHAR.

@mrotteveel mrotteveel changed the title Support VARCHAR without explicit length Support VARCHAR/VARBINARY without explicit length Feb 17, 2026
@mrotteveel
Copy link
Member Author

@dyemanov Any objections if I merge? (The build failure is a transient failure that occurs regularly on macOS.)

@mrotteveel mrotteveel merged commit 789d467 into FirebirdSQL:master Feb 18, 2026
45 of 46 checks passed
@mrotteveel mrotteveel deleted the varchar-opt-length branch February 18, 2026 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants