GROUP with duplicate (equal) BY expressions fails with AssertionError (v6 -> master regression)

Open
#1,704 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
java
Domain
backend

Research direction

Start with LogicsModule.mapLGProp and the GroupProperty reverse-interface mapping, then inspect AddGroupProperty.getGroupKeys, OrderGroupProperty.getGroupKeys, and mapSelectTop. Reproduce the minimal lsf GROUP SUM case and verify startup succeeds with duplicate BY expressions, including the SUM/MAX and CONCAT/LAST paths described in the issue.

Written by the indexing model from the issue text.

Description

Problem

A GROUP property with duplicate (textually equal) BY expressions crashes the logics startup with an AssertionError (or an NPE if -ea is disabled). The same logics starts fine on v6, so this is a v6 -> master regression.

Minimal repro:

CLASS Order;
price = DATA NUMERIC[10,2] (Order);
quantity = DATA NUMERIC[10,2] (Order);

quantityOut(NUMERIC[10,2] p, NUMERIC[10,2] c) = GROUP SUM quantity(Order o) BY price(o), price(o);
java.lang.AssertionError
	at lsfusion.base.col.implementations.abs.AMap.toRevExclMap(...)
	at lsfusion.server.logics.LogicsModule.mapLGProp(...)
	at lsfusion.server.logics.LogicsModule.addSGProp(...)

Real-world case: a report property grouping by a price expression that is (legitimately) passed twice — once as the price output and once as the cost output:

quantityOut(Article a, Sort s, NUMERIC[7,2] p, NUMERIC[16,5] c) <-
    [GROUP SUM Sale.quantity(Sale.ShipmentDetail l) IF ... BY article(Sale.sku(l)), sort(Sale.sku(l)), Sale.price(l), Sale.price(l)](a, s, p, c);

Cause

Since b082332c2c (#1489) equal BY expressions produce equal (hashEquals) PropertyInterfaceImplements. The group property still creates a distinct interface per BY position (GroupProperty.Interface equality is identity-based), but its interface -> key mapping is no longer injective, so every toRevExclMap() over that mapping now fails its bijection assertion:

  • LogicsModule.mapLGProp — at construction of the resulting LP (the startup crash above);
  • AddGroupProperty.getGroupKeys — the value-pushing optimization on the SUM/MAX calc path;
  • OrderGroupProperty constructor (mapSelectTop, getMapRevInterfaces() is called unconditionally, even with an empty SELECT TOP) and OrderGroupProperty.getGroupKeys — the CONCAT/LAST paths.

On v6 the equal BY expressions were distinct objects, so all these reverse maps stayed bijective by accident.

Semantically the duplicate keys are valid and were supported on v6: GROUP BY price, price ≡ GROUP BY price, with both outer interfaces reading (and being equated to) the same group column.

Fix

Make the group machinery tolerant to duplicate (equal, and therefore interchangeable) group keys instead of requiring bijection:

  • LogicsModule.mapLGProp matches the interfaces to the implement list positionally when duplicates are present (the plain bijective path is kept otherwise);
  • GroupProperty.getMergeMapRevInterfaces() — a merge-based (non-exclusive) reverse of getMapInterfaces, used by AddGroupProperty.getGroupKeys, OrderGroupProperty.getGroupKeys and mapSelectTop. Any of the equal interfaces works there: equal keys read equal group exprs, and both outer equality conditions are still applied by GroupExpr.create through joinImplement.
Dominant language
Java
Stars
214
Forks
46
PR merge metrics
No merged PRs in 30d

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lsfusion/platform

All issues in lsfusion/platform

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.