Skip to content

Commit

Permalink
various adjustments, to fix inconsistencies and potential edge-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Apr 2, 2024
1 parent 414757d commit 0fb952a
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,6 @@ public Expression implement( RexToLixTranslator translator, RexCall call, List<E
map.put( OperatorRegistry.get( OperatorName.LOCALTIME ), systemFunctionImplementor );
map.put( OperatorRegistry.get( OperatorName.LOCALTIMESTAMP ), systemFunctionImplementor );

defineMethod( OperatorRegistry.get( OperatorName.UNWRAP_INTERVAL ), BuiltInMethod.UNWRAP_INTERVAL.method, NullPolicy.NONE );

aggMap.put( OperatorRegistry.getAgg( OperatorName.COUNT ), constructorSupplier( CountImplementor.class ) );
aggMap.put( OperatorRegistry.getAgg( OperatorName.REGR_COUNT ), constructorSupplier( CountImplementor.class ) );
aggMap.put( OperatorRegistry.getAgg( OperatorName.SUM0 ), constructorSupplier( SumImplementor.class ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1303,8 +1303,6 @@ public enum OperatorName {
*/
PATTERN_EXCLUDE( Operator.class ),

UNWRAP_INTERVAL( LangFunctionOperator.class ),

//-------------------------------------------------------------
// SET OPERATORS
//-------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.polypheny.db.type.entity.PolyInterval;
import org.polypheny.db.type.entity.PolyLong;
import org.polypheny.db.type.entity.PolyString;
import org.polypheny.db.type.entity.PolyValue;
import org.polypheny.db.type.entity.category.PolyNumber;
import org.polypheny.db.type.entity.document.PolyDocument;
import org.polypheny.db.util.Pair;

Expand Down Expand Up @@ -83,8 +80,4 @@ public static PolyDocument mergeDocuments( PolyString[] keys, PolyValue... value
}


public static PolyNumber unwrapInterval( PolyInterval interval ) {
return PolyLong.of( interval.getMergedMillis() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
import org.polypheny.db.nodes.IntervalQualifier;
import org.polypheny.db.type.PolySerializable;
import org.polypheny.db.type.PolyType;
Expand All @@ -48,8 +49,8 @@ public class PolyInterval extends PolyValue {


/**
* Creates a PolyInterval, which includes a millis and a month value, to allow for combinations liki 7-1 Month to Day, which is 7 months and 7 days.
* And required for example by the SQL standard.
* Creates a PolyInterval, which includes a millis and a month value, to allow for combinations like 7-1 Month to Day (e.g. used in SQL).
* Which is 7 months and 1 day (represented as PolyInterval with 7 months and 1*24h*60min*60s*1000ms).
*
* @param millis millis since Epoch.
* @param months months since Epoch.
Expand Down Expand Up @@ -77,7 +78,7 @@ private static MonthsMilliseconds normalize( Long value, TimeUnit unit ) {
} else if ( unit == TimeUnit.MILLISECOND ) {
return new MonthsMilliseconds( 0, value );
} else {
throw new NotImplementedException( "since Epoch" );
throw new GenericRuntimeException( "Normalization is not supported" );

}

Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/org/polypheny/db/util/BsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ private static BsonValue handleMultimedia( GridFSBucket bucket, PolyValue o ) {


private static BsonValue handleInterval( PolyValue obj ) {
return new BsonDecimal128( new Decimal128( obj.asInterval().getMergedMillis() ) );
return new BsonDocument() {{
this.put( "m", new BsonInt64( obj.asInterval().getMonths() ) );
this.put( "ms", new BsonInt64( obj.asInterval().getMillis() ) );
}};
}


Expand Down Expand Up @@ -668,7 +671,7 @@ public static PolyValue toPolyValue( BsonValue input ) {
case DECIMAL128:
return PolyBigDecimal.of( input.asDecimal128().getValue().bigDecimalValue() );
}
throw new org.apache.commons.lang3.NotImplementedException( "Not considered: " + input.getBsonType() );
throw new GenericRuntimeException( "Not considered: " + input.getBsonType() );
}

}
2 changes: 0 additions & 2 deletions core/src/main/java/org/polypheny/db/util/BuiltInMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
import org.polypheny.db.functions.Functions;
import org.polypheny.db.functions.Functions.FlatProductInputType;
import org.polypheny.db.functions.MqlFunctions;
import org.polypheny.db.functions.RefactorFunctions;
import org.polypheny.db.functions.TemporalFunctions;
import org.polypheny.db.interpreter.Context;
import org.polypheny.db.interpreter.Row;
Expand Down Expand Up @@ -424,7 +423,6 @@ public enum BuiltInMethod {
AGG_LAMBDA_FACTORY_ACC_SINGLE_GROUP_RESULT_SELECTOR( AggregateLambdaFactory.class, "singleGroupResultSelector", Function1.class ),
RESULTSET_GETBYTES( ResultSet.class, "getBytes", int.class ),
RESULTSET_GETBINARYSTREAM( ResultSet.class, "getBinaryStream", int.class ),
UNWRAP_INTERVAL( RefactorFunctions.class, "unwrapInterval", PolyInterval.class ),
/// MQL BUILT-IN METHODS
MQL_EQ( MqlFunctions.class, "docEq", PolyValue.class, PolyValue.class ),
MQL_GT( MqlFunctions.class, "docGt", PolyValue.class, PolyValue.class ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public SqlNode getCastSpec( AlgDataType type ) {
castSpec = "_BLOB";
break;
case INTERVAL:
castSpec = "INTERVAL";
castSpec = "_INTERVAL";
break;
default:
return super.getCastSpec( type );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.polypheny.db.type.PolyType;
import org.polypheny.db.type.entity.PolyBinary;
import org.polypheny.db.type.entity.PolyBoolean;
import org.polypheny.db.type.entity.PolyInterval;
import org.polypheny.db.type.entity.PolyList;
import org.polypheny.db.type.entity.PolyLong;
import org.polypheny.db.type.entity.PolyNull;
Expand Down Expand Up @@ -196,6 +197,7 @@ private static PolyValue convert( BsonValue o, MongoTupleType type ) {
throw new NotImplementedException();
}
}
case INTERVAL -> new PolyInterval( o.asDocument().get( "ms" ).asNumber().longValue(), o.asDocument().get( "m" ).asNumber().longValue() );
case BINARY -> PolyBinary.of( o.asBinary().getData() );
case TIMESTAMP -> PolyTimestamp.of( o.asNumber().longValue() );
case TIME -> PolyTime.of( o.asNumber().longValue() );
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private static QueryContext removeLimit( QueryContext queryContext ) {
}

// ends with "LIMIT <number>" or "LIMIT <number>;" with optional whitespace, matches <number>
Pattern pattern = Pattern.compile( "LIMIT\\s+(\\d+)\\s*(?:;\\s*\\z|$)", Pattern.CASE_INSENSITIVE );
Pattern pattern = Pattern.compile( "LIMIT\\s+(\\d+)(?:,(\\d+))?\\s*((?:;\\s*\\z|$)|OFFSET\\s*\\d+;$)", Pattern.CASE_INSENSITIVE );
String limitClause = null;
Matcher matcher = pattern.matcher( lowercase );
if ( matcher.find() && matcher.groupCount() > 0 ) {
Expand Down Expand Up @@ -2518,11 +2518,6 @@ public void unparse( SqlWriter writer, SqlCall call, int leftPrec, int rightPrec
OperatorName.CROSS_MODEL_ITEM,
new LangFunctionOperator( OperatorName.CROSS_MODEL_ITEM.name(), Kind.CROSS_MODEL_ITEM ) );

/*
* Operator for unwrapping an interval value to handle it as number.
*/
register( OperatorName.UNWRAP_INTERVAL, new LangFunctionOperator( OperatorName.UNWRAP_INTERVAL.name(), Kind.OTHER_FUNCTION ) );

/*
* Operator which transforms a value to JSON.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.polypheny.db.sql.language;


import static org.polypheny.db.sql.language.SqlIntervalQualifier.intervalString;

import java.util.Objects;
import lombok.Getter;
import org.apache.calcite.linq4j.tree.Expression;
Expand Down Expand Up @@ -100,7 +98,7 @@ public static class IntervalValue extends PolyInterval {
super( interval.millis, interval.months );
this.intervalQualifier = intervalQualifier;
this.sign = interval.millis < 0 ? -1 : 1;
this.intervalStr = intervalString( interval, intervalQualifier );
this.intervalStr = SqlIntervalQualifier.intervalString( interval, intervalQualifier );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1402,12 +1402,6 @@ private RexNode toInterval( RexBuilder rexBuilder, RexNode interval2Add, SqlInte
}


private RexNode interval2Num( RexBuilder rexBuilder, RexLiteral rexLiteral ) {
return rexBuilder.makeCall(
AlgDataTypeFactory.DEFAULT.createPolyType( PolyType.BIGINT ),
OperatorRegistry.get( OperatorName.UNWRAP_INTERVAL ),
rexLiteral );
}

}

Expand Down

0 comments on commit 0fb952a

Please sign in to comment.