Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multidiff #4706

Closed
DiTo97 opened this issue Feb 4, 2025 · 1 comment
Closed

multidiff #4706

DiTo97 opened this issue Feb 4, 2025 · 1 comment

Comments

@DiTo97
Copy link

DiTo97 commented Feb 4, 2025

Is your feature request related to a problem? Please describe.
multidiff feature handling SQL statements with more than one expressions.

Describe the solution you'd like
I have implemented such feature as part of a text-to-SQL suite, opting for a Levenshtein-like approach if the two statements have a different number of expressions using empty expressions as filler padding and adjusting the diff accordingly.

from typing import Any, overload

from sqlglot import diff, parse, Expression
from sqlglot.diff import Keep, Insert, Move, Remove, Update
from sqlglot.optimizer import optimize


padding = Expression()
Edit = Keep | Insert | Move | Remove | Update


@overload
def multidiff(
    source: list[Expression],
    target: list[Expression],
    schema: dict[str, Any] | None = None,
) -> list[list[Edit]]:
    ...


@overload
def multidiff(
    source: str,
    target: str,
    dialect: str | None = None,
    schema: dict[str, Any] | None = None,
) -> list[list[Edit]]:
    ...


def multidiff(source, target, dialect=None, schema=None):
    source = parse(source, dialect=dialect) if isinstance(source, str) else source
    target = parse(target, dialect=dialect) if isinstance(target, str) else target

    if schema:
        source = [optimize(S, schema=schema) for S in source]
        target = [optimize(T, schema=schema) for T in target]

    maxlen = max(len(source), len(target))

    source += [padding] * (maxlen - len(source))
    target += [padding] * (maxlen - len(target))

    multidifference = []

    for S, T in zip(source, target):
        difference = diff(S, T)
        
        if S == padding: difference.pop(0)
        if T == padding: difference.pop()
            
        multidifference.append(difference)

    return multidifference


def get_diff_distance(difference: list[Edit]) -> int:
    return sum(0 if isinstance(E, Keep) else 1 for E in difference)


def get_multidiff_distance(multidifference: list[list[Edit]]) -> int:
    return sum(map(get_diff_distance, multidifference))
@tobymao
Copy link
Owner

tobymao commented Feb 5, 2025

this is very cool but we won't be implementing this in core, feel free to make a library for this if you would like

@tobymao tobymao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants