Skip to content

Commit ceb6549

Browse files
committed
Add support for pg_relation_filenode(), upgrade compress
1 parent dd478cd commit ceb6549

5 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎server/config.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
const (
12-
VERSION = "2.0.1"
12+
VERSION = "2.1.0"
1313

1414
ENV_LOG_LEVEL = "LOG_LEVEL"
1515
ENV_AUDIT_ENABLED = "AUDIT_ENABLED"

‎server/go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ require (
4646
github.com/google/flatbuffers v25.12.19+incompatible // indirect
4747
github.com/jackc/pgpassfile v1.0.0 // indirect
4848
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
49-
github.com/klauspost/compress v1.18.3 // indirect
49+
github.com/klauspost/compress v1.18.7 // indirect
5050
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
5151
github.com/pierrec/lz4/v4 v4.1.25 // indirect
5252
github.com/xdg-go/pbkdf2 v1.0.0 // indirect

‎server/go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo
8181
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
8282
github.com/klauspost/asmfmt v1.3.2 h1:4Ri7ox3EwapiOjCki+hw14RyKk201CN4rzyCJRFLpK4=
8383
github.com/klauspost/asmfmt v1.3.2/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j0HLHbNSE=
84-
github.com/klauspost/compress v1.18.3 h1:9PJRvfbmTabkOX8moIpXPbMMbYN60bWImDDU7L+/6zw=
85-
github.com/klauspost/compress v1.18.3/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
84+
github.com/klauspost/compress v1.18.7 h1:aUyZsS4kH3QTKurYhAOwAHxllVPnOthb3vPfnF1Ehjw=
85+
github.com/klauspost/compress v1.18.7/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
8686
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
8787
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
8888
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 h1:AMFGa4R4MiIpspGNG7Z948v4n35fFGB3RR3G/ry4FWs=

‎server/query_handler_test.go‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,65 @@ type queryResponseExpectation struct {
2020
values []string
2121
}
2222

23+
func TestHandleQueryRelationFilenode(t *testing.T) {
24+
queryHandler := initQueryHandlerForTest(t)
25+
for _, query := range []string{
26+
`SELECT pg_relation_filenode('"audit"."changes"'::regclass)`,
27+
`SELECT pg_catalog.pg_relation_filenode('"audit"."changes"'::pg_catalog.regclass)`,
28+
`SELECT pg_relation_filenode(123::oid)`,
29+
`SELECT pg_relation_filenode(NULL::regclass)`,
30+
} {
31+
t.Run(query, func(t *testing.T) {
32+
messages, err := queryHandler.HandleSimpleQuery(query)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
assertMessageTypes(t, messages, "*pgproto3.RowDescription", "*pgproto3.DataRow", "*pgproto3.CommandComplete")
37+
description := messages[0].(*pgproto3.RowDescription)
38+
if string(description.Fields[0].Name) != "pg_relation_filenode" || description.Fields[0].DataTypeOID != pgtype.Int8OID {
39+
t.Fatalf("unexpected filenode description: %+v", description.Fields[0])
40+
}
41+
if messages[1].(*pgproto3.DataRow).Values[0] != nil {
42+
t.Fatal("filenode must be SQL NULL")
43+
}
44+
})
45+
}
46+
}
47+
48+
func TestHandlePreparedQueryRelationFilenode(t *testing.T) {
49+
queryHandler := initQueryHandlerForTest(t)
50+
for _, query := range []string{
51+
`SELECT pg_relation_filenode($1::regclass)`,
52+
`SELECT pg_catalog.pg_relation_filenode($1::pg_catalog.regclass)`,
53+
} {
54+
t.Run(query, func(t *testing.T) {
55+
_, statement, err := queryHandler.HandleParseQuery(&pgproto3.Parse{Query: query})
56+
if err != nil {
57+
t.Fatal(err)
58+
}
59+
defer statement.Statement.Close()
60+
_, statement, err = queryHandler.HandleBindQuery(&pgproto3.Bind{
61+
Parameters: [][]byte{[]byte(`"audit"."changes"`)},
62+
}, statement)
63+
if err != nil {
64+
t.Fatal(err)
65+
}
66+
_, statement, err = queryHandler.HandleDescribeQuery(&pgproto3.Describe{ObjectType: 'P'}, statement)
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
messages, err := queryHandler.HandleExecuteQuery(&pgproto3.Execute{}, statement)
71+
if err != nil {
72+
t.Fatal(err)
73+
}
74+
assertMessageTypes(t, messages, "*pgproto3.DataRow", "*pgproto3.CommandComplete")
75+
if messages[0].(*pgproto3.DataRow).Values[0] != nil {
76+
t.Fatal("filenode must be SQL NULL")
77+
}
78+
})
79+
}
80+
}
81+
2382
func TestHandleQueryExpandArray(t *testing.T) {
2483
queryHandler := initQueryHandlerForTest(t)
2584
tests := []struct {

‎server/query_remapper_function.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func CreatePgCatalogMacroQueries(config *Config) []string {
2525
"CREATE MACRO pg_get_viewdef(view_oid) AS pg_catalog.pg_get_viewdef(view_oid), (view_oid, pretty_bool) AS pg_catalog.pg_get_viewdef(view_oid)",
2626
"CREATE MACRO pg_indexes_size(regclass) AS 0",
2727
"CREATE MACRO pg_is_in_recovery() AS false",
28+
"CREATE MACRO pg_relation_filenode(relation) AS NULL::BIGINT",
2829
"CREATE MACRO pg_table_size(regclass) AS 0",
2930
"CREATE MACRO pg_tablespace_location(tablespace_oid) AS ''",
3031
"CREATE MACRO pg_total_relation_size(regclass) AS 0",
@@ -140,6 +141,16 @@ func (remapper *QueryRemapperFunction) RemapFunctionCall(functionCall *pgQuery.F
140141

141142
// pg_catalog.func() -> main.func()
142143
case PG_SCHEMA_PG_CATALOG, "":
144+
if schemaFunction.Function == "pg_relation_filenode" {
145+
parserTypeCast := NewParserTypeCast(remapper.config)
146+
for _, argument := range functionCall.Args {
147+
typeCast := parserTypeCast.TypeCast(argument)
148+
parserTypeCast.RemovePgCatalog(typeCast)
149+
if parserTypeCast.TypeName(typeCast) == "regclass" {
150+
parserTypeCast.SetTypeName(typeCast, "text")
151+
}
152+
}
153+
}
143154
if PG_CATALOG_MACRO_FUNCTION_NAMES.Contains(schemaFunction.Function) || BUILTIN_DUCKDB_PG_FUNCTION_NAMES.Contains(schemaFunction.Function) {
144155
remapper.parserFunction.RemapSchemaToMain(functionCall)
145156
return schemaFunction

0 commit comments

Comments
 (0)