MCPcopy Create free account
hub / github.com/araddon/qlbridge / joinNodesForFrom

Function joinNodesForFrom

rel/sql_rewrite.go:222–317  ·  view source on GitHub ↗
(stmt *SqlSelect, from *SqlSource, node expr.Node, depth int)

Source from the content-addressed store, hash-verified

220}
221
222func joinNodesForFrom(stmt *SqlSelect, from *SqlSource, node expr.Node, depth int) expr.Node {
223
224 switch nt := node.(type) {
225 case *expr.IdentityNode:
226 if left, right, hasLeft := nt.LeftRight(); hasLeft {
227 //u.Debugf("joinNodesForFrom from.Name:%v l:%v r:%v", from.alias, left, right)
228 if left == from.alias {
229 identNode := expr.IdentityNode{Text: right}
230 //u.Debugf("%d nice, found it! identnode=%q fromnode:%q", depth, identNode.String(), nt.String())
231 if depth == 1 {
232 from.joinNodes = append(from.joinNodes, &identNode)
233 return nil
234 }
235 return &identNode
236 } else {
237 // This is for other side of join, ignore
238 //u.Warnf("what to do? source:%v %v", from.alias, nt.String())
239 }
240 } else {
241 u.Warnf("dropping join expr node: %q", nt.String())
242 }
243 case *expr.NumberNode, *expr.NullNode, *expr.StringNode, *expr.ValueNode:
244 //u.Warnf("skipping? %v", nt.String())
245 return nt
246 case *expr.FuncNode:
247 //u.Warnf("%v try join from func node: %v", depth, nt.String())
248 args := make([]expr.Node, len(nt.Args))
249 for i, arg := range nt.Args {
250 args[i] = rewriteNode(from, arg)
251 if args[i] == nil {
252 // What???
253 //u.Infof("error, from:%q arg:%q", from.String(), arg.String())
254 return nil
255 }
256 }
257 fn := expr.NewFuncNode(nt.Name, nt.F)
258 fn.Args = args
259 if depth == 1 {
260 //u.Infof("adding func: %s", fn.String())
261 from.joinNodes = append(from.joinNodes, fn)
262 return nil
263 }
264 return fn
265 case *expr.BinaryNode:
266 //u.Infof("%v binaryNode %v", depth, nt.String())
267 switch nt.Operator.T {
268 case lex.TokenAnd, lex.TokenLogicAnd, lex.TokenLogicOr:
269 n1 := joinNodesForFrom(stmt, from, nt.Args[0], depth+1)
270 n2 := joinNodesForFrom(stmt, from, nt.Args[1], depth+1)
271
272 if n1 != nil && n2 != nil {
273 //u.Debugf("%d neither nil: n1=%v n2=%v %q", depth, n1, n2, nt.String())
274 //return &BinaryNode{Operator: nt.Operator, Args: [2]Node{n1, n2}}
275 } else if n1 != nil {
276 //u.Debugf("%d n1 not nil: n1=%v n2=%v %q", depth, n1, n2, nt.String())
277 return n1
278 } else if n2 != nil {
279 //u.Debugf("%d n2 not nil n1=%v n2=%v %q", depth, n1, n2, nt.String())

Callers 1

RewriteSqlSourceFunction · 0.85

Calls 4

NewFuncNodeFunction · 0.92
rewriteNodeFunction · 0.85
StringMethod · 0.65
LeftRightMethod · 0.45

Tested by

no test coverage detected