Skip to content

Commit

Permalink
Return the actual database name from pg_catalog.pg_database
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Dec 18, 2024
1 parent 3987690 commit 133ce1e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 105 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Sync data from a Postgres database:
./bemidb --pg-database-url postgres://postgres:postgres@localhost:5432/dbname sync
```

Run BemiDB database:
Then run BemiDB database:

```sh
./bemidb start
Expand All @@ -61,7 +61,7 @@ Run Postgres queries on top of the BemiDB database:

```sh
# List all tables
psql postgres://localhost:54321/bemidb -c "SELECT * FROM information_schema.tables"
psql postgres://localhost:54321/bemidb -c "SELECT table_schema, table_name FROM information_schema.tables"

# Query a table
psql postgres://localhost:54321/bemidb -c "SELECT COUNT(*) FROM [table_name]"
Expand Down Expand Up @@ -294,7 +294,6 @@ SELECT * FROM [TABLE] WHERE [JSON_COLUMN]->>'[JSON_KEY]' = '[JSON_VALUE]';
- [ ] Iceberg table compaction and partitioning.
- [ ] Cache layer for frequently accessed data.
- [ ] Materialized views.
- [ ] Support for custom S3-compatible endpoints.

## Alternatives

Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

const VERSION = "0.27.2"
const VERSION = "0.28.0"

func main() {
config := LoadConfig()
Expand Down
2 changes: 1 addition & 1 deletion src/query_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestHandleQuery(t *testing.T) {
},
"SELECT oid, datname, datdba FROM pg_catalog.pg_database where oid = 1146": {
"description": {"oid", "datname", "datdba"},
"values": {"1146", "memory", ""},
"values": {"1146", "bemidb", "10"},
},
"SELECT * FROM pg_catalog.pg_stat_gssapi": {
"description": {"pid", "gss_authenticated", "principal", "encrypted", "credentials_delegated"},
Expand Down
130 changes: 32 additions & 98 deletions src/query_parser_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,107 +91,21 @@ func (parser *QueryParserTable) MakePgExtensionNode(alias string) *pgQuery.Node
}

// pg_catalog.pg_database -> VALUES(values...) t(columns...)
func (parser *QueryParserTable) MakePgDatabaseNode(alias string) *pgQuery.Node {
targetList := []*pgQuery.Node{
pgQuery.MakeResTargetNodeWithNameAndVal(
"oid",
pgQuery.MakeColumnRefNode([]*pgQuery.Node{pgQuery.MakeStrNode("oid")}, 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datname",
pgQuery.MakeColumnRefNode([]*pgQuery.Node{pgQuery.MakeStrNode("datname")}, 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datdba",
pgQuery.MakeAConstStrNode("", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"encoding",
pgQuery.MakeAConstStrNode("6", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datlocprovider",
pgQuery.MakeAConstStrNode("c", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datistemplate",
pgQuery.MakeAConstStrNode("f", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datallowconn",
pgQuery.MakeAConstStrNode("t", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datconnlimit",
pgQuery.MakeAConstStrNode("-1", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datfrozenxid",
pgQuery.MakeAConstStrNode("722", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datminmxid",
pgQuery.MakeAConstStrNode("1", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"dattablespace",
pgQuery.MakeAConstStrNode("1663", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datcollate",
pgQuery.MakeAConstStrNode("en_US.UTF-8", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datctype",
pgQuery.MakeAConstStrNode("en_US.UTF-8", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"daticulocale",
pgQuery.MakeAConstStrNode("", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"daticurules",
pgQuery.MakeAConstStrNode("", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datcollversion",
pgQuery.MakeAConstStrNode("", 0),
0,
),
pgQuery.MakeResTargetNodeWithNameAndVal(
"datacl",
pgQuery.MakeAConstStrNode("", 0),
0,
),
}
func (parser *QueryParserTable) MakePgDatabaseNode(database string, alias string) *pgQuery.Node {
columns := PG_DATABASE_VALUE_BY_COLUMN.Keys()
staticRowValues := PG_DATABASE_VALUE_BY_COLUMN.Values()

fromClause := &pgQuery.Node{
Node: &pgQuery.Node_RangeVar{
RangeVar: &pgQuery.RangeVar{
Schemaname: "pg_catalog",
Relname: "pg_database",
Inh: true,
Relpersistence: "p",
},
},
var rowsValues [][]string
rowValues := make([]string, len(staticRowValues))
copy(rowValues, staticRowValues)
for i, column := range columns {
if column == "datname" {
rowValues[i] = database
}
}
rowsValues = append(rowsValues, rowValues)

return parser.utils.MakeSubselectFromNode(targetList, fromClause, alias)
return parser.utils.MakeSubselectWithRowsNode(columns, rowsValues, alias)
}

// System pg_* tables
Expand Down Expand Up @@ -628,6 +542,26 @@ var PG_EXTENSION_VALUE_BY_COLUMN = NewOrderedMap([][]string{
{"extcondition", "NULL"},
})

var PG_DATABASE_VALUE_BY_COLUMN = NewOrderedMap([][]string{
{"oid", "1146"},
{"datname", "bemidb"},
{"datdba", "10"},
{"encoding", "6"},
{"datlocprovider", "c"},
{"datistemplate", "FALSE"},
{"datallowconn", "TRUE"},
{"datconnlimit", "-1"},
{"datfrozenxid", "722"},
{"datminmxid", "1"},
{"dattablespace", "1663"},
{"datcollate", "en_US.UTF-8"},
{"datctype", "en_US.UTF-8"},
{"daticulocale", "NULL"},
{"daticurules", "NULL"},
{"datcollversion", "NULL"},
{"datacl", "NULL"},
})

type DuckDBKeyword struct {
word string
category string
Expand Down
4 changes: 2 additions & 2 deletions src/select_remapper_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (remapper *SelectRemapperTable) RemapTable(node *pgQuery.Node) *pgQuery.Nod
tableNode := parser.MakeEmptyTableNode(PG_REPLICATION_SLOTS_COLUMNS, qSchemaTable.Alias)
return remapper.overrideTable(node, tableNode)
case PG_TABLE_PG_DATABASE:
// pg_catalog.pg_database -> extend with additional Postgres columns
tableNode := parser.MakePgDatabaseNode(qSchemaTable.Alias)
// pg_catalog.pg_database -> return hard-coded database info
tableNode := parser.MakePgDatabaseNode(remapper.config.Database, qSchemaTable.Alias)
return remapper.overrideTable(node, tableNode)
case PG_TABLE_PG_STAT_GSSAPI:
// pg_catalog.pg_stat_gssapi -> return nothing
Expand Down

0 comments on commit 133ce1e

Please sign in to comment.