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 205d5df
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 22 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 @@ -326,8 +326,12 @@ public interface LogicalRelationalCatalog extends LogicalCatalog {

Map<Long, LogicalConstraint> getConstraints();

void setNodeAndCollation( long id, AlgNode node, AlgCollation collation );

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
33 changes: 33 additions & 0 deletions core/src/main/java/org/polypheny/db/catalog/impl/PolyCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.polypheny.db.adapter.AdapterManager.Function5;
import org.polypheny.db.adapter.DeployMode;
import org.polypheny.db.adapter.java.AdapterTemplate;
import org.polypheny.db.algebra.AlgRoot;
import org.polypheny.db.catalog.Catalog;
import org.polypheny.db.catalog.IdBuilder;
import org.polypheny.db.catalog.catalogs.AdapterCatalog;
Expand All @@ -62,6 +63,7 @@
import org.polypheny.db.catalog.entity.LogicalQueryInterface;
import org.polypheny.db.catalog.entity.LogicalUser;
import org.polypheny.db.catalog.entity.logical.LogicalNamespace;
import org.polypheny.db.catalog.entity.logical.LogicalView;
import org.polypheny.db.catalog.entity.physical.PhysicalEntity;
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
import org.polypheny.db.catalog.impl.allocation.PolyAllocDocCatalog;
Expand All @@ -71,13 +73,19 @@
import org.polypheny.db.catalog.impl.logical.GraphCatalog;
import org.polypheny.db.catalog.impl.logical.RelationalCatalog;
import org.polypheny.db.catalog.logistic.DataModel;
import org.polypheny.db.catalog.logistic.Pattern;
import org.polypheny.db.catalog.persistance.FilePersister;
import org.polypheny.db.catalog.persistance.InMemoryPersister;
import org.polypheny.db.catalog.persistance.Persister;
import org.polypheny.db.catalog.snapshot.Snapshot;
import org.polypheny.db.catalog.snapshot.impl.SnapshotBuilder;
import org.polypheny.db.catalog.util.ConstraintCondition;
import org.polypheny.db.config.RuntimeConfig;
import org.polypheny.db.iface.QueryInterfaceManager.QueryInterfaceTemplate;
import org.polypheny.db.nodes.Node;
import org.polypheny.db.processing.Processor;
import org.polypheny.db.processing.QueryContext.ParsedQueryContext;
import org.polypheny.db.transaction.Statement;
import org.polypheny.db.transaction.Transaction;
import org.polypheny.db.type.PolySerializable;
import org.polypheny.db.util.Pair;
Expand Down Expand Up @@ -534,6 +542,31 @@ public void restore( Transaction transaction ) {
} );

updateSnapshot();

restoreViews( transaction );

updateSnapshot();
}


private void restoreViews( Transaction transaction ) {
Statement statement = transaction.createStatement();
snapshot.rel().getTables( (Pattern) null, null ).forEach( table -> {
if ( table instanceof LogicalView view ) {
Processor sqlProcessor = statement.getTransaction().getProcessor( view.language );
Node node = sqlProcessor.parse( view.query ).get( 0 );
AlgRoot algRoot = sqlProcessor.translate( statement,
ParsedQueryContext.builder()
.query( view.query )
.language( view.language )
.queryNode( sqlProcessor.validate(
statement.getTransaction(), node, RuntimeConfig.ADD_DEFAULT_VALUES_IN_INSERTS.getBoolean() ).left )
.origin( statement.getTransaction().getOrigin() )
.build() );
getLogicalRel( view.namespaceId ).setNodeAndCollation( view.id, algRoot.alg, algRoot.collation );
}
} );
transaction.commit();
}


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 Down Expand Up @@ -80,13 +81,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 +129,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 +165,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 +184,6 @@ public LogicalMaterializedView addMaterializedView( final String name, long name
name,
namespaceId,
query,
algCollation,
underlyingTables,
language,
materializedCriteria,
Expand All @@ -189,6 +192,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 Expand Up @@ -589,4 +593,10 @@ public boolean isTableFlaggedForDeletion( long tableId ) {
return tablesFlaggedForDeletion.contains( tableId );
}


public void setNodeAndCollation( long id, AlgNode node, AlgCollation collation ) {
this.nodes.put( id, node );
this.collations.put( id, collation );
}

}
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ public void execute( Context context, Statement statement, ParsedQueryContext pa

PlacementType placementType = PlacementType.AUTOMATIC;

QueryLanguage language = QueryLanguage.from( "sql" );
Processor sqlProcessor = statement.getTransaction().getProcessor( language );
Processor sqlProcessor = statement.getTransaction().getProcessor( query.getLanguage() );
AlgRoot algRoot = sqlProcessor.translate( statement,
ParsedQueryContext.builder()
.query( query.toString() )
.language( language )
.query( query.toSqlString( PolyphenyDbSqlDialect.DEFAULT ).toString() )
.language( query.getLanguage() )
.queryNode(
sqlProcessor.validate(
statement.getTransaction(), this.query, RuntimeConfig.ADD_DEFAULT_VALUES_IN_INSERTS.getBoolean() ).left )
Expand Down

0 comments on commit 205d5df

Please sign in to comment.