(pythonExecutor *pythonExecutor, stmt *ir.OptimizeStmt)
| 468 | } |
| 469 | |
| 470 | func executeOptimizeUsingOptFlow(pythonExecutor *pythonExecutor, stmt *ir.OptimizeStmt) error { |
| 471 | dbName, tableName, err := createTmpTableFromSelect(stmt.Select, pythonExecutor.Session.DbConnStr) |
| 472 | if err != nil { |
| 473 | return err |
| 474 | } |
| 475 | |
| 476 | dropTmpTableFunc := func(table string) { |
| 477 | dropTmpTables([]string{table}, pythonExecutor.Session.DbConnStr) |
| 478 | } |
| 479 | |
| 480 | if len(stmt.Variables) > 2 { |
| 481 | joinedVarName := strings.Join(stmt.Variables, "__") |
| 482 | concatColumnNames := make([]string, 0) |
| 483 | for i, v := range stmt.Variables { |
| 484 | if i >= 1 { |
| 485 | concatColumnNames = append(concatColumnNames, `','`) |
| 486 | } |
| 487 | concatColumnNames = append(concatColumnNames, v) |
| 488 | } |
| 489 | concatColumnExpr := fmt.Sprintf("CONCAT(%s) AS %s", strings.Join(concatColumnNames, ","), joinedVarName) |
| 490 | selectStmt := fmt.Sprintf("SELECT *, %s FROM %s.%s", concatColumnExpr, dbName, tableName) |
| 491 | newDBName, newTableName, err := createTmpTableFromSelect(selectStmt, pythonExecutor.Session.DbConnStr) |
| 492 | dropTmpTableFunc(tableName) // drop the first created table whatever |
| 493 | if err != nil { |
| 494 | return err |
| 495 | } |
| 496 | stmt.Variables = []string{joinedVarName} |
| 497 | dbName = newDBName |
| 498 | tableName = newTableName |
| 499 | } |
| 500 | |
| 501 | defer dropTmpTableFunc(tableName) |
| 502 | |
| 503 | db, err := database.OpenAndConnectDB(pythonExecutor.Session.DbConnStr) |
| 504 | if err != nil { |
| 505 | return err |
| 506 | } |
| 507 | defer db.Close() |
| 508 | |
| 509 | splittedResultTable := strings.SplitN(stmt.ResultTable, ".", 2) |
| 510 | var resultTable string |
| 511 | if len(splittedResultTable) == 2 { |
| 512 | if splittedResultTable[0] != dbName { |
| 513 | return fmt.Errorf("database name of result table must be the same as source table") |
| 514 | } |
| 515 | resultTable = stmt.ResultTable |
| 516 | } else { |
| 517 | resultTable = fmt.Sprintf("%s.%s", dbName, stmt.ResultTable) |
| 518 | } |
| 519 | |
| 520 | _, err = db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", resultTable)) |
| 521 | if err != nil { |
| 522 | return err |
| 523 | } |
| 524 | |
| 525 | code, err := optimize.GenerateOptimizeCode(stmt, pythonExecutor.Session, tableName, true) |
| 526 | if err != nil { |
| 527 | return err |
no test coverage detected