( ctx *sql.Context, conn *stdsql.Conn, tx *stdsql.Tx, table tableIdentifier, appender *DeltaAppender, stats *FlushStats, )
| 440 | } |
| 441 | |
| 442 | func (c *DeltaController) handleGeneralCase( |
| 443 | ctx *sql.Context, |
| 444 | conn *stdsql.Conn, |
| 445 | tx *stdsql.Tx, |
| 446 | table tableIdentifier, |
| 447 | appender *DeltaAppender, |
| 448 | stats *FlushStats, |
| 449 | ) error { |
| 450 | if err := c.materializeCondensedDelta(ctx, conn, tx, table, appender, stats); err != nil { |
| 451 | return err |
| 452 | } |
| 453 | defer tx.ExecContext(ctx, "DROP TABLE IF EXISTS temp.main.delta") |
| 454 | |
| 455 | qualifiedTableName := catalog.ConnectIdentifiersANSI(table.dbName, table.tableName) |
| 456 | affected := int64(0) |
| 457 | |
| 458 | // Insert or replace new rows (action = INSERT) into the base table. |
| 459 | insertSQL := "INSERT OR REPLACE INTO " + |
| 460 | qualifiedTableName + |
| 461 | " SELECT * EXCLUDE (" + AugmentedColumnList + ") FROM temp.main.delta WHERE action = " + |
| 462 | strconv.Itoa(int(binlog.InsertRowEvent)) |
| 463 | result, err := tx.ExecContext(ctx, insertSQL) |
| 464 | if err == nil { |
| 465 | affected, err = result.RowsAffected() |
| 466 | } |
| 467 | if err != nil { |
| 468 | return err |
| 469 | } |
| 470 | stats.Insertions += affected |
| 471 | |
| 472 | if log := ctx.GetLogger(); log.Logger.IsLevelEnabled(logrus.DebugLevel) { |
| 473 | log.WithFields(logrus.Fields{ |
| 474 | "db": table.dbName, |
| 475 | "table": table.tableName, |
| 476 | "rows": affected, |
| 477 | }).Debug("Upserted") |
| 478 | } |
| 479 | |
| 480 | // Delete rows that have been deleted. |
| 481 | // The plan for `IN` is optimized to a SEMI JOIN, |
| 482 | // which is more efficient than ordinary INNER JOIN. |
| 483 | // DuckDB does not support multiple columns in `IN` clauses, |
| 484 | // so we need to handle this case separately using the `row()` function. |
| 485 | inTuple := getPrimaryKeyStruct(appender.BaseSchema()) |
| 486 | deleteSQL := "DELETE FROM " + qualifiedTableName + |
| 487 | " WHERE " + inTuple + " IN (SELECT " + inTuple + |
| 488 | "FROM temp.main.delta WHERE action = " + strconv.Itoa(int(binlog.DeleteRowEvent)) + ")" |
| 489 | result, err = tx.ExecContext(ctx, deleteSQL) |
| 490 | if err == nil { |
| 491 | affected, err = result.RowsAffected() |
| 492 | } |
| 493 | if err != nil { |
| 494 | return err |
| 495 | } |
| 496 | stats.Deletions += affected |
| 497 | |
| 498 | // For debugging: |
| 499 | // |
no test coverage detected