githubEdit

Domain Depth

This function returns the number of dot-separated levels in a domain. It extracts the host from a URL and counts the labels. Returns 0 for IP addresses, empty strings, and invalid input. Returns NULL for NULL input.

D SELECT domain_depth('example.com') AS depth;
β”Œβ”€β”€β”€β”€β”€β”€β”€β”
β”‚ depth β”‚
β”‚ int32 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚   2   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”˜

D SELECT domain_depth('https://www.example.com/page') AS depth;
β”Œβ”€β”€β”€β”€β”€β”€β”€β”
β”‚ depth β”‚
β”‚ int32 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚   3   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”˜

D SELECT domain_depth('http://a.b.c.example.co.uk/page') AS depth;
β”Œβ”€β”€β”€β”€β”€β”€β”€β”
β”‚ depth β”‚
β”‚ int32 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚   6   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”˜

Special Cases

  • IP addresses return 0 (both IPv4 and IPv6 β€” they are not domains)

  • Single-label names like localhost return 1

  • Trailing dots are stripped before counting (DNS canonical form)

Last updated

Was this helpful?