Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
devstein committed Jan 22, 2025
1 parent fdf7c21 commit 3f2c45c
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 151 deletions.
61 changes: 36 additions & 25 deletions internal/analysis/analysis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions internal/compiler/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,24 @@ func convertTypeName(id *analyzer.Identifier) *ast.TypeName {
func convertColumn(c *analyzer.Column) *Column {
length := int(c.Length)
return &Column{
Name: c.Name,
OriginalName: c.OriginalName,
DataType: c.DataType,
NotNull: c.NotNull,
Unsigned: c.Unsigned,
IsArray: c.IsArray,
ArrayDims: int(c.ArrayDims),
Comment: c.Comment,
Length: &length,
IsNamedParam: c.IsNamedParam,
IsFuncCall: c.IsFuncCall,
Scope: c.Scope,
Table: convertTableName(c.Table),
TableAlias: c.TableAlias,
Type: convertTypeName(c.Type),
EmbedTable: convertTableName(c.EmbedTable),
IsSqlcSlice: c.IsSqlcSlice,
Name: c.Name,
OriginalName: c.OriginalName,
DataType: c.DataType,
NotNull: c.NotNull,
Unsigned: c.Unsigned,
IsArray: c.IsArray,
ArrayDims: int(c.ArrayDims),
Comment: c.Comment,
Length: &length,
IsNamedParam: c.IsNamedParam,
IsFuncCall: c.IsFuncCall,
Scope: c.Scope,
Table: convertTableName(c.Table),
TableAlias: c.TableAlias,
Type: convertTypeName(c.Type),
EmbedTable: convertTableName(c.EmbedTable),
IsSqlcSlice: c.IsSqlcSlice,
IsDefaultNull: c.DefaultNull,
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (c *Compiler) OutputColumns(stmt ast.Node) ([]*catalog.Column, error) {
ArrayDims: col.ArrayDims,
Comment: col.Comment,
Length: col.Length,
// EDIT?
})
}
return catCols, nil
Expand Down
23 changes: 12 additions & 11 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ type Table struct {
}

type Column struct {
Name string
OriginalName string
DataType string
NotNull bool
Unsigned bool
IsArray bool
ArrayDims int
Comment string
Length *int
IsNamedParam bool
IsFuncCall bool
Name string
OriginalName string
DataType string
NotNull bool
Unsigned bool
IsArray bool
ArrayDims int
Comment string
Length *int
IsNamedParam bool
IsFuncCall bool
IsDefaultNull bool // is this a default null column

// XXX: Figure out what PostgreSQL calls `foo.id`
Scope string
Expand Down
23 changes: 12 additions & 11 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,18 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: p.Name(),
OriginalName: c.Name,
DataType: dataType(&c.Type),
NotNull: p.NotNull(),
Unsigned: c.IsUnsigned,
IsArray: c.IsArray,
ArrayDims: c.ArrayDims,
Length: c.Length,
Table: table,
IsNamedParam: isNamed,
IsSqlcSlice: p.IsSqlcSlice(),
Name: p.Name(),
OriginalName: c.Name,
DataType: dataType(&c.Type),
NotNull: p.NotNull(),
Unsigned: c.IsUnsigned,
IsArray: c.IsArray,
ArrayDims: c.ArrayDims,
Length: c.Length,
Table: table,
IsNamedParam: isNamed,
IsSqlcSlice: p.IsSqlcSlice(),
IsDefaultNull: c.IsDefaultNull,
},
})
}
Expand Down
15 changes: 10 additions & 5 deletions internal/endtoend/testdata/codegen_json/gen/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
"embed_table": null,
"original_name": "",
"unsigned": false,
"array_dims": 0
"array_dims": 0,
"default_null": true
}
],
"comment": ""
Expand Down Expand Up @@ -65043,7 +65044,8 @@
"embed_table": null,
"original_name": "bio",
"unsigned": false,
"array_dims": 0
"array_dims": 0,
"default_null": true
}
],
"params": [
Expand Down Expand Up @@ -65162,7 +65164,8 @@
"embed_table": null,
"original_name": "bio",
"unsigned": false,
"array_dims": 0
"array_dims": 0,
"default_null": true
}
],
"params": [],
Expand Down Expand Up @@ -65251,7 +65254,8 @@
"embed_table": null,
"original_name": "bio",
"unsigned": false,
"array_dims": 0
"array_dims": 0,
"default_null": true
}
],
"params": [
Expand Down Expand Up @@ -65310,7 +65314,8 @@
"embed_table": null,
"original_name": "bio",
"unsigned": false,
"array_dims": 0
"array_dims": 0,
"default_null": true
}
}
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
bio text DEFAULT NULL
);
13 changes: 7 additions & 6 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,13 @@ func convertColumnDef(def *pcast.ColumnDef) *ast.ColumnDef {
}
}
columnDef := ast.ColumnDef{
Colname: def.Name.String(),
TypeName: &ast.TypeName{Name: types.TypeToStr(def.Tp.GetType(), def.Tp.GetCharset())},
IsNotNull: isNotNull(def),
IsUnsigned: isUnsigned(def),
Comment: comment,
Vals: vals,
Colname: def.Name.String(),
TypeName: &ast.TypeName{Name: types.TypeToStr(def.Tp.GetType(), def.Tp.GetCharset())},
IsNotNull: isNotNull(def),
IsDefaultNull: isDefaultNull(def),
IsUnsigned: isUnsigned(def),
Comment: comment,
Vals: vals,
}
if def.Tp.GetFlen() >= 0 {
length := def.Tp.GetFlen()
Expand Down
21 changes: 21 additions & 0 deletions internal/engine/dolphin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ func isNotNull(n *pcast.ColumnDef) bool {
return false
}

// NOTE: This is a placeholder! It isn't tested!
// https://github.com/pingcap/tidb/blob/35f329d3adaf9108a3eb97537937bf5d89dc626f/pkg/parser/ast/ast.go#L4
func isDefaultNull(n *pcast.ColumnDef) bool {
if isNotNull(n) {
return false
}
// get the default value option
if len(n.Options) == 0 {
return false
}
// check if the default value is null
for i := range n.Options {
if n.Options[i].Tp == pcast.ColumnOptionDefaultValue {
if n.Options[i].Expr == nil {
return true
}
}
}
return false
}

func convertToRangeVarList(list *ast.List, result *ast.List) {
if len(list.Items) == 0 {
return
Expand Down
Loading

0 comments on commit 3f2c45c

Please # to comment.