[Bug] legalize_vector_name does not escape SQL keywords case-insensitively

Open Beginner friendly
#8,150 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
83/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, python
Domain
backend, databases

Research direction

Read python/grass/script/utils.py at legalize_vector_name() and compare its keyword check with lib/vector/Vlib/legal_vname.c. Add or update focused tests for lowercase and mixed-case SQL keywords, then run the relevant Python tests. Done means all casing variants are legalized consistently with the C function.

Written by the indexing model from the issue text.

Description

Describe the bug

grass.script.utils.legalize_vector_name() only escapes the SQL keywords and, or and not when they are lowercase. Any other casing is returned unchanged, so the function returns names that GRASS itself rejects as illegal.

The Python function says it is based on Vect_legal_filename(), but the C function compares the keywords case-insensitively (lib/vector/Vlib/legal_vname.c):

static const char *keywords[] = {"and", "or", "not", NULL};
...
if (G_strcasecmp(buf, keywords[i]) == 0) {

while the Python version uses an exact match (python/grass/script/utils.py):

keywords = ["and", "or", "not"]
if name in keywords:
    name = "{name}_".format(**locals())

To reproduce

from grass.script.utils import legalize_vector_name

for name in ["and", "AND", "Or", "nOt"]:
    print(repr(name), "->", repr(legalize_vector_name(name)))

Output on current main:

'and' -> 'and_'
'AND' -> 'AND'
'Or' -> 'Or'
'nOt' -> 'nOt'

AND, Or and nOt are then rejected by Vect_legal_filename() with "SQL keyword cannot be used as vector map name".

Expected behavior

All casings of the keywords are made legal, e.g. 'AND' -> 'AND_', matching the case-insensitive check in Vect_legal_filename(). A fix would be to compare name.lower() against the keyword list.

System description

  • Operating System: Windows 11
  • GRASS version: main (b0a6d7703c); the function is pure Python, reproduced by loading python/grass/script/utils.py directly with Python 3.12

Additional context

Related but different: #3615 (SQL keywords as attribute column names).

Dominant language
C
Stars
1.2k
Forks
447
Avg merge
1d 19h
Merged PRs (30d)
62

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from OSGeo/grass

All issues in OSGeo/grass

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.