leftjoins.jq 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. #
  10. # leftjoins.jq — find unsimplified left joins
  11. include "defs";
  12. # PATTERN (in EXPLAIN syntax):
  13. #
  14. # Union // { arity: 13 }
  15. # Map (null, null) // { arity: 13 }
  16. # Union // { arity: 11 }
  17. # Negate // { arity: 11 }
  18. # Project (#0..=#10) // { arity: 11 }
  19. # Get l9 // { arity: 13 }
  20. # Get l8 // { arity: 11 }
  21. # Get l9 // { arity: 13 }
  22. def isleftjoin(expr):
  23. isunion(expr)
  24. # check antijoin
  25. and ismap(expr.Union.base) # TODO check for Nulls being tacked on there
  26. and isunion(expr.Union.base.Map.input)
  27. and (expr.Union.base.Map.input.Union.inputs | length == 1)
  28. and isnegate(expr.Union.base.Map.input.Union.base)
  29. and isproject(expr.Union.base.Map.input.Union.base.Negate.input)
  30. # check outer term
  31. and (expr.Union.inputs | length == 1)
  32. # check moral identity of negated inner term and outer term
  33. ;
  34. [ ..
  35. | select(isleftjoin(.))
  36. | { "negated": .Union.base.Map.input.Union.base.Negate.input.Project.input,
  37. "outer": .Union.inputs[0],
  38. "inner": .Union.base.Map.input.Union.inputs[0] }
  39. #| select(.outer == .negated)
  40. #| { outer, inner }
  41. ] | length