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

Code before:


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
    try:
        transpiled_sql = transpile_to_dialect(sql, target_engine)
        if transpiled_sql != sql:
            logger.info(
                "Transpiled virtual dataset SQL for '%s' to %s dialect",
                config.get("table_name", "unknown"),
                target_engine,
            )
            config["sql"] = transpiled_sql
    except QueryClauseValidationException as ex:
        logger.warning(
            "Could not transpile SQL for dataset '%s' to %s: %s. "
            "Using original SQL which may not be compatible.",
            config.get("table_name", "unknown"),
            target_engine,
            ex,
        )


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

    dao = BaseDAO
    model_name = "model"
    schemas: dict[str, Schema] = {
        "charts/": ImportV1ChartSchema(),
        "dashboards/": ImportV1DashboardSchema(),

Review comment (suggestion):
This only works when `sql` is in the "Generic" dialect, but here it's a specific dialect coming from the YAML file.

A good solution would be to modify the `transpile_to_dialect` function to receive a source dialect, defaulting to the generic `Dialect`.

Code after:


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,
        )


class ImportExamplesCommand(ImportModelsCommand):