Repository: apache/superset
File: superset/commands/importers/v1/examples.py (Python)

Code before:
logger = logging.getLogger(__name__)


def transpile_virtual_dataset_sql(config: dict[str, Any], database_id: int) -> None:
    """
    Transpile virtual dataset SQL to the target database dialect.

    This ensures that virtual datasets exported from one database type
    (e.g., PostgreSQL) can be loaded into a different database type
    (e.g., MySQL, DuckDB, SQLite).

    Args:
        config: Dataset configuration dict (modified in place)
        database_id: ID of the target database
    """
    sql = config.get("sql")
    if not sql:
        return

    database = db.session.query(Database).get(database_id)
    if not database:
        logger.warning("Database %s not found, skipping SQL transpilation", database_id)
        return

    target_engine = database.db_engine_spec.engine
    source_engine = config.get("source_db_engine")

    try:
        transpiled_sql = transpile_to_dialect(sql, target_engine, source_engine)
        if transpiled_sql != sql:
            logger.info(
                "Transpiled virtual dataset SQL for '%s' from %s to %s dialect",
                config.get("table_name", "unknown"),
                source_engine or "generic",
                target_engine,
            )
            config["sql"] = transpiled_sql
    except QueryClauseValidationException as ex:
        logger.warning(
            "Could not transpile SQL for dataset '%s' from %s to %s: %s. "
            "Using original SQL which may not be compatible.",
            config.get("table_name", "unknown"),
            source_engine or "generic",
            target_engine,
            ex,
        )


class ImportExamplesCommand(ImportModelsCommand):
    """Import examples"""


Review comment (suggestion):
Let's add an early return here:

```suggestion
    target_engine = database.db_engine_spec.engine
    source_engine = config.get("source_db_engine")
    if target_engine == source_engine:
        logger.info("Source and target dialects are identical, skipping transpilation")
        return
```

Code after:
logger = logging.getLogger(__name__)


def transpile_virtual_dataset_sql(config: dict[str, Any], database_id: int) -> None:
    """
    Transpile virtual dataset SQL to the target database dialect.

    This ensures that virtual datasets exported from one database type
    (e.g., PostgreSQL) can be loaded into a different database type
    (e.g., MySQL, DuckDB, SQLite).

    Args:
        config: Dataset configuration dict (modified in place)
        database_id: ID of the target database
    """
    sql = config.get("sql")
    if not sql:
        return

    database = db.session.query(Database).get(database_id)
    if not database:
        logger.warning("Database %s not found, skipping SQL transpilation", database_id)
        return

    target_engine = database.db_engine_spec.engine
    source_engine = config.get("source_db_engine")
    if target_engine == source_engine:
        logger.info("Source and target dialects are identical, skipping transpilation")
        return

    try:
        transpiled_sql = transpile_to_dialect(sql, target_engine, source_engine)
        if transpiled_sql != sql:
            logger.info(
                "Transpiled virtual dataset SQL for '%s' from %s to %s dialect",
                config.get("table_name", "unknown"),
                source_engine or "generic",
                target_engine,
            )
            config["sql"] = transpiled_sql
    except QueryClauseValidationException as ex:
        logger.warning(
            "Could not transpile SQL for dataset '%s' from %s to %s: %s. "
            "Using original SQL which may not be compatible.",
            config.get("table_name", "unknown"),
            source_engine or "generic",
            target_engine,
            ex,
        )