[Accelerated Tables] fetch_only Questions

A couple of questions (out of curiosity) I am having trouble working out the answers to:

  1. What if anything would the effect of using an empty q.fetch_only())

  2. Is there a way to invert fetch_only …essentially don’t fetch for columns that you don’t want cached with out typing out all the column names
    `q.not_(fetch_only(“non_essential_info_column”))

I am sure I could roll my own using the column names in a list and manipulating a list but wondered if there was a neat option here.

I have tried to verify with timings but I have found it a little difficult to confirm (with out this FR any way :smiley: )

1 Like
  1. What if anything would the effect of using an empty q.fetch_only())

This will load a set of references to the rows, but none of the data. This is sometimes useful - eg if you just want to grab all the row IDs, or use them in a query for referenced rows, but don’t care about the data.

Is there a way to invert fetch_only

Not yet! :wink:

3 Likes

This is completely untested, but here is the most concise version I could come up with:

from anvil.tables import app_tables
from anvil.tables import query as q

def q_fetch_none_of(table, column_names_list):
  table = getattr(app_tables, table) if isinstance(table, str) else table
  for row in table.search(q.page_size(1)):
      column_names_list = [x for x in dict(row) if x not in column_names_list]
      break
  return q.fetch_only(*column_names_list)

2 Likes

Nice!!

Riffing on this (also untested) we could use the table.list_columns() function (docs link) and some list comprehension to do it without the search. Presumably this would handle an empty table.

from anvil.tables import app_tables
from anvil.tables import query as q

def q_fetch_none_of(table, column_names_list):
  table = getattr(app_tables, table) if isinstance(table, str) else table
  table_columns = [c['name'] for c in table.list_columns()]
  column_names_list = [x for x in table_columns if x not in column_names_list]
  return q.fetch_only(*column_names_list)

I will test this tomorrow and report back

Thanks @ianb :slight_smile:

4 Likes

Hahaha:


def q_fetch_none_of(table, column_names_list):
  table = getattr(app_tables, table) if isinstance(table, str) else table
  return q.fetch_only(*[c['name'] for c in table.list_columns() if c['name'] not in column_names_list])

:golfing_man: :golf: :golfing_man:

4 Likes

iteration is beautiful :cry:

2 Likes

For readability, may I suggest:

def q_fetch_none_of(table, column_names_to_exclude):
  table = (
    getattr(app_tables, table)  if isinstance(table, str)
    else  table
  )
  return q.fetch_only(*[
    c['name']
    for c in table.list_columns()
    if c['name'] not in column_names_to_exclude
  ])
3 Likes

I agree, I use black as a formatter, so I don’t generally have to increase readability for myself. :slightly_smiling_face:

also github works really well when everything is formatted exactly the same way by an unforgiving robot

1 Like