Skip to content

Improve the output of the enhanced Arel AST #113

@mvgijssel

Description

@mvgijssel

Currently for SELECT * FROM posts the following enhanced AST is printed. This is a huge amount of lines for a simple statement and there's probably some compacting that can be done here.

For example

  • Do not render SQL?
  • Make scalar nodes (true, false, string, etc) more compact
    • not render parent
    • not render at all?

Let's see what can be removed but still resulting in a useful printed AST.

Secondly, the paths printed in the inspect are absolute and full paths. It is useful is the path that's printed is related to the current node and not the absolute path relative to the root node.

node.ast.cores

This can also be an opportunity to reuse the to_sql_and_binds for to_sql added in #128.

=> <Node(Arel::SelectManager) []
  sql = SELECT * FROM "posts"
  parent = nil
  ast =
    <Node(Arel::Nodes::SelectStatement) ['ast']
      sql = SELECT * FROM "posts"
      parent = Node(Arel::SelectManager)
      cores =
        <Node(Array) ['ast', 'cores']
          sql = SELECT * FROM "posts"
          parent = Node(Arel::Nodes::SelectStatement)
          0 =
            <Node(Arel::Nodes::SelectCore) ['ast', 'cores', 0]
              sql = SELECT * FROM "posts"
              parent = Node(Array)
              source =
                <Node(Arel::Nodes::JoinSource) ['ast', 'cores', 0, 'source']
                  sql = "posts"
                  parent = Node(Arel::Nodes::SelectCore)
                  left =
                    <Node(Arel::Table) ['ast', 'cores', 0, 'source', 'left']
                      sql = "posts"
                      parent = Node(Arel::Nodes::JoinSource)
                      name =
                        <Node(String) ['ast', 'cores', 0, 'source', 'left', 'name']
                          parent = Node(Arel::Table)
                          value = "posts">
                      only =
                        <Node(FalseClass) ['ast', 'cores', 0, 'source', 'left', 'only']
                          parent = Node(Arel::Table)
                          value = false>
                      schema_name =
                        <Node(NilClass) ['ast', 'cores', 0, 'source', 'left', 'schema_name']
                          parent = Node(Arel::Table)
                          value = nil>
                      relpersistence =
                        <Node(String) ['ast', 'cores', 0, 'source', 'left', 'relpersistence']
                          parent = Node(Arel::Table)
                          value = "p">
                    >
                  right =
                    <Node(Array) ['ast', 'cores', 0, 'source', 'right']
                      parent = Node(Arel::Nodes::JoinSource)
                      value = nil>
                >
              projections =
                <Node(Array) ['ast', 'cores', 0, 'projections']
                  sql = *
                  parent = Node(Arel::Nodes::SelectCore)
                  0 =
                    <Node(Arel::Nodes::UnqualifiedColumn) ['ast', 'cores', 0, 'projections', 0]
                      sql = *
                      parent = Node(Array)
                      expr =
                        <Node(Arel::Attributes::Attribute) ['ast', 'cores', 0, 'projections', 0, 'expr']
                          sql = *
                          parent = Node(Arel::Nodes::UnqualifiedColumn)
                          relation =
                            <Node(NilClass) ['ast', 'cores', 0, 'projections', 0, 'expr', 'relation']
                              parent = Node(Arel::Attributes::Attribute)
                              value = nil>
                          name =
                            <Node(Arel::Nodes::SqlLiteral) ['ast', 'cores', 0, 'projections', 0, 'expr', 'name']
                              parent = Node(Arel::Attributes::Attribute)
                              value = "*">
                        >
                    >
                >
              wheres =
                <Node(Array) ['ast', 'cores', 0, 'wheres']
                  parent = Node(Arel::Nodes::SelectCore)
                  value = nil>
              windows =
                <Node(Array) ['ast', 'cores', 0, 'windows']
                  parent = Node(Arel::Nodes::SelectCore)
                  value = nil>
              into =
                <Node(NilClass) ['ast', 'cores', 0, 'into']
                  parent = Node(Arel::Nodes::SelectCore)
                  value = nil>
            >
        >
      limit =
        <Node(NilClass) ['ast', 'limit']
          parent = Node(Arel::Nodes::SelectStatement)
          value = nil>
      orders =
        <Node(Array) ['ast', 'orders']
          parent = Node(Arel::Nodes::SelectStatement)
          value = nil>
      offset =
        <Node(NilClass) ['ast', 'offset']
          parent = Node(Arel::Nodes::SelectStatement)
          value = nil>
      lock =
        <Node(NilClass) ['ast', 'lock']
          parent = Node(Arel::Nodes::SelectStatement)
          value = nil>
      with =
        <Node(NilClass) ['ast', 'with']
          parent = Node(Arel::Nodes::SelectStatement)
          value = nil>
      union =
        <Node(NilClass) ['ast', 'union']
          parent = Node(Arel::Nodes::SelectStatement)
          value = nil>
      values_lists =
        <Node(NilClass) ['ast', 'values_lists']
          parent = Node(Arel::Nodes::SelectStatement)
          value = nil>
    >
>

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions