-
Notifications
You must be signed in to change notification settings - Fork 962
fix(postgresql): set query result column nullable if selected column contains json operator or null value in case expression #3739
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
9ea4ce3
25274e8
f61bfcc
049fb49
ebc8a77
f154a2f
822e73f
e7ddabf
d0cbfe4
409d2fb
19ab642
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… user more freedom to choose query column type
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,7 +132,11 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er | |
| if res.Name != nil { | ||
| name = *res.Name | ||
| } | ||
| cols = append(cols, convertAConstToColumn(n, name)) | ||
| col := convertAConstToColumn(n, name) | ||
| if col.DataType == "null" { | ||
| col.DataType = "any" | ||
| } | ||
| cols = append(cols, col) | ||
|
|
||
| case *ast.A_Expr: | ||
| name := "" | ||
|
|
@@ -164,50 +168,67 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er | |
| cols = append(cols, &Column{Name: name, DataType: "bool", NotNull: notNull}) | ||
|
|
||
| case *ast.CaseExpr: | ||
| name := "" | ||
| var name string | ||
| if res.Name != nil { | ||
| name = *res.Name | ||
| } | ||
|
|
||
| typePrecedence := map[string]int{ | ||
| "any": 0, | ||
| "bool": 1, | ||
| "int": 2, | ||
| "pg_catalog.int4": 2, | ||
| "float": 3, | ||
| "pg_catalog.float8": 3, | ||
| "text": 4, | ||
| } | ||
|
|
||
| chosenType := "any" | ||
| chosenType := "" | ||
| chosenNullable := false | ||
|
|
||
| for _, i := range n.Args.Items { | ||
| cw := i.(*ast.CaseWhen) | ||
| col, err := convertCaseExprCondToColumn(cw.Result, res.Name) | ||
| col, err := convertCaseExprCondToColumn(cw.Result, &name) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if typePrecedence[col.DataType] > typePrecedence[chosenType] { | ||
| chosenType = col.DataType | ||
| if col.DataType == "null" { | ||
| // we don't choose type from this column if its value is null, only choose nullability | ||
| chosenNullable = true | ||
| continue | ||
| } | ||
| if col.DataType != chosenType { | ||
| if chosenType == "" { | ||
| chosenType = col.DataType | ||
| } else { | ||
| chosenType = "any" | ||
| } | ||
| } | ||
| if !col.NotNull { | ||
| chosenNullable = true | ||
| } | ||
| } | ||
|
|
||
| if n.Defresult != nil { | ||
| defaultCol, err := convertCaseExprCondToColumn(n.Defresult, res.Name) | ||
| var defaultCol *Column | ||
| if n.Defresult.Pos() != 0 { | ||
| defaultCol, err = convertCaseExprCondToColumn(n.Defresult, &name) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if typePrecedence[defaultCol.DataType] > typePrecedence[chosenType] { | ||
| chosenType = defaultCol.DataType | ||
| } else { | ||
| defaultCol = &Column{Name: name, DataType: "null", NotNull: false} | ||
| } | ||
|
|
||
| if defaultCol.DataType == "null" { | ||
| // we don't choose type from this column if its value is null, only choose nullability | ||
| chosenNullable = true | ||
| } else { | ||
| if defaultCol.DataType != chosenType { | ||
| if chosenType == "" { | ||
| chosenType = defaultCol.DataType | ||
| } else { | ||
| chosenType = "any" | ||
| } | ||
| } | ||
| if !defaultCol.NotNull { | ||
| chosenNullable = true | ||
| } | ||
| } | ||
|
|
||
| if chosenType == "" { | ||
| chosenType = "any" | ||
| } | ||
|
|
||
| chosenColumn := &Column{Name: name, DataType: chosenType, NotNull: !chosenNullable} | ||
| cols = append(cols, chosenColumn) | ||
|
|
||
|
|
@@ -811,7 +832,7 @@ func convertAExprToColumn(aexpr *ast.A_Expr, name string) *Column { | |
| return col | ||
| } | ||
|
|
||
| func convertAConstToColumn(aconst *ast.A_Const, name string) *Column { | ||
| func convertAConstToColumn(aconst *ast.A_Const, name string) (*Column) { | ||
| var col *Column | ||
| switch aconst.Val.(type) { | ||
| case *ast.String: | ||
|
|
@@ -822,6 +843,8 @@ func convertAConstToColumn(aconst *ast.A_Const, name string) *Column { | |
| col = &Column{Name: name, DataType: "float", NotNull: true} | ||
| case *ast.Boolean: | ||
| col = &Column{Name: name, DataType: "bool", NotNull: true} | ||
| case *ast.Null: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is new addition to handle null const value. I need to distinguish between for |
||
| col = &Column{Name: name, DataType: "null", NotNull: false} | ||
| default: | ||
| col = &Column{Name: name, DataType: "any", NotNull: false} | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing
ELSEclause on CaseExpr is considered to return null value