Welcome to the new DOCS.vastdata.com. We've added new menus and a version selector, plus new persona and resource pages to help customers get started. Looking for a previous article? Use the docs search.

translate

Prev Next

Applies to VAST Cluster 5.5.

Functions › Text basics › Changing text. Replaces single characters: each character of from found in the string becomes the character at the same position in to, or is removed if to has no character at that position. translate has no aliases.

Syntax

translate(s, from, to)
Argument Type Description
s VARCHAR The string to change.
from VARCHAR The characters to replace.
to VARCHAR The replacement for each character of from, by position.

Returns: VARCHAR.

Aliases: none.

Set up the examples

The examples connect with the ADBC driver, as in VAST Query Engine (ADBC), and work in a schema called docs_sql_fn_translate. Set QE_ENDPOINT, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and VASTDB_BUCKET (SQL reference), and run the blocks in order in one Python session. Below each block is the output you should see.

This block connects, defines the run() and plan() helpers, and creates a table of four contacts with phone numbers written in different styles. The last row has no values:

import os
import re

import adbc_driver_manager
import adbc_driver_manager.dbapi as dbapi
import adbc_driver_vastdb

BUCKET = os.environ["VASTDB_BUCKET"]
SCHEMA = f'"{BUCKET}/docs_sql_fn_translate"'

conn = dbapi.connect(
    driver=adbc_driver_vastdb.get_driver_path(),
    db_kwargs={
        "vast.db.endpoint": os.environ["QE_ENDPOINT"],
        "vast.db.access_key": os.environ["AWS_ACCESS_KEY_ID"],
        "vast.db.secret_key": os.environ["AWS_SECRET_ACCESS_KEY"],
    },
    autocommit=True,
)

def server_message(e):
    """The server's message in a driver error, without the request ID or the echoed statement."""
    text = re.sub(r"\[req=0x[0-9a-f]+\]: ", "", str(e))
    found = (re.search(r'message: "(.*?)", request_id', text, re.S)
             or re.search(r"description: (.*)\)", text, re.S))
    message = found.group(1) if found else text.strip().splitlines()[-1]
    message = re.sub(r"^Plan query: '.*?' failed with: ", "", message, flags=re.S)
    message = re.split(r"\\n|\n", message)[0].replace('\\"', '"')
    return message.replace(BUCKET, "<bucket>")

def run(sql):
    """Run one statement. Print the rows it returns, or the error."""
    try:
        with conn.cursor() as cur:
            cur.execute(sql)
            rows = cur.fetchall() if cur.description else []
    except (adbc_driver_manager.Error, OSError) as e:
        print(f"{type(e).__name__}: {server_message(e)}")
    else:
        print(rows)

def plan(sql):
    """Print the plan without its ~N Rows lines: the table's row count, which can still be 0 just after an insert."""
    with conn.cursor() as cur:
        cur.execute("EXPLAIN " + sql)
        steps = cur.fetchall()[0][1]
    print("\n".join(line for line in steps.splitlines() if "Rows" not in line))

TABLE = f"{SCHEMA}.contacts"
run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, name VARCHAR, phone VARCHAR)")
run(f"INSERT INTO {TABLE} VALUES "
    "(1, 'Ada Lovelace', '+44 20 7946 0018'), "
    "(2, 'Grace Hopper', '+1 (555) 010-2368'), "
    "(3, 'José Martí', '+53 7-555-0142'), "
    "(4, NULL, NULL)")
[]
[]
[(4,)]

What it does

translate reads from and to as two lists of characters and pairs them by position: the first character of from with the first of to, and so on. Each character of s that appears in from is replaced by its partner in to; the other characters are copied unchanged.

Call Result Why
translate('Hello', 'el', 'EL') 'HELLo' e becomes E and every l becomes L.
translate('abc', 'abc', 'x') 'x' a becomes x; b and c have no partner in to, so they're removed.
translate('a-b_c', '-_', '') 'abc' With an empty to, every character of from is removed.
translate('abc', 'a', 'xyz') 'xbc' Extra characters in to are ignored.
translate('aaa', 'aa', 'xy') 'xxx' A character listed twice in from uses its first partner.
translate('abc', '', 'x') 'abc' An empty from changes nothing.
translate('Hello', 'h', 'X') 'Hello' The match is case-sensitive: h doesn't match H.
translate('café', 'é', 'e') 'cafe' Characters outside ASCII are translated like any other.
translate('Łódź ø', 'Łø', 'Lo') 'Lódź o' Only the characters listed in from change; ó and ź are copied as they are.

translate works on Unicode code points, not on what a reader sees as one character. In 'e\u0301' (an e followed by the combining accent U+0301, displayed as é), the e is translated and the accent stays, so translating e to E gives É. For the same reason, a precomposed é (U+00E9) in from doesn't match e followed by U+0301 in s: that text is returned unchanged. See length for code points.

run("SELECT translate('e\u0301', 'el', 'EL') = 'E\u0301', length(translate('e\u0301', 'el', 'EL'))")
[(True, 2)]

To replace a substring of several characters as a whole, use replace.

NULLs and edge cases

Input Call Result
s is NULL translate(NULL, 'el', 'EL') NULL
from is NULL translate('abc', NULL, 'x') NULL
to is NULL translate('abc', 'a', NULL) NULL
'' (empty string) translate('', 'el', 'EL') ''
from is '' translate('abc', '', 'x') 'abc', unchanged
to is '' translate('a-b_c', '-_', '') 'abc': every character of from removed
to shorter than from translate('abc', 'abc', 'x') 'x': b and c have no partner and are removed
to longer than from translate('abc', 'a', 'xyz') 'xbc': the extra characters are ignored
A character twice in from translate('aaa', 'aa', 'xy') 'xxx': the first partner wins
A character outside ASCII translate('café', 'é', 'e') 'cafe'
A number as s translate(123, 'el', 'EL') Error: No function matches the given name and argument types
A BLOB as s translate('\xAA'::BLOB, 'el', 'EL') Error: No function matches the given name and argument types

The same cases, as the cluster returns them. They use the table and helpers from Set up the examples:

run("SELECT translate(NULL, 'el', 'EL'), translate('abc', NULL, 'x'), translate('abc', 'a', NULL), translate('', 'el', 'EL')")
run("SELECT translate('abc', '', 'x'), translate('a-b_c', '-_', ''), translate('abc', 'abc', 'x'), translate('abc', 'a', 'xyz'), translate('aaa', 'aa', 'xy'), translate('café', 'é', 'e')")
run("SELECT translate(123, 'el', 'EL')")
run("SELECT translate('\\xAA'::BLOB, 'el', 'EL')")
[(None, None, None, '')]
[('abc', 'abc', 'x', 'xbc', 'xxx', 'cafe')]
InternalError: Binder Error: No function matches the given name and argument types 'translate(INTEGER_LITERAL, STRING_LITERAL, STRING_LITERAL)'. You might need to add explicit type casts.
InternalError: Binder Error: No function matches the given name and argument types 'translate(BLOB, STRING_LITERAL, STRING_LITERAL)'. You might need to add explicit type casts.

Examples

Remove the spaces, brackets and hyphens from each phone number. to is empty, so every character of from is removed:

run(f"SELECT id, translate(phone, ' ()-', '') FROM {TABLE} ORDER BY id LIMIT 10")
[(1, '+442079460018'), (2, '+15550102368'), (3, '+5375550142'), (4, None)]

Replace accented vowels with plain ones, character by character. from and to have the same length, so each accented vowel has a partner:

run(f"SELECT id, translate(name, 'áéíóú', 'aeiou') FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 'Ada Lovelace'), (2, 'Grace Hopper'), (3, 'Jose Marti'), (4, None)]

Find a contact by phone number, whatever punctuation it was stored with:

run(f"SELECT id, name FROM {TABLE} WHERE translate(phone, ' ()-', '') = '+15550102368' ORDER BY id LIMIT 10")
[(2, 'Grace Hopper')]

How it runs in VAST

The Query Engine runs on the cluster's CNodes, and a query on a table starts with a scan in VAST DataBase (VAST_SCAN). VAST DataBase can evaluate a comparison of a column with a constant, but not a function of a column, so translate() is always computed by the Query Engine:

  • In the SELECT list, the scan reads the column and the Query Engine computes translate() for each row it returns.
  • In WHERE, the scan sends the column for every row, and a FILTER step in the Query Engine computes translate() and keeps the rows that match. No rows are skipped in storage.

The plan of the WHERE example shows the FILTER step above the scan:

plan(f"SELECT id FROM {TABLE} WHERE translate(phone, ' ()-', '') = '+15550102368'")
┌───────────────────────────┐
│         PROJECTION        │
│    ────────────────────   │
│             #1            │
│                           │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│           FILTER          │
│    ────────────────────   │
│ (translate(phone, ' ()-', │
│   '') = '+15550102368')   │
│                           │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│         VAST_SCAN         │
│    ────────────────────   │
│    Function: VAST_SCAN    │
│                           │
│        Projections:       │
│           phone           │
│             id            │
│                           │
└───────────────────────────┘

Make it faster

  • Store the cleaned value if you filter on it often. Apply translate() when you write the rows, keep the result in its own column, and compare that column with a constant. The comparison is pushed down to VAST DataBase, so rows that don't match never leave storage. The plan for phone = '+44 20 7946 0018' has no FILTER step:
plan(f"SELECT id FROM {TABLE} WHERE phone = '+44 20 7946 0018'")
┌───────────────────────────┐
│         VAST_SCAN         │
│    ────────────────────   │
│    Function: VAST_SCAN    │
│      Projections: id      │
│                           │
└───────────────────────────┘
  • Combine it with a pushed-down condition. In id > 100 AND translate(phone, ' ()-', '') = '...', id > 100 is still evaluated in VAST DataBase, so translate() runs only on the rows that pass it.

Limitations and errors

  • Strings only. A number, a decimal, a BLOB, a date, a boolean or a list fails with No function matches the given name and argument types. Cast a value to VARCHAR first.
  • One character for one character. A character of from can't become two characters, and a group of characters can't be matched as a whole. Use replace for substrings and regexp_replace for patterns.
  • Case-sensitive. translate('Hello', 'h', 'X') changes nothing. List both cases in from if you need both.
  • Code points, not characters. An accent written as a separate combining character isn't part of the letter before it, and a precomposed é in from doesn't match e followed by U+0301. See What it does. To remove accents without listing them, use strip_accents.
run("SELECT translate(DATE '2026-10-16', '-', '/')")
InternalError: Binder Error: No function matches the given name and argument types 'translate(DATE, STRING_LITERAL, STRING_LITERAL)'. You might need to add explicit type casts.

Similar functions

Function What it replaces Call Result
translate single characters: a becomes x, and b, which has no partner, is removed translate('abcabc', 'ab', 'x') 'xcxc'
replace each occurrence of the substring 'ab' replace('abcabc', 'ab', 'x') 'xcxc'
regexp_replace matches of a regular expression, here with the 'g' option regexp_replace('abcabc', 'ab', 'x', 'g') 'xcxc'
translate each a (the second a in from is ignored) translate('aaa', 'aa', 'xy') 'xxx'
replace 'aa', without overlaps replace('aaa', 'aa', 'b') 'ba'
strip_accents accents, removed from every letter strip_accents('Crème') 'Creme'
run("SELECT translate('abcabc', 'ab', 'x'), replace('abcabc', 'ab', 'x'), regexp_replace('abcabc', 'ab', 'x', 'g')")
run("SELECT translate('aaa', 'aa', 'xy'), replace('aaa', 'aa', 'b'), strip_accents('Crème')")
[('xcxc', 'xcxc', 'xcxc')]
[('xxx', 'ba', 'Creme')]

Clean up:

run(f"DROP TABLE IF EXISTS {TABLE}")
run(f"DROP SCHEMA IF EXISTS {SCHEMA}")
conn.close()
[]
[]

See also