Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion internal/core/analyzer/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,29 @@ func (a *analyzer) appendFromItem(sc *scope, item ast.Node) error {
sc.rels = append(sc.rels, rel)
return nil
case *ast.JoinExpr:
start := len(sc.rels)
if err := a.appendFromItem(sc, v.Larg); err != nil {
return err
}
return a.appendFromItem(sc, v.Rarg)
mid := len(sc.rels)
if err := a.appendFromItem(sc, v.Rarg); err != nil {
return err
}
// An outer join fills the unmatched side with NULLs, so that side's
// columns can be NULL whatever the table says, unless the dialect
// fills it with defaults instead.
if a.cat.OuterJoinFillsDefaults() {
return nil
}
switch v.Jointype {
case ast.JoinTypeLeft:
markNullable(sc.rels[mid:])
case ast.JoinTypeRight:
markNullable(sc.rels[start:mid])
case ast.JoinTypeFull:
markNullable(sc.rels[start:])
}
return nil
case *ast.RangeFunction:
rel, err := a.bindRangeFunction(v)
if err != nil {
Expand Down Expand Up @@ -83,6 +102,19 @@ func (a *analyzer) appendFromItem(sc *scope, item ast.Node) error {
}
}

// markNullable makes every column of rels nullable. The column lists are the
// catalog's own, so they are copied rather than changed in place.
func markNullable(rels []scopeRel) {
for i := range rels {
cols := make([]core.ClassColumn, len(rels[i].cols))
for j, c := range rels[i].cols {
c.NotNull = false
cols[j] = c
}
rels[i].cols = cols
}
}

// bindRangeFunction binds a function called in FROM. A set-returning function
// stands in for a relation of a single column named after it.
func (a *analyzer) bindRangeFunction(rf *ast.RangeFunction) (scopeRel, error) {
Expand Down
15 changes: 15 additions & 0 deletions internal/core/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const FlagPropagateNullable = "functions.propagate_nullable"
// e.id.
const FlagQualifyDuplicateColumns = "columns.qualify_duplicates"

// FlagOuterJoinDefaults is set for a dialect whose outer join fills the
// unmatched side with each column's default rather than NULL, as ClickHouse
// does unless join_use_nulls is on.
const FlagOuterJoinDefaults = "joins.outer_defaults"

// FlagDefaultSchema holds the schema the dialect puts an unqualified object
// in, when that is not the catalog's own default: a type in it is reported
// without its schema.
Expand Down Expand Up @@ -196,6 +201,16 @@ func (c *Catalog) QualifiesDuplicateColumns() bool {
return v == "true"
}

// OuterJoinFillsDefaults reports whether an outer join fills the unmatched
// side with each column's default rather than NULL.
func (c *Catalog) OuterJoinFillsDefaults() bool {
if c.dialectOID == 0 {
return false
}
v, _ := c.DialectFlag(c.dialectOID, FlagOuterJoinDefaults)
return v == "true"
}

// DefaultNamespaces lists the namespaces a type is reported from without
// qualification: the catalog's default, PostgreSQL's system catalog, and
// the dialect's own default schema when it names one.
Expand Down
10 changes: 10 additions & 0 deletions internal/core/seed/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ type Settings struct {
// ClickHouse names the second id of a join e.id.
QualifyDuplicateColumns bool `json:"qualify_duplicate_columns,omitempty"`

// OuterJoinDefaults keeps the columns of an outer join's unmatched side
// as the table declares them, for a dialect that fills that side with
// each column's default rather than NULL, as ClickHouse does.
OuterJoinDefaults bool `json:"outer_join_defaults,omitempty"`

// Comparison operators are registered as (T, T) -> Bool for every type in
// ComparisonCategories.
Comparison []string `json:"comparison,omitempty"`
Expand Down Expand Up @@ -660,6 +665,11 @@ func (b *builder) consts() error {
return err
}
}
if b.settings.OuterJoinDefaults {
if err := b.cat.SetDialectFlag(b.dialectOID, core.FlagOuterJoinDefaults, "true"); err != nil {
return err
}
}
// A schema declares types the seed knows nothing about — enums, domains,
// arrays, a SQLite column typed whatever the author felt like. Recording
// the comparison operators lets the catalog give those types the same ones.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@
{
"name": "created",
"type": {
"name": "text"
"name": "text",
"nullable": true
},
"table": "posts"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "analyze",
"args": ["--dialect", "duckdb", "--schema", "schema.sql", "query.sql"],
"contexts": ["base"]
}
24 changes: 24 additions & 0 deletions internal/endtoend/testdata/analyze_outer_join/duckdb/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- name: InnerJoin :many
SELECT p.id, s.status
FROM players p
JOIN statuses s ON s.player_id = p.id;

-- name: LeftJoin :many
SELECT p.id, p.name, s.status
FROM players p
LEFT JOIN statuses s ON s.player_id = p.id;

-- name: RightJoin :many
SELECT p.name, s.status
FROM players p
RIGHT JOIN statuses s ON s.player_id = p.id;

-- name: FullJoin :many
SELECT p.name, s.status
FROM players p
FULL JOIN statuses s ON s.player_id = p.id;

-- name: LeftJoinStar :many
SELECT *
FROM players p
LEFT JOIN statuses s ON s.player_id = p.id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE players (
id BIGINT NOT NULL,
name VARCHAR NOT NULL
);

CREATE TABLE statuses (
player_id BIGINT NOT NULL,
status VARCHAR NOT NULL
);
134 changes: 134 additions & 0 deletions internal/endtoend/testdata/analyze_outer_join/duckdb/stdout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
[
{
"name": "InnerJoin",
"cmd": ":many",
"columns": [
{
"name": "id",
"type": {
"name": "bigint"
},
"table": "players"
},
{
"name": "status",
"type": {
"name": "varchar"
},
"table": "statuses"
}
],
"params": []
},
{
"name": "LeftJoin",
"cmd": ":many",
"columns": [
{
"name": "id",
"type": {
"name": "bigint"
},
"table": "players"
},
{
"name": "name",
"type": {
"name": "varchar"
},
"table": "players"
},
{
"name": "status",
"type": {
"name": "varchar",
"nullable": true
},
"table": "statuses"
}
],
"params": []
},
{
"name": "RightJoin",
"cmd": ":many",
"columns": [
{
"name": "name",
"type": {
"name": "varchar",
"nullable": true
},
"table": "players"
},
{
"name": "status",
"type": {
"name": "varchar"
},
"table": "statuses"
}
],
"params": []
},
{
"name": "FullJoin",
"cmd": ":many",
"columns": [
{
"name": "name",
"type": {
"name": "varchar",
"nullable": true
},
"table": "players"
},
{
"name": "status",
"type": {
"name": "varchar",
"nullable": true
},
"table": "statuses"
}
],
"params": []
},
{
"name": "LeftJoinStar",
"cmd": ":many",
"columns": [
{
"name": "id",
"type": {
"name": "bigint"
},
"table": "players"
},
{
"name": "name",
"type": {
"name": "varchar"
},
"table": "players"
},
{
"name": "player_id",
"type": {
"name": "bigint",
"nullable": true
},
"table": "statuses"
},
{
"name": "status",
"type": {
"name": "varchar",
"nullable": true
},
"table": "statuses"
}
],
"params": []
}
]
1 change: 1 addition & 0 deletions internal/engine/clickhouse/dialect/dialect.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"untyped": "Nothing",
"propagate_nullable": true,
"qualify_duplicate_columns": true,
"outer_join_defaults": true,
"comparison": ["=", "==", "<>", "!=", "<", "<=", ">", ">="],
"comparison_categories": "NBSDU",
"arithmetic": ["+", "-", "*", "/", "%"],
Expand Down
Loading