0

In Python, I can unpack a list into individual variables:

>>> name,age,date = ['Bob',20,'2025-1-1']
>>> name
'Bob'
>>> age
20
>>> date
'2025-1-1'

In DolphinDB, I want to achieve a similar “unpacking” operation for column names stored in a list. For example, given a table T and a name_set list containing column names, how can I dynamically unpack these names into a SQL query to select those columns?

T = table(1..3 as id, 2..4 as val1, 3..5 as val2, 4..6 as val3)
name_set = ["val1", "val2", "val3"]

// Goal: Equivalent to `select val1, val2, val3 from T`
select *name_set from T  // Hypothetical syntax (not working)

I have tried directly using select *name_set fails because DolphinDB does not support unpacking syntax like Python. I’ve explored using meta programming but haven’t found a clear way to dynamically unpack column names from a list.
How can I programmatically expand the name_set list into column names in a DolphinDB SQL query, similar to Python’s unpacking?

1
  • How are you querying the DB? From Python, from CLI, etc? Commented Jul 29 at 10:07

1 Answer 1

1

DolphinDB is a database system. You use it to store entities (customers, orders, items, countries, ...) and their relations. The entities are stored in tables and their attributes in columns of these tables. In order to deal with the entities, attributes and relations, the DBMS stores their names.

So there is a reason the DBMS stores column names, there should be no reason, however, for you to do so. If you do, this is likely an indicator for an inappropriate database model where you mix data and structure. It is very, very rare that you really need such a mix. I think, in my 30+ years as a database developer, I only saw this once or twice in a production database.

If you are in such a rare situation where you must build your queries dynamically based on meta data then you are looking for "dynamic SQL". Generally this is simply a program written in a programming language reading meta data like column names from the database, using this to build a SQL string and then run this created final query in the DBMS to get the desired data from it.

Depending on the DBMS you are using, the DBMS itself may offer ways to get dynamic SQL with built-in functionality. For DolphinSQL this is the case. They call this "SQL Metaprogramming" and the documentation can be found here: https://docs.dolphindb.com/en/Programming/Metaprogramming/sql_meta.html

Again: You may not actually need this. Check this before continuing, because with an inappropriate data model you may be forced again and again to find complex ways to solve tasks that would be simple, if you only used the DBMS as it is meant to be used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.