MCPcopy Create free account
hub / github.com/TanStack/db / unionSameFieldPredicates

Function unionSameFieldPredicates

packages/db/src/query/predicate-utils.ts:1369–1516  ·  view source on GitHub ↗
(
  predicates: Array<BasicExpression<boolean>>,
)

Source from the content-addressed store, hash-verified

1367}
1368
1369function unionSameFieldPredicates(
1370 predicates: Array<BasicExpression<boolean>>,
1371): BasicExpression<boolean> | null {
1372 if (predicates.length === 1) {
1373 return predicates[0]!
1374 }
1375
1376 // Try to extract range constraints
1377 let maxGt: number | null = null
1378 let maxGte: number | null = null
1379 let minLt: number | null = null
1380 let minLte: number | null = null
1381 const eqValues: Set<any> = new Set()
1382 const inValues: Set<any> = new Set()
1383 const otherPredicates: Array<BasicExpression<boolean>> = []
1384
1385 for (const pred of predicates) {
1386 if (pred.type === `func`) {
1387 const func = pred as Func
1388 const field = extractComparisonField(func)
1389
1390 if (field) {
1391 const value = field.value
1392 if (func.name === `gt`) {
1393 maxGt = maxGt === null ? value : minValue(maxGt, value)
1394 } else if (func.name === `gte`) {
1395 maxGte = maxGte === null ? value : minValue(maxGte, value)
1396 } else if (func.name === `lt`) {
1397 minLt = minLt === null ? value : maxValue(minLt, value)
1398 } else if (func.name === `lte`) {
1399 minLte = minLte === null ? value : maxValue(minLte, value)
1400 } else if (func.name === `eq`) {
1401 eqValues.add(value)
1402 } else {
1403 otherPredicates.push(pred)
1404 }
1405 } else {
1406 const inField = extractInField(func)
1407 if (inField) {
1408 for (const val of inField.values) {
1409 inValues.add(val)
1410 }
1411 } else {
1412 otherPredicates.push(pred)
1413 }
1414 }
1415 } else {
1416 otherPredicates.push(pred)
1417 }
1418 }
1419
1420 // If we have multiple equality values, combine into IN
1421 if (eqValues.size > 1 || (eqValues.size > 0 && inValues.size > 0)) {
1422 const allValues = [...eqValues, ...inValues]
1423 const ref = predicates.find((p) => {
1424 if (p.type === `func`) {
1425 const field =
1426 extractComparisonField(p as Func) || extractInField(p as Func)

Callers

nothing calls this directly

Calls 6

extractComparisonFieldFunction · 0.85
minValueFunction · 0.85
maxValueFunction · 0.85
extractInFieldFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected