-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(core): views #5720
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
base: master
Are you sure you want to change the base?
feat(core): views #5720
Conversation
core/src/main/java/io/questdb/griffin/engine/ops/AlterOperation.java
Outdated
Show resolved
Hide resolved
|
|
||
| import static io.questdb.cairo.wal.seq.TableSequencer.NO_TXN; | ||
|
|
||
| public class ViewWalWriter extends WalWriterBase implements AutoCloseable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a specialized WAL writer? Please add a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a comment in 0bae0c4
| } | ||
|
|
||
| @Override | ||
| public void commit() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is nothing to commit; no rows added.It looks like this method does not do anything, can it be throw new NotSupported()? Same with rollback()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes the API a bit strange, but removed rollback() and commit() in 96a8532.
Moved event file sync into replaceViewDefinition().
| final WalEventCursor.ViewDefinitionInfo info = walEventCursor.getViewDefinitionInfo(); | ||
| engine.updateViewDefinition(viewToken, info.getViewSql(), info.getViewDependencies(), seqTxn, blockFileWriter, path); | ||
| } catch (CairoException e) { | ||
| LOG.error().$("could not update view definition [view=").$(viewToken) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the error occurs on the replica, for example, due to an FD limit? How will it go back, in sync with the primary if this transaction is skipped?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 46bbc9f by re-throwing the exception.
View will be suspended, when resumed the view definition should be updated.
| } | ||
| // WAL-E files can be deleted by the purge job after a commit. | ||
| // Update the view state before committing the transaction. | ||
| writer.setSeqTxn(seqTxn); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is redundant, the same call is inside markSeqTxnCommitted()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed redundant setSeqTxn() calls in 1f9c1c4
[PR Coverage check]😍 pass : 2079 / 2342 (88.77%) file detail
|
Support for Database Views
This PR introduces support for database views.
Views are virtual tables defined by a
SELECTstatement. They do not store data themselves; instead, their defining query is executed as a sub-query whenever the view is referenced in a SQL statement.How Views Work in the Engine
In SQL queries views behave like regular tables or materialized views, and can be used anywhere a table is allowed.
During query compilation, if a view is referenced, its defining SQL is compiled and inserted into the execution model as a nested query, allowing the optimizer to consider the entire plan holistically.
Metadata and State Management
Automatic Recompilation
Views are automatically recompiled when operations occur on its dependencies. The view's dependencies are the tables, materialized views or other views referenced in its defining query.
The operations that trigger recompilation include:
CREATE/DROPtable, materialized view, or viewRENAME TABLEDROP/RENAMEcolumnRecompilation always updates the view's state depending on the outcome of the operation.
For example:
Any query that fails will publish a recompilation event for all involved table-like objects too, allowing the view compiler job to refresh the state of all views dependent on the table-like objects involved in the failed query. This also helps to keep the state of the views up-to-date.
Manual View Compilation
The
COMPILE VIEWcommand can be used to manually test if a view is valid.This command will force recompilation of the view, and its state will be updated.
Discoverability
Views can be listed using the
views()command, which also displays their current state.required by https://github.com/questdb/questdb-enterprise/pull/633
TODOs
CREATE VIEW <viewName> AS (query)@glasstigerDROP VIEW <viewName>@glasstigerviews()statement @glasstigerSHOW CREATE VIEW <viewName>@glasstigertables(), introduce table type:T,V,MV@glasstigerlastUpdatedTimestampfield to view state andviews()@glasstigerDECLARE@glasstigerRENAME TABLE viewName@glasstigerCOMPILE VIEW <viewName>to test view state @glasstigerBump WAL and replication format version?ALTER VIEW <viewName> AS (query)@glasstigerALTER VIEW@glasstigerWalWriternot to create empty column files for views, get rid of_metafile too @glasstigerSequencernot to create_metafile for views @glasstigerSELECTpermission check refactoring to allow row level security via views @glasstigerDECLAREoverride for views @glasstigerDECLARE's syntax to allowconstvariables which cannot be overridden with view parameters @jerrinottoday()@jerrinotCREATE OR REPLACE VIEW@glasstigerRemoveALTER VIEWsyntaxexecutionContext.isValidationOnly()handled on all view related SQL commands @glasstigerTableReferenceOutOfDateExceptionif view definition has changed between query compilation and execution. This will make sure factories taken from query cache are recompiled.Issues from review (Javier):
views()should fail whencairo.view.enabledis disabled (currently runs even when CREATE VIEW fails)Views should not be queryable/listed as regular tables when views are disabledselect * from view_with_limit_20 limit 10ignores thelimit 10) @glasstigerORDER BY timestamp, but equivalent CTE works @glasstigertimestamp(timestamp)) appears to be ignoredCOMPILE VIEW view1 AS ...should fail, the syntax should allow onlyCOMPILE VIEW view1@glasstiger