Skip to content

Commit

Permalink
Make views serialize and deserialize correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
gartens committed Dec 20, 2024
1 parent 3599759 commit 9d2c04d
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ public interface LogicalRelationalCatalog extends LogicalCatalog {

Map<Long, AlgNode> getNodes();

Map<Long, AlgCollation> getCollations();

void deleteKey( long id );

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,14 +43,27 @@ public class LogicalMaterializedView extends LogicalView {
public boolean ordered;


public LogicalMaterializedView(
long id,
String name,
long namespaceId,
String query,
Map<Long, List<Long>> 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<Long, List<Long>> underlyingTables,
@Deserialize("language") QueryLanguage language,
@Deserialize("languageName") String languageName,
@Deserialize("materializedCriteria") MaterializedCriteria materializedCriteria,
@Deserialize("ordered") boolean ordered
) {
Expand All @@ -61,9 +73,8 @@ public LogicalMaterializedView(
namespaceId,
EntityType.MATERIALIZED_VIEW,
query,
algCollation,
underlyingTables,
language );
languageName );

Map<Long, ImmutableList<Long>> map = new HashMap<>();
for ( Entry<Long, List<Long>> e : underlyingTables.entrySet() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,22 +50,32 @@ public class LogicalView extends LogicalTable {
@Serialize
public ImmutableMap<Long, List<Long>> 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<Long, List<Long>> 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<Long, List<Long>> underlyingTables,
@Deserialize("language") QueryLanguage language ) {
@Deserialize("languageName") String languageName ) {
super(
id,
name,
Expand All @@ -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 );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -80,13 +82,14 @@ public class RelationalCatalog implements PolySerializable, LogicalRelationalCat

@Serialize
@JsonProperty
public Map<Long, LogicalTable> tables;
public Map<Long, @SerializeClass(subclasses = { LogicalView.class, LogicalTable.class, LogicalMaterializedView.class }) LogicalTable> tables;

@Serialize
@JsonProperty
public Map<Long, LogicalColumn> columns;

public Map<Long, AlgNode> nodes;
public Map<Long, AlgCollation> collations;


@Serialize
Expand Down Expand Up @@ -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() );
}

Expand Down Expand Up @@ -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<Long, List<Long>> underlyingTables, List<Long> 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;
}
Expand All @@ -180,7 +185,6 @@ public LogicalMaterializedView addMaterializedView( final String name, long name
name,
namespaceId,
query,
algCollation,
underlyingTables,
language,
materializedCriteria,
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -288,6 +289,7 @@ public interface LogicalRelSnapshot {


AlgNode getNodeInfo( long id );
AlgCollation getCollationInfo( long id );

List<LogicalView> getConnectedViews( long id );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,6 +102,8 @@ public class LogicalRelSnapshotImpl implements LogicalRelSnapshot {

ImmutableMap<Long, AlgNode> nodes;

ImmutableMap<Long, AlgCollation> collations;

ImmutableMap<Long, List<LogicalView>> connectedViews;


Expand Down Expand Up @@ -152,6 +155,8 @@ public LogicalRelSnapshotImpl( Map<Long, LogicalRelationalCatalog> 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();
Expand Down Expand Up @@ -526,6 +531,12 @@ public AlgNode getNodeInfo( long id ) {
}


@Override
public AlgCollation getCollationInfo( long id ) {
return collations.get( id );
}


@Override
public List<LogicalView> getConnectedViews( long id ) {
return connectedViews.get( id );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -250,6 +251,12 @@ public AlgNode getNodeInfo( long id ) {
}


@Override
public AlgCollation getCollationInfo( long id ) {
throw new UnsupportedOperationException();
}


@Override
public List<LogicalView> getConnectedViews( long id ) {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public void addData( Transaction transaction, @Nullable List<DataStore<?>> 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() ) {
Expand Down

0 comments on commit 9d2c04d

Please sign in to comment.