Applies to VAST Cluster 5.5.
Functions › Trimming and padding › Trimming and padding. Removes characters from the start (the left) of a string: spaces by default, or any of the characters you list. It's the same as trim(LEADING characters FROM s).
Syntax
ltrim(s)
ltrim(s, characters)
| Argument | Type | Description |
|---|---|---|
s |
VARCHAR |
The string to trim. |
characters |
VARCHAR |
Optional. The set of characters to remove, in any order. Leave it out to remove spaces. |
Returns: VARCHAR.
Related: trim(LEADING characters FROM s) does the same; rtrim trims the end and trim both ends.
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_ltrim. 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 account numbers stored as text: padded with spaces, with leading zeros, with a tab in front, and all zeros. 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_ltrim"'
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}.accounts"
run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, account VARCHAR)")
run(f"INSERT INTO {TABLE} VALUES "
"(1, ' 4217'), "
"(2, '0004217'), "
"(3, '\t4217'), "
"(4, '0000'), "
"(5, NULL)")
[]
[]
[(5,)]
What it does
Without characters, ltrim(s) removes spaces from the start of the string, including Unicode spaces such as the no-break space (U+00A0), but not tabs or newlines: ltrim('\ta') returns the string unchanged. The end of the string is never touched: ltrim(' a ') returns 'a '.
With characters, ltrim(s, characters) removes every character in the set from the start, until it reaches one that isn't in the set. The set has no order and no wildcards, and the match is case-sensitive:
| Call | Result |
|---|---|
ltrim('0042', '0') |
'42' |
ltrim('0000', '0') |
'': every character is in the set |
ltrim('xyxa', 'yx') |
'a': the order of the set doesn't matter |
ltrim('-+5', '+-') |
'5' |
ltrim('axx', 'x') |
'axx': only the start is trimmed |
ltrim('a', '') |
'a': an empty set removes nothing |
The set is made of code points. A precomposed é is one code point, so ltrim('éa', 'é') returns 'a'. An é typed as e and a combining accent is two: ltrim('e\u0301a', 'e') removes the e and leaves the accent alone at the start, '\u0301a'.
NULLs and edge cases
| Input | Result |
|---|---|
s is NULL |
NULL |
characters is NULL |
NULL |
'' (empty string) |
'' |
ltrim('\u00a0a') (a no-break space) |
'a' |
ltrim('\ta'), ltrim('\na') |
unchanged |
ltrim('e\u0301a', 'e') |
'\u0301a': the accent alone |
ltrim(LEADING FROM ' a') |
Error: syntax error at or near "LEADING" |
s a number, date, boolean, BLOB or list, or a number as characters |
Error: No function matches the given name and argument types |
| Three arguments | 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 ltrim(NULL), ltrim('a', NULL), ltrim(NULL, 'x'), ltrim(''), ltrim('\u00a0a'), ltrim('\ta'), ltrim('\na')")
run("SELECT ltrim(' a '), ltrim('0042', '0'), ltrim('0000', '0'), ltrim('xyxa', 'yx'), ltrim('-+5', '+-'), "
"ltrim('axx', 'x'), ltrim('a', '')")
run("SELECT ltrim('\u00e9a', '\u00e9'), ltrim('e\u0301a', 'e')")
run("SELECT ltrim(LEADING FROM ' a')")
run("SELECT ltrim(42)")
[(None, None, None, '', 'a', '\ta', '\na')]
[('a ', '42', '', 'a', '5', 'axx', 'a')]
[('a', '́a')]
InternalError: Parse query: 'SELECT ltrim(LEADING FROM ' a')' failed with: Parser Error: syntax error at or near "LEADING"
InternalError: Binder Error: No function matches the given name and argument types 'ltrim(INTEGER_LITERAL)'. You might need to add explicit type casts.
Examples
Remove the leading spaces, then the leading zeros, then both. Row 3 keeps its tab unless you list it, and row 4 becomes an empty string. The brackets show where each result starts:
run(f"SELECT id, '[' || ltrim(account) || ']', ltrim(account, '0'), ltrim(account, ' \t0') "
f"FROM {TABLE} ORDER BY id LIMIT 10")
[(1, '[4217]', ' 4217', '4217'), (2, '[0004217]', '4217', '4217'), (3, '[\t4217]', '\t4217', '4217'), (4, '[0000]', '', ''), (5, None, None, None)]
Find an account whatever its leading zeros:
run(f"SELECT id FROM {TABLE} WHERE ltrim(account, ' \t0') = '4217' ORDER BY id LIMIT 10")
[(1,), (2,), (3,)]
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 ltrim() is always computed by the Query Engine:
- In the
SELECTlist, the scan reads the column and the Query Engine trims it for each row it returns. - In
WHERE, the scan sends the column for every row, and aFILTERstep in the Query Engine trims it 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 ltrim(account, ' \t0') = '4217'")
┌───────────────────────────┐
│ PROJECTION │
│ ──────────────────── │
│ #1 │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ FILTER │
│ ──────────────────── │
│ (ltrim(account, ' 0') = │
│ '4217') │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ │
│ Projections: │
│ account │
│ id │
│ │
└───────────────────────────┘
Make it faster
- Store one form. Trim the values when you write them, so that a query can compare the column with a constant and the comparison is pushed down to VAST DataBase.
- Pad the constant, not the column. If a column holds fixed-width codes such as
'0004217', compare it with'0004217', which is pushed down, instead ofltrim(account, '0') = '4217'. lpad builds the constant:lpad('4217', 7, '0'). The plan of a raw column compared with a constant has noFILTERstep. - Combine it with a pushed-down condition. In
id > 1 AND ltrim(account, '0') = '4217',id > 1is still evaluated in VAST DataBase, soltrim()runs only on the rows that pass it.
run(f"SELECT id FROM {TABLE} WHERE account = lpad('4217', 7, '0') ORDER BY id LIMIT 10")
plan(f"SELECT id FROM {TABLE} WHERE account = '0004217'")
[(2,)]
┌───────────────────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ Projections: id │
│ │
└───────────────────────────┘
Limitations and errors
- Tabs and newlines stay unless you list them.
ltrim(s)removes spaces only. List the characters, as inltrim(s, ' \t\n'). - A set of characters, not a prefix.
ltrim(s, 'xy')removes any mix ofxandyat the start, not the textxy. To remove a prefix only once, use regexp_replace with'^xy'. - No
LEADING ... FROMform.ltrim(LEADING FROM s)is a syntax error; useltrim(s)ortrim(LEADING FROM s). VARCHARonly. A number fails withNo function matches the given name and argument types 'ltrim(INTEGER_LITERAL)'. Cast it first:ltrim(CAST(n AS VARCHAR), '0').
run("SELECT ltrim('xyxya', 'xy'), regexp_replace('xyxya', '^xy', ''), trim(LEADING FROM ' a ')")
run("SELECT ltrim(CAST(42 AS VARCHAR), '4')")
[('a', 'xya', 'a ')]
[('2',)]
Similar functions
| Function | Removes | Result for 'xyaxy' and 'xy' |
|---|---|---|
ltrim(s, 'xy') |
the characters at the start | 'axy' |
rtrim (s, 'xy') |
the characters at the end | 'xya' |
trim (s, 'xy') |
the characters at both ends | 'a' |
trim(LEADING 'xy' FROM s) |
the characters at the start | 'axy' |
regexp_replace (s, '^xy', '') |
a prefix, once | 'axy' |
run("SELECT ltrim('xyaxy', 'xy'), rtrim('xyaxy', 'xy'), trim('xyaxy', 'xy'), trim(LEADING 'xy' FROM 'xyaxy'), "
"regexp_replace('xyaxy', '^xy', '')")
[('axy', 'xya', 'a', 'axy', 'axy')]
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.
- trim, rtrim and lpad.
- Where filters run and pushdowns.
- EXPLAIN: how to read a plan.