Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BuildPG doesn't quote column names on SELECT statements #195

Open
blaketeres opened this issue Oct 31, 2024 · 1 comment
Open

BuildPG doesn't quote column names on SELECT statements #195

blaketeres opened this issue Oct 31, 2024 · 1 comment
Assignees

Comments

@blaketeres
Copy link

blaketeres commented Oct 31, 2024

Issue

TiPg responds with a 500 error if any Postgresql keywords are used as column names. I need to support generic datasets without modifying the column names. I have no control over the column names.

I did some debugging, and this comes down to how buildpg generates its SELECT statements without adding double-quotes around column names. Seems like it would be an easy fix in buildpg, but that repo appears unmaintained.

Steps to reproduce

Build test table

CREATE SCHEMA IF NOT EXISTS test;
CREATE SEQUENCE IF NOT EXISTS test.my_table_ogc_fid_seq;
CREATE TABLE "test"."my_table" (
    "ogc_fid" int4 NOT NULL DEFAULT nextval('test.my_table_ogc_fid_seq'::regclass),
    "end" varchar,  -- this can be any postgresql keyword
    "wkb_geometry" geometry,
    PRIMARY KEY ("ogc_fid")
);

Set env vars

Set environment variables to point to your local database. Also set the following

export TIPG_DB_SCHEMAS='["test"]'

Run TiPg and make request

uvicorn tipg.main:app
$ curl --url '127.0.0.1:8000/collections/test.my_table/items' -v 
*   Trying 127.0.0.1:8000...
* Connected to 127.0.0.1 (127.0.0.1) port 8000
> GET /collections/data_catalog.org_6542_dataset_4819273041a44d3c92cb6015a6ca873e/items HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/8.7.1
> Accept: */*
> 
* Request completely sent off
< HTTP/1.1 500 Internal Server Error
< date: Thu, 31 Oct 2024 16:57:24 GMT
< server: uvicorn
< content-length: 44
< content-type: application/json
< 
* Connection #0 to host 127.0.0.1 left intact
{"detail":"syntax error at or near \"end\""}% 

By placing a breakpoint in the linked location, you can see the generated query in variable c here https://github.com/developmentseed/tipg/blob/main/tipg/collections.py#L647 does not have quotes around column names. I believe that double quoting column names will fix this problem.

Thanks in advance for looking at this!

@vincentsarago
Copy link
Member

I wonder if this is related to samuelcolvin/buildpg#38 and developmentseed/tifeatures#60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants