8000 Fix create table as select by goccy · Pull Request #39 · goccy/go-zetasqlite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix create table as select #39

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

Merged
merged 2 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ CREATE TABLE _table_a (
timeValue TIME,
timestampValue TIMESTAMP
)`,
},
{
name: "create table as select",
query: `
CREATE TABLE foo ( id STRING, name STRING );
CREATE TABLE bar ( id STRING, name STRING );
CREATE OR REPLACE TABLE new_table_as_select AS (
SELECT t1.id, t2.name FROM foo t1 JOIN bar t2 ON t1.id = t2.id
);
`,
},
{
name: "recreate table",
Expand Down
1 change: 1 addition & 0 deletions internal/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ func (it *AnalyzerOutputIterator) Analyze(ctx context.Context) (*AnalyzerOutput,
case ast.CreateTableStmt:
return it.analyzeCreateTableStmt(ctx, stmtNode.(*ast.CreateTableStmtNode))
case ast.CreateTableAsSelectStmt:
ctx = withUseColumnID(ctx)
return it.analyzeCreateTableAsSelectStmt(ctx, stmtNode.(*ast.CreateTableAsSelectStmtNode))
case ast.CreateFunctionStmt:
return it.analyzeCreateFunctionStmt(ctx, stmtNode.(*ast.CreateFunctionStmtNode))
Expand Down
9 changes: 8 additions & 1 deletion internal/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,19 @@ func newTableSpec(namePath []string, stmt *ast.CreateTableStmtNode) *TableSpec {
}

func newTableAsSelectSpec(namePath []string, query string, stmt *ast.CreateTableAsSelectStmtNode) *TableSpec {
var outputColumns []string
for _, column := range stmt.OutputColumnList() {
outputColumns = append(
outputColumns,
fmt.Sprintf("`%[1]s#%[2]d` AS `%[1]s`", column.Name(), column.Column().ColumnID()),
)
}
return &TableSpec{
IsTemp: stmt.CreateScope() == ast.CreateScopeTemp,
NamePath: MergeNamePath(namePath, stmt.NamePath()),
Columns: newColumnsFromDef(stmt.ColumnDefinitionList()),
CreateMode: stmt.CreateMode(),
Query: query,
Query: fmt.Sprintf("SELECT %s FROM (%s)", strings.Join(outputColumns, ","), query),
}
}

Expand Down
0