Skip to content

Commit

Permalink
Merge branch 'refactor' of https://github.com/polypheny/Polypheny-DB
Browse files Browse the repository at this point in the history
…into refactor
  • Loading branch information
datomo committed Dec 1, 2023
2 parents 4ca57cc + bbaf695 commit 7357987
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
10 changes: 6 additions & 4 deletions dbms/src/main/java/org/polypheny/db/ddl/DdlManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ public void createView( String viewName, long namespaceId, AlgNode algNode, AlgC
findUnderlyingTablesOfView( algNode, underlyingTables, fieldList );

// add check if underlying table is of model document -> mql, relational -> sql
underlyingTables.keySet().forEach( tableId -> checkModelLangCompatibility( language, namespaceId ) );
underlyingTables.keySet().forEach( tableId -> checkModelLangCompatibility( language.getDataModel(), namespaceId ) );

LogicalView view = catalog.getLogicalRel( namespaceId ).addView(
viewName,
Expand Down Expand Up @@ -1593,7 +1593,7 @@ public void createMaterializedView( String viewName, long namespaceId, AlgRoot a
LogicalRelSnapshot relSnapshot = snapshot.rel();

// add check if underlying table is of model document -> mql, relational -> sql
underlying.keySet().forEach( tableId -> checkModelLangCompatibility( language, namespaceId ) );
underlying.keySet().forEach( tableId -> checkModelLangCompatibility( language.getDataModel(), namespaceId ) );

if ( materializedCriteria.getCriteriaType() == CriteriaType.UPDATE ) {
List<EntityType> entityTypes = new ArrayList<>();
Expand Down Expand Up @@ -1646,9 +1646,9 @@ public void createMaterializedView( String viewName, long namespaceId, AlgRoot a
}


private void checkModelLangCompatibility( QueryLanguage language, long namespaceId ) {
private void checkModelLangCompatibility( DataModel model, long namespaceId ) {
LogicalNamespace namespace = catalog.getSnapshot().getNamespace( namespaceId ).orElseThrow();
if ( namespace.dataModel != language.getDataModel() ) {
if ( namespace.dataModel != model ) {
throw new GenericRuntimeException(
"The used language cannot execute schema changing queries on this entity with the data model %s.",
namespace.getDataModel() );
Expand Down Expand Up @@ -2002,6 +2002,8 @@ private void buildNamespace( long namespaceId, LogicalTable logical, Adapter<?>
public void createCollection( long namespaceId, String name, boolean ifNotExists, List<DataStore<?>> stores, PlacementType placementType, Statement statement ) {
String adjustedName = adjustNameIfNeeded( name, namespaceId );

checkModelLangCompatibility( DataModel.DOCUMENT, namespaceId );

if ( assertEntityExists( namespaceId, adjustedName, ifNotExists ) ) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public Enumerable<Object> handleDirectDML( Operation operation, String filter, L
try {
final long changes = doDML( operation, filter, operations, onlyOne, needsDocument, xid, bucket );

return Linq4j.asEnumerable( Collections.singletonList( PolyLong.of( changes ) ) );
return Linq4j.asEnumerable( Collections.singletonList( new PolyValue[]{ PolyLong.of( changes ) } ) );
} catch ( MongoException e ) {
entity.getTransactionProvider().rollback( xid );
log.warn( "Failed" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ private static List<ParsedQueryContext> anyQuerySplitter( QueryContext context )
if ( queryNode.getEntity() == null ) {
continue;
}
Optional<LogicalCollection> collection = snapshot.doc().getCollection( query.getNamespaceId(), queryNode.getEntity() );
Optional<LogicalCollection> collection = snapshot.doc().getCollection( context.getNamespaceId(), queryNode.getEntity() );
if ( collection.isEmpty() && !created.contains( Pair.of( context.getNamespaceId(), queryNode.getEntity() ) ) ) {
if ( queryNode instanceof MqlCreateCollection || queryNode instanceof MqlCreateView ) {
// entity was created during this query
created.add( Pair.of( context.getNamespaceId(), queryNode.getEntity() ) );
} else {
// we have to create this query manually
toCreate.add( 0, Pair.of( query.getNamespaceId(), queryNode.getEntity() ) );
toCreate.add( 0, Pair.of( context.getNamespaceId(), queryNode.getEntity() ) );
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions webui/src/main/java/org/polypheny/db/webui/WebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.polypheny.db.catalog.Catalog;
import org.polypheny.db.catalog.entity.logical.LogicalNamespace;
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
import org.polypheny.db.catalog.logistic.DataModel;
import org.polypheny.db.languages.QueryLanguage;
import org.polypheny.db.processing.QueryContext;
import org.polypheny.db.type.entity.graph.PolyGraph;
Expand Down Expand Up @@ -124,6 +125,7 @@ public void onMessage( final WsMessageContext ctx ) {
.language( language )
.isAnalysed( queryRequest.analyze )
.usesCache( queryRequest.cache )
.namespaceId( LanguageCrud.getNamespaceIdOrDefault( queryRequest.namespace ) )
.origin( POLYPHENY_UI ).batch( queryRequest.noLimit ? -1 : crud.getPageSize() )
.transactionManager( crud.getTransactionManager() )
.informationTarget( i -> i.setSession( ctx.session ) ).build(), queryRequest );
Expand Down Expand Up @@ -157,16 +159,18 @@ public void onMessage( final WsMessageContext ctx ) {
UIRequest uiRequest = ctx.messageAsClass( UIRequest.class );
try {
LogicalNamespace namespace = Catalog.getInstance().getSnapshot().getNamespace( uiRequest.namespace ).orElse( null );
switch ( namespace.dataModel ) {
switch ( namespace == null ? DataModel.RELATIONAL : namespace.dataModel ) {
case RELATIONAL:
result = crud.getTable( uiRequest );
break;
case DOCUMENT:
String entity = Catalog.snapshot().doc().getCollection( uiRequest.entityId ).map( c -> c.name ).orElse( "" );
result = LanguageCrud.anyQueryResult(
QueryContext.builder()
.query( "db.%s.find({})" )
.query( String.format( "db.%s.find({})", entity ) )
.language( QueryLanguage.from( "mongo" ) )
.origin( POLYPHENY_UI )
.batch( uiRequest.noLimit ? -1 : crud.getPageSize() )
.transactionManager( crud.getTransactionManager() )
.informationTarget( i -> i.setSession( ctx.session ) )
.namespaceId( namespace == null ? Catalog.defaultNamespaceId : namespace.id )
Expand All @@ -178,6 +182,7 @@ public void onMessage( final WsMessageContext ctx ) {
.query( "MATCH (n) RETURN n" )
.language( QueryLanguage.from( "cypher" ) )
.origin( POLYPHENY_UI )
.batch( uiRequest.noLimit ? -1 : crud.getPageSize() )
.namespaceId( namespace == null ? Catalog.defaultNamespaceId : namespace.id )
.transactionManager( crud.getTransactionManager() )
.informationTarget( i -> i.setSession( ctx.session ) )
Expand Down
46 changes: 31 additions & 15 deletions webui/src/main/java/org/polypheny/db/webui/crud/LanguageCrud.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,25 @@ public static void deleteToResult( QueryLanguage language ) {
public static void anyQuery( Context ctx ) {
QueryRequest request = ctx.bodyAsClass( QueryRequest.class );
QueryLanguage language = QueryLanguage.from( request.language );

QueryContext context = QueryContext.builder()
.query( request.query )
.language( language )
.isAnalysed( request.analyze )
.usesCache( request.cache )
.origin( "Polypheny-UI" )
.namespaceId( getNamespaceIdOrDefault( request.namespace ) )
.batch( request.noLimit ? -1 : crud.getPageSize() )
.transactionManager( crud.getTransactionManager() ).build();
ctx.json( anyQueryResult( context, request ) );
}


public static long getNamespaceIdOrDefault( String namespace ) {
return namespace == null ? Catalog.defaultNamespaceId : Catalog.snapshot().getNamespace( namespace ).orElseThrow().id;
}


public static List<? extends Result<?, ?>> anyQueryResult( QueryContext context, UIRequest request ) {
Transaction transaction = context.getTransactionManager().startTransaction( context.getUserId(), Catalog.defaultNamespaceId, context.isAnalysed(), context.getOrigin() );
transaction.setUseCache( context.isUsesCache() );
Expand All @@ -141,8 +148,7 @@ public static void anyQuery( Context ctx ) {

for ( ExecutedContext executedContext : executedContexts ) {
if ( executedContext.getError().isPresent() ) {
attachError( transaction, results, executedContext, executedContext.getError().get() );
return results;
return List.of( buildErrorResult( transaction, executedContext, executedContext.getError().get() ).build() );
}

results.add( builder.apply( executedContext, request, executedContext.getStatement() ).build() );
Expand Down Expand Up @@ -216,18 +222,18 @@ public static PolyGraph getGraph( String namespace, TransactionManager manager,
}


public static void attachError( Transaction transaction, List<Result<?, ?>> results, ExecutedContext context, Throwable t ) {
public static ResultBuilder<?, ?, ?, ?> buildErrorResult( Transaction transaction, ExecutedContext context, Throwable t ) {
//String msg = t.getMessage() == null ? "" : t.getMessage();
Result<?, ?> result;
ResultBuilder<?, ?, ?, ?> result;
switch ( context.getQuery().getLanguage().getDataModel() ) {
case RELATIONAL:
result = RelationalResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() ).build();
result = RelationalResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() );
break;
case DOCUMENT:
result = DocResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() ).build();
result = DocResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() );
break;
case GRAPH:
result = GraphResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() ).build();
result = GraphResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() );
break;
default:
throw new GenericRuntimeException( "Unknown data model." );
Expand All @@ -241,7 +247,7 @@ public static void attachError( Transaction transaction, List<Result<?, ?>> resu
}
}

results.add( result );
return result;
}


Expand All @@ -250,14 +256,14 @@ public static void attachError( Transaction transaction, List<Result<?, ?>> resu
Catalog catalog = Catalog.getInstance();
ResultIterator iterator = context.getIterator();
List<List<PolyValue>> rows = new ArrayList<>();
for ( int i = 0; i < request.currentPage; i++ ) {
rows = iterator.getNextBatch();
}

try {
for ( int i = 0; i < request.currentPage; i++ ) {
rows = iterator.getNextBatch();
}

iterator.close();
} catch ( Exception e ) {
throw new GenericRuntimeException( e );
return buildErrorResult( statement.getTransaction(), context, e );
}

boolean hasMoreRows = context.getIterator().hasMoreRows();
Expand Down Expand Up @@ -365,6 +371,7 @@ public static List<String[]> computeResultData( final List<List<PolyValue>> rows
.query( context.getQuery().getQuery() )
.language( context.getQuery().getLanguage() )
.dataModel( context.getIterator().getImplementation().getDataModel() )
.affectedTuples( data.size() )
.xid( statement.getTransaction().getXid().toString() )
.namespace( request.namespace );

Expand All @@ -378,18 +385,27 @@ public static List<String[]> computeResultData( final List<List<PolyValue>> rows

public static ResultBuilder<?, ?, ?, ?> getDocResult( ExecutedContext context, UIRequest request, Statement statement ) {
ResultIterator iterator = context.getIterator();
List<List<PolyValue>> data = iterator.getNextBatch();
List<List<PolyValue>> data = new ArrayList<>();

try {
for ( int i = 0; i < request.currentPage; i++ ) {
data = iterator.getNextBatch();
}

iterator.close();
} catch ( Exception e ) {
throw new GenericRuntimeException( e );
return buildErrorResult( statement.getTransaction(), context, e );
}

boolean hasMoreRows = context.getIterator().hasMoreRows();

return DocResult.builder()
.header( context.getIterator().getImplementation().rowType.getFields().stream().map( FieldDefinition::of ).toArray( FieldDefinition[]::new ) )
.data( data.stream().map( d -> d.get( 0 ).toJson() ).toArray( String[]::new ) )
.query( context.getQuery().getQuery() )
.language( context.getQuery().getLanguage() )
.hasMore( hasMoreRows )
.affectedTuples( data.size() )
.xid( statement.getTransaction().getXid().toString() )
.dataModel( context.getIterator().getImplementation().getDataModel() )
.namespace( request.namespace );
Expand Down

0 comments on commit 7357987

Please sign in to comment.