apache / apache/shardingsphere
gorm insert postgreSQL with returning npe
- Dominant language
- Java
- Stars
- 20.8k
- Forks
- 6.9k
- Avg merge
- 11h 38m
- Merged PRs (30d)
- 326
Description
## Bug Report
### Which version of ShardingSphere did you use?
5.3.0
### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
proxy
### Expected behavior
insert success using go orm
### Actual behavior
[org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask][,,,,][,,] - Exception occur:
java.lang.NullPointerException: null
at org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.describe.PostgreSQLComDescribeExecutor.describeInsertStatementByDatabaseMetaData(PostgreSQLComDescribeExecutor.java 137)
### Reason analyze (If you can)
this is the payload from gorm insert
```
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 50 00 00 00 63 73 74 6d 74 63 61 63 68 65 5f 31 |P...cstmtcache_1|
|00000010| 00 49 4e 53 45 52 54 20 49 4e 54 4f 20 22 74 5f |.INSERT INTO "t_|
|00000020| 73 74 75 64 65 6e 74 5f 69 6e 66 6f 22 20 28 22 |student_info" ("|
|00000030| 73 74 75 64 65 6e 74 5f 6e 61 6d 65 22 2c 22 69 |student_name","i|
|00000040| 64 22 29 20 56 41 4c 55 45 53 20 28 24 31 2c 24 |d") VALUES ($1,$|
|00000050| 32 29 20 52 45 54 55 52 4e 49 4e 47 20 22 69 64 |2) RETURNING "id|
|00000060| 22 00 00 00 44 00 00 00 11 53 73 74 6d 74 63 61 |"...D....Sstmtca|
|00000070| 63 68 65 5f 31 00 53 00 00 00 04 |che_1.S.... |
+--------+-------------------------------------------------+----------------+
[DEBUG] 2023-03-15 10:39:50.958 [Connection-4-ThreadExecutor] o.a.s.p.f.p.c.PostgreSQLCommandExecutorFactory - Execute packet type: PARSE_COMMAND, value: PostgreSQLAggregatedCommandPacket(packets=[PostgreSQLComParsePacket(payload=org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload@3e6ee0b9, statementId=stmtcache_1, sql=INSERT INTO "t_student_info" ("student_name","id") VALUES ($1,$2) RETURNING "id"), PostgreSQLComDescribePacket(type=S, name=stmtcache_1), PostgreSQLComSyncPacket()], containsBatchedStatements=false, firstBindIndex=-1, lastExecuteIndex=-1)
```
org.apache.shardingsphere.proxy.frontend.postgresql.command.query.extended.describe.PostgreSQLComDescribeExecutor#describeInsertStatementByDatabaseMetaData
insertStatement.getTable().getOwner() is null , then it gets the default schema "public" , that cause the meta of table[t_student_info] empty
### Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.
proxy deployed with cluster mode;
insert with gorm
### Example codes for reproduce this issue (such as a github link).
go code
```
package main
import (
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
dsn := "host=127.0.0.1 user=userName password={pwd} dbname={dbname} port={port} sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err == nil {
student := Student{ID: 11, StudentName: "hello go"}
fmt.Println(db.Create(student))
}
}
type Student struct {
ID int64
StudentName string
}
func (Student) TableName() string {
return "t_student_info"
}
```
Contributor guide
Research direction
Reproduce the GORM insert against ShardingSphere-Proxy in PostgreSQL mode using the provided SQL and cluster setup. Start at PostgreSQLComDescribeExecutor#describeInsertStatementByDatabaseMetaData and inspect how the missing table owner and default schema affect table metadata. Done means the INSERT ... RETURNING request completes successfully without a NullPointerException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, java, postgresql
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100