-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the ability to create (materialized) views of queries (#77)
* Added the ability to create (materialized) views of queries * Stylefix * Fixed typo * Fixed another typo
- Loading branch information
1 parent
99ca7a1
commit 8abc48c
Showing
9 changed files
with
304 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import logging | ||
|
||
from dask_sql.physical.rel.base import BaseRelPlugin | ||
from dask_sql.datacontainer import DataContainer | ||
from dask_sql.mappings import sql_to_python_value | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CreateAsPlugin(BaseRelPlugin): | ||
""" | ||
Create a table or view from the given SELECT query | ||
and register it at the context. | ||
The SQL call looks like | ||
CREATE TABLE <table-name> AS | ||
<some select query> | ||
It sends the select query through the normal parsing | ||
and optimization and conversation before registering it. | ||
Using this SQL is equivalent to just doing | ||
df = context.sql("<select query>") | ||
context.create_table(<table-name>, df) | ||
but can also be used without writing a single line of code. | ||
Nothing is returned. | ||
""" | ||
|
||
class_name = "com.dask.sql.parser.SqlCreateTableAs" | ||
|
||
def convert( | ||
self, sql: "org.apache.calcite.sql.SqlNode", context: "dask_sql.Context" | ||
) -> DataContainer: | ||
sql_select = sql.getSelect() | ||
table_name = str(sql.getTableName()) | ||
persist = bool(sql.isPersist()) | ||
|
||
logger.debug( | ||
f"Creating new table with name {table_name} and query {sql_select}" | ||
) | ||
|
||
sql_select_query = context._to_sql_string(sql_select) | ||
df = context.sql(sql_select_query) | ||
|
||
context.create_table(table_name, df, persist=persist) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
planner/src/main/java/com/dask/sql/parser/SqlCreateTableAs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.dask.sql.parser; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.calcite.sql.SqlCall; | ||
import org.apache.calcite.sql.SqlIdentifier; | ||
import org.apache.calcite.sql.SqlNode; | ||
import org.apache.calcite.sql.SqlOperator; | ||
import org.apache.calcite.sql.SqlSelect; | ||
import org.apache.calcite.sql.SqlWriter; | ||
import org.apache.calcite.sql.parser.SqlParserPos; | ||
|
||
public class SqlCreateTableAs extends SqlCall { | ||
final SqlIdentifier tableName; | ||
final SqlSelect select; | ||
final boolean persist; | ||
|
||
public SqlCreateTableAs(final SqlParserPos pos, final SqlIdentifier tableName, final SqlSelect select, | ||
final boolean persist) { | ||
super(pos); | ||
this.tableName = tableName; | ||
this.select = select; | ||
this.persist = persist; | ||
} | ||
|
||
@Override | ||
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) { | ||
writer.keyword("CREATE"); | ||
if (this.persist) { | ||
writer.keyword("TABLE"); | ||
} else { | ||
writer.keyword("VIEW"); | ||
} | ||
this.tableName.unparse(writer, leftPrec, rightPrec); | ||
writer.keyword("AS"); | ||
this.select.unparse(writer, leftPrec, rightPrec); | ||
} | ||
|
||
@Override | ||
public SqlOperator getOperator() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public List<SqlNode> getOperandList() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public SqlIdentifier getTableName() { | ||
return this.tableName; | ||
} | ||
|
||
public SqlSelect getSelect() { | ||
return this.select; | ||
} | ||
|
||
public boolean isPersist() { | ||
return this.persist; | ||
} | ||
} |
Oops, something went wrong.