Skip to content

Commit

Permalink
fixed all sql integrationtests, rewrote view and datapoints logic, me…
Browse files Browse the repository at this point in the history
…rged statistics
  • Loading branch information
datomo committed Jul 10, 2023
1 parent eeb4004 commit 7472b88
Show file tree
Hide file tree
Showing 30 changed files with 336 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@

import com.google.common.collect.ImmutableList;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import org.polypheny.db.algebra.AlgCollationTraitDef;
import org.polypheny.db.algebra.AlgInput;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.core.relational.RelScan;
import org.polypheny.db.catalog.entity.CatalogEntity;
import org.polypheny.db.catalog.entity.logical.LogicalMaterializedView;
import org.polypheny.db.catalog.entity.logical.LogicalTable;
import org.polypheny.db.catalog.logistic.EntityType;
import org.polypheny.db.plan.AlgOptCluster;
import org.polypheny.db.plan.AlgOptEntity;
import org.polypheny.db.plan.AlgTraitSet;
import org.polypheny.db.plan.Convention;
import org.polypheny.db.schema.trait.ModelTrait;
import org.polypheny.db.view.ViewManager;


/**
Expand Down Expand Up @@ -120,5 +125,17 @@ public static LogicalRelScan create( AlgOptCluster cluster, final CatalogEntity
return new LogicalRelScan( cluster, traitSet, entity );
}


@Override
public AlgNode unfoldView( @Nullable AlgNode parent, int index, AlgOptCluster cluster ) {
if ( false ) {
LogicalTable catalogTable = entity.unwrap( LogicalTable.class );
if ( catalogTable.entityType == EntityType.MATERIALIZED_VIEW && ((LogicalMaterializedView) catalogTable).isOrdered() ) {
return ViewManager.orderMaterialized( this );
}
}
return super.unfoldView( parent, index, cluster );
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public boolean containsView() {

@Override
public AlgNode unfoldView( @Nullable AlgNode parent, int index, AlgOptCluster cluster ) {
AlgNode unfolded = unfoldView( cluster );
AlgNode unfolded = unfoldView( cluster ).unfoldView( this, 0, cluster );
if ( parent != null ) {
parent.replaceInput( index, unfolded );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,13 @@ private Calendar timestampValue( RexLiteral timeLiteral ) {
switch ( timeLiteral.getPolyType() ) {
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
final TimeZone tz = TimeZone.getTimeZone( this.timeZone );
return Util.calendar( Functions.timestampWithLocalTimeZoneToTimestamp( timeLiteral.value.asTimeStamp().sinceEpoch, tz ) );
return Util.calendar( Functions.timestampWithLocalTimeZoneToTimestamp( timeLiteral.value.asTimeStamp().milliSinceEpoch, tz ) );
case TIMESTAMP:
return Util.calendar( timeLiteral.value.asTimeStamp().sinceEpoch );
return Util.calendar( timeLiteral.value.asTimeStamp().milliSinceEpoch );
case DATE:
// Cast date to timestamp with local time zone
//final DateString d = timeLiteral.getValueAs( DateString.class );
return Util.calendar( timeLiteral.value.asDate().sinceEpoch );
return Util.calendar( timeLiteral.value.asDate().milliSinceEpoch );
default:
throw Util.unexpected( timeLiteral.getPolyType() );
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/org/polypheny/db/functions/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class Functions {

private static final DecimalFormat DOUBLE_FORMAT = NumberUtil.decimalFormat( "0.0E0" );

private static final TimeZone LOCAL_TZ = TimeZone.getDefault();
public static final TimeZone LOCAL_TZ = TimeZone.getDefault();

private static final Function1<List<?>, Enumerable<?>> LIST_AS_ENUMERABLE = Linq4j::asEnumerable;

Expand Down Expand Up @@ -991,17 +991,17 @@ public static PolyBoolean lt( PolyNumber b0, PolyNumber b1 ) {


public static PolyBoolean lt( PolyTemporal b0, PolyTemporal b1 ) {
return lt( PolyLong.of( b0.getSinceEpoch() ), PolyLong.of( b1.getSinceEpoch() ) );
return lt( PolyLong.of( b0.getMilliSinceEpoch() ), PolyLong.of( b1.getMilliSinceEpoch() ) );
}


public static PolyBoolean lt( PolyTemporal b0, PolyNumber b1 ) {
return lt( PolyLong.of( b0.getSinceEpoch() ), b1 );
return lt( PolyLong.of( b0.getMilliSinceEpoch() ), b1 );
}


public static PolyBoolean lt( PolyNumber b0, PolyTemporal b1 ) {
return lt( b0, PolyLong.of( b1.getSinceEpoch() ) );
return lt( b0, PolyLong.of( b1.getMilliSinceEpoch() ) );
}
// <=

Expand Down Expand Up @@ -1166,7 +1166,7 @@ public static PolyBoolean ge( PolyNumber b0, PolyNumber b1 ) {


public static PolyBoolean ge( PolyTemporal b0, PolyTemporal b1 ) {
return ge( PolyLong.of( b0.getSinceEpoch() ), PolyLong.of( b1.getSinceEpoch() ) );
return ge( PolyLong.of( b0.getMilliSinceEpoch() ), PolyLong.of( b1.getMilliSinceEpoch() ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TemporalFunctions {

@SuppressWarnings("unused")
public static PolyString unixDateToString( PolyDate date ) {
return PolyString.of( DateTimeUtils.unixDateToString( date.sinceEpoch.intValue() ) );
return PolyString.of( DateTimeUtils.unixDateToString( date.milliSinceEpoch.intValue() ) );
}


Expand All @@ -44,7 +44,7 @@ public static PolyString unixTimeToString( PolyTime time ) {

@SuppressWarnings("unused")
public static PolyString unixTimestampToString( PolyTimeStamp timeStamp ) {
return PolyString.of( DateTimeUtils.unixTimestampToString( timeStamp.sinceEpoch ) );
return PolyString.of( DateTimeUtils.unixTimestampToString( timeStamp.milliSinceEpoch ) );
}


Expand All @@ -62,31 +62,31 @@ public static PolyString intervalDayTimeToString( PolyInterval interval, TimeUni

@SuppressWarnings("unused")
public static PolyLong unixDateExtract( TimeUnitRange unitRange, PolyTemporal date ) {
return PolyLong.of( DateTimeUtils.unixDateExtract( unitRange, date.getSinceEpoch() ) );
return PolyLong.of( DateTimeUtils.unixDateExtract( unitRange, date.getMilliSinceEpoch() ) );
}


@SuppressWarnings("unused")
public static PolyLong unixDateFloor( TimeUnitRange unitRange, PolyDate date ) {
return PolyLong.of( DateTimeUtils.unixDateFloor( unitRange, date.sinceEpoch ) );
return PolyLong.of( DateTimeUtils.unixDateFloor( unitRange, date.milliSinceEpoch ) );
}


@SuppressWarnings("unused")
public static PolyLong unixDateCeil( TimeUnitRange unitRange, PolyDate date ) {
return PolyLong.of( DateTimeUtils.unixDateCeil( unitRange, date.sinceEpoch ) );
return PolyLong.of( DateTimeUtils.unixDateCeil( unitRange, date.milliSinceEpoch ) );
}


@SuppressWarnings("unused")
public static PolyTimeStamp unixTimestampFloor( TimeUnitRange unitRange, PolyTimeStamp timeStamp ) {
return PolyTimeStamp.of( DateTimeUtils.unixTimestampFloor( unitRange, timeStamp.sinceEpoch ) );
return PolyTimeStamp.of( DateTimeUtils.unixTimestampFloor( unitRange, timeStamp.milliSinceEpoch ) );
}


@SuppressWarnings("unused")
public static PolyTimeStamp unixTimestampCeil( TimeUnitRange unitRange, PolyTimeStamp timeStamp ) {
return PolyTimeStamp.of( DateTimeUtils.unixTimestampFloor( unitRange, timeStamp.sinceEpoch ) );
return PolyTimeStamp.of( DateTimeUtils.unixTimestampFloor( unitRange, timeStamp.milliSinceEpoch ) );
}


Expand All @@ -95,9 +95,9 @@ public static PolyTimeStamp unixTimestampCeil( TimeUnitRange unitRange, PolyTime
*/
@SuppressWarnings("unused")
public static PolyTimeStamp addMonths( PolyTimeStamp timestamp, PolyNumber m ) {
final long millis = DateTimeUtils.floorMod( timestamp.sinceEpoch, DateTimeUtils.MILLIS_PER_DAY );
final PolyDate x = addMonths( PolyDate.of( timestamp.sinceEpoch - millis / DateTimeUtils.MILLIS_PER_DAY ), m );
return PolyTimeStamp.of( x.sinceEpoch * DateTimeUtils.MILLIS_PER_DAY + millis );
final long millis = DateTimeUtils.floorMod( timestamp.milliSinceEpoch, DateTimeUtils.MILLIS_PER_DAY );
final PolyDate x = addMonths( PolyDate.of( timestamp.milliSinceEpoch - millis / DateTimeUtils.MILLIS_PER_DAY ), m );
return PolyTimeStamp.of( x.milliSinceEpoch * DateTimeUtils.MILLIS_PER_DAY + millis );
}


Expand All @@ -106,9 +106,9 @@ public static PolyTimeStamp addMonths( PolyTimeStamp timestamp, PolyNumber m ) {
*/
@SuppressWarnings("unused")
public static PolyDate addMonths( PolyDate date, PolyNumber m ) {
int y0 = (int) DateTimeUtils.unixDateExtract( TimeUnitRange.YEAR, date.sinceEpoch / DateTimeUtils.MILLIS_PER_DAY );
int m0 = (int) DateTimeUtils.unixDateExtract( TimeUnitRange.MONTH, date.sinceEpoch / DateTimeUtils.MILLIS_PER_DAY );
int d0 = (int) DateTimeUtils.unixDateExtract( TimeUnitRange.DAY, date.sinceEpoch / DateTimeUtils.MILLIS_PER_DAY );
int y0 = (int) DateTimeUtils.unixDateExtract( TimeUnitRange.YEAR, date.milliSinceEpoch / DateTimeUtils.MILLIS_PER_DAY );
int m0 = (int) DateTimeUtils.unixDateExtract( TimeUnitRange.MONTH, date.milliSinceEpoch / DateTimeUtils.MILLIS_PER_DAY );
int d0 = (int) DateTimeUtils.unixDateExtract( TimeUnitRange.DAY, date.milliSinceEpoch / DateTimeUtils.MILLIS_PER_DAY );
int y = m.intValue() / 12;
y0 += y;
m0 += m.intValue() - y * 12;
Expand Down Expand Up @@ -152,19 +152,19 @@ private static int lastDay( int y, int m ) {
*/
@SuppressWarnings("unused")
public static PolyNumber subtractMonths( PolyDate date0, PolyDate date1 ) {
if ( date0.sinceEpoch < date1.sinceEpoch ) {
if ( date0.milliSinceEpoch < date1.milliSinceEpoch ) {
return subtractMonths( date1, date0 ).negate();
}
// Start with an estimate.
// Since no month has more than 31 days, the estimate is <= the true value.
long m = (date0.sinceEpoch - date1.sinceEpoch) / 31;
long m = (date0.milliSinceEpoch - date1.milliSinceEpoch) / 31;
for ( ; ; ) {
long date2 = addMonths( date1, PolyLong.of( m ) ).sinceEpoch;
if ( date2 >= date0.sinceEpoch ) {
long date2 = addMonths( date1, PolyLong.of( m ) ).milliSinceEpoch;
if ( date2 >= date0.milliSinceEpoch ) {
return PolyLong.of( m );
}
long date3 = addMonths( date1, PolyLong.of( m + 1 ) ).sinceEpoch;
if ( date3 > date0.sinceEpoch ) {
long date3 = addMonths( date1, PolyLong.of( m + 1 ) ).milliSinceEpoch;
if ( date3 > date0.milliSinceEpoch ) {
return PolyLong.of( m );
}
++m;
Expand All @@ -174,12 +174,12 @@ public static PolyNumber subtractMonths( PolyDate date0, PolyDate date1 ) {

@SuppressWarnings("unused")
public static PolyNumber subtractMonths( PolyTimeStamp t0, PolyTimeStamp t1 ) {
final long millis0 = floorMod( PolyLong.of( t0.sinceEpoch ), PolyInteger.of( DateTimeUtils.MILLIS_PER_DAY ) ).longValue();
final int d0 = floorDiv( PolyLong.of( t0.sinceEpoch - millis0 ), PolyInteger.of( DateTimeUtils.MILLIS_PER_DAY ) ).intValue();
final long millis1 = floorMod( PolyLong.of( t1.sinceEpoch ), PolyLong.of( DateTimeUtils.MILLIS_PER_DAY ) ).longValue();
final int d1 = floorDiv( PolyLong.of( t1.sinceEpoch - millis1 ), PolyInteger.of( DateTimeUtils.MILLIS_PER_DAY ) ).intValue();
final long millis0 = floorMod( PolyLong.of( t0.milliSinceEpoch ), PolyInteger.of( DateTimeUtils.MILLIS_PER_DAY ) ).longValue();
final int d0 = floorDiv( PolyLong.of( t0.milliSinceEpoch - millis0 ), PolyInteger.of( DateTimeUtils.MILLIS_PER_DAY ) ).intValue();
final long millis1 = floorMod( PolyLong.of( t1.milliSinceEpoch ), PolyLong.of( DateTimeUtils.MILLIS_PER_DAY ) ).longValue();
final int d1 = floorDiv( PolyLong.of( t1.milliSinceEpoch - millis1 ), PolyInteger.of( DateTimeUtils.MILLIS_PER_DAY ) ).intValue();
PolyNumber x = subtractMonths( PolyDate.of( d0 ), PolyDate.of( d1 ) );
final long d2 = addMonths( PolyDate.of( d1 ), x ).sinceEpoch;
final long d2 = addMonths( PolyDate.of( d1 ), x ).milliSinceEpoch;
if ( d2 == d0 && millis0 < millis1 ) {
x = x.subtract( PolyInteger.of( 1 ) );
}
Expand Down
Loading

0 comments on commit 7472b88

Please sign in to comment.