From 9d2c04dc30c9f81ef363bcbd0fc6dd94f0d10593 Mon Sep 17 00:00:00 2001 From: Martin Vahlensieck Date: Fri, 20 Dec 2024 10:39:31 +0100 Subject: [PATCH] Make views serialize and deserialize correctly --- .../relational/LogicalRelViewScan.java | 3 ++- .../catalogs/LogicalRelationalCatalog.java | 2 ++ .../logical/LogicalMaterializedView.java | 23 ++++++++++++++----- .../catalog/entity/logical/LogicalView.java | 23 +++++++++++++------ .../impl/logical/RelationalCatalog.java | 11 ++++++--- .../catalog/snapshot/LogicalRelSnapshot.java | 2 ++ .../snapshot/impl/LogicalRelSnapshotImpl.java | 11 +++++++++ .../db/snapshot/MockRelSnapshot.java | 7 ++++++ .../db/view/MaterializedViewManagerImpl.java | 2 +- 9 files changed, 66 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/polypheny/db/algebra/logical/relational/LogicalRelViewScan.java b/core/src/main/java/org/polypheny/db/algebra/logical/relational/LogicalRelViewScan.java index ad135122e1..b2f15c3dbb 100644 --- a/core/src/main/java/org/polypheny/db/algebra/logical/relational/LogicalRelViewScan.java +++ b/core/src/main/java/org/polypheny/db/algebra/logical/relational/LogicalRelViewScan.java @@ -26,6 +26,7 @@ import org.polypheny.db.algebra.AlgNode; import org.polypheny.db.algebra.core.relational.RelScan; import org.polypheny.db.algebra.type.AlgDataType; +import org.polypheny.db.catalog.Catalog; import org.polypheny.db.catalog.entity.Entity; import org.polypheny.db.catalog.entity.logical.LogicalView; import org.polypheny.db.plan.AlgCluster; @@ -63,7 +64,7 @@ public static AlgNode create( AlgCluster cluster, final Entity entity ) { } ); LogicalView logicalView = entity.unwrap( LogicalView.class ).orElseThrow(); - AlgCollation algCollation = logicalView.getAlgCollation(); + AlgCollation algCollation = Catalog.snapshot().rel().getCollationInfo( entity.id ); return new LogicalRelViewScan( cluster, traitSet, entity, logicalView.prepareView( cluster ), algCollation ); } diff --git a/core/src/main/java/org/polypheny/db/catalog/catalogs/LogicalRelationalCatalog.java b/core/src/main/java/org/polypheny/db/catalog/catalogs/LogicalRelationalCatalog.java index d9e35236a6..44f43a031a 100644 --- a/core/src/main/java/org/polypheny/db/catalog/catalogs/LogicalRelationalCatalog.java +++ b/core/src/main/java/org/polypheny/db/catalog/catalogs/LogicalRelationalCatalog.java @@ -328,6 +328,8 @@ public interface LogicalRelationalCatalog extends LogicalCatalog { Map getNodes(); + Map getCollations(); + void deleteKey( long id ); } diff --git a/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalMaterializedView.java b/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalMaterializedView.java index 58748a2989..59a55e6419 100644 --- a/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalMaterializedView.java +++ b/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalMaterializedView.java @@ -26,7 +26,6 @@ import lombok.EqualsAndHashCode; import lombok.Value; import lombok.experimental.SuperBuilder; -import org.polypheny.db.algebra.AlgCollation; import org.polypheny.db.catalog.entity.MaterializedCriteria; import org.polypheny.db.catalog.logistic.EntityType; import org.polypheny.db.languages.QueryLanguage; @@ -44,14 +43,27 @@ public class LogicalMaterializedView extends LogicalView { public boolean ordered; + public LogicalMaterializedView( + long id, + String name, + long namespaceId, + String query, + Map> underlyingTables, + QueryLanguage language, + MaterializedCriteria materializedCriteria, + boolean ordered + ) { + this( id, name, namespaceId, query, underlyingTables, language.serializedName(), materializedCriteria, ordered ); + } + + public LogicalMaterializedView( @Deserialize("id") long id, @Deserialize("name") String name, @Deserialize("namespaceId") long namespaceId, - @Deserialize("entityType") String query, - @Deserialize("algCollation") AlgCollation algCollation, + @Deserialize("query") String query, @Deserialize("underlyingTables") Map> underlyingTables, - @Deserialize("language") QueryLanguage language, + @Deserialize("languageName") String languageName, @Deserialize("materializedCriteria") MaterializedCriteria materializedCriteria, @Deserialize("ordered") boolean ordered ) { @@ -61,9 +73,8 @@ public LogicalMaterializedView( namespaceId, EntityType.MATERIALIZED_VIEW, query, - algCollation, underlyingTables, - language ); + languageName ); Map> map = new HashMap<>(); for ( Entry> e : underlyingTables.entrySet() ) { diff --git a/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalView.java b/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalView.java index ddd03dd91b..a156720d51 100644 --- a/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalView.java +++ b/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalView.java @@ -28,7 +28,6 @@ import lombok.experimental.NonFinal; import lombok.experimental.SuperBuilder; import org.polypheny.db.algebra.AbstractAlgNode; -import org.polypheny.db.algebra.AlgCollation; import org.polypheny.db.algebra.AlgNode; import org.polypheny.db.algebra.BiAlg; import org.polypheny.db.algebra.SingleAlg; @@ -51,22 +50,32 @@ public class LogicalView extends LogicalTable { @Serialize public ImmutableMap> underlyingTables; @Serialize + public String languageName; public QueryLanguage language; @Serialize - public AlgCollation algCollation; - @Serialize public String query; + public LogicalView( + long id, + String name, + long namespaceId, + EntityType entityType, + String query, + Map> underlyingTables, + QueryLanguage language ) { + this( id, name, namespaceId, entityType, query, underlyingTables, language.serializedName() ); + } + + public LogicalView( @Deserialize("id") long id, @Deserialize("name") String name, @Deserialize("namespaceId") long namespaceId, @Deserialize("entityType") EntityType entityType, @Deserialize("query") String query, - @Deserialize("algCollation") AlgCollation algCollation, @Deserialize("underlyingTables") Map> underlyingTables, - @Deserialize("language") QueryLanguage language ) { + @Deserialize("languageName") String languageName ) { super( id, name, @@ -75,9 +84,9 @@ public LogicalView( null, false ); this.query = query; - this.algCollation = algCollation; this.underlyingTables = ImmutableMap.copyOf( underlyingTables ); - this.language = language; + this.languageName = languageName; + this.language = QueryLanguage.from( languageName ); } diff --git a/core/src/main/java/org/polypheny/db/catalog/impl/logical/RelationalCatalog.java b/core/src/main/java/org/polypheny/db/catalog/impl/logical/RelationalCatalog.java index 568eedd67e..65aa086382 100644 --- a/core/src/main/java/org/polypheny/db/catalog/impl/logical/RelationalCatalog.java +++ b/core/src/main/java/org/polypheny/db/catalog/impl/logical/RelationalCatalog.java @@ -20,6 +20,7 @@ import io.activej.serializer.BinarySerializer; import io.activej.serializer.annotations.Deserialize; import io.activej.serializer.annotations.Serialize; +import io.activej.serializer.annotations.SerializeClass; import java.beans.PropertyChangeSupport; import java.sql.Timestamp; import java.util.HashSet; @@ -28,6 +29,7 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import lombok.Getter; import lombok.Value; import lombok.experimental.SuperBuilder; import org.jetbrains.annotations.Nullable; @@ -80,13 +82,14 @@ public class RelationalCatalog implements PolySerializable, LogicalRelationalCat @Serialize @JsonProperty - public Map tables; + public Map tables; @Serialize @JsonProperty public Map columns; public Map nodes; + public Map collations; @Serialize @@ -127,6 +130,7 @@ public RelationalCatalog( this.keys = new ConcurrentHashMap<>( keys ); this.constraints = new ConcurrentHashMap<>( constraints ); this.nodes = new ConcurrentHashMap<>(); + this.collations = new ConcurrentHashMap<>(); listeners.addPropertyChangeListener( Catalog.getInstance().getChangeListener() ); } @@ -162,10 +166,11 @@ public LogicalTable addTable( String name, EntityType entityType, boolean modifi public LogicalView addView( String name, long namespaceId, boolean modifiable, AlgNode definition, AlgCollation algCollation, Map> underlyingTables, List connectedViews, AlgDataType fieldList, String query, QueryLanguage language ) { long id = idBuilder.getNewLogicalId(); - LogicalView view = new LogicalView( id, name, namespaceId, EntityType.VIEW, query, algCollation, underlyingTables, language ); + LogicalView view = new LogicalView( id, name, namespaceId, EntityType.VIEW, query, underlyingTables, language ); tables.put( id, view ); nodes.put( id, definition ); + collations.put( id, algCollation ); change( CatalogEvent.VIEW_CREATED, null, id ); return view; } @@ -180,7 +185,6 @@ public LogicalMaterializedView addMaterializedView( final String name, long name name, namespaceId, query, - algCollation, underlyingTables, language, materializedCriteria, @@ -189,6 +193,7 @@ public LogicalMaterializedView addMaterializedView( final String name, long name tables.put( id, materializedViewTable ); nodes.put( id, definition ); + collations.put( id, algCollation ); change( CatalogEvent.MATERIALIZED_VIEW_CREATED, null, id ); return materializedViewTable; } diff --git a/core/src/main/java/org/polypheny/db/catalog/snapshot/LogicalRelSnapshot.java b/core/src/main/java/org/polypheny/db/catalog/snapshot/LogicalRelSnapshot.java index 905b0b69a3..a4b489e797 100644 --- a/core/src/main/java/org/polypheny/db/catalog/snapshot/LogicalRelSnapshot.java +++ b/core/src/main/java/org/polypheny/db/catalog/snapshot/LogicalRelSnapshot.java @@ -21,6 +21,7 @@ import javax.annotation.Nullable; import lombok.NonNull; import org.jetbrains.annotations.NotNull; +import org.polypheny.db.algebra.AlgCollation; import org.polypheny.db.algebra.AlgNode; import org.polypheny.db.catalog.Catalog; import org.polypheny.db.catalog.entity.LogicalConstraint; @@ -288,6 +289,7 @@ public interface LogicalRelSnapshot { AlgNode getNodeInfo( long id ); + AlgCollation getCollationInfo( long id ); List getConnectedViews( long id ); diff --git a/core/src/main/java/org/polypheny/db/catalog/snapshot/impl/LogicalRelSnapshotImpl.java b/core/src/main/java/org/polypheny/db/catalog/snapshot/impl/LogicalRelSnapshotImpl.java index 758ca873e4..4e93cc1046 100644 --- a/core/src/main/java/org/polypheny/db/catalog/snapshot/impl/LogicalRelSnapshotImpl.java +++ b/core/src/main/java/org/polypheny/db/catalog/snapshot/impl/LogicalRelSnapshotImpl.java @@ -33,6 +33,7 @@ import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.polypheny.db.algebra.AlgCollation; import org.polypheny.db.algebra.AlgNode; import org.polypheny.db.catalog.catalogs.LogicalRelationalCatalog; import org.polypheny.db.catalog.entity.LogicalConstraint; @@ -101,6 +102,8 @@ public class LogicalRelSnapshotImpl implements LogicalRelSnapshot { ImmutableMap nodes; + ImmutableMap collations; + ImmutableMap> connectedViews; @@ -152,6 +155,8 @@ public LogicalRelSnapshotImpl( Map catalogs ) { /// ALGNODES e.g. views and materializedViews this.nodes = ImmutableMap.copyOf( catalogs.values().stream().flatMap( c -> c.getNodes().entrySet().stream() ).collect( Collectors.toMap( Entry::getKey, Entry::getValue, getDuplicateError() ) ) ); + this.collations = ImmutableMap.copyOf( catalogs.values().stream().flatMap( c -> c.getCollations().entrySet().stream() ).collect( Collectors.toMap( Entry::getKey, Entry::getValue, getDuplicateError() ) ) ); + this.views = buildViews(); this.connectedViews = buildConnectedViews(); @@ -526,6 +531,12 @@ public AlgNode getNodeInfo( long id ) { } + @Override + public AlgCollation getCollationInfo( long id ) { + return collations.get( id ); + } + + @Override public List getConnectedViews( long id ) { return connectedViews.get( id ); diff --git a/core/src/test/java/org/polypheny/db/snapshot/MockRelSnapshot.java b/core/src/test/java/org/polypheny/db/snapshot/MockRelSnapshot.java index a5de34af9b..df347cd5bd 100644 --- a/core/src/test/java/org/polypheny/db/snapshot/MockRelSnapshot.java +++ b/core/src/test/java/org/polypheny/db/snapshot/MockRelSnapshot.java @@ -21,6 +21,7 @@ import lombok.NonNull; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.polypheny.db.algebra.AlgCollation; import org.polypheny.db.algebra.AlgNode; import org.polypheny.db.catalog.entity.LogicalConstraint; import org.polypheny.db.catalog.entity.logical.LogicalColumn; @@ -250,6 +251,12 @@ public AlgNode getNodeInfo( long id ) { } + @Override + public AlgCollation getCollationInfo( long id ) { + throw new UnsupportedOperationException(); + } + + @Override public List getConnectedViews( long id ) { throw new UnsupportedOperationException(); diff --git a/dbms/src/main/java/org/polypheny/db/view/MaterializedViewManagerImpl.java b/dbms/src/main/java/org/polypheny/db/view/MaterializedViewManagerImpl.java index 2e0631b705..e8ec5e5c59 100644 --- a/dbms/src/main/java/org/polypheny/db/view/MaterializedViewManagerImpl.java +++ b/dbms/src/main/java/org/polypheny/db/view/MaterializedViewManagerImpl.java @@ -313,7 +313,7 @@ public void addData( Transaction transaction, @Nullable List> store DataMigrator dataMigrator = transaction.getDataMigrator(); for ( AllocationEntity allocation : transaction.getSnapshot().alloc().getFromLogical( materializedView.id ) ) { Statement sourceStatement = transaction.createStatement(); - prepareSourceAlg( sourceStatement, materializedView.getAlgCollation(), algRoot.alg ); + prepareSourceAlg( sourceStatement, Catalog.snapshot().rel().getCollationInfo( materializedView.id ), algRoot.alg ); Statement targetStatement = transaction.createStatement(); if ( allocation.unwrap( AllocationTable.class ).isPresent() ) {