question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat: create a table of literal values

See original GitHub issue

I’d like to be able to create a table from literal values. This can be handy for small utility tables that I might like to join to. It’s also handy for test scripts to avoid having to create physical tables when not needed.

One possible way to implement this would be with the UNNEST operator (https://github.com/ibis-project/ibis/issues/1146), though there may be other ways of accomplishing this.

Expected Ibis usage

With UNNEST support, I’d expect it to be used in combination with an array literal or struct literals.

table = ibis.unnest(
  ibis.array(
    [
      ibis.struct(dict(col1='hello', col2=1), type='struct<col1: string, col2: integer>'),
      ibis.struct(dict(col1='world', col2=2), type='struct<col1: string, col2: integer>'),
      ibis.struct(dict(col1='!', col2=3), type='struct<col1: string, col2: integer>'),
    ]
  )
)

Equivalent BigQuery SQL

Using the UNNEST operator (reference) on an array literal of struct literals results in an anonymous table-valued expression.

SELECT col1, col2
FROM UNNEST(
  [
    STRUCT<col1 STRING, col2 INT64>("hello", 1),
    STRUCT<col1 STRING, col2 INT64>("world", 2),
    STRUCT<col1 STRING, col2 INT64>("!", 3)
  ]
)
-- Optionally, include a table name via the alias feature
AS my_temp_table
Row col1 col2
1 hello 1
2 world 2
3 ! 3

Alternative Ibis expression

Alternatively, maybe ibis.table could take an optional values parameter? In the BigQuery dialect, I’d still expect this to compile to an UNNEST operator.

table = ibis.table(
  values=[
      ibis.struct(dict(col1='hello', col2=1), type='struct<col1: string, col2: integer>'),
      ibis.struct(dict(col1='world', col2=2), type='struct<col1: string, col2: integer>'),
      ibis.struct(dict(col1='!', col2=3), type='struct<col1: string, col2: integer>'),
  ],
)

See also

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cpcloudcommented, Aug 5, 2022

However, I would be in favor of a top-level ibis.rows() API that produces a Rows node that would be compiled to VALUES in SQL. The pandas backend can call pd.DataFrame on the input during execution.

0reactions
cpcloudcommented, Aug 5, 2022

I suspect that table-valued-functions will require some breaking changes to the SQL compilers to support custom TableNode instances. Right now, our handling of tables is essentially hard-coded.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can you define "literal" tables in SQL? - Stack Overflow
I suppose you could do a subquery with several SELECT s combined with UNION s. SELECT a, b, c, d FROM ( SELECT...
Read more >
Easy Initializing for Records and Arrays - Oracle Blogs
For a nested table or varray type, I simply pass in a list of expressions, and the function returns a collection populated with...
Read more >
ES6 Template Literals (Template Strings) - CanIUse
Template literals are string literals allowing embedded expressions using backtick characters (`). You can use multi-line strings and string interpolation ...
Read more >
Sequence Features - NCBI
A feature table is a set of Seq-feat gathered together within a Seq-annot (see Biological Sequences). The Seq-annot allows the features to be...
Read more >
Table Literals - Snowflake Documentation
Table literals are used to pass the name of a table or a placeholder value (instead of a table name) to a query....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found