Skip to content

Commit

Permalink
simplification for document, dont merge
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Dec 4, 2023
1 parent 8b0d5c3 commit 970943b
Show file tree
Hide file tree
Showing 21 changed files with 600 additions and 565 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@


/**
* Call to an aggregate function within an {@link org.polypheny.db.algebra.core.Aggregate}.
* Call to an aggregate function within an {@link Aggregate}.
*/
public class AggregateCall {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019-2023 The Polypheny Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.polypheny.db.algebra.core;

import java.util.Optional;
import org.polypheny.db.algebra.fun.AggFunction;
import org.polypheny.db.rex.RexNode;

public class DocumentAggregateCall {

public final String name;
public final AggFunction function;
private final RexNode input;


public DocumentAggregateCall( String name, AggFunction function, RexNode input ) {
this.name = name;
this.function = function;
this.input = input;
}


public static DocumentAggregateCall create( String name, AggFunction function, RexNode input ) {
return new DocumentAggregateCall( name, function, input );
}


public Optional<RexNode> getInput() {
return Optional.ofNullable( input );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,33 @@
import java.util.Objects;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.SingleAlg;
import org.polypheny.db.algebra.core.AggregateCall;
import org.polypheny.db.algebra.core.DocumentAggregateCall;
import org.polypheny.db.algebra.type.DocumentType;
import org.polypheny.db.plan.AlgOptCluster;
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.rex.RexNode;
import org.polypheny.db.schema.trait.ModelTrait;


public class DocumentAggregate extends SingleAlg implements DocumentAlg {

public final boolean indicator;
public final List<AggregateCall> aggCalls;
public final List<String> groupSet;
public final List<List<String>> groupSets;
public final List<String> names;
@NotNull
public final List<DocumentAggregateCall> aggCalls;
@Nullable // null means "group by all columns in input row"
public final RexNode group;


/**
* Creates a {@link DocumentAggregate}.
* {@link ModelTrait#DOCUMENT} native node of an aggregate.
*/
protected DocumentAggregate( AlgOptCluster cluster, AlgTraitSet traits, AlgNode child, boolean indicator, @NotNull List<String> groupSet, List<List<String>> groupSets, List<AggregateCall> aggCalls, List<String> names ) {
protected DocumentAggregate( AlgOptCluster cluster, AlgTraitSet traits, AlgNode child, @Nullable RexNode group, List<DocumentAggregateCall> aggCalls ) {
super( cluster, traits, child );
this.indicator = indicator; // true is allowed, but discouraged
this.group = group;
this.aggCalls = ImmutableList.copyOf( aggCalls );
this.groupSet = Objects.requireNonNull( groupSet );
this.names = names;
if ( groupSets == null ) {
this.groupSets = ImmutableList.of( groupSet );
} else {
this.groupSets = ImmutableList.copyOf( groupSets );
//assert ImmutableBitSet.ORDERING.isStrictlyOrdered( groupSets ) : groupSets;
/*for ( List<String> set : groupSets ) {
assert groupSet.contains( set );
}*/
}
assert groupSet.size() <= child.getRowType().getFieldCount();
this.rowType = DocumentType.ofDoc();
}

Expand All @@ -67,10 +56,7 @@ protected DocumentAggregate( AlgOptCluster cluster, AlgTraitSet traits, AlgNode
public String algCompareString() {
return this.getClass().getSimpleName() + "$" +
input.algCompareString() + "$" +
(aggCalls != null ? aggCalls.stream().map( Objects::toString ).collect( Collectors.joining( " $ " ) ) : "") + "$" +
(groupSet != null ? groupSet.toString() : "") + "$" +
(groupSets != null ? groupSets.stream().map( Objects::toString ).collect( Collectors.joining( " $ " ) ) : "") + "$" +
indicator + "&";
(aggCalls != null ? aggCalls.stream().map( Objects::toString ).collect( Collectors.joining( " $ " ) ) : "") + "&";
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ protected DocumentProject( AlgOptCluster cluster, AlgTraitSet traits, AlgNode in
super( cluster, traits, input );
this.includes = includes;
this.excludes = excludes;
this.rowType = DocumentType.ofDoc();
this.rowType = DocumentType.ofIncludes( includes ).ofExcludes( excludes );
}


@Override
public String algCompareString() {
return "$" + getClass().getSimpleName() + "$" + includes.hashCode() + "$" + getInput().algCompareString();
return "$" + getClass().getSimpleName() + "$" + includes.hashCode() + "$" + excludes.hashCode() + getInput().algCompareString();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,21 @@
package org.polypheny.db.algebra.enumerable.document;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.enumerable.EnumerableConvention;
import org.polypheny.db.algebra.enumerable.EnumerableProject;
import org.polypheny.db.algebra.logical.document.LogicalDocumentAggregate;
import org.polypheny.db.algebra.logical.document.LogicalDocumentProject;
import org.polypheny.db.algebra.logical.relational.LogicalProject;
import org.polypheny.db.algebra.operators.OperatorName;
import org.polypheny.db.algebra.type.AlgDataTypeField;
import org.polypheny.db.algebra.type.DocumentType;
import org.polypheny.db.languages.OperatorRegistry;
import org.polypheny.db.languages.QueryLanguage;
import org.polypheny.db.plan.AlgOptRule;
import org.polypheny.db.plan.AlgOptRuleCall;
import org.polypheny.db.rex.RexIndexRef;
import org.polypheny.db.rex.RexNode;
import org.polypheny.db.schema.trait.ModelTrait;
import org.polypheny.db.tools.AlgBuilder;
import org.polypheny.db.tools.AlgBuilder.GroupKey;
import org.polypheny.db.type.PolyType;
import org.polypheny.db.type.entity.PolyList;
import org.polypheny.db.type.entity.PolyString;
import org.polypheny.db.util.ImmutableBitSet;

public class DocumentAggregateToAggregateRule extends AlgOptRule {

public static final DocumentAggregateToAggregateRule INSTANCE = new DocumentAggregateToAggregateRule();


public DocumentAggregateToAggregateRule() {
super( operand( LogicalDocumentAggregate.class, any() ), "DOCUMENT_AGGREGATE_TO_AGGREGATE" );
super( operand( LogicalDocumentAggregate.class, any() ), DocumentAggregateToAggregateRule.class.getSimpleName() );
}


Expand All @@ -68,10 +48,11 @@ public void onMatch( AlgOptRuleCall call ) {
List<RexNode> nodes = new ArrayList<>();
List<String> names = new ArrayList<>();
RexIndexRef parent = builder.getRexBuilder().makeInputRef( alg.getInput(), 0 );
// todo dl fix
//nodes.add( parent );
//names.add( alg.getInput().getRowType().getFieldNames().get( 0 ) );

for ( String path : alg.groupSet ) {
/*for ( String path : alg.groupSet ) {
RexNode node = builder.getRexBuilder().makeCall(
DocumentType.ofId(),
OperatorRegistry.get( QueryLanguage.from( "mongo" ), OperatorName.MQL_QUERY_VALUE ),
Expand Down Expand Up @@ -119,7 +100,7 @@ public void onMatch( AlgOptRuleCall call ) {
Map<String, RexNode> docs = enumerableAggregate.getRowType().getFields().stream().collect( Collectors.toMap( AlgDataTypeField::getName, e -> builder.getRexBuilder().makeInputRef( DocumentType.ofDoc(), e.getIndex() ) ) );
call.transformTo( LogicalDocumentProject.create( enumerableAggregate, docs, List.of() ) );
// call.transformTo( LogicalAggregate.create( alg.getInput(), alg.groupSet, alg.groupSets, alg.aggCalls ) );
// call.transformTo( LogicalAggregate.create( alg.getInput(), alg.groupSet, alg.groupSets, alg.aggCalls ) );*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.polypheny.db.algebra.enumerable.EnumerableCalc;
import org.polypheny.db.algebra.enumerable.EnumerableConvention;
import org.polypheny.db.algebra.logical.document.LogicalDocumentProject;
import org.polypheny.db.algebra.type.DocumentType;
import org.polypheny.db.plan.Convention;
import org.polypheny.db.rex.RexFieldAccess;
import org.polypheny.db.rex.RexNode;
Expand All @@ -44,7 +45,7 @@ public DocumentProjectToCalcRule() {
public AlgNode convert( AlgNode alg ) {
final LogicalDocumentProject project = (LogicalDocumentProject) alg;
final AlgNode input = project.getInput();
final RexProgram program = RexProgram.create( input.getRowType(), List.of( replaceAccess( project.asSingleProject() ) ), null, project.getRowType(), project.getCluster().getRexBuilder() );
final RexProgram program = RexProgram.create( input.getRowType(), List.of( replaceAccess( project.asSingleProject() ) ), null, DocumentType.ofId(), project.getCluster().getRexBuilder() );
return EnumerableCalc.create( convert( input, input.getTraitSet().replace( EnumerableConvention.INSTANCE ) ), program );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,60 @@
package org.polypheny.db.algebra.logical.document;

import java.util.List;
import java.util.stream.IntStream;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.AlgShuttle;
import org.polypheny.db.algebra.core.Aggregate;
import org.polypheny.db.algebra.core.AggregateCall;
import org.polypheny.db.algebra.core.DocumentAggregateCall;
import org.polypheny.db.algebra.core.document.DocumentAggregate;
import org.polypheny.db.algebra.type.AlgDataType;
import org.polypheny.db.algebra.type.AlgDataTypeFactory;
import org.polypheny.db.algebra.type.DocumentType;
import org.polypheny.db.plan.AlgOptCluster;
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.plan.Convention;
import org.polypheny.db.util.ImmutableBitSet;
import org.polypheny.db.rex.RexNode;


public class LogicalDocumentAggregate extends DocumentAggregate {

/**
* Subclass of {@link DocumentAggregate} not targeted at any particular engine or calling convention.
*/
protected LogicalDocumentAggregate( AlgOptCluster cluster, AlgTraitSet traits, AlgNode child, boolean indicator, List<String> groupSet, List<List<String>> groupSets, List<AggregateCall> aggCalls, List<String> names ) {
super( cluster, traits, child, indicator, groupSet, groupSets, aggCalls, names );
protected LogicalDocumentAggregate( AlgOptCluster cluster, AlgTraitSet traits, AlgNode child, RexNode group, List<DocumentAggregateCall> aggCalls ) {
super( cluster, traits, child, group, aggCalls );
}


/**
* Creates a LogicalAggregate.
*/
public static LogicalDocumentAggregate create( final AlgNode input, List<String> groupSet, List<List<String>> groupSets, List<AggregateCall> aggCalls, List<String> names ) {
return create_( input, false, groupSet, groupSets, aggCalls, names );
public static LogicalDocumentAggregate create( final AlgNode input, RexNode group, List<DocumentAggregateCall> aggCalls ) {
return create_( input, group, aggCalls );
}


private static LogicalDocumentAggregate create_( final AlgNode input, boolean indicator, List<String> groupSet, List<List<String>> groupSets, List<AggregateCall> aggCalls, List<String> names ) {
private static LogicalDocumentAggregate create_( final AlgNode input, RexNode group, List<DocumentAggregateCall> aggCalls ) {
final AlgOptCluster cluster = input.getCluster();
final AlgTraitSet traitSet = cluster.traitSetOf( Convention.NONE );
return new LogicalDocumentAggregate( cluster, traitSet, input, indicator, groupSet, groupSets, aggCalls, names );
return new LogicalDocumentAggregate( cluster, traitSet, input, group, aggCalls );
}


@Override
protected AlgDataType deriveRowType() {
return Aggregate.deriveRowType( getCluster().getTypeFactory(), getInput().getRowType(), indicator, getBitGroupSet(), null, aggCalls );
}
AlgDataTypeFactory.Builder builder = getCluster().getTypeFactory().builder();
builder.add( "_id", null, DocumentType.ofDoc() );

for ( DocumentAggregateCall aggCall : aggCalls ) {
builder.add( aggCall.name, null, DocumentType.ofDoc() );
}

public ImmutableBitSet getBitGroupSet() {
return ImmutableBitSet.of( IntStream.range( 0, groupSet.size() ).toArray() );
return builder.build();
}


@Override
public AlgNode copy( AlgTraitSet traitSet, List<AlgNode> inputs ) {
return new LogicalDocumentAggregate( inputs.get( 0 ).getCluster(), traitSet, inputs.get( 0 ), indicator, groupSet, groupSets, aggCalls, names );
return new LogicalDocumentAggregate( inputs.get( 0 ).getCluster(), traitSet, inputs.get( 0 ), group, aggCalls );
}


Expand Down
Loading

0 comments on commit 970943b

Please sign in to comment.