Skip to content

Commit

Permalink
Rename FromKnot -> FromIterate, IntJoin -> RoutedJoin
Browse files Browse the repository at this point in the history
  • Loading branch information
xitology committed Feb 15, 2024
1 parent 96b4497 commit 597dbfb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 74 deletions.
10 changes: 5 additions & 5 deletions src/link.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function dismantle(n::FromFunctionNode, ctx)
FromFunction(over = over′, columns = n.columns)
end

dismantle(n::Union{FromKnotNode, FromNothingNode, FromTableExpressionNode, FromTableNode, FromValuesNode}, ctx) =
dismantle(n::Union{FromIterateNode, FromNothingNode, FromTableExpressionNode, FromTableNode, FromValuesNode}, ctx) =
convert(SQLNode, n)

function dismantle_scalar(n::FunctionNode, ctx)
Expand All @@ -133,7 +133,7 @@ function dismantle(n::JoinNode, ctx)
over′ = dismantle(n.over, ctx)
joinee′ = dismantle(n.joinee, ctx)
on′ = dismantle_scalar(n.on, ctx)
IntJoin(over = over′, joinee = joinee′, on = on′, router = router, left = n.left, right = n.right, optional = n.optional)
RoutedJoin(over = over′, joinee = joinee′, on = on′, router = router, left = n.left, right = n.right, optional = n.optional)
end

function dismantle(n::LimitNode, ctx)
Expand Down Expand Up @@ -268,7 +268,7 @@ end
link(n::Union{FromFunctionNode, FromNothingNode, FromTableNode, FromValuesNode}, ctx) =
convert(SQLNode, n)

function link(n::FromKnotNode, ctx)
function link(n::FromIterateNode, ctx)
append!(ctx.knot_refs, ctx.refs)
n
end
Expand Down Expand Up @@ -358,7 +358,7 @@ function route(r::JoinRouter, ref::SQLNode)
return -1
end

function link(n::IntJoinNode, ctx)
function link(n::RoutedJoinNode, ctx)
lrefs = SQLNode[]
rrefs = SQLNode[]
for ref in ctx.refs
Expand All @@ -382,7 +382,7 @@ function link(n::IntJoinNode, ctx)
end
over′ = Linked(lrefs, ln_ext_refs, over = link(n.over, ctx, lrefs))
joinee′ = Linked(rrefs, rn_ext_refs, over = link(n.joinee, ctx, rrefs))
IntJoinNode(
RoutedJoinNode(
over = over′,
joinee = joinee′,
on = n.on,
Expand Down
12 changes: 6 additions & 6 deletions src/nodes/from.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
abstract type AbstractSource
end

struct KnotSource <: AbstractSource
struct IterateSource <: AbstractSource
end

struct ValuesSource <: AbstractSource
Expand All @@ -15,17 +15,17 @@ struct FunctionSource <: AbstractSource
columns::Vector{Symbol}
end

_from_source(source::Union{SQLTable, KnotSource, FunctionSource, ValuesSource, Nothing}) =
_from_source(source::Union{SQLTable, IterateSource, FunctionSource, ValuesSource, Nothing}) =
source

_from_source(source::AbstractString) =
Symbol(source)

_from_source(source::Symbol) =
source === :^ ? KnotSource() : source
source === :^ ? IterateSource() : source

_from_source(::typeof(^)) =
KnotSource()
IterateSource()

function _from_source(node::AbstractSQLNode;
columns::AbstractVector{<:Union{Symbol, AbstractString}})
Expand All @@ -51,7 +51,7 @@ function _from_source(source)
end

mutable struct FromNode <: TabularNode
source::Union{SQLTable, Symbol, KnotSource, ValuesSource, FunctionSource, Nothing}
source::Union{SQLTable, Symbol, IterateSource, ValuesSource, FunctionSource, Nothing}

FromNode(; source, kws...) =
new(_from_source(source; kws...))
Expand Down Expand Up @@ -230,7 +230,7 @@ function PrettyPrinting.quoteof(n::FromNode, ctx::QuoteContext)
Expr(:call, nameof(From), tex)
elseif source isa Symbol
Expr(:call, nameof(From), QuoteNode(source))
elseif source isa KnotSource
elseif source isa IterateSource
Expr(:call, nameof(From), :^)
elseif source isa ValuesSource
Expr(:call, nameof(From), quoteof(source.columns, ctx))
Expand Down
30 changes: 15 additions & 15 deletions src/nodes/internal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ dissect(scr::Symbol, ::typeof(Nested), pats::Vector{Any}) =
PrettyPrinting.quoteof(n::NestedNode, ctx::QuoteContext) =
Expr(:call, nameof(Nested), Expr(:kw, :over, quoteof(n.over, ctx)), Expr(:kw, :name, QuoteNode(n.name)))

# Var() that has the corresponding Bind()
# Var() that found the corresponding Bind()
mutable struct BoundVariableNode <: AbstractSQLNode
name::Symbol
depth::Int
Expand All @@ -123,7 +123,7 @@ PrettyPrinting.quoteof(n::BoundVariableNode, ctx::QuoteContext) =
Expr(:call, nameof(BoundVariable), QuoteNode(n.name), n.depth)

# A generic From node is specialized to FromNothing, FromTable,
# FromTableExpression, FromKnot, FromValues, or FromFunction.
# FromTableExpression, FromIterate, FromValues, or FromFunction.
mutable struct FromNothingNode <: TabularNode
end

Expand Down Expand Up @@ -168,14 +168,14 @@ FromTableExpression(args...; kws...) =
PrettyPrinting.quoteof(n::FromTableExpressionNode, ctx::QuoteContext) =
Expr(:call, nameof(FromTableExpression), QuoteNode(n.name), n.depth)

mutable struct FromKnotNode <: TabularNode
mutable struct FromIterateNode <: TabularNode
end

FromKnot(args...; kws...) =
FromKnotNode(args...; kws...) |> SQLNode
FromIterate(args...; kws...) =
FromIterateNode(args...; kws...) |> SQLNode

PrettyPrinting.quoteof(n::FromKnotNode, ctx::QuoteContext) =
Expr(:call, nameof(FromKnot))
PrettyPrinting.quoteof(n::FromIterateNode, ctx::QuoteContext) =
Expr(:call, nameof(FromIterate))

mutable struct FromValuesNode <: TabularNode
columns::NamedTuple
Expand Down Expand Up @@ -216,7 +216,7 @@ end
PrettyPrinting.quoteof(r::JoinRouter) =
Expr(:call, nameof(JoinRouter), quoteof(r.label_set), quoteof(r.group))

mutable struct IntJoinNode <: TabularNode
mutable struct RoutedJoinNode <: TabularNode
over::Union{SQLNode, Nothing}
joinee::SQLNode
on::SQLNode
Expand All @@ -226,18 +226,18 @@ mutable struct IntJoinNode <: TabularNode
lateral::Bool
optional::Bool

IntJoinNode(; over, joinee, on, router, left, right, lateral = false, optional = false) =
RoutedJoinNode(; over, joinee, on, router, left, right, lateral = false, optional = false) =
new(over, joinee, on, router, left, right, lateral, optional)
end

IntJoinNode(joinee, on; over = nothing, router, left = false, right = false, lateral = false, optional = false) =
IntJoinNode(over = over, joinee = joinee, on = on, router, left = left, right = right, lateral = lateral, optional = optional)
RoutedJoinNode(joinee, on; over = nothing, router, left = false, right = false, lateral = false, optional = false) =
RoutedJoinNode(over = over, joinee = joinee, on = on, router, left = left, right = right, lateral = lateral, optional = optional)

IntJoin(args...; kws...) =
IntJoinNode(args...; kws...) |> SQLNode
RoutedJoin(args...; kws...) =
RoutedJoinNode(args...; kws...) |> SQLNode

function PrettyPrinting.quoteof(n::IntJoinNode, ctx::QuoteContext)
ex = Expr(:call, nameof(IntJoin))
function PrettyPrinting.quoteof(n::RoutedJoinNode, ctx::QuoteContext)
ex = Expr(:call, nameof(RoutedJoin))
if !ctx.limit
push!(ex.args, quoteof(n.joinee, ctx))
push!(ex.args, quoteof(n.on, ctx))
Expand Down
6 changes: 3 additions & 3 deletions src/resolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ resolve(ns::Vector{SQLNode}, ctx) =
function resolve(::Nothing, ctx)
t = ctx.knot_type
if t !== nothing && ctx.implicit_knot
n = FromKnot()
n = FromIterate()
else
n = FromNothing()
t = EMPTY_ROW
Expand Down Expand Up @@ -278,15 +278,15 @@ function resolve(n::FromNode, ctx)
n′ = FromTable(table = table)
t = RowType(table)
end
elseif source isa KnotSource
elseif source isa IterateSource
t = ctx.knot_type
if t === nothing
throw(
ReferenceError(
REFERENCE_ERROR_TYPE.INVALID_SELF_REFERENCE,
path = get_path(ctx)))
end
n′ = FromKnot()
n′ = FromIterate()
elseif source isa ValuesSource
n′ = FromValues(columns = source.columns)
fields = FieldTypeMap()
Expand Down
90 changes: 45 additions & 45 deletions src/translate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ function assemble(n::FromFunctionNode, ctx)
Assemblage(label(n.over), c, cols = cols, repl = repl)
end

function assemble(n::FromKnotNode, ctx)
function assemble(n::FromIterateNode, ctx)
cte_a = ctx.ctes[ctx.knot]
name = cte_a.a.name
alias = allocate_alias(ctx, name)
Expand Down Expand Up @@ -635,50 +635,6 @@ function assemble(n::GroupNode, ctx)
return Assemblage(base.name, c, cols = cols, repl = repl)
end

function assemble(n::IntJoinNode, ctx)
left = assemble(n.over, ctx)
if @dissect(left.clause, tail := FROM() || JOIN())
left_alias = nothing
else
left_alias = allocate_alias(ctx, left)
tail = FROM(AS(over = complete(left), name = left_alias))
end
lateral = n.lateral
subs = make_subs(left, left_alias)
if lateral
right = assemble(n.joinee, TranslateContext(ctx, subs = subs))
else
right = assemble(n.joinee, ctx)
end
if @dissect(right.clause, (joinee := (nothing || nothing |> ID()) |> ID() |> AS(name = right_alias, columns = nothing)) |> FROM()) ||
@dissect(right.clause, (joinee := nothing |> ID(name = right_alias)) |> FROM()) ||
@dissect(right.clause, (joinee := FUN() |> AS(name = right_alias)) |> FROM())
for (ref, name) in right.repl
subs[ref] = right.cols[name]
end
if ctx.dialect.has_implicit_lateral
lateral = false
end
else
right_alias = allocate_alias(ctx, right)
joinee = AS(over = complete(right), name = right_alias)
right_cache = Dict{Symbol, SQLClause}()
for (ref, name) in right.repl
subs[ref] = get(right_cache, name) do
ID(over = right_alias, name = name)
end
end
end
on = translate(n.on, ctx, subs)
c = JOIN(over = tail, joinee = joinee, on = on, left = n.left, right = n.right, lateral = lateral)
trns = Pair{SQLNode, SQLClause}[]
for ref in ctx.refs
push!(trns, ref => subs[ref])
end
repl, cols = make_repl_cols(trns)
Assemblage(left.name, c, cols = cols, repl = repl)
end

function assemble(n::IterateNode, ctx)
ctx′ = TranslateContext(ctx, vars = Base.ImmutableDict{Tuple{Symbol, Int}, SQLClause}())
left = assemble(n.over, ctx)
Expand Down Expand Up @@ -866,6 +822,50 @@ function assemble(n::PartitionNode, ctx)
Assemblage(base.name, c, cols = cols, repl = repl)
end

function assemble(n::RoutedJoinNode, ctx)
left = assemble(n.over, ctx)
if @dissect(left.clause, tail := FROM() || JOIN())
left_alias = nothing
else
left_alias = allocate_alias(ctx, left)
tail = FROM(AS(over = complete(left), name = left_alias))
end
lateral = n.lateral
subs = make_subs(left, left_alias)
if lateral
right = assemble(n.joinee, TranslateContext(ctx, subs = subs))
else
right = assemble(n.joinee, ctx)
end
if @dissect(right.clause, (joinee := (nothing || nothing |> ID()) |> ID() |> AS(name = right_alias, columns = nothing)) |> FROM()) ||
@dissect(right.clause, (joinee := nothing |> ID(name = right_alias)) |> FROM()) ||
@dissect(right.clause, (joinee := FUN() |> AS(name = right_alias)) |> FROM())
for (ref, name) in right.repl
subs[ref] = right.cols[name]
end
if ctx.dialect.has_implicit_lateral
lateral = false
end
else
right_alias = allocate_alias(ctx, right)
joinee = AS(over = complete(right), name = right_alias)
right_cache = Dict{Symbol, SQLClause}()
for (ref, name) in right.repl
subs[ref] = get(right_cache, name) do
ID(over = right_alias, name = name)
end
end
end
on = translate(n.on, ctx, subs)
c = JOIN(over = tail, joinee = joinee, on = on, left = n.left, right = n.right, lateral = lateral)
trns = Pair{SQLNode, SQLClause}[]
for ref in ctx.refs
push!(trns, ref => subs[ref])
end
repl, cols = make_repl_cols(trns)
Assemblage(left.name, c, cols = cols, repl = repl)
end

function assemble(n::SelectNode, ctx)
base = assemble(n.over, ctx)
if !@dissect(base.clause, SELECT() || UNION())
Expand Down

0 comments on commit 597dbfb

Please sign in to comment.