Skip to content

Commit

Permalink
feat: type cross join
Browse files Browse the repository at this point in the history
  • Loading branch information
Ugzuzg committed Jan 26, 2025
1 parent 49c0a0e commit a0514ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
16 changes: 10 additions & 6 deletions __tests__/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ describe("type tests", () => {
fl: Float64;
}>
>(a.join(b, { leftOn: "id", rightOn: ["age"] }));

const c = a.join(b, { on: ["id"], suffix: "Right" });

const d = DataFrame({ a: [1n, 2n], id: [3, 4] })
.join(a, { on: "id" })
.getColumn("id");
expectType<
DataFrame<{
id: Int64;
id_right: Int64;
age: Int64;
age_right: Int64;
name: PlString;
fl: Float64;
}>
>(a.join(b, { how: "cross" }));
});
});
25 changes: 13 additions & 12 deletions polars/dataframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,24 +206,25 @@ type JoinSchemas<
[K_SUFFIXED in keyof S1 &
Exclude<
keyof S2,
Opt extends Pick<JoinOptions, "on">
? ExtractJoinKeys<Opt["on"]>
: Opt extends Pick<JoinOptions, "leftOn" | "rightOn">
? ExtractJoinKeys<Opt["rightOn"]>
: never
Opt extends { how: "cross" }
? never
: Opt extends Pick<JoinOptions, "on">
? ExtractJoinKeys<Opt["on"]>
: Opt extends Pick<JoinOptions, "leftOn" | "rightOn">
? ExtractJoinKeys<Opt["rightOn"]>
: never
> as `${K_SUFFIXED extends string ? K_SUFFIXED : never}${ExtractSuffix<Opt>}`]: K_SUFFIXED extends string
? S2[K_SUFFIXED]
: never;
}
>;

type JoinDataFrames<T1, T2, Opt extends JoinOptions> = T1 extends DataFrame<
infer T1Schema
>
? T2 extends DataFrame<infer T2Schema>
? DataFrame<JoinSchemas<T1Schema, T2Schema, Opt>>
: never
: never;
type JoinDataFrames<T1, T2, Opt extends JoinOptions> =
T1 extends DataFrame<infer T1Schema>
? T2 extends DataFrame<infer T2Schema>
? DataFrame<JoinSchemas<T1Schema, T2Schema, Opt>>
: never
: never;

/**
* A DataFrame is a two-dimensional data structure that represents data as a table
Expand Down

0 comments on commit a0514ee

Please sign in to comment.