Skip to content

Commit 9c5c424

Browse files
authored
Correctly update snapshot on rollback & improve TEXT handling (#522)
1 parent 415fa1a commit 9c5c424

File tree

34 files changed

+346
-671
lines changed

34 files changed

+346
-671
lines changed

core/src/main/java/org/polypheny/db/algebra/AlgFieldCollation.java

+34-64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 The Polypheny Project
2+
* Copyright 2019-2024 The Polypheny Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636

3737
import java.io.Serializable;
3838
import java.util.Objects;
39+
import lombok.Getter;
3940
import org.polypheny.db.algebra.constant.Monotonicity;
4041

4142

@@ -105,72 +106,52 @@ public enum Direction {
105106
* Converts the direction to a {@link Monotonicity}.
106107
*/
107108
public Monotonicity monotonicity() {
108-
switch ( this ) {
109-
case ASCENDING:
110-
return Monotonicity.INCREASING;
111-
case STRICTLY_ASCENDING:
112-
return Monotonicity.STRICTLY_INCREASING;
113-
case DESCENDING:
114-
return Monotonicity.DECREASING;
115-
case STRICTLY_DESCENDING:
116-
return Monotonicity.STRICTLY_DECREASING;
117-
case CLUSTERED:
118-
return Monotonicity.MONOTONIC;
119-
default:
120-
throw new AssertionError( "unknown: " + this );
121-
}
109+
//
110+
return switch ( this ) {
111+
case ASCENDING -> Monotonicity.INCREASING;
112+
case STRICTLY_ASCENDING -> Monotonicity.STRICTLY_INCREASING;
113+
case DESCENDING -> Monotonicity.DECREASING;
114+
case STRICTLY_DESCENDING -> Monotonicity.STRICTLY_DECREASING;
115+
case CLUSTERED -> Monotonicity.MONOTONIC;
116+
};
122117
}
123118

124119

125120
/**
126121
* Converts a {@link Monotonicity} to a direction.
127122
*/
128123
public static Direction of( Monotonicity monotonicity ) {
129-
switch ( monotonicity ) {
130-
case INCREASING:
131-
return ASCENDING;
132-
case DECREASING:
133-
return DESCENDING;
134-
case STRICTLY_INCREASING:
135-
return STRICTLY_ASCENDING;
136-
case STRICTLY_DECREASING:
137-
return STRICTLY_DESCENDING;
138-
case MONOTONIC:
139-
return CLUSTERED;
140-
default:
141-
throw new AssertionError( "unknown: " + monotonicity );
142-
}
124+
return switch ( monotonicity ) {
125+
case INCREASING -> ASCENDING;
126+
case DECREASING -> DESCENDING;
127+
case STRICTLY_INCREASING -> STRICTLY_ASCENDING;
128+
case STRICTLY_DECREASING -> STRICTLY_DESCENDING;
129+
case MONOTONIC -> CLUSTERED;
130+
case CONSTANT, NOT_MONOTONIC -> throw new UnsupportedOperationException( "Cannot convert monotonicity " + monotonicity + " into a direction" );
131+
};
143132
}
144133

145134

146135
/**
147136
* Returns the null direction if not specified. Consistent with Oracle, NULLS are sorted as if they were positive infinity.
148137
*/
149138
public NullDirection defaultNullDirection() {
150-
switch ( this ) {
151-
case ASCENDING:
152-
case STRICTLY_ASCENDING:
153-
return NullDirection.LAST;
154-
case DESCENDING:
155-
case STRICTLY_DESCENDING:
156-
return NullDirection.FIRST;
157-
default:
158-
return NullDirection.UNSPECIFIED;
159-
}
139+
return switch ( this ) {
140+
case ASCENDING, STRICTLY_ASCENDING -> NullDirection.LAST;
141+
case DESCENDING, STRICTLY_DESCENDING -> NullDirection.FIRST;
142+
default -> NullDirection.UNSPECIFIED;
143+
};
160144
}
161145

162146

163147
/**
164148
* Returns whether this is {@link #DESCENDING} or {@link #STRICTLY_DESCENDING}.
165149
*/
166150
public boolean isDescending() {
167-
switch ( this ) {
168-
case DESCENDING:
169-
case STRICTLY_DESCENDING:
170-
return true;
171-
default:
172-
return false;
173-
}
151+
return switch ( this ) {
152+
case DESCENDING, STRICTLY_DESCENDING -> true;
153+
default -> false;
154+
};
174155
}
175156
}
176157

@@ -195,11 +176,13 @@ public enum NullDirection {
195176
/**
196177
* 0-based index of field being sorted.
197178
*/
179+
@Getter
198180
private final int fieldIndex;
199181

200182
/**
201183
* Direction of sorting.
202184
*/
185+
@Getter
203186
public final Direction direction;
204187

205188
/**
@@ -269,16 +252,6 @@ public int hashCode() {
269252
}
270253

271254

272-
public int getFieldIndex() {
273-
return fieldIndex;
274-
}
275-
276-
277-
public AlgFieldCollation.Direction getDirection() {
278-
return direction;
279-
}
280-
281-
282255
public String toString() {
283256
if ( direction == Direction.ASCENDING && nullDirection == direction.defaultNullDirection() ) {
284257
return String.valueOf( fieldIndex );
@@ -296,14 +269,11 @@ public String shortString() {
296269
if ( nullDirection == direction.defaultNullDirection() ) {
297270
return direction.shortString;
298271
}
299-
switch ( nullDirection ) {
300-
case FIRST:
301-
return direction.shortString + "-nulls-first";
302-
case LAST:
303-
return direction.shortString + "-nulls-last";
304-
default:
305-
return direction.shortString;
306-
}
272+
return switch ( nullDirection ) {
273+
case FIRST -> direction.shortString + "-nulls-first";
274+
case LAST -> direction.shortString + "-nulls-last";
275+
default -> direction.shortString;
276+
};
307277
}
308278

309279
}

core/src/main/java/org/polypheny/db/algebra/constant/Monotonicity.java

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 The Polypheny Project
2+
* Copyright 2019-2024 The Polypheny Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,22 +72,6 @@ public Monotonicity reverse() {
7272
}
7373

7474

75-
/**
76-
* Whether values of this monotonicity are decreasing. That is, if a value at a given point in a sequence is X, no point later in the sequence will have a value greater than X.
77-
*
78-
* @return whether values are decreasing
79-
*/
80-
public boolean isDecreasing() {
81-
switch ( this ) {
82-
case STRICTLY_DECREASING:
83-
case DECREASING:
84-
return true;
85-
default:
86-
return false;
87-
}
88-
}
89-
90-
9175
/**
9276
* Returns whether values of this monotonicity may ever repeat after moving to another value: true for {@link #NOT_MONOTONIC} and {@link #CONSTANT}, false otherwise.
9377
*
@@ -105,4 +89,3 @@ public boolean mayRepeat() {
10589
}
10690
}
10791
}
108-

core/src/main/java/org/polypheny/db/algebra/metadata/AlgMdColumnUniqueness.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,9 @@ public Boolean areColumnsUnique( Exchange alg, AlgMetadataQuery mq, ImmutableBit
154154

155155

156156
public Boolean areColumnsUnique( Correlate alg, AlgMetadataQuery mq, ImmutableBitSet columns, boolean ignoreNulls ) {
157-
switch ( alg.getJoinType() ) {
158-
case ANTI:
159-
case SEMI:
160-
return mq.areColumnsUnique( alg.getLeft(), columns, ignoreNulls );
161-
case LEFT:
162-
case INNER:
157+
return switch ( alg.getJoinType() ) {
158+
case ANTI, SEMI -> mq.areColumnsUnique( alg.getLeft(), columns, ignoreNulls );
159+
case LEFT, INNER -> {
163160
final Pair<ImmutableBitSet, ImmutableBitSet> leftAndRightColumns = splitLeftAndRightColumns( alg.getLeft().getTupleType().getFieldCount(), columns );
164161
final ImmutableBitSet leftColumns = leftAndRightColumns.left;
165162
final ImmutableBitSet rightColumns = leftAndRightColumns.right;
@@ -170,16 +167,15 @@ public Boolean areColumnsUnique( Correlate alg, AlgMetadataQuery mq, ImmutableBi
170167
Boolean leftUnique = mq.areColumnsUnique( left, leftColumns, ignoreNulls );
171168
Boolean rightUnique = mq.areColumnsUnique( right, rightColumns, ignoreNulls );
172169
if ( leftUnique == null || rightUnique == null ) {
173-
return null;
170+
yield null;
174171
} else {
175-
return leftUnique && rightUnique;
172+
yield leftUnique && rightUnique;
176173
}
177174
} else {
178-
return null;
175+
yield null;
179176
}
180-
default:
181-
throw new IllegalStateException( "Unknown join type " + alg.getJoinType() + " for correlate relation " + alg );
182-
}
177+
}
178+
};
183179
}
184180

185181

core/src/main/java/org/polypheny/db/algebra/rules/JoinToCorrelateRule.java

+4-12
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.polypheny.db.tools.AlgBuilderFactory;
5252
import org.polypheny.db.util.ImmutableBitSet;
5353
import org.polypheny.db.util.ImmutableBitSet.Builder;
54-
import org.polypheny.db.util.Util;
5554

5655

5756
/**
@@ -85,16 +84,10 @@ public JoinToCorrelateRule( AlgBuilderFactory algBuilderFactory ) {
8584
@Override
8685
public boolean matches( AlgOptRuleCall call ) {
8786
LogicalRelJoin join = call.alg( 0 );
88-
switch ( join.getJoinType() ) {
89-
case INNER:
90-
case LEFT:
91-
return true;
92-
case FULL:
93-
case RIGHT:
94-
return false;
95-
default:
96-
throw Util.unexpected( join.getJoinType() );
97-
}
87+
return switch ( join.getJoinType() ) {
88+
case INNER, LEFT -> true;
89+
case FULL, RIGHT -> false;
90+
};
9891
}
9992

10093

@@ -132,4 +125,3 @@ public RexNode visitIndexRef( RexIndexRef input ) {
132125
}
133126

134127
}
135-

core/src/main/java/org/polypheny/db/algebra/rules/PushProjector.java

+11-28
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,12 @@ public PushProjector( Project origProj, RexNode origFilter, AlgNode childRel, Ex
206206
childBitmap = ImmutableBitSet.range( 0, nFields );
207207
rightBitmap = ImmutableBitSet.range( nFields, nChildFields );
208208

209-
switch ( joinRel.getJoinType() ) {
210-
case INNER:
211-
strongBitmap = ImmutableBitSet.of();
212-
break;
213-
case RIGHT: // All the left-input's columns must be strong
214-
strongBitmap = ImmutableBitSet.range( 0, nFields );
215-
break;
216-
case LEFT: // All the right-input's columns must be strong
217-
strongBitmap = ImmutableBitSet.range( nFields, nChildFields );
218-
break;
219-
case FULL:
220-
default:
221-
strongBitmap = ImmutableBitSet.range( 0, nChildFields );
222-
}
209+
strongBitmap = switch ( joinRel.getJoinType() ) {
210+
case INNER -> ImmutableBitSet.of();
211+
case RIGHT -> ImmutableBitSet.range( 0, nFields );// All the left-input's columns must be strong
212+
case LEFT -> ImmutableBitSet.range( nFields, nChildFields );// All the right-input's columns must be strong
213+
case FULL -> ImmutableBitSet.range( 0, nChildFields );
214+
};
223215

224216
} else if ( childRel instanceof Correlate corrAlg ) {
225217
List<AlgDataTypeField> leftFields = corrAlg.getLeft().getTupleType().getFields();
@@ -240,20 +232,11 @@ public PushProjector( Project origProj, RexNode origFilter, AlgNode childRel, Ex
240232
// Required columns need to be included in project
241233
projRefs.or( BitSets.of( corrAlg.getRequiredColumns() ) );
242234

243-
switch ( joinType ) {
244-
case INNER:
245-
strongBitmap = ImmutableBitSet.of();
246-
break;
247-
case ANTI:
248-
case SEMI: // All the left-input's columns must be strong
249-
strongBitmap = ImmutableBitSet.range( 0, nFields );
250-
break;
251-
case LEFT: // All the right-input's columns must be strong
252-
strongBitmap = ImmutableBitSet.range( nFields, nChildFields );
253-
break;
254-
default:
255-
strongBitmap = ImmutableBitSet.range( 0, nChildFields );
256-
}
235+
strongBitmap = switch ( joinType ) {
236+
case INNER -> ImmutableBitSet.of();
237+
case ANTI, SEMI -> ImmutableBitSet.range( 0, nFields ); // All the left-input's columns must be strong
238+
case LEFT -> ImmutableBitSet.range( nFields, nChildFields ); // All the right-input's columns must be strong
239+
};
257240
} else {
258241
nFields = nChildFields;
259242
nFieldsRight = 0;

core/src/main/java/org/polypheny/db/catalog/impl/PolyCatalog.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public class PolyCatalog extends Catalog implements PolySerializable {
107107
private final Collection<ConstraintCondition> commitConstraints = new ConcurrentLinkedDeque<>();
108108
private final Collection<Runnable> commitActions = new ConcurrentLinkedDeque<>();
109109

110+
// indicates if the state has advanced and the snapshot has to be recreated or can be reused // trx without ddl
111+
private long lastCommitSnapshotId = 0;
110112

111113
@Serialize
112114
@JsonProperty
@@ -267,6 +269,8 @@ public synchronized void commit() {
267269
this.dirty.set( false );
268270
this.commitConstraints.clear();
269271
this.commitActions.clear();
272+
273+
this.lastCommitSnapshotId = snapshot.id();
270274
}
271275

272276

@@ -288,19 +292,18 @@ public synchronized void commit() {
288292

289293

290294
public void rollback() {
291-
long id = snapshot.id();
292-
293295
commitActions.clear();
294296
commitConstraints.clear();
295297

296298
restoreLastState();
297299

298300
log.debug( "rollback" );
299301

300-
if ( id != snapshot.id() ) {
302+
if ( lastCommitSnapshotId != snapshot.id() ) {
301303
updateSnapshot();
302-
}
303304

305+
lastCommitSnapshotId = snapshot.id();
306+
}
304307
}
305308

306309

core/src/main/java/org/polypheny/db/catalog/logistic/EntityType.java

-17
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
package org.polypheny.db.catalog.logistic;
1818

1919
import lombok.Getter;
20-
import lombok.RequiredArgsConstructor;
21-
import org.polypheny.db.type.entity.PolyString;
22-
import org.polypheny.db.type.entity.PolyValue;
2320

2421
@Getter
2522
public enum EntityType {
@@ -36,18 +33,4 @@ public enum EntityType {
3633
this.id = id;
3734
}
3835

39-
40-
// Used for creating ResultSets
41-
public PolyValue[] getParameterArray() {
42-
return new PolyValue[]{ PolyString.of( name() ) };
43-
}
44-
45-
46-
// Required for building JDBC result set
47-
@RequiredArgsConstructor
48-
public static class PrimitiveTableType {
49-
50-
public final String tableType;
51-
52-
}
5336
}

0 commit comments

Comments
 (0)