Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated(computed) Columns #93

Open
kevinresol opened this issue Mar 4, 2020 · 5 comments
Open

Generated(computed) Columns #93

kevinresol opened this issue Mar 4, 2020 · 5 comments

Comments

@kevinresol
Copy link
Member

kevinresol commented Mar 4, 2020

MySQL/Postgres: (Postgres does not support VIRTUAL)

column_name data_type [GENERATED ALWAYS] AS (expression)
   [VIRTUAL | STORED] [UNIQUE [KEY]]

SQL server:

column_name AS expression [PERSISTED]

Proposed Haxe syntax:

@:generated // computed on the fly
@:generated(Stored) or @:generated(true) // store computed value in table
var foo:Int = bar * 2;
@kevinresol
Copy link
Member Author

kevinresol commented Mar 4, 2020

A few notes on implementation:

  1. Generated field should not appear in a insert query
  2. But it should appear in the result of a select query
  3. I think it shouldn't be too hard to convert the expression into CREATE/ALTER TABLE query since we already have the AST at compile time? @benmerckx
  4. How to implement function calls? (e.g. as illustrated in the age-from-dob example here)

@benmerckx
Copy link
Member

Formatting/printing SQL has always been the easy part to tink_sql changes :)
What's the added benefit over making it part of the select (and thereby also supporting databases without computed columns)?

@kevinresol
Copy link
Member Author

kevinresol commented Mar 16, 2020

Maybe my description was confusing, I meant this:

  1. $type(db.Table.insertOne) => {nonComputed:String}->Promise<Int>
  2. $type(db.Table.first()) => Promise<{nonComputed:String, computed:String}>
  3. db.Table.select($type({...})) => {?nonComputed, ?computed}

So what I mean is simply: computed values should be supported as readonly.

@back2dos
Copy link
Member

I can see why this would be useful. OTOH I wonder if it's not easier and more portable to have the computation in application code rather than the database?

@kevinresol
Copy link
Member Author

Computed columns can be used for (speeding up) aggregation. So no, I don't think they are equivalent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants