diff --git a/engine/src/main/java/com/arcadedb/query/sql/parser/Projection.java b/engine/src/main/java/com/arcadedb/query/sql/parser/Projection.java index 47fbb94e821..c5c2035a265 100644 --- a/engine/src/main/java/com/arcadedb/query/sql/parser/Projection.java +++ b/engine/src/main/java/com/arcadedb/query/sql/parser/Projection.java @@ -99,7 +99,8 @@ public void toString(final Map params, final StringBuilder build if (distinct) builder.append("DISTINCT "); - // print * before + // print * before. A per-record materialized view relies on it: its derived query appends `@rid AS _sourceRID` and is + // rendered and parsed again, so the link comes after * and overwrites a same-named field (Issue11552PerRecordSourceRidReservedTest) for (final ProjectionItem item : items) { if (item.isAll()) { if (!first) @@ -159,6 +160,7 @@ public Result calculateSingle(final CommandContext context, final Result record, return record; final ResultInternal result = new ResultInternal(context.getDatabase()); + // In order, so a later item overwrites a same-named column an earlier * copied (Issue11552PerRecordSourceRidReservedTest) for (int i = 0; i < items.size(); i++) { final ProjectionItem item = items.get(i); if (item.exclude) diff --git a/engine/src/main/java/com/arcadedb/schema/MaterializedViewPerRecordRefresher.java b/engine/src/main/java/com/arcadedb/schema/MaterializedViewPerRecordRefresher.java index 0eea36b3776..ec5c374053a 100644 --- a/engine/src/main/java/com/arcadedb/schema/MaterializedViewPerRecordRefresher.java +++ b/engine/src/main/java/com/arcadedb/schema/MaterializedViewPerRecordRefresher.java @@ -72,6 +72,13 @@ static void addLink(final LocalSchema schema, final MaterializedViewImpl view) { * The defining query with {@code @rid AS _sourceRID} added to its projection, rendered from a copy of the parsed statement. A * query with no projection ({@code SELECT FROM X}) returns the whole record, so it gains {@code *} as well. *

+ * {@link #SOURCE_RID} is a reserved column of the view (issue #11552). The classifier refuses a query that projects a column of + * that name or reads a type declaring it through {@code *}, but a schemaless source record can still carry an undeclared field + * of that name, as can a record of a type that declared it after the view was created: {@code *} copies it into the row. The + * link always wins over it, because the rendered projection prints {@code *} before every other item and + * {@code Projection.calculateSingle} writes the items in order, so the link added last overwrites the field. The field's own + * value is not in the view. + *

* The RID-list variant reads the records bound to {@link #RIDS_PARAMETER} instead of the source type, and keeps the WHERE with * {@code @this INSTANCEOF ''} added: a RID-list target returns whatever record sits at each RID, whatever its type, and * the listener fires for the subtypes of the source too, which {@code INSTANCEOF} accepts. diff --git a/engine/src/main/java/com/arcadedb/schema/MaterializedViewQueryClassifier.java b/engine/src/main/java/com/arcadedb/schema/MaterializedViewQueryClassifier.java index 495b2385121..2c23f4b7d61 100644 --- a/engine/src/main/java/com/arcadedb/schema/MaterializedViewQueryClassifier.java +++ b/engine/src/main/java/com/arcadedb/schema/MaterializedViewQueryClassifier.java @@ -133,8 +133,9 @@ public static boolean isPerRecord(final String sql, final Database database) { /** * Whether the type, a supertype or a subtype (whose records {@code SELECT FROM} the type returns too) declares a property named - * like the reserved column, in any case. Read at DDL time only. A schemaless record can still carry one undeclared: no static - * reading of the schema can see that. + * like the reserved column, in any case. Read at DDL time only. A schemaless record can still carry one undeclared, which no + * static reading of the schema can see: there the link the refresh adds wins, and the field's value is not in the view (issue + * #11552, see {@code MaterializedViewPerRecordRefresher.buildDerivedQuery}). */ private static boolean declaresSourceRid(final Schema schema, final String typeName) { if (!schema.existsType(typeName)) diff --git a/engine/src/test/java/com/arcadedb/schema/Issue11552PerRecordSourceRidReservedTest.java b/engine/src/test/java/com/arcadedb/schema/Issue11552PerRecordSourceRidReservedTest.java new file mode 100644 index 00000000000..0d45e0f4fd5 --- /dev/null +++ b/engine/src/test/java/com/arcadedb/schema/Issue11552PerRecordSourceRidReservedTest.java @@ -0,0 +1,158 @@ +/* + * Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) + * SPDX-License-Identifier: Apache-2.0 + */ +package com.arcadedb.schema; + +import com.arcadedb.TestHelper; +import com.arcadedb.database.Document; +import com.arcadedb.database.MutableDocument; +import com.arcadedb.database.RID; +import com.arcadedb.query.sql.executor.Result; +import com.arcadedb.query.sql.executor.ResultSet; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Issue #11552: {@code _sourceRID} is a reserved column on a per-record materialized view. A schemaless source record can carry + * an undeclared field of that name, which a whole-record projection ({@code SELECT *}, {@code SELECT FROM}) copies into the row; + * the link the refresh adds always wins over it, on the full pass and on the delta pass alike, so every row stays on its own + * source record. A field holding the RID of ANOTHER source record is the case that matters: had it won, two rows would claim the + * same source, and the next pass would write one over the other. + */ +class Issue11552PerRecordSourceRidReservedTest extends TestHelper { + private static final String SOURCE_RID = MaterializedViewPerRecordRefresher.SOURCE_RID; + + @ParameterizedTest + @ValueSource(strings = { "SELECT * FROM P", "SELECT FROM P", "SELECT *, name AS n FROM P" }) + void fullPassLinkWinsOverAnUndeclaredSourceRidField(final String query) { + database.getSchema().createDocumentType("P"); + final RID first = insert("a", null); + insert("b", "junk"); + insert("c", first); + + final MaterializedViewImpl view = createView(query); + assertThat(view.isPerRecord()).as("per-record: " + query).isTrue(); + assertEveryRowOnItsOwnSource(3); + + // the explicit full pass, over rows that already exist: each one is matched by its link and written in place + final Map before = rowBySource(); + database.command("sql", "REFRESH MATERIALIZED VIEW V"); + assertEveryRowOnItsOwnSource(3); + assertThat(rowBySource()).isEqualTo(before); + } + + @ParameterizedTest + @ValueSource(strings = { "SELECT * FROM P", "SELECT FROM P" }) + void deltaPassLinkWinsOverAnUndeclaredSourceRidField(final String query) { + database.getSchema().createDocumentType("P"); + final RID first = insert("a", null); + final RID second = insert("b", null); + createView(query); + assertEveryRowOnItsOwnSource(2); + + // created with the field: a string, and the RID of another source record that already has its row + insert("c", "junk"); + insert("d", first); + assertEveryRowOnItsOwnSource(4); + + // an existing source gains the field on update + database.transaction(() -> ((Document) database.lookupByRID(second, true)).modify().set(SOURCE_RID, first).save()); + assertEveryRowOnItsOwnSource(4); + + // and the full pass that follows agrees with the delta passes + database.command("sql", "REFRESH MATERIALIZED VIEW V"); + assertEveryRowOnItsOwnSource(4); + } + + /** + * The classifier reads the schema at DDL time only, so a property declared after the view was created is the same case as an + * undeclared field: the view stays per-record and the link wins. + */ + @Test + void propertyDeclaredAfterTheViewIsOverriddenByTheLink() { + database.getSchema().createDocumentType("P"); + final RID first = insert("a", null); + createView("SELECT * FROM P"); + assertThat(((MaterializedViewImpl) database.getSchema().getMaterializedView("V")).isPerRecord()).isTrue(); + + database.command("sql", "CREATE PROPERTY P." + SOURCE_RID + " STRING"); + insert("b", "declared"); + insert("c", first.toString()); + assertEveryRowOnItsOwnSource(3); + + database.command("sql", "REFRESH MATERIALIZED VIEW V"); + assertEveryRowOnItsOwnSource(3); + } + + private RID insert(final String name, final Object sourceRidField) { + final RID[] rid = new RID[1]; + database.transaction(() -> { + final MutableDocument doc = database.newDocument("P").set("name", name); + if (sourceRidField != null) + doc.set(SOURCE_RID, sourceRidField); + rid[0] = doc.save().getIdentity(); + }); + return rid[0]; + } + + private MaterializedViewImpl createView(final String query) { + database.transaction(() -> database.getSchema().buildMaterializedView() + .withName("V") + .withQuery(query) + .withRefreshMode(MaterializedViewRefreshMode.INCREMENTAL) + .create()); + return (MaterializedViewImpl) database.getSchema().getMaterializedView("V"); + } + + /** Every source record has exactly one row, linked to it, carrying its name; and the view is VALID. */ + private void assertEveryRowOnItsOwnSource(final int expectedRows) { + assertThat(database.getSchema().getMaterializedView("V").getStatus()).isEqualTo(MaterializedViewStatus.VALID.name()); + assertThat(database.countType("P", true)).isEqualTo(expectedRows); + + final Map rows = rowBySource(); + assertThat(rows).hasSize(expectedRows); + try (final ResultSet rs = database.query("sql", "SELECT FROM V")) { + while (rs.hasNext()) { + final Result row = rs.next(); + final Object link = row.getProperty(SOURCE_RID); + assertThat(link).as("the link column holds a RID, not the source's own field: " + row.toJSON()).isInstanceOf(RID.class); + final Document source = (Document) database.lookupByRID((RID) link, true); + assertThat(source.getTypeName()).isEqualTo("P"); + assertThat(row.getProperty("name")).as("row " + row.toJSON() + " sits on its own source").isEqualTo(source.getString("name")); + } + } + } + + private Map rowBySource() { + final Map map = new HashMap<>(); + try (final ResultSet rs = database.query("sql", "SELECT FROM V")) { + while (rs.hasNext()) { + final Result row = rs.next(); + final RID source = row.getProperty(SOURCE_RID); + assertThat(map.put(source, row.getIdentity().get())).as("one row per source record").isNull(); + } + } + return map; + } +}