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

Outer Full Join outside the API, with some performance improvement for some reasons(?) #167

Open
patarapolw opened this issue Apr 24, 2019 · 0 comments

Comments

@patarapolw
Copy link

patarapolw commented Apr 24, 2019

I'm submitting a...


[x] Feature request

Current behavior

According to techfort/LokiJS#688, I tried to implement Full Outer Join in TypeScript, with success; and noticed performance improvement for no reasons. (I have 100K rows, and 4 tables' join is usually around 2-3 sec. It becomes like 1 sec.)

The code is

interface IJoinCollection<T> {
    data: T[];
    key: keyof T;
}

function fullJoin<T, U>(
    colL: IJoinCollection<T>,
    colR: IJoinCollection<U>,
    mapFn: (l: T, r: U) => any,
    isFull: boolean = true
): any[] {
    const joinMapL: any = {};
    const joinMapR: any = {};
    const result: any[] = [];

    for (const rowR of colR.data) {
        const v = rowR[colR.key];

        if (v) {
            joinMapR[v] = joinMapR[v] || [];
            joinMapR[v].push(rowR);
        } else {
            result.push({} as T, rowR);
        }
    }

    for (const rowL of colL.data) {
        const v = rowL[colL.key];

        if (v) {
            for (const vR of joinMapR[v] || [{}]) {
                result.push(mapFn(rowL, vR));
            }

            if (isFull) {
                joinMapL[v] = joinMapL[v] || [];
                joinMapL[v].push(rowL);
            }
        } else {
            result.push(mapFn(rowL, {} as U));
        }
    }

    if (isFull) {
        for (const rowR of colR.data) {
            const v = rowR[colR.key];

            if (v) {
                for (const vL of joinMapL[v] || [{}]) {
                    result.push(mapFn(vL, rowR));
                }
            }
        }
    }

    return result;
}

Environment


LokiDB version: 2.0.0-beta.8 with @lokidb/fs-storage 2.0.0-beta.8
Browser/Node version: Node v11.11.0 Yarn 1.15.2 MacOS
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

1 participant