Skip to content

Commit

Permalink
Improve set related operations (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
gartens authored Jan 17, 2025
1 parent 4461bcf commit 2f9cfd5
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 92 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/org/polypheny/db/functions/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,7 @@ public static boolean memberOf( Object object, Collection<?> collection ) {
public static <E> Collection<E> multisetIntersectDistinct( Collection<E> c1, Collection<E> c2 ) {
final Set<E> result = new HashSet<>( c1 );
result.retainAll( c2 );
return new ArrayList<>( result );
return result;
}


Expand Down Expand Up @@ -2314,7 +2314,7 @@ public static <E> Collection<E> multisetExceptAll( Collection<E> c1, Collection<
public static <E> Collection<E> multisetExceptDistinct( Collection<E> c1, Collection<E> c2 ) {
final Set<E> result = new HashSet<>( c1 );
result.removeAll( c2 );
return new ArrayList<>( result );
return result;
}


Expand Down Expand Up @@ -2363,7 +2363,7 @@ public static Collection<String> multisetUnionDistinct( Collection<String> colle
Set<String> resultCollection = new HashSet<>( Math.max( (int) ((collection1.size() + collection2.size()) / .75f) + 1, 16 ) );
resultCollection.addAll( collection1 );
resultCollection.addAll( collection2 );
return new ArrayList<>( resultCollection );
return resultCollection;
}


Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/polypheny/db/rex/RexSimplify.java
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ private <C extends Comparable<C>> RexNode simplifyAnd2ForUnknownAsFalse( List<Re
// Example #1. x AND y AND z AND NOT (x AND y) - not satisfiable
// Example #2. x AND y AND NOT (x AND y) - not satisfiable
// Example #3. x AND y AND NOT (x AND y AND z) - may be satisfiable
final Set<RexNode> termsSet = new HashSet<>( terms );
final Set<RexNode> termsSet = Set.copyOf( terms );
for ( RexNode notDisjunction : notTerms ) {
if ( !RexUtil.isDeterministic( notDisjunction ) ) {
continue;
Expand Down Expand Up @@ -1439,7 +1439,7 @@ private RexNode verify( RexNode before, RexUnknownAs unknownAs, Function<RexSimp
// Analyzer cannot handle this expression currently
return simplified;
}
if ( !new HashSet<>( foo0.variables ).containsAll( foo1.variables ) ) {
if ( !Set.copyOf( foo0.variables ).containsAll( foo1.variables ) ) {
throw new AssertionError( "variable mismatch: " + before + " has " + foo0.variables + ", " + simplified + " has " + foo1.variables );
}
assignment_loop:
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/polypheny/db/util/EquivalenceSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;

Expand Down Expand Up @@ -169,7 +170,7 @@ public int size() {
* Returns the number of equivalence classes in this equivalence set.
*/
public int classCount() {
return new HashSet<>( parents.values() ).size();
return Set.copyOf( parents.values() ).size();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void testAsList() {
assertEquals( list1, listView );
assertThat( list1.hashCode(), equalTo( listView.hashCode() ) );

final Set<Integer> set = new HashSet<>( list1 );
final Set<Integer> set = Set.copyOf( list1 );
assertThat( setView.hashCode(), is( set.hashCode() ) );
assertThat( setView, equalTo( set ) );

Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/org/polypheny/db/util/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ private List<Integer> checkIntegerIntervalSet( String s, int... ints ) {
final Set<Integer> set = IntegerIntervalSet.of( s );
assertEquals( set.size(), ints.length );
List<Integer> list = new ArrayList<>( set );
assertEquals( new HashSet<>( Ints.asList( ints ) ), set );
assertEquals( Set.copyOf( Ints.asList( ints ) ), set );
return list;
}

Expand Down Expand Up @@ -980,7 +980,7 @@ public void testCompositeMap() {
private void checkCompositeMap( String[] beatles, Map<String, Integer> map ) {
assertThat( 4, equalTo( map.size() ) );
assertThat( false, equalTo( map.isEmpty() ) );
assertThat( map.keySet(), equalTo( (Set<String>) new HashSet<>( Arrays.asList( beatles ) ) ) );
assertThat( map.keySet(), equalTo( Set.copyOf( Arrays.asList( beatles ) ) ) );
assertThat( ImmutableMultiset.copyOf( map.values() ), equalTo( ImmutableMultiset.copyOf( Arrays.asList( 4, 4, 6, 5 ) ) ) );
}

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

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.polypheny.db.StatisticsManager;
Expand Down Expand Up @@ -86,7 +85,7 @@ private void updateDdlStatistics( DdlDataPoint dataPoint, StatisticsManager stat

private void updateQueryStatistics( QueryDataPointImpl dataPoint, StatisticsManager statisticsManager ) {
if ( !dataPoint.getAvailableColumnsWithTable().isEmpty() ) {
Set<Long> values = new HashSet<>( dataPoint.getAvailableColumnsWithTable().values() );
Set<Long> values = Set.copyOf( dataPoint.getAvailableColumnsWithTable().values() );
boolean isOneTable = values.size() == 1;
Snapshot snapshot = Catalog.snapshot();

Expand Down Expand Up @@ -120,7 +119,7 @@ private void updateDmlStatistics( DmlDataPoint dataPoint, StatisticsManager stat
return;
}

Set<Long> values = new HashSet<>( dataPoint.getAvailableColumnsWithTable().values() );
Set<Long> values = Set.copyOf( dataPoint.getAvailableColumnsWithTable().values() );
boolean isOneTable = values.size() == 1;

Snapshot snapshot = Catalog.snapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@


import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import lombok.NonNull;
import lombok.Value;
Expand All @@ -51,7 +48,6 @@
import org.polypheny.db.algebra.type.AlgDataTypeSystemImpl;
import org.polypheny.db.languages.OperatorRegistry;
import org.polypheny.db.languages.ParserPos;
import org.polypheny.db.nodes.Operator;
import org.polypheny.db.nodes.TimeUnitRange;
import org.polypheny.db.sql.language.dialect.JethroDataSqlDialect;
import org.polypheny.db.sql.language.dialect.JethroDataSqlDialect.JethroInfo;
Expand Down Expand Up @@ -84,60 +80,6 @@ public class SqlDialect {
*/
public static final Context EMPTY_CONTEXT = emptyContext();

/**
* Built-in scalar functions and operators common for every dialect.
*/
protected static final Set<Operator> BUILT_IN_OPERATORS_LIST =
ImmutableSet.<Operator>builder()
.add( OperatorRegistry.get( OperatorName.ABS ) )
.add( OperatorRegistry.get( OperatorName.ACOS ) )
.add( OperatorRegistry.get( OperatorName.AND ) )
.add( OperatorRegistry.get( OperatorName.ASIN ) )
.add( OperatorRegistry.get( OperatorName.BETWEEN ) )
.add( OperatorRegistry.get( OperatorName.CASE ) )
.add( OperatorRegistry.get( OperatorName.CAST ) )
.add( OperatorRegistry.get( OperatorName.CEIL ) )
.add( OperatorRegistry.get( OperatorName.CHAR_LENGTH ) )
.add( OperatorRegistry.get( OperatorName.CHARACTER_LENGTH ) )
.add( OperatorRegistry.get( OperatorName.COALESCE ) )
.add( OperatorRegistry.get( OperatorName.CONCAT ) )
.add( OperatorRegistry.get( OperatorName.COS ) )
.add( OperatorRegistry.get( OperatorName.COT ) )
.add( OperatorRegistry.get( OperatorName.DIVIDE ) )
.add( OperatorRegistry.get( OperatorName.EQUALS ) )
.add( OperatorRegistry.get( OperatorName.FLOOR ) )
.add( OperatorRegistry.get( OperatorName.GREATER_THAN ) )
.add( OperatorRegistry.get( OperatorName.GREATER_THAN_OR_EQUAL ) )
.add( OperatorRegistry.get( OperatorName.IN ) )
.add( OperatorRegistry.get( OperatorName.IS_NOT_NULL ) )
.add( OperatorRegistry.get( OperatorName.IS_NULL ) )
.add( OperatorRegistry.get( OperatorName.LESS_THAN ) )
.add( OperatorRegistry.get( OperatorName.LESS_THAN_OR_EQUAL ) )
.add( OperatorRegistry.get( OperatorName.LIKE ) )
.add( OperatorRegistry.get( OperatorName.LN ) )
.add( OperatorRegistry.get( OperatorName.LOG10 ) )
.add( OperatorRegistry.get( OperatorName.MINUS ) )
.add( OperatorRegistry.get( OperatorName.MOD ) )
.add( OperatorRegistry.get( OperatorName.MULTIPLY ) )
.add( OperatorRegistry.get( OperatorName.NOT ) )
.add( OperatorRegistry.get( OperatorName.NOT_BETWEEN ) )
.add( OperatorRegistry.get( OperatorName.NOT_EQUALS ) )
.add( OperatorRegistry.get( OperatorName.NOT_IN ) )
.add( OperatorRegistry.get( OperatorName.NOT_LIKE ) )
.add( OperatorRegistry.get( OperatorName.OR ) )
.add( OperatorRegistry.get( OperatorName.PI ) )
.add( OperatorRegistry.get( OperatorName.PLUS ) )
.add( OperatorRegistry.get( OperatorName.POWER ) )
.add( OperatorRegistry.get( OperatorName.RAND ) )
.add( OperatorRegistry.get( OperatorName.ROUND ) )
.add( OperatorRegistry.get( OperatorName.ROW ) )
.add( OperatorRegistry.get( OperatorName.SIN ) )
.add( OperatorRegistry.get( OperatorName.SQRT ) )
.add( OperatorRegistry.get( OperatorName.SUBSTRING ) )
.add( OperatorRegistry.get( OperatorName.TAN ) )
.build();


@NonNull
String name;

Expand Down Expand Up @@ -749,7 +691,7 @@ public boolean supportsPostGIS() {


public List<OperatorName> supportedGeoFunctions() {
return ImmutableList.of();
return List.of();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.polypheny.db.TestHelper;
Expand Down Expand Up @@ -796,41 +797,41 @@ public void testDivideAny() {
public void testMultiset() {
final List<String> abacee = Arrays.asList( "a", "b", "a", "c", "e", "e" );
final List<String> adaa = Arrays.asList( "a", "d", "a", "a" );
final List<String> addc = Arrays.asList( "a", "d", "c", "d", "c" );
final List<String> adcdc = Arrays.asList( "a", "d", "c", "d", "c" );
final List<String> z = Collections.emptyList();
assertThat( Functions.multisetExceptAll( abacee, addc ), is( Arrays.asList( "b", "a", "e", "e" ) ) );
assertThat( Functions.multisetExceptAll( abacee, adcdc ), is( Arrays.asList( "b", "a", "e", "e" ) ) );
assertThat( Functions.multisetExceptAll( abacee, z ), is( abacee ) );
assertThat( Functions.multisetExceptAll( z, z ), is( z ) );
assertThat( Functions.multisetExceptAll( z, addc ), is( z ) );
assertThat( Functions.multisetExceptAll( z, adcdc ), is( z ) );

assertThat( Functions.multisetExceptDistinct( abacee, addc ), is( Arrays.asList( "b", "e" ) ) );
assertThat( Functions.multisetExceptDistinct( abacee, z ), is( Arrays.asList( "a", "b", "c", "e" ) ) );
assertThat( Functions.multisetExceptDistinct( z, z ), is( z ) );
assertThat( Functions.multisetExceptDistinct( z, addc ), is( z ) );
assertThat( Functions.multisetExceptDistinct( abacee, adcdc ), is( Set.of( "b", "e" ) ) );
assertThat( Functions.multisetExceptDistinct( abacee, z ), is( Set.of( "a", "b", "c", "e" ) ) );
assertThat( Functions.multisetExceptDistinct( z, z ), is( Set.of() ) );
assertThat( Functions.multisetExceptDistinct( z, adcdc ), is( Set.of() ) );

assertThat( Functions.multisetIntersectAll( abacee, addc ), is( Arrays.asList( "a", "c" ) ) );
assertThat( Functions.multisetIntersectAll( abacee, adcdc ), is( Arrays.asList( "a", "c" ) ) );
assertThat( Functions.multisetIntersectAll( abacee, adaa ), is( Arrays.asList( "a", "a" ) ) );
assertThat( Functions.multisetIntersectAll( adaa, abacee ), is( Arrays.asList( "a", "a" ) ) );
assertThat( Functions.multisetIntersectAll( abacee, z ), is( z ) );
assertThat( Functions.multisetIntersectAll( z, z ), is( z ) );
assertThat( Functions.multisetIntersectAll( z, addc ), is( z ) );
assertThat( Functions.multisetIntersectAll( z, adcdc ), is( z ) );

assertThat( Functions.multisetIntersectDistinct( abacee, addc ), is( Arrays.asList( "a", "c" ) ) );
assertThat( Functions.multisetIntersectDistinct( abacee, adaa ), is( Collections.singletonList( "a" ) ) );
assertThat( Functions.multisetIntersectDistinct( adaa, abacee ), is( Collections.singletonList( "a" ) ) );
assertThat( Functions.multisetIntersectDistinct( abacee, z ), is( z ) );
assertThat( Functions.multisetIntersectDistinct( z, z ), is( z ) );
assertThat( Functions.multisetIntersectDistinct( z, addc ), is( z ) );
assertThat( Functions.multisetIntersectDistinct( abacee, adcdc ), is( Set.of( "a", "c" ) ) );
assertThat( Functions.multisetIntersectDistinct( abacee, adaa ), is( Set.of( "a" ) ) );
assertThat( Functions.multisetIntersectDistinct( adaa, abacee ), is( Set.of( "a" ) ) );
assertThat( Functions.multisetIntersectDistinct( abacee, z ), is( Set.of() ) );
assertThat( Functions.multisetIntersectDistinct( z, z ), is( Set.of() ) );
assertThat( Functions.multisetIntersectDistinct( z, adcdc ), is( Set.of() ) );

assertThat( Functions.multisetUnionAll( abacee, addc ), is( Arrays.asList( "a", "b", "a", "c", "e", "e", "a", "d", "c", "d", "c" ) ) );
assertThat( Functions.multisetUnionAll( abacee, adcdc ), is( Arrays.asList( "a", "b", "a", "c", "e", "e", "a", "d", "c", "d", "c" ) ) );
assertThat( Functions.multisetUnionAll( abacee, z ), is( abacee ) );
assertThat( Functions.multisetUnionAll( z, z ), is( z ) );
assertThat( Functions.multisetUnionAll( z, addc ), is( addc ) );
assertThat( Functions.multisetUnionAll( z, adcdc ), is( adcdc ) );

assertThat( Functions.multisetUnionDistinct( abacee, addc ), is( Arrays.asList( "a", "b", "c", "d", "e" ) ) );
assertThat( Functions.multisetUnionDistinct( abacee, z ), is( Arrays.asList( "a", "b", "c", "e" ) ) );
assertThat( Functions.multisetUnionDistinct( z, z ), is( z ) );
assertThat( Functions.multisetUnionDistinct( z, addc ), is( Arrays.asList( "a", "c", "d" ) ) );
assertThat( Functions.multisetUnionDistinct( abacee, adcdc ), is( Set.of( "a", "b", "c", "d", "e" ) ) );
assertThat( Functions.multisetUnionDistinct( abacee, z ), is( Set.of( "a", "b", "c", "e" ) ) );
assertThat( Functions.multisetUnionDistinct( z, z ), is( Set.of() ) );
assertThat( Functions.multisetUnionDistinct( z, adcdc ), is( Set.of( "a", "c", "d" ) ) );
}

}

0 comments on commit 2f9cfd5

Please sign in to comment.