Skip to content

Error when joining with a computed value #592

@duailibe

Description

@duailibe

Right now when trying to do a join using a computed value in the comparison, we get an error.

Reproduction

Ran this in packages/db/tests/query/join.test.ts:

type Product = {
  productId: number;
  region: 'A' | 'B' | 'C';
  sku: string;
  title: string;
}

type ProductInventory = {
  inventoryId: number;
  region: 'A' | 'B' | 'C';
  sku: string;
  quantity: number;
};

test('join with concat', () => {
  const productsCollection = createCollection(
    mockSyncCollectionOptions<Product>({
      id: `test-products-join`,
      getKey: (product) => product.productId,
      initialData: [
        { productId: 1, region: 'A', sku: 'sku1', title: 'A1' },
        { productId: 2, region: 'B', sku: 'sku1', title: 'B1' },
        { productId: 3, region: 'C', sku: 'sku2', title: 'C2' },
        { productId: 4, region: 'A', sku: 'sku2', title: 'A2' },
      ],
    })
  );
  const inventoriesCollection = createCollection(
    mockSyncCollectionOptions<ProductInventory>({
      id: `test-inventories-join`,
      getKey: (inventory) => inventory.inventoryId,
      initialData: [
        { inventoryId: 1, region: 'A', sku: 'sku1', quantity: 10 },
        { inventoryId: 3, region: 'C', sku: 'sku2', quantity: 30 },
      ],
    })
  );

  const joinQuery = createLiveQueryCollection({
    startSync: true,
    query: (q) =>
      q
        .from({ product: productsCollection })
        .join(
          { inventory: inventoriesCollection },
          ({ product, inventory }) =>
            eq(concat(product.region, product.sku),
              concat(inventory.region, inventory.sku)),
        )
  });

  expect(joinQuery.toArray).toHaveLength(2);
});

And it fails with:

TypeError: Cannot read properties of undefined (reading 'length')
 ❯ followRef src/query/compiler/index.ts:4147:16
    512|     }
    513|   }
    514| }
       |  ^
    515|
 ❯ processJoin src/query/compiler/joins.ts:425:29
 ❯ processJoins src/query/compiler/joins.ts:139:3
 ❯ compileQuery src/query/compiler/index.ts:198:5
 ❯ CollectionConfigBuilder.compileBasePipeline src/query/live/collection-config-builder.ts:337:9
 ❯ new CollectionConfigBuilder src/query/live/collection-config-builder.ts:151:5
 ❯ liveQueryCollectionOptions src/query/live-query-collection.ts:269:60
 ❯ createLiveQueryCollection src/query/live-query-collection.ts:287:46

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions