Skip to content

Commit e6f88e6

Browse files
committed
🐛 Fix issue #44 (Create table from csv/parquet file)
1 parent d4deaec commit e6f88e6

File tree

4 files changed

+4997
-4922
lines changed

4 files changed

+4997
-4922
lines changed

controller/middleware.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -503,20 +503,20 @@ func middlewareFileQuery(queryData *QueryData) bool {
503503
}
504504

505505
// Walk the AST and replace the table functions
506-
sqlparser.Rewrite(stmt, nil, func(cursor *sqlparser.Cursor) bool {
507-
// Get the table function
506+
// with a CREATE VIRTUAL TABLE statement
507+
rewrite := func(cursor *sqlparser.Cursor) bool {
508+
// Get the table name
508509
tableFunction, ok := cursor.Node().(sqlparser.TableName)
509510
if !ok {
510511
return true
511512
}
512513

513514
loweredName := strings.ToLower(tableFunction.Name.String())
514-
// Check if the table function is a file module
515+
515516
if !strings.HasPrefix(loweredName, "read_") {
516517
return true
517518
}
518519

519-
// Replace the table function with a random one
520520
tableName := generateRandomString(16)
521521
preExecBuilder := strings.Builder{}
522522
preExecBuilder.WriteString("CREATE VIRTUAL TABLE ")
@@ -525,11 +525,9 @@ func middlewareFileQuery(queryData *QueryData) bool {
525525
if reader, ok := supportedTableFunctions[loweredName]; ok {
526526
preExecBuilder.WriteString(reader)
527527
} else {
528-
// If the user writes read_foo, and we don't have a reader for foo
529-
// we skip the table function
530528
return true
531529
}
532-
// Add the arguments if any
530+
533531
if len(tableFunction.Args) > 0 {
534532
preExecBuilder.WriteString("(")
535533
for i, arg := range tableFunction.Args {
@@ -543,17 +541,22 @@ func middlewareFileQuery(queryData *QueryData) bool {
543541

544542
preExecBuilder.WriteString(";")
545543

546-
// Add the pre-execution statement
547544
queryData.PreExec = append(queryData.PreExec, preExecBuilder.String())
548545

549-
// Add a post-execution statement to drop the table
550546
queryData.PostExec = append(queryData.PostExec, "DROP TABLE "+tableName+";")
551547

552-
// Replace the table function with the new table name
553548
cursor.Replace(sqlparser.NewTableName(tableName))
554549

555550
return true
556-
})
551+
}
552+
sqlparser.Rewrite(stmt, nil, rewrite)
553+
554+
// In case it is a CREATE TABLE statement, we need to rewrite the select statement
555+
if createTable, ok := stmt.(*sqlparser.CreateTable); ok {
556+
if createTable.Select != nil {
557+
sqlparser.Rewrite(createTable.Select, nil, rewrite)
558+
}
559+
}
557560

558561
// Deparse the query
559562
queryData.SQLQuery = sqlparser.String(stmt)

0 commit comments

Comments
 (0)