IMPORT of a file with no rows keeps the data of the previous import: FIELDS ... DO repeats the old rows and ELSE is skipped

Open
#1,832 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
Active
Tech stack
java
Domain
backend, data

Research direction

Start with the IMPORT entry point and run the supplied two-file lsf reproducer, then compare the empty-file cases for flat, JSON/XML, and form imports. Done means a later empty import clears all writable imported properties and row or object marks, skips FIELDS ... DO, and executes ELSE.

Written by the indexing model from the issue text.

Description

bug
Description

Before it writes the new data, IMPORT is documented to cancel the changes that the imported properties already have in the current session. In fact it does so only for the properties that received at least one value from the file. When the file — or one object group of it — contains no rows, some or all of what the previous IMPORT wrote in the same session stays in place, and the code that follows the import processes the rows of the previous file once more.

Minimal reproducer — one action that imports two files one after another in one session:

log = DATA LOCAL TEXT ();

importNames (CSVFILE f) {
    IMPORT CSV HEADER FROM f FIELDS STRING name DO
        log() <- CONCAT ' ', log(), name;
    ELSE
        log() <- CONCAT ' ', log(), 'EMPTY';
}

run () {
    importNames(CSVFILE(r'name
A
B'));
    importNames(CSVFILE(r'name
'));              // a header and no rows
    MESSAGE log();   // expected: A B EMPTY    actual: A B A B
}

What survives depends on the format:

Imported file What stays from the previous import
flat format (CSV, XLS, DBF) with no rows — imported with TO, with FIELDS or into a form everything: the imported values and the marks of the imported rows (imported[INTEGER], the WHERE property, the internal mark of FIELDS). FIELDS ... DO runs again for every row of the previous file with its old values, ELSE does not run
JSON / XML with an empty array or without the expected key — imported with TO or FIELDS the marks of the imported rows; the values are reset. FOR imported(INTEGER i) and FIELDS ... DO go over the row numbers of the previous file with NULL values, ELSE does not run
JSON / XML imported into a form, when one of its object groups gets no objects (empty array, no such key) the properties written through the FILTERS of that object group; the properties of the form are reset

The last case covers the mark of the read objects (FILTERS imported(o)) and, which is worse, the link to the upper object:

rName = DATA LOCAL STRING (INTEGER);
lReceipt = DATA LOCAL INTEGER (INTEGER);
lName = DATA LOCAL STRING (INTEGER);

FORM receipts
    OBJECTS receipts = INTEGER
    PROPERTIES(receipts) rName EXTID 'name'
    OBJECTS lines = INTEGER
    PROPERTIES(lines) lName EXTID 'item'
    FILTERS lReceipt(lines) = receipts
;
// 1st file: {"receipts":[{"name":"A","lines":[{"item":"a1"},{"item":"a2"}]}]}
// 2nd file: {"receipts":[{"name":"B","lines":[]}]}
// after the 2nd import lReceipt(0) and lReceipt(1) still point to receipt 0,
// so receipt B gets the two lines of receipt A, with NULL items

Verified on 7.0-SNAPSHOT; this is not a regression — 5.x and 6.x behave the same way.

Workaround until it is fixed: run every import in a session of its own (NEWSESSION), or, for TO, reset the mark and the imported properties before the import (imported(INTEGER i) <- NULL;).

Reason

Importing several files in one session is the ordinary shape of an integration: a loop over a directory or a mailbox, a polling action, an upload of several files. An empty file is an ordinary event there, and every time it happens the rows of the previous file are processed a second time — objects are created twice, a document without lines receives the lines of the previous one, and the ELSE branch that should report "nothing to import" never runs. Nothing signals it, and the outcome depends on what was imported earlier in the same session, so the cause is very hard to find. It also contradicts the documented behaviour ("before importing, any existing changes to the imported properties in the current session are canceled").

Fix

Every IMPORT cancels the previous session changes of all the properties it can write to — the imported properties, the property that marks the imported rows (imported[INTEGER], the WHERE property, the internal one of FIELDS) and the properties in the filters of the imported form — no matter how many rows or objects the file yields. After a file with no rows these properties are empty, FIELDS ... DO is not executed and ELSE is.

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.