Skip to content

Commit

Permalink
don't merge, fixes for documentagg
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Dec 4, 2023
1 parent bd6e849 commit 1c0e6b6
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 12 deletions.
6 changes: 6 additions & 0 deletions core/src/main/java/org/polypheny/db/type/PolyType.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ public enum PolyType {
Types.VARCHAR,
PolyTypeFamily.CHARACTER ),

TEXT(
PrecScale.NO_NO,
false,
Types.VARCHAR,
PolyTypeFamily.CHARACTER ),

BINARY(
PrecScale.NO_NO | PrecScale.YES_NO,
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ public PolyString asString() {
}


public boolean isText() {
return type == PolyType.TEXT;
}

public boolean isBinary() {
return type == PolyType.BINARY;
}
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/main/java/org/polypheny/db/PolyphenyDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class PolyphenyDb {
public boolean daemonMode = false;

@Option(name = { "-defaultStore" }, description = "Type of default storeId")
public String defaultStoreName = "hsqldb";
public String defaultStoreName = "mongodb";

@Option(name = { "-defaultSource" }, description = "Type of default source")
public String defaultSourceName = "csv";
Expand Down
5 changes: 4 additions & 1 deletion dbms/src/test/java/org/polypheny/db/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,10 @@ public static boolean checkDocResultSet( DocResult result, List<String> expected
fail();
throw new RuntimeException( "Should contain " + DocumentType.DOCUMENT_ID + " field." );
}
doc.remove( DocumentType.DOCUMENT_ID );
if ( excludeId ) {
doc.remove( DocumentType.DOCUMENT_ID );
}

}
parsedResults.add( doc );
}
Expand Down
6 changes: 3 additions & 3 deletions dbms/src/test/java/org/polypheny/db/mql/AggregateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ public void groupSubFieldTest() {
@Test
public void groupAvgTest() {
List<String> expected = MongoConnection.arrayToDoc( List.of(
new Object[]{ "val2", 5 },
new Object[]{ "val1", 7 } ),
"test", "avgValue" );
new Object[]{ "val2", 5.0 },
new Object[]{ "val1", 7.0 } ),
"_id", "avgValue" );
insertMany( DATA_1 );

DocResult result = aggregate( $group(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ private long doDML( Operation operation, String filter, List<String> operations,
if ( !dataContext.getParameterValues().isEmpty() ) {
assert operations.size() == 1;
// prepared
MongoDynamic util = new MongoDynamic( BsonDocument.parse( operations.get( 0 ) ), bucket, dataContext, getEntity().getDataModel() );
MongoDynamic util = MongoDynamic.create( BsonDocument.parse( operations.get( 0 ) ), bucket, dataContext, getEntity().getDataModel() );
List<Document> inserts = util.getAll( dataContext.getParameterValues() );
entity.getCollection().insertMany( session, inserts );
return inserts.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,26 @@
import org.apache.calcite.linq4j.function.Function1;
import org.apache.commons.lang3.NotImplementedException;
import org.bson.BsonDocument;
import org.bson.BsonType;
import org.bson.BsonValue;
import org.bson.Document;
import org.polypheny.db.adapter.mongodb.util.MongoTupleType;
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
import org.polypheny.db.type.PolyType;
import org.polypheny.db.type.entity.PolyBigDecimal;
import org.polypheny.db.type.entity.PolyBinary;
import org.polypheny.db.type.entity.PolyBoolean;
import org.polypheny.db.type.entity.PolyDate;
import org.polypheny.db.type.entity.PolyDouble;
import org.polypheny.db.type.entity.PolyInteger;
import org.polypheny.db.type.entity.PolyList;
import org.polypheny.db.type.entity.PolyLong;
import org.polypheny.db.type.entity.PolyNull;
import org.polypheny.db.type.entity.PolyString;
import org.polypheny.db.type.entity.PolyTime;
import org.polypheny.db.type.entity.PolyTimeStamp;
import org.polypheny.db.type.entity.PolyValue;
import org.polypheny.db.type.entity.document.PolyDocument;


/**
Expand Down Expand Up @@ -232,6 +239,10 @@ static Function1<Document, PolyValue[]> listGetter( final MongoTupleType type )
for ( MongoTupleType sub : type.subs ) {
trans.add( o -> convert( o.asDocument().get( sub.name ), sub ) );
}
if ( type.type == PolyType.DOCUMENT ) {
trans.add( o -> convert( o, type ) );
}

return e -> {
BsonDocument doc = e.toBsonDocument();

Expand All @@ -251,6 +262,7 @@ private static PolyValue convert( BsonValue o, MongoTupleType type ) {
if ( o == null || o.isNull() ) {
return new PolyNull();
}

switch ( type.type ) {
case BIGINT:
return PolyLong.of( o.asNumber().longValue() );
Expand All @@ -268,6 +280,8 @@ private static PolyValue convert( BsonValue o, MongoTupleType type ) {
return PolyTime.of( o.asNumber().longValue() );
case DATE:
return PolyDate.of( o.asNumber().longValue() );
case DOCUMENT:
return polyDocumentFromBson( o.asDocument() );
}
throw new NotImplementedException();

Expand Down Expand Up @@ -301,5 +315,48 @@ private static PolyValue convert( BsonValue o, MongoTupleType type ) {
}


private static PolyDocument polyDocumentFromBson( BsonDocument document ) {
PolyDocument doc = new PolyDocument();
for ( String key : document.keySet() ) {
doc.put( PolyString.of( key ), convert( document.get( key ), document.get( key ).getBsonType() ) );
}
return doc;
}


private static PolyValue convert( BsonValue value, BsonType bsonType ) {
switch ( bsonType ) {
case DOUBLE:
return PolyDouble.of( value.asDouble().getValue() );
case STRING:
return PolyString.of( value.asString().getValue() );
case DOCUMENT:
return polyDocumentFromBson( value.asDocument() );
case ARRAY:
return PolyList.of( value.asArray().getValues().stream().map( v -> convert( v, v.getBsonType() ) ).toArray( PolyValue[]::new ) );
case BINARY:
return PolyBinary.of( value.asBinary().getData() );
case OBJECT_ID:
return PolyString.of( value.asObjectId().getValue().toHexString() );
case BOOLEAN:
return PolyBoolean.of( value.asBoolean().getValue() );
case DATE_TIME:
return PolyTimeStamp.of( value.asDateTime().getValue() );
case NULL:
return PolyNull.NULL;
case INT32:
return PolyInteger.of( value.asInt32().getValue() );
case TIMESTAMP:
return PolyTimeStamp.of( value.asTimestamp().getValue() );
case INT64:
return PolyLong.of( value.asInt64().getValue() );
case DECIMAL128:
return PolyBigDecimal.of( value.asDecimal128().decimal128Value().bigDecimalValue() );
default:
throw new NotImplementedException();
}
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class MongoDynamic {

private final Map<Long, List<KeyWrapper>> keyHandles = new HashMap<>(); // parent, index,
private final Map<Long, Function<PolyValue, BsonValue>> transformers = new HashMap<>();
private final GridFSBucket bucket;
protected final GridFSBucket bucket;
private final BsonDocument document;
private final Map<Long, Boolean> isRegexMap = new HashMap<>();
private final Map<Long, Boolean> isFuncMap = new HashMap<>();
Expand All @@ -73,12 +73,15 @@ public MongoDynamic( BsonDocument document, GridFSBucket bucket, DataContext dat
this.document = document.clone();
this.bucket = bucket;
this.isProject = !document.isEmpty() && document.getFirstKey().equals( "$project" );
if ( dataModel == DataModel.RELATIONAL ) {
this.document.forEach( ( k, bsonValue ) -> replaceDynamic( bsonValue, this.document, k, true, false ) );
} else {
handleDynamic( this.document, new BsonString( "" ), "", true, false );
}
this.document.forEach( ( k, bsonValue ) -> replaceDynamic( bsonValue, this.document, k, true, false ) );
}


public static MongoDynamic create( BsonDocument document, GridFSBucket bucket, DataContext dataContext, DataModel dataModel ) {
if ( dataModel == DataModel.DOCUMENT ) {
return new MongoDocumentDynamic( document, bucket, dataContext, DataModel.DOCUMENT );
}
return new MongoDynamic( document, bucket, dataContext, DataModel.RELATIONAL );
}


Expand Down Expand Up @@ -379,4 +382,20 @@ public void insert( BsonValue value ) {

}


public static class MongoDocumentDynamic extends MongoDynamic {

public MongoDocumentDynamic( BsonDocument document, GridFSBucket bucket, DataContext dataContext, DataModel dataModel ) {
super( document, bucket, dataContext, dataModel );
}


@Override
public List<Document> getAll( List<Map<Long, PolyValue>> parameterValues ) {
return parameterValues.stream().flatMap( e -> e.entrySet().stream().map( v -> Document.parse( v.getValue().asDocument().toJson() ) ) ).collect( Collectors.toList() );
}


}

}

0 comments on commit 1c0e6b6

Please sign in to comment.