1- import type { BasicExpression , Func , OrderBy , PropRef } from "./ir.js"
1+ import { Func , Value } from "./ir.js"
2+ import type { BasicExpression , OrderBy , PropRef } from "./ir.js"
23import type { LoadSubsetOptions } from "../types.js"
34
45/**
@@ -42,6 +43,25 @@ export function isWhereSubset(
4243 return isWhereSubsetInternal ( subset ! , superset ! )
4344}
4445
46+ function makeDisjunction (
47+ preds : Array < BasicExpression < boolean > >
48+ ) : BasicExpression < boolean > {
49+ if ( preds . length === 0 ) {
50+ return new Value ( false )
51+ }
52+ if ( preds . length === 1 ) {
53+ return preds [ 0 ] !
54+ }
55+ return new Func ( `or` , preds )
56+ }
57+
58+ function convertInToOr ( inField : InField ) {
59+ const equalities = inField . values . map (
60+ ( value ) => new Func ( `eq` , [ inField . ref , new Value ( value ) ] )
61+ )
62+ return makeDisjunction ( equalities )
63+ }
64+
4565function isWhereSubsetInternal (
4666 subset : BasicExpression < boolean > ,
4767 superset : BasicExpression < boolean >
@@ -68,6 +88,22 @@ function isWhereSubsetInternal(
6888 )
6989 }
7090
91+ // Turn x IN [A, B, C] into x = A OR x = B OR x = C
92+ // for unified handling of IN and OR
93+ if ( subset . type === `func` && subset . name === `in` ) {
94+ const inField = extractInField ( subset )
95+ if ( inField ) {
96+ return isWhereSubsetInternal ( convertInToOr ( inField ) , superset )
97+ }
98+ }
99+
100+ if ( superset . type === `func` && superset . name === `in` ) {
101+ const inField = extractInField ( superset )
102+ if ( inField ) {
103+ return isWhereSubsetInternal ( subset , convertInToOr ( inField ) )
104+ }
105+ }
106+
71107 // Handle OR in subset: (A OR B) is subset of C only if both A and B are subsets of C
72108 if ( subset . type === `func` && subset . name === `or` ) {
73109 return subset . args . every ( ( arg ) =>
@@ -106,6 +142,7 @@ function isWhereSubsetInternal(
106142 )
107143 }
108144
145+ /*
109146 // Handle eq vs in
110147 if (subsetFunc.name === `eq` && supersetFunc.name === `in`) {
111148 const subsetFieldEq = extractEqualityField(subsetFunc)
@@ -147,6 +184,7 @@ function isWhereSubsetInternal(
147184 )
148185 }
149186 }
187+ */
150188 }
151189
152190 // Conservative: if we can't determine, return false
0 commit comments