diff --git a/internal/core/analyzer/scope.go b/internal/core/analyzer/scope.go index 218a8e0b11..ed9b674df0 100644 --- a/internal/core/analyzer/scope.go +++ b/internal/core/analyzer/scope.go @@ -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 { @@ -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) { diff --git a/internal/core/dialect.go b/internal/core/dialect.go index e192c442c9..d3ce94e15a 100644 --- a/internal/core/dialect.go +++ b/internal/core/dialect.go @@ -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. @@ -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. diff --git a/internal/core/seed/seed.go b/internal/core/seed/seed.go index 064afedae8..86eba13ea9 100644 --- a/internal/core/seed/seed.go +++ b/internal/core/seed/seed.go @@ -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"` @@ -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. diff --git a/internal/endtoend/testdata/analyze_expressions/sqlite/stdout.json b/internal/endtoend/testdata/analyze_expressions/sqlite/stdout.json index 24326932cd..18d046f31a 100644 --- a/internal/endtoend/testdata/analyze_expressions/sqlite/stdout.json +++ b/internal/endtoend/testdata/analyze_expressions/sqlite/stdout.json @@ -182,7 +182,8 @@ { "name": "created", "type": { - "name": "text" + "name": "text", + "nullable": true }, "table": "posts" } diff --git a/internal/endtoend/testdata/analyze_outer_join/duckdb/exec.json b/internal/endtoend/testdata/analyze_outer_join/duckdb/exec.json new file mode 100644 index 0000000000..56cb4b3fff --- /dev/null +++ b/internal/endtoend/testdata/analyze_outer_join/duckdb/exec.json @@ -0,0 +1,5 @@ +{ + "command": "analyze", + "args": ["--dialect", "duckdb", "--schema", "schema.sql", "query.sql"], + "contexts": ["base"] +} diff --git a/internal/endtoend/testdata/analyze_outer_join/duckdb/query.sql b/internal/endtoend/testdata/analyze_outer_join/duckdb/query.sql new file mode 100644 index 0000000000..cc2a64c7be --- /dev/null +++ b/internal/endtoend/testdata/analyze_outer_join/duckdb/query.sql @@ -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; diff --git a/internal/endtoend/testdata/analyze_outer_join/duckdb/schema.sql b/internal/endtoend/testdata/analyze_outer_join/duckdb/schema.sql new file mode 100644 index 0000000000..493d64af88 --- /dev/null +++ b/internal/endtoend/testdata/analyze_outer_join/duckdb/schema.sql @@ -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 +); diff --git a/internal/endtoend/testdata/analyze_outer_join/duckdb/stdout.json b/internal/endtoend/testdata/analyze_outer_join/duckdb/stdout.json new file mode 100644 index 0000000000..1b6828dc7e --- /dev/null +++ b/internal/endtoend/testdata/analyze_outer_join/duckdb/stdout.json @@ -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": [] + } +] diff --git a/internal/engine/clickhouse/dialect/dialect.json b/internal/engine/clickhouse/dialect/dialect.json index 51cb0b641a..4bda797756 100644 --- a/internal/engine/clickhouse/dialect/dialect.json +++ b/internal/engine/clickhouse/dialect/dialect.json @@ -12,6 +12,7 @@ "untyped": "Nothing", "propagate_nullable": true, "qualify_duplicate_columns": true, + "outer_join_defaults": true, "comparison": ["=", "==", "<>", "!=", "<", "<=", ">", ">="], "comparison_categories": "NBSDU", "arithmetic": ["+", "-", "*", "/", "%"],