PDGGK opened a new pull request, #19200:
URL: https://github.com/apache/iotdb/pull/19200

   ## Description
   
   `COUNT DEVICES` on a table view over tree data returns too few devices when 
its `WHERE` applies a function to a TAG column, unless the view's path prefix 
has exactly three levels. `SHOW DEVICES` with the same condition is correct.
   
   ```sql
   -- tree model: root.sg.d1.s1, root.sg.d2.s1
   CREATE VIEW dv.v (device_id STRING TAG, s1 INT64 FIELD) AS root.sg.**;
   
   SHOW DEVICES FROM dv.v WHERE upper(device_id) = 'D1';   -- d1
   COUNT DEVICES FROM dv.v WHERE upper(device_id) = 'D1';  -- 0, should be 1
   ```
   
   This happens on master and on 2.0.11. Conditions that are not evaluated 
device by device, such as `device_id = 'd1'` or `device_id LIKE 'd%'`, are not 
affected. I mentioned this case in #19195.
   
   ### Cause
   
   `DevicePredicateHandler` reads the TAG values from the device path starting 
at `tagIndex`. It uses 3 when `PathUtils.isTableModelDatabase(database)` is 
true. For a view, `database` is the view's own table-model database, so it is 
always 3. With the prefix `root.sg`, the tags start at index 2 and index 3 is 
past the end of `root.sg.d1`, so the TAG reads as NULL and the condition is 
false for every device. A prefix of exactly three levels, such as 
`root.vehicle.testNaN`, works by coincidence.
   
   `TableDeviceQuerySource` decides the same thing with 
`TreeViewSchema.isTreeViewTable(table)`.
   
   ### Change
   
   `DevicePredicateHandler` now uses `TreeViewSchema.isTreeViewTable(table)` 
too. Nothing changes for ordinary tables. `DeviceUpdater` and 
`DeviceBlackListConstructor` extend this class, but they only see ordinary 
tables: `UPDATE` and `DELETE DEVICES` on a view are rejected with `The table 
... is a view from tree, cannot be written or deleted from`.
   
   ### Verification
   
   - **Real server.** On `apache/iotdb:2.0.11-standalone` and master `cd31721`, 
the `COUNT DEVICES` above returns 0. With this change it returns 1, and `COUNT 
DEVICES FROM dv.v WHERE substring(device_id, 1, 1) = 'd'` goes from 0 to 2. 
`SHOW DEVICES`, `SELECT count(*)`, an unfiltered `COUNT DEVICES`, and `COUNT 
DEVICES` with `device_id = 'd1'`, `LIKE 'd%'`, `IS NULL` and `IS NOT NULL` give 
the same results as before.
   - **New `testCountDevicesWithFunctionOnTag` in `IoTDBFilterTableViewIT`** 
covers a two-level prefix (`root.sg1.**`, the bug) and a three-level one 
(`root.vehicle.testNaN.**`, which already worked). Without the change it fails 
with `expected:<[1],> but was:<[0],>`.
   - `IoTDBFilterTableViewIT` and `IoTDBTableIT`, which has the existing tree 
view tests, pass with `-PTableSimpleIT` and `-PTableClusterIT` (1C3D).
   - `spotless:check` passes for datanode and integration-test.
   
   <hr>
   
   This PR has:
   - [x] been self-reviewed.
   - [x] added integration tests.
   - [x] been tested in a test IoTDB cluster.
   
   <hr>
   
   ##### Key changed/added classes (or packages if there are too many classes) 
in this PR
   
   - `DevicePredicateHandler`
   - `IoTDBFilterTableViewIT`
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to