defs.jq 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. # defs.jq — utility functions for working with MIR ASTs
  11. def typename(expr):
  12. if expr | type == "object"
  13. then expr.keys[0]
  14. else expr
  15. end
  16. ;
  17. def is_mir_relexpr:
  18. type == "object" and
  19. ( has("Get")
  20. or has("Constant")
  21. or has("Let")
  22. or has("LetRec")
  23. or has("Project")
  24. or has("Map")
  25. or has("FlatMap")
  26. or has("Filter")
  27. or has("Join")
  28. or has("Reduce")
  29. or has("TopK")
  30. or has("Negate")
  31. or has("Threshold")
  32. or has("Union")
  33. or has("ArrangeBy"))
  34. ;
  35. def ast_node_names:
  36. ((if type == "object"
  37. then [keys[] | select(test("^[A-Z][A-Za-z]*$"))]
  38. else []
  39. end)
  40. + [.[]? | ast_node_names]) | flatten | unique
  41. ;
  42. def iscall(expr):
  43. expr | type == "object" and (has("CallVariadic") or has("CallBinary") or has("CallUnary"))
  44. ;
  45. def iscolumn(expr):
  46. expr | type == "object" and has("Column")
  47. ;
  48. def isliteral(expr):
  49. expr | type == "object" and has("Literal")
  50. ;
  51. def isunion(expr):
  52. expr | type == "object" and has("Union")
  53. ;
  54. def ismap(expr):
  55. expr | type == "object" and has("Map")
  56. ;
  57. def isnegate(expr):
  58. expr | type == "object" and has("Negate")
  59. ;
  60. def isproject(expr):
  61. expr | type == "object" and has("Project")
  62. ;
  63. def parts(expr):
  64. .CallUnary?.expr[]?, .CallBinary?.expr1[]?, .CallBinary?.expr2[]?, .CallVariadic?.exprs[]?
  65. ;
  66. def subexprs(expr):
  67. if expr | type == "object"
  68. then
  69. if has("CallUnary")
  70. then [.CallUnary.expr]
  71. elif has("CallBinary")
  72. then [.CallBinary | .expr1, .expr2 ]
  73. elif has("CallVariadic")
  74. then .exprs[]
  75. else []
  76. end
  77. else []
  78. end
  79. ;
  80. def summarize(expr):
  81. [ expr | group_by(.)[] | { "term": .[0], "occurrences": length } ] | sort_by(.occurrences) | reverse
  82. ;