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

Numeric postgres schemas cannot be queried #470

Open
MarsCapone opened this issue Nov 29, 2024 · 1 comment
Open

Numeric postgres schemas cannot be queried #470

MarsCapone opened this issue Nov 29, 2024 · 1 comment
Assignees
Labels

Comments

@MarsCapone
Copy link

Describe the bug

When setting a Postgres schema with a numeric name, there is an error:

sq: invalid active schema specified via --src.schema: invalid catalog.schema: loadtest.10000

To Reproduce

Steps to reproduce the behavior.

  1. Start a new docker container
    docker run -d -e POSTGRES_HOST_AUTH_METHOD=trust -p5432:5432 postgres
    
  2. Create a numeric schema
    $ psql postgres://postgres@localhost:5432
    > create schema "10000";
    > set search_path = "10000";
    > create table bars (id int primary key);
    
  3. Add the connection to sq and inspect
❯ sq add postgres://postgres@localhost:5432 --handle @testdb
@testdb  postgres  postgres://postgres@localhost:5432

❯ sq inspect @testdb
SOURCE   DRIVER    NAME      FQ NAME          SIZE   TABLES  VIEWS  LOCATION
@testdb  postgres  postgres  postgres.public  7.3MB  0       0      postgres://postgres@localhost:5432

❯ sq inspect --src.schema 10000 @testdb
sq: invalid active schema specified via --src.schema: invalid catalog.schema: 10000

❯ sq inspect --src.schema postgres.10000 @testdb
sq: invalid active schema specified via --src.schema: invalid catalog.schema: postgres.10000

Expected behavior

sq inspect --src.schema postgres.10000 @testdb should not produce an error

If you query a table, that should not produce an error either. i.e. this should produce no output (because the table is empty)

❯ sq --src @testdb --src.schema 10000 '.bars'
sq: invalid active schema specified via --src.schema: invalid catalog.schema: 10000

sq version

Paste the output of sq version --yaml into the code block below:

# $ sq version --yaml
version: v0.48.4
commit: RELEASE
timestamp: 2024-11-24T22:30:03.000Z
latest_version: v0.48.4
host:
  platform: darwin
  arch: arm64
  kernel: Darwin
  kernel_version: 23.6.0
  variant: macOS
  variant_version: 14.7.1

Source details

If your issue pertains to a particular source (e.g. a Postgres database),
paste the output of sq inspect --overview --yaml @your_source into the
code block below. You may redact any sensitive fields.

# $ sq inspect --overview --yaml @testdb
handle: "@testdb"
location: postgres://postgres@localhost:5432
name: postgres
name_fq: postgres.public
schema: public
catalog: postgres
driver: postgres
db_driver: postgres
db_product: PostgreSQL 17.2 (Debian 17.2-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
db_version: 17.2 (Debian 17.2-1.pgdg120+1)
user: postgres
size: 7687315

Logs

Pretty sure this is the important bit, but attached all the logs too: sq.log

01:35:59.071180 ERR cli.PrintError cli/error.go:55 EXECUTION FAILED pid=38028 err.msg="invalid active schema specified via --src.schema: invalid catalog.schema: 10000" err.type=*errz.errz cmd=slq
*errz.errz: *errz.errz
invalid active schema specified via --src.schema: invalid catalog.schema: 10000
github.com/neilotoole/sq/cli.processFlagActiveSchema
  github.com/neilotoole/sq/cli/source.go:232
github.com/neilotoole/sq/cli.activeSrcAndSchemaFromFlagsOrConfig
  github.com/neilotoole/sq/cli/source.go:108
github.com/neilotoole/sq/cli.determineSources
  github.com/neilotoole/sq/cli/source.go:32
github.com/neilotoole/sq/cli.execSLQ
  github.com/neilotoole/sq/cli/cmd_slq.go:62
github.com/neilotoole/sq/cli.addCmd.func2
  github.com/neilotoole/sq/cli/cli.go:355
github.com/spf13/cobra.(*Command).execute
  github.com/spf13/[email protected]/command.go:985
github.com/spf13/cobra.(*Command).ExecuteC
  github.com/spf13/[email protected]/command.go:1117
github.com/spf13/cobra.(*Command).Execute
  github.com/spf13/[email protected]/command.go:1041
github.com/spf13/cobra.(*Command).ExecuteContext
  github.com/spf13/[email protected]/command.go:1034
github.com/neilotoole/sq/cli.ExecuteWith
  github.com/neilotoole/sq/cli/cli.go:217
github.com/neilotoole/sq/cli.Execute
  github.com/neilotoole/sq/cli/cli.go:76
main.main
  github.com/neilotoole/sq/main.go:56
runtime.main
  runtime/proc.go:272
runtime.goexit
  runtime/asm_arm64.s:1223
@MarsCapone
Copy link
Author

I think the relevant code is here: https://github.com/neilotoole/sq/blob/master/libsq/ast/ast.go#L213

Playing around in jq I realise I need to wrap the number in double quotes

❯ echo '{"10000": ["foo"]}' |  jq '.10000'
0.10000

❯ echo '{"10000": ["foo"]}' | jq '."10000"'
[
  "foo"
]

Which reveals a similar workaround for sq

❯ sq --src @testdb --src.schema 'postgres."10000"' '.bars'
❯ sq --src @testdb --src.schema '"10000"' '.bars'

So maybe this could just be a note in the documentation?

@neilotoole neilotoole self-assigned this Jan 20, 2025
@neilotoole neilotoole added the bug label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants