Skip to content

Commit

Permalink
added models for adapter types, using namespaceId as much as possible…
Browse files Browse the repository at this point in the history
…, fix renaming of documents
  • Loading branch information
datomo committed Aug 29, 2023
1 parent 25caa61 commit 18d786b
Show file tree
Hide file tree
Showing 36 changed files with 266 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ public interface LogicalDocumentCatalog extends LogicalCatalog {

Map<Long, LogicalCollection> getCollections();


void renameCollection( LogicalCollection collection, String newName );

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.Serializable;
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.experimental.SuperBuilder;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
import org.polypheny.db.catalog.Catalog;
Expand All @@ -29,6 +30,7 @@

@EqualsAndHashCode(callSuper = true)
@Value
@SuperBuilder(toBuilder = true)
public class LogicalCollection extends LogicalEntity implements CatalogObject {

private static final long serialVersionUID = -6490762948368178584L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ public void deleteCollection( long id ) {
}


@Override
public void renameCollection( LogicalCollection collection, String newName ) {
LogicalCollection newCollection = collection.toBuilder().name( newName ).build();
collections.put( newCollection.id, newCollection );
}


@Override
public LogicalCatalog withLogicalNamespace( LogicalNamespace namespace ) {
return toBuilder().logicalNamespace( namespace ).build();
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/org/polypheny/db/ddl/DdlManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,13 @@ public static DdlManager getInstance() {
/**
* Rename a table (changing the logical name of the table)
*
* @param tble the table to be renamed
* @param table the table to be renamed
* @param newTableName the new name for the table
* @param statement the used statement
*/
public abstract void renameTable( LogicalTable tble, String newTableName, Statement statement );
public abstract void renameTable( LogicalTable table, String newTableName, Statement statement );

public abstract void renameCollection( LogicalCollection collection, String newName, Statement statement );

/**
* Rename a column of a table (changing the logical name of the column)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,34 @@

import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.polypheny.db.catalog.logistic.NamespaceType;
import org.polypheny.db.languages.QueryParameters;


@Getter
@EqualsAndHashCode(callSuper = true)
@Value
public class ExtendedQueryParameters extends QueryParameters {

List<String> nodeLabels = new ArrayList<>();
List<String> relationshipLabels = new ArrayList<>();
final String databaseName;
@Setter
Long databaseId;
final boolean fullGraph;
public List<String> nodeLabels = new ArrayList<>();
public List<String> relationshipLabels = new ArrayList<>();
@NonFinal
public Long namespaceId;
public boolean fullGraph;


public ExtendedQueryParameters( String query, NamespaceType namespaceType, String databaseName ) {
public ExtendedQueryParameters( String query, NamespaceType namespaceType, Long namespaceId ) {
super( query, namespaceType );
this.databaseName = databaseName;
this.namespaceId = namespaceId;
this.fullGraph = false;
}


public ExtendedQueryParameters( String databaseName ) {
public ExtendedQueryParameters( Long namespaceId ) {
super( "*", NamespaceType.GRAPH );
this.databaseName = databaseName;
this.namespaceId = namespaceId;
this.fullGraph = true;
}

Expand Down
19 changes: 19 additions & 0 deletions dbms/src/main/java/org/polypheny/db/ddl/DdlManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,25 @@ public void renameTable( LogicalTable table, String newTableName, Statement stat
}


@Override
public void renameCollection( LogicalCollection collection, String newName, Statement statement ) {
if ( catalog.getSnapshot().rel().getTable( collection.namespaceId, newName ).isPresent() ) {
throw new GenericRuntimeException( "An entity with name %s already exists", newName );
}
// Check if views are dependent from this view
//checkViewDependencies( collection );

if ( !catalog.getSnapshot().getNamespace( collection.namespaceId ).orElseThrow().caseSensitive ) {
newName = newName.toLowerCase();
}

catalog.getLogicalDoc( collection.namespaceId ).renameCollection( collection, newName );

// Reset plan cache implementation cache & routing cache
statement.getQueryProcessor().resetCaches();
}


@Override
public void renameColumn( LogicalTable table, String columnName, String newColumnName, Statement statement ) {
LogicalColumn logicalColumn = catalog.getSnapshot().rel().getColumn( table.id, columnName ).orElseThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void stop() {
executionTime = System.nanoTime();

Statement statement = transaction.createStatement();
ExtendedQueryParameters parameters = new ExtendedQueryParameters( query, NamespaceType.GRAPH, request.database );
ExtendedQueryParameters parameters = new ExtendedQueryParameters( query, NamespaceType.GRAPH, request.namespaceId );

if ( transaction.isAnalyze() ) {
statement.getOverviewDuration().start( "Parsing" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,28 @@
import org.polypheny.db.algebra.constant.ExplainFormat;
import org.polypheny.db.algebra.constant.ExplainLevel;
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.catalog.Catalog;
import org.polypheny.db.cypher.cypher2alg.CypherToAlgConverter;
import org.polypheny.db.cypher.parser.CypherParser;
import org.polypheny.db.cypher.parser.CypherParser.CypherParserConfig;
import org.polypheny.db.ddl.DdlManager;
import org.polypheny.db.languages.NodeParseException;
import org.polypheny.db.languages.QueryParameters;
import org.polypheny.db.nodes.Node;
import org.polypheny.db.plan.AlgOptCluster;
import org.polypheny.db.plan.AlgOptUtil;
import org.polypheny.db.processing.AutomaticDdlProcessor;
import org.polypheny.db.processing.ExtendedQueryParameters;
import org.polypheny.db.processing.Processor;
import org.polypheny.db.rex.RexBuilder;
import org.polypheny.db.tools.AlgBuilder;
import org.polypheny.db.transaction.Lock.LockMode;
import org.polypheny.db.transaction.LockManager;
import org.polypheny.db.transaction.Statement;
import org.polypheny.db.transaction.Transaction;
import org.polypheny.db.transaction.TransactionException;
import org.polypheny.db.transaction.TransactionImpl;
import org.polypheny.db.util.DeadlockException;
import org.polypheny.db.util.Pair;

@Slf4j
public class CypherProcessorImpl extends AutomaticDdlProcessor {
public class CypherProcessorImpl extends Processor {

private static final CypherParserConfig parserConfig;

Expand Down Expand Up @@ -151,28 +148,4 @@ public AlgDataType getParameterRowType( Node left ) {
return null;
}


@Override
public void autoGenerateDDL( Statement statement, Node node, QueryParameters parameters ) {

assert parameters instanceof ExtendedQueryParameters;
try {
DdlManager ddlManager = DdlManager.getInstance();
long namespaceId = ddlManager.createGraph(
((ExtendedQueryParameters) parameters).getDatabaseName(), true, null, true, false, true, statement );

statement.getTransaction().commit();
((ExtendedQueryParameters) parameters).setDatabaseId( namespaceId );
Catalog.getInstance().updateSnapshot();
} catch ( TransactionException e ) {
throw new RuntimeException( "Unable to commit auto-generated structure:" + e.getMessage() );
}
}


@Override
public boolean needsDdlGeneration( Node query, QueryParameters parameters ) {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ public CypherToAlgConverter( Statement statement, AlgBuilder builder, RexBuilder


public AlgRoot convert( CypherNode query, ExtendedQueryParameters parameters, AlgOptCluster cluster ) {
long databaseId;
if ( parameters.getDatabaseId() == null ) {
databaseId = getDatabaseId( parameters );
long namespaceId;
if ( parameters.namespaceId == null ) {
namespaceId = getDatabaseId( parameters );
} else {
databaseId = parameters.getDatabaseId();
namespaceId = parameters.namespaceId;
}

LogicalEntity entity = getEntity( databaseId, parameters );
LogicalEntity entity = getEntity( namespaceId, parameters );

if ( parameters.isFullGraph() ) {
// simple full graph scan
Expand All @@ -140,12 +140,12 @@ private LogicalEntity getEntity( long databaseId, ExtendedQueryParameters parame
return optionalGraph.get();
}

Optional<LogicalTable> optionalTable = this.snapshot.rel().getTable( databaseId, parameters.getDatabaseName() );
Optional<LogicalTable> optionalTable = this.snapshot.rel().getTable( databaseId );
if ( optionalTable.isPresent() ) {
return optionalTable.get();
}

return this.snapshot.doc().getCollection( databaseId, parameters.getDatabaseName() ).orElseThrow();
return this.snapshot.doc().getCollection( databaseId ).orElseThrow();
}


Expand All @@ -159,7 +159,7 @@ private AlgNode buildFullScan( LogicalGraph graph ) {


private long getDatabaseId( ExtendedQueryParameters parameters ) {
return snapshot.getNamespace( parameters.getDatabaseName() ).orElseThrow().id;
return snapshot.getNamespace( parameters.getNamespaceId() ).orElseThrow().id;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class RelationalExploreResult extends RelationalResult {
builder.request( rel.request );
builder.affectedRows( rel.affectedRows );
builder.exception( rel.exception );
builder.generatedQuery( rel.generatedQuery );
builder.generatedQuery( rel.query );
builder.language( rel.language );
builder.type( rel.type );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.polypheny.db.algebra.AlgRoot;
import org.polypheny.db.algebra.constant.Kind;
import org.polypheny.db.algebra.operators.OperatorName;
import org.polypheny.db.catalog.Catalog;
import org.polypheny.db.catalog.logistic.NamespaceType;
import org.polypheny.db.information.InformationManager;
import org.polypheny.db.languages.mql.Mql.Family;
Expand Down Expand Up @@ -114,14 +115,14 @@ public static void startup() {

String[] mqls = request.query.trim().split( "\\n(?=(use|db.|show))" );

String database = request.database;
Long namespaceId = request.namespaceId;
long executionTime = System.nanoTime();
boolean noLimit = false;

for ( String query : mqls ) {
try {
Statement statement = transaction.createStatement();
QueryParameters parameters = new MqlQueryParameters( query, database, NamespaceType.DOCUMENT );
QueryParameters parameters = new MqlQueryParameters( query, namespaceId, NamespaceType.DOCUMENT );

if ( transaction.isAnalyze() ) {
statement.getOverviewDuration().start( "Parsing" );
Expand All @@ -132,7 +133,7 @@ public static void startup() {
}

if ( parsed instanceof MqlUseDatabase ) {
database = ((MqlUseDatabase) parsed).getDatabase();
namespaceId = Catalog.snapshot().getNamespace( ((MqlUseDatabase) parsed).getDatabase() ).orElseThrow().id;
//continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.polypheny.db.algebra.constant.ExplainLevel;
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.catalog.Catalog;
import org.polypheny.db.catalog.logistic.Pattern;
import org.polypheny.db.languages.mql.MqlCollectionStatement;
import org.polypheny.db.languages.mql.MqlCreateCollection;
import org.polypheny.db.languages.mql.MqlNode;
Expand Down Expand Up @@ -104,7 +103,7 @@ public Pair<Node, AlgDataType> validate( Transaction transaction, Node parsed, b
public boolean needsDdlGeneration( Node query, QueryParameters parameters ) {
if ( query instanceof MqlCollectionStatement ) {
return Catalog.snapshot()
.getNamespaces( Pattern.of( ((MqlQueryParameters) parameters).getDatabase() ) )
.getNamespace( ((MqlQueryParameters) parameters).getNamespaceId() )
.stream().flatMap( n -> Catalog.getInstance().getSnapshot().doc().getCollections( n.id, null ).stream() )
.noneMatch( t -> t.name.equals( ((MqlCollectionStatement) query).getCollection() ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public MqlAddPlacement( ParserPos pos, String collection, List<String> stores )
public void execute( Context context, Statement statement, QueryParameters parameters ) {
AdapterManager adapterManager = AdapterManager.getInstance();

long namespaceId = context.getSnapshot().getNamespace( ((MqlQueryParameters) parameters).getDatabase() ).orElseThrow().id;
long namespaceId = context.getSnapshot().getNamespace( ((MqlQueryParameters) parameters).getNamespaceId() ).orElseThrow().id;

List<LogicalCollection> collections = context.getSnapshot().doc().getCollections( namespaceId, new Pattern( getCollection() ) );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String toString() {
public void execute( Context context, Statement statement, QueryParameters parameters ) {
AdapterManager adapterManager = AdapterManager.getInstance();

long namespaceId = context.getSnapshot().getNamespace( ((MqlQueryParameters) parameters).getDatabase() ).orElseThrow().id;
long namespaceId = context.getSnapshot().getNamespace( ((MqlQueryParameters) parameters).getNamespaceId() ).orElseThrow().id;

PlacementType placementType = PlacementType.AUTOMATIC;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public MqlCreateView( ParserPos pos, String name, String source, BsonArray pipel

@Override
public void execute( Context context, Statement statement, QueryParameters parameters ) {
String database = ((MqlQueryParameters) parameters).getDatabase();
Long database = ((MqlQueryParameters) parameters).getNamespaceId();

long schemaId = context.getSnapshot().getNamespace( database ).orElseThrow().id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public MqlDeletePlacement( ParserPos pos, String collection, List<String> stores
public void execute( Context context, Statement statement, QueryParameters parameters ) {
AdapterManager adapterManager = AdapterManager.getInstance();

long namespaceId = context.getSnapshot().getNamespace( ((MqlQueryParameters) parameters).getDatabase() ).orElseThrow().id;
long namespaceId = context.getSnapshot().getNamespace( ((MqlQueryParameters) parameters).getNamespaceId() ).orElseThrow().id;

LogicalCollection collection = context.getSnapshot().doc().getCollection( namespaceId, getCollection() ).orElseThrow();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.polypheny.db.languages.mql;

import java.util.List;
import java.util.Optional;
import org.polypheny.db.catalog.entity.logical.LogicalCollection;
import org.polypheny.db.catalog.entity.logical.LogicalNamespace;
import org.polypheny.db.catalog.logistic.Pattern;
Expand Down Expand Up @@ -45,14 +46,15 @@ public Type getMqlKind() {
@Override
public void execute( Context context, Statement statement, QueryParameters parameters ) {
DdlManager ddlManager = DdlManager.getInstance();
String database = ((MqlQueryParameters) parameters).getDatabase();
Long namespaceId = ((MqlQueryParameters) parameters).getNamespaceId();

if ( context.getSnapshot().getNamespaces( new Pattern( database ) ).size() != 1 ) {
Optional<LogicalNamespace> optionalNamespace = context.getSnapshot().getNamespace( namespaceId );
if ( optionalNamespace.isEmpty() ) {
// dropping a document database( Polyschema ), which does not exist, which is a no-op
return;
}

LogicalNamespace namespace = context.getSnapshot().getNamespaces( new Pattern( database ) ).get( 0 );
LogicalNamespace namespace = optionalNamespace.get();
List<LogicalCollection> collections = context.getSnapshot().doc().getCollections( namespace.id, new Pattern( getCollection() ) );
if ( collections.size() != 1 ) {
// dropping a collection, which does not exist, which is a no-op
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.polypheny.db.languages.mql;

import org.polypheny.db.catalog.Catalog;
import org.polypheny.db.ddl.DdlManager;
import org.polypheny.db.languages.ParserPos;
import org.polypheny.db.languages.QueryParameters;
Expand All @@ -34,9 +35,9 @@ public MqlDropDatabase( ParserPos pos ) {

@Override
public void execute( Context context, Statement statement, QueryParameters parameters ) {
String database = ((MqlQueryParameters) parameters).getDatabase();
Long namespaceId = ((MqlQueryParameters) parameters).getNamespaceId();

DdlManager.getInstance().dropNamespace( database, true, statement );
DdlManager.getInstance().dropNamespace( Catalog.snapshot().getNamespace( namespaceId ).orElseThrow().name, true, statement );
}


Expand Down
Loading

0 comments on commit 18d786b

Please sign in to comment.