Applies to VAST Cluster 5.5.
Functions › Trimming and padding › Trimming and padding. rpad(s, n, fill) returns s padded on the right with fill, repeated as often as needed, until it is n characters long. A string longer than n is cut to its first n characters.
Syntax
rpad(s, n, fill)
| Argument | Type | Description |
|---|---|---|
s |
VARCHAR |
The string to pad. |
n |
INTEGER |
The length of the result, in characters (Unicode code points). |
fill |
VARCHAR |
The characters to add on the right, repeated and cut to fit. |
Returns: VARCHAR.
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_string_rpad. 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 five parts with a code and a label. 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_string_rpad"'
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}.parts"
run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, code VARCHAR, label VARCHAR)")
run(f"INSERT INTO {TABLE} VALUES "
"(1, '42', 'Bolt'), "
"(2, '7', 'Nut'), "
"(3, '123456', 'Washer'), "
"(4, 'caf\u00e9', 'Caf\u00e9 table'), "
"(5, NULL, NULL)")
[]
[]
[(5,)]
What it does
rpad measures s in characters (Unicode code points), then:
| Case | What happens | Example | Result |
|---|---|---|---|
s is shorter than n |
fill is added on the right, repeated and cut so the result is exactly n characters |
rpad('abc', 8, 'xyz') |
'abcxyzxy' |
s is n characters |
s unchanged |
rpad('abc', 3, '*') |
'abc' |
s is longer than n |
s is cut to its first n characters |
rpad('abc', 2, '*') |
'ab' |
n is 0 or negative |
an empty string | rpad('abc', -1, '*') |
'' |
A character is a code point, not what a reader sees: e followed by a combining accent (U+0301) counts as two, and so does a flag emoji. A cut can split such a pair, in s or in fill, and leave a bare e. All three arguments are required; for spaces, pass ' ' as the fill.
NULLs and edge cases
| Input | Result |
|---|---|
NULL as any argument |
NULL |
rpad('', 3, '0') |
'000' |
rpad('abc', 5, '') (empty fill, padding needed) |
Error: Insufficient padding in RPAD. |
rpad('abc', 3, ''), rpad('abc', 2, '') (no padding needed) |
'abc', 'ab' |
rpad('é', 3, '*') (precomposed) |
'é**' |
rpad('e\u0301', 3, '*') (e and a combining accent) |
e and the accent, then '*' |
rpad('abc', 4, 'e\u0301') (the fill is cut after e) |
'abce' |
rpad('abc', '5', '*') (n as a string) |
'abc**' |
rpad('abc', 2.5, '*') |
Error: No function matches the given name and argument types 'rpad(STRING_LITERAL, DECIMAL(2,1), STRING_LITERAL)' |
rpad(42, 5, '0') (a number as s) |
Error: No function matches the given name and argument types 'rpad(INTEGER_LITERAL, INTEGER_LITERAL, STRING_LITERAL)' |
The same cases, as the cluster returns them. They use the table and helpers from Set up the examples:
run("SELECT rpad(NULL, 5, '*'), rpad('abc', NULL, '*'), rpad('abc', 5, NULL), rpad('', 3, '0')")
run("SELECT rpad('abc', 5, '')")
run("SELECT rpad('abc', 3, ''), rpad('abc', 2, '')")
run("SELECT rpad('\u00e9', 3, '*'), rpad('e\u0301', 3, '*'), rpad('abc', 4, 'e\u0301'), rpad('abc', '5', '*')")
run("SELECT rpad('abc', 2.5, '*')")
run("SELECT rpad(42, 5, '0')")
[(None, None, None, '000')]
OSError: Insufficient padding in RPAD.
[('abc', 'ab')]
[('é**', 'é*', 'abce', 'abc**')]
InternalError: Binder Error: No function matches the given name and argument types 'rpad(STRING_LITERAL, DECIMAL(2,1), STRING_LITERAL)'. You might need to add explicit type casts.
InternalError: Binder Error: No function matches the given name and argument types 'rpad(INTEGER_LITERAL, INTEGER_LITERAL, STRING_LITERAL)'. You might need to add explicit type casts.
Examples
Pad each label with dots to a fixed width of 12, as in a table of contents, and each code with spaces to six characters. The | shows where each value ends:
run(f"SELECT id, rpad(label, 12, '.') || '|', rpad(code, 6, ' ') || '|' FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 'Bolt........|', '42 |'), (2, 'Nut.........|', '7 |'), (3, 'Washer......|', '123456|'), (4, 'Café table..|', 'café |'), (5, None, None)]
Cut every label to its first four characters. rpad cuts a longer string, so with a width shorter than every value it works like left, except that it also pads the shorter ones:
run(f"SELECT id, rpad(label, 4, '_'), left(label, 4) FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 'Bolt', 'Bolt'), (2, 'Nut_', 'Nut'), (3, 'Wash', 'Wash'), (4, 'Café', 'Café'), (5, None, None)]
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 rpad() is always computed by the Query Engine:
- In the
SELECTlist, the scan reads the column and the Query Engine pads each value it returns. - In
WHERE, the scan sends the column for every row, and aFILTERstep in the Query Engine computesrpad()and keeps the rows that match.
The plan of a padded comparison shows the FILTER step above the scan:
plan(f"SELECT id FROM {TABLE} WHERE rpad(code, 5, '0') = '42000'")
┌───────────────────────────┐
│ PROJECTION │
│ ──────────────────── │
│ #1 │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ FILTER │
│ ──────────────────── │
│ (rpad(code, 5, '0') = │
│ '42000') │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ │
│ Projections: │
│ code │
│ id │
│ │
└───────────────────────────┘
Make it faster
- Compare the raw column.
code = '42'is pushed down to VAST DataBase, so rows that don't match never leave storage;rpad(code, 5, '0') = '42000'isn't. The plan has noFILTERstep:
plan(f"SELECT id FROM {TABLE} WHERE code = '42'")
┌───────────────────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ Projections: id │
│ │
└───────────────────────────┘
- Pad at load time if you filter on the padded form. Store the padded value in its own column, and compare that column with a constant.
- Combine it with a pushed-down condition. In
id > 100 AND rpad(code, 5, '0') = '42000',id > 100is still evaluated in VAST DataBase, sorpad()runs only on the rows that pass it.
Limitations and errors
- Strings only. A number, a date, a boolean, a
BLOBor a list assfails withNo function matches the given name and argument types; the error lists the one form,rpad(VARCHAR, INTEGER, VARCHAR) -> VARCHAR. Cast the value toVARCHARfirst. - The fill is required.
rpad(s, n)fails withNo function matches the given name and argument types 'rpad(STRING_LITERAL, INTEGER_LITERAL)'. nis anINTEGER. A decimal such as2.5, or a literal larger than anINTEGERsuch as9223372036854775807, fails withNo function matches the given name and argument types.- An empty fill fails when padding is needed, with
Insufficient padding in RPAD.. That includes a fill that comes from a column: one empty value fails the whole query.
run("SELECT rpad('a', 3)")
run("SELECT rpad('a', 3, ' ') || '|', rpad(CAST(42 AS VARCHAR), 5, '0')")
InternalError: Binder Error: No function matches the given name and argument types 'rpad(STRING_LITERAL, INTEGER_LITERAL)'. You might need to add explicit type casts.
[('a |', '42000')]
Similar functions
| Function | Pads | Result for '42' to five characters |
Cuts a longer string |
|---|---|---|---|
lpad('42', 5, '0') |
on the left | '00042' |
yes, to the first n characters |
printf('%-5s', '42') |
on the right, with spaces | '42 ' |
|
printf('%05d', 42) |
a number, on the left with zeros | '00042' |
no |
left('abcdef', 3) |
(cuts only) | 'abc' |
run("SELECT rpad('42', 5, '0'), lpad('42', 5, '0'), printf('%-5s|', '42'), printf('%05d', 42)")
run("SELECT rpad('abcdef', 3, '*'), lpad('abcdef', 3, '*'), left('abcdef', 3), printf('%3s', 'abcdef')")
[('42000', '00042', '42 |', '00042')]
[('abc', 'abc', 'abc', 'abcdef')]
Clean up:
run(f"DROP TABLE IF EXISTS {TABLE}")
run(f"DROP SCHEMA IF EXISTS {SCHEMA}")
conn.close()
[]
[]
See also
- Trimming and padding: all the trimming and padding functions, including trim, ltrim and rtrim.
- Where filters run and pushdowns.
- EXPLAIN: how to read a plan.