Unverified 提交 0661149e authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1392 from katzyn/misc

Some refactoring and assorted minor optimizations
...@@ -9,7 +9,7 @@ import static org.h2.util.DateTimeUtils.NANOS_PER_MINUTE; ...@@ -9,7 +9,7 @@ import static org.h2.util.DateTimeUtils.NANOS_PER_MINUTE;
import static org.h2.util.DateTimeUtils.NANOS_PER_SECOND; import static org.h2.util.DateTimeUtils.NANOS_PER_SECOND;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.DateTimeUtils; import org.h2.util.IntervalUtils;
/** /**
* INTERVAL representation for result sets. * INTERVAL representation for result sets.
...@@ -471,7 +471,7 @@ public final class Interval { ...@@ -471,7 +471,7 @@ public final class Interval {
public Interval(IntervalQualifier qualifier, boolean negative, long leading, long remaining) { public Interval(IntervalQualifier qualifier, boolean negative, long leading, long remaining) {
this.qualifier = qualifier; this.qualifier = qualifier;
try { try {
this.negative = DateTimeUtils.validateInterval(qualifier, negative, leading, remaining); this.negative = IntervalUtils.validateInterval(qualifier, negative, leading, remaining);
} catch (DbException e) { } catch (DbException e) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
...@@ -523,7 +523,7 @@ public final class Interval { ...@@ -523,7 +523,7 @@ public final class Interval {
* @return years, or 0 * @return years, or 0
*/ */
public long getYears() { public long getYears() {
return DateTimeUtils.yearsFromInterval(qualifier, negative, leading, remaining); return IntervalUtils.yearsFromInterval(qualifier, negative, leading, remaining);
} }
/** /**
...@@ -532,7 +532,7 @@ public final class Interval { ...@@ -532,7 +532,7 @@ public final class Interval {
* @return months, or 0 * @return months, or 0
*/ */
public long getMonths() { public long getMonths() {
return DateTimeUtils.monthsFromInterval(qualifier, negative, leading, remaining); return IntervalUtils.monthsFromInterval(qualifier, negative, leading, remaining);
} }
/** /**
...@@ -541,7 +541,7 @@ public final class Interval { ...@@ -541,7 +541,7 @@ public final class Interval {
* @return days, or 0 * @return days, or 0
*/ */
public long getDays() { public long getDays() {
return DateTimeUtils.daysFromInterval(qualifier, negative, leading, remaining); return IntervalUtils.daysFromInterval(qualifier, negative, leading, remaining);
} }
/** /**
...@@ -550,7 +550,7 @@ public final class Interval { ...@@ -550,7 +550,7 @@ public final class Interval {
* @return hours, or 0 * @return hours, or 0
*/ */
public long getHours() { public long getHours() {
return DateTimeUtils.hoursFromInterval(qualifier, negative, leading, remaining); return IntervalUtils.hoursFromInterval(qualifier, negative, leading, remaining);
} }
/** /**
...@@ -559,7 +559,7 @@ public final class Interval { ...@@ -559,7 +559,7 @@ public final class Interval {
* @return minutes, or 0 * @return minutes, or 0
*/ */
public long getMinutes() { public long getMinutes() {
return DateTimeUtils.minutesFromInterval(qualifier, negative, leading, remaining); return IntervalUtils.minutesFromInterval(qualifier, negative, leading, remaining);
} }
/** /**
...@@ -599,7 +599,7 @@ public final class Interval { ...@@ -599,7 +599,7 @@ public final class Interval {
* @return nanoseconds (including seconds), or 0 * @return nanoseconds (including seconds), or 0
*/ */
public long getSecondsAndNanos() { public long getSecondsAndNanos() {
return DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining); return IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining);
} }
@Override @Override
...@@ -628,7 +628,7 @@ public final class Interval { ...@@ -628,7 +628,7 @@ public final class Interval {
@Override @Override
public String toString() { public String toString() {
return DateTimeUtils.intervalToString(qualifier, negative, leading, remaining); return IntervalUtils.intervalToString(qualifier, negative, leading, remaining);
} }
} }
...@@ -189,7 +189,7 @@ import org.h2.table.TableFilter; ...@@ -189,7 +189,7 @@ import org.h2.table.TableFilter;
import org.h2.table.TableFilter.TableFilterVisitor; import org.h2.table.TableFilter.TableFilterVisitor;
import org.h2.table.TableView; import org.h2.table.TableView;
import org.h2.util.DateTimeFunctions; import org.h2.util.DateTimeFunctions;
import org.h2.util.DateTimeUtils; import org.h2.util.IntervalUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.ParserUtil; import org.h2.util.ParserUtil;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
...@@ -3691,7 +3691,7 @@ public class Parser { ...@@ -3691,7 +3691,7 @@ public class Parser {
qualifier = IntervalQualifier.SECOND; qualifier = IntervalQualifier.SECOND;
} }
try { try {
return ValueExpression.get(DateTimeUtils.parseInterval(qualifier, negative, s)); return ValueExpression.get(IntervalUtils.parseInterval(qualifier, negative, s));
} catch (Exception e) { } catch (Exception e) {
throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "INTERVAL", s); throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2, e, "INTERVAL", s);
} }
......
...@@ -10,6 +10,7 @@ import java.util.HashSet; ...@@ -10,6 +10,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.command.Prepared; import org.h2.command.Prepared;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.Mode.ModeEnum; import org.h2.engine.Mode.ModeEnum;
...@@ -39,40 +40,56 @@ import org.h2.value.ValueNull; ...@@ -39,40 +40,56 @@ import org.h2.value.ValueNull;
*/ */
public abstract class Query extends Prepared { public abstract class Query extends Prepared {
/**
* The column list, including invisible expressions such as order by expressions.
*/
ArrayList<Expression> expressions;
/**
* Array of expressions.
*
* @see #expressions
*/
Expression[] expressionArray;
ArrayList<SelectOrderBy> orderList;
SortOrder sort;
/** /**
* The limit expression as specified in the LIMIT or TOP clause. * The limit expression as specified in the LIMIT or TOP clause.
*/ */
protected Expression limitExpr; Expression limitExpr;
/** /**
* Whether limit expression specifies percentage of rows. * Whether limit expression specifies percentage of rows.
*/ */
protected boolean fetchPercent; boolean fetchPercent;
/** /**
* Whether tied rows should be included in result too. * Whether tied rows should be included in result too.
*/ */
protected boolean withTies; boolean withTies;
/** /**
* The offset expression as specified in the LIMIT .. OFFSET clause. * The offset expression as specified in the LIMIT .. OFFSET clause.
*/ */
protected Expression offsetExpr; Expression offsetExpr;
/** /**
* The sample size expression as specified in the SAMPLE_SIZE clause. * The sample size expression as specified in the SAMPLE_SIZE clause.
*/ */
protected Expression sampleSizeExpr; Expression sampleSizeExpr;
/** /**
* Whether the result must only contain distinct rows. * Whether the result must only contain distinct rows.
*/ */
protected boolean distinct; boolean distinct;
/** /**
* Whether the result needs to support random access. * Whether the result needs to support random access.
*/ */
protected boolean randomAccessResult; boolean randomAccessResult;
private boolean noCache; private boolean noCache;
private int lastLimit; private int lastLimit;
...@@ -144,7 +161,9 @@ public abstract class Query extends Prepared { ...@@ -144,7 +161,9 @@ public abstract class Query extends Prepared {
* *
* @return the list of expressions * @return the list of expressions
*/ */
public abstract ArrayList<Expression> getExpressions(); public ArrayList<Expression> getExpressions() {
return expressions;
}
/** /**
* Calculate the cost to execute this query. * Calculate the cost to execute this query.
...@@ -178,14 +197,18 @@ public abstract class Query extends Prepared { ...@@ -178,14 +197,18 @@ public abstract class Query extends Prepared {
* *
* @param order the order by list * @param order the order by list
*/ */
public abstract void setOrder(ArrayList<SelectOrderBy> order); public void setOrder(ArrayList<SelectOrderBy> order) {
orderList = order;
}
/** /**
* Whether the query has an order. * Whether the query has an order.
* *
* @return true if it has * @return true if it has
*/ */
public abstract boolean hasOrder(); public boolean hasOrder() {
return orderList != null || sort != null;
}
/** /**
* Set the 'for update' flag. * Set the 'for update' flag.
...@@ -600,6 +623,11 @@ public abstract class Query extends Prepared { ...@@ -600,6 +623,11 @@ public abstract class Query extends Prepared {
return new SortOrder(session.getDatabase(), index, sortType, orderList); return new SortOrder(session.getDatabase(), index, sortType, orderList);
} }
@Override
public int getType() {
return CommandInterface.SELECT;
}
public void setOffset(Expression offset) { public void setOffset(Expression offset) {
this.offsetExpr = offset; this.offsetExpr = offset;
} }
......
...@@ -13,7 +13,6 @@ import java.util.HashSet; ...@@ -13,7 +13,6 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.api.Trigger; import org.h2.api.Trigger;
import org.h2.command.CommandInterface;
import org.h2.command.Parser; import org.h2.command.Parser;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.engine.Database; import org.h2.engine.Database;
...@@ -76,12 +75,6 @@ public class Select extends Query { ...@@ -76,12 +75,6 @@ public class Select extends Query {
private final ArrayList<TableFilter> filters = Utils.newSmallArrayList(); private final ArrayList<TableFilter> filters = Utils.newSmallArrayList();
private final ArrayList<TableFilter> topFilters = Utils.newSmallArrayList(); private final ArrayList<TableFilter> topFilters = Utils.newSmallArrayList();
/**
* The column list, including synthetic columns (columns not shown in the
* result).
*/
ArrayList<Expression> expressions;
private Expression[] expressionArray;
private Expression having; private Expression having;
private Expression condition; private Expression condition;
...@@ -98,7 +91,6 @@ public class Select extends Query { ...@@ -98,7 +91,6 @@ public class Select extends Query {
private int[] distinctIndexes; private int[] distinctIndexes;
private int distinctColumnCount; private int distinctColumnCount;
private ArrayList<SelectOrderBy> orderList;
private ArrayList<Expression> group; private ArrayList<Expression> group;
/** /**
...@@ -136,7 +128,6 @@ public class Select extends Query { ...@@ -136,7 +128,6 @@ public class Select extends Query {
private boolean isQuickAggregateQuery, isDistinctQuery; private boolean isQuickAggregateQuery, isDistinctQuery;
private boolean isPrepared, checkInit; private boolean isPrepared, checkInit;
private boolean sortUsingIndex; private boolean sortUsingIndex;
private SortOrder sort;
/** /**
* The id of the current group. * The id of the current group.
...@@ -245,16 +236,6 @@ public class Select extends Query { ...@@ -245,16 +236,6 @@ public class Select extends Query {
return currentGroupRowId; return currentGroupRowId;
} }
@Override
public void setOrder(ArrayList<SelectOrderBy> order) {
orderList = order;
}
@Override
public boolean hasOrder() {
return orderList != null || sort != null;
}
@Override @Override
public void setDistinct() { public void setDistinct() {
if (distinctExpressions != null) { if (distinctExpressions != null) {
...@@ -1452,11 +1433,6 @@ public class Select extends Query { ...@@ -1452,11 +1433,6 @@ public class Select extends Query {
return topTableFilter; return topTableFilter;
} }
@Override
public ArrayList<Expression> getExpressions() {
return expressions;
}
@Override @Override
public void setForUpdate(boolean b) { public void setForUpdate(boolean b) {
this.isForUpdate = b; this.isForUpdate = b;
...@@ -1617,11 +1593,6 @@ public class Select extends Query { ...@@ -1617,11 +1593,6 @@ public class Select extends Query {
return !isForUpdate; return !isForUpdate;
} }
@Override
public int getType() {
return CommandInterface.SELECT;
}
@Override @Override
public boolean allowGlobalConditions() { public boolean allowGlobalConditions() {
return offsetExpr == null && (limitExpr == null || sort == null); return offsetExpr == null && (limitExpr == null || sort == null);
......
...@@ -9,7 +9,6 @@ import java.util.ArrayList; ...@@ -9,7 +9,6 @@ import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.engine.Mode; import org.h2.engine.Mode;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
...@@ -23,7 +22,6 @@ import org.h2.result.LazyResult; ...@@ -23,7 +22,6 @@ import org.h2.result.LazyResult;
import org.h2.result.LocalResult; import org.h2.result.LocalResult;
import org.h2.result.ResultInterface; import org.h2.result.ResultInterface;
import org.h2.result.ResultTarget; import org.h2.result.ResultTarget;
import org.h2.result.SortOrder;
import org.h2.table.Column; import org.h2.table.Column;
import org.h2.table.ColumnResolver; import org.h2.table.ColumnResolver;
import org.h2.table.Table; import org.h2.table.Table;
...@@ -73,10 +71,6 @@ public class SelectUnion extends Query { ...@@ -73,10 +71,6 @@ public class SelectUnion extends Query {
*/ */
final Query right; final Query right;
private ArrayList<Expression> expressions;
private Expression[] expressionArray;
private ArrayList<SelectOrderBy> orderList;
private SortOrder sort;
private boolean isPrepared, checkInit; private boolean isPrepared, checkInit;
private boolean isForUpdate; private boolean isForUpdate;
...@@ -110,21 +104,6 @@ public class SelectUnion extends Query { ...@@ -110,21 +104,6 @@ public class SelectUnion extends Query {
return right; return right;
} }
@Override
public void setSQL(String sql) {
this.sqlStatement = sql;
}
@Override
public void setOrder(ArrayList<SelectOrderBy> order) {
orderList = order;
}
@Override
public boolean hasOrder() {
return orderList != null || sort != null;
}
@Override @Override
public void setDistinctIfPossible() { public void setDistinctIfPossible() {
setDistinct(); setDistinct();
...@@ -376,11 +355,6 @@ public class SelectUnion extends Query { ...@@ -376,11 +355,6 @@ public class SelectUnion extends Query {
return set; return set;
} }
@Override
public ArrayList<Expression> getExpressions() {
return expressions;
}
@Override @Override
public void setForUpdate(boolean forUpdate) { public void setForUpdate(boolean forUpdate) {
left.setForUpdate(forUpdate); left.setForUpdate(forUpdate);
...@@ -484,11 +458,6 @@ public class SelectUnion extends Query { ...@@ -484,11 +458,6 @@ public class SelectUnion extends Query {
right.fireBeforeSelectTriggers(); right.fireBeforeSelectTriggers();
} }
@Override
public int getType() {
return CommandInterface.SELECT;
}
@Override @Override
public boolean allowGlobalConditions() { public boolean allowGlobalConditions() {
return left.allowGlobalConditions() && right.allowGlobalConditions(); return left.allowGlobalConditions() && right.allowGlobalConditions();
......
...@@ -23,6 +23,7 @@ import org.h2.table.IndexColumn; ...@@ -23,6 +23,7 @@ import org.h2.table.IndexColumn;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.IntervalUtils;
import org.h2.value.CompareMode; import org.h2.value.CompareMode;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueDate; import org.h2.value.ValueDate;
...@@ -261,9 +262,9 @@ class AggregateDataMedian extends AggregateDataCollecting { ...@@ -261,9 +262,9 @@ class AggregateDataMedian extends AggregateDataCollecting {
case Value.INTERVAL_HOUR_TO_MINUTE: case Value.INTERVAL_HOUR_TO_MINUTE:
case Value.INTERVAL_HOUR_TO_SECOND: case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND: case Value.INTERVAL_MINUTE_TO_SECOND:
return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.valueOf(dataType - Value.INTERVAL_YEAR), return IntervalUtils.intervalFromAbsolute(IntervalQualifier.valueOf(dataType - Value.INTERVAL_YEAR),
DateTimeUtils.intervalToAbsolute((ValueInterval) v0) IntervalUtils.intervalToAbsolute((ValueInterval) v0)
.add(DateTimeUtils.intervalToAbsolute((ValueInterval) v1)).shiftRight(1)); .add(IntervalUtils.intervalToAbsolute((ValueInterval) v1)).shiftRight(1));
default: default:
// Just return first // Just return first
return v0.convertTo(dataType); return v0.convertTo(dataType);
......
...@@ -2324,20 +2324,24 @@ public class Function extends Expression implements FunctionCall { ...@@ -2324,20 +2324,24 @@ public class Function extends Expression implements FunctionCall {
d = displaySize; d = displaySize;
break; break;
case TRUNCATE: case TRUNCATE:
t = p0.getType(); switch (p0.getType()) {
s = p0.getScale(); case Value.STRING:
p = p0.getPrecision(); case Value.DATE:
d = p0.getDisplaySize(); case Value.TIMESTAMP:
if (t == Value.NULL) { t = Value.TIMESTAMP;
t = Value.INT; p = d = ValueTimestamp.DEFAULT_PRECISION;
p = ValueInt.PRECISION; s = 0;
d = ValueInt.DISPLAY_SIZE; break;
case Value.TIMESTAMP_TZ:
t = Value.TIMESTAMP;
p = d = ValueTimestampTimeZone.DEFAULT_PRECISION;
s = 0; s = 0;
} else if (t == Value.TIMESTAMP) { break;
t = Value.DATE; default:
p = ValueDate.PRECISION; t = Value.DOUBLE;
s = 0; s = 0;
d = ValueDate.PRECISION; p = ValueDouble.PRECISION;
d = ValueDouble.DISPLAY_SIZE;
} }
break; break;
case ABS: case ABS:
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
*/ */
package org.h2.expression; package org.h2.expression;
import static org.h2.util.IntervalUtils.NANOS_PER_DAY_BI;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
...@@ -16,6 +18,7 @@ import org.h2.table.ColumnResolver; ...@@ -16,6 +18,7 @@ import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.DateTimeFunctions; import org.h2.util.DateTimeFunctions;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.IntervalUtils;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueDate; import org.h2.value.ValueDate;
...@@ -131,9 +134,9 @@ public class IntervalOperation extends Expression { ...@@ -131,9 +134,9 @@ public class IntervalOperation extends Expression {
switch (opType) { switch (opType) {
case INTERVAL_PLUS_INTERVAL: case INTERVAL_PLUS_INTERVAL:
case INTERVAL_MINUS_INTERVAL: { case INTERVAL_MINUS_INTERVAL: {
BigInteger a1 = DateTimeUtils.intervalToAbsolute((ValueInterval) l); BigInteger a1 = IntervalUtils.intervalToAbsolute((ValueInterval) l);
BigInteger a2 = DateTimeUtils.intervalToAbsolute((ValueInterval) r); BigInteger a2 = IntervalUtils.intervalToAbsolute((ValueInterval) r);
return DateTimeUtils.intervalFromAbsolute( return IntervalUtils.intervalFromAbsolute(
IntervalQualifier.valueOf(Value.getHigherOrder(lType, rType) - Value.INTERVAL_YEAR), IntervalQualifier.valueOf(Value.getHigherOrder(lType, rType) - Value.INTERVAL_YEAR),
opType == IntervalOpType.INTERVAL_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2)); opType == IntervalOpType.INTERVAL_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2));
} }
...@@ -142,9 +145,9 @@ public class IntervalOperation extends Expression { ...@@ -142,9 +145,9 @@ public class IntervalOperation extends Expression {
return getDateTimeWithInterval(l, r, lType, rType); return getDateTimeWithInterval(l, r, lType, rType);
case INTERVAL_MULTIPLY_NUMERIC: case INTERVAL_MULTIPLY_NUMERIC:
case INTERVAL_DIVIDE_NUMERIC: { case INTERVAL_DIVIDE_NUMERIC: {
BigDecimal a1 = new BigDecimal(DateTimeUtils.intervalToAbsolute((ValueInterval) l)); BigDecimal a1 = new BigDecimal(IntervalUtils.intervalToAbsolute((ValueInterval) l));
BigDecimal a2 = r.getBigDecimal(); BigDecimal a2 = r.getBigDecimal();
return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.valueOf(lType - Value.INTERVAL_YEAR), return IntervalUtils.intervalFromAbsolute(IntervalQualifier.valueOf(lType - Value.INTERVAL_YEAR),
(opType == IntervalOpType.INTERVAL_MULTIPLY_NUMERIC ? a1.multiply(a2) : a1.divide(a2)) (opType == IntervalOpType.INTERVAL_MULTIPLY_NUMERIC ? a1.multiply(a2) : a1.divide(a2))
.toBigInteger()); .toBigInteger());
} }
...@@ -168,9 +171,9 @@ public class IntervalOperation extends Expression { ...@@ -168,9 +171,9 @@ public class IntervalOperation extends Expression {
} else { } else {
long[] a = DateTimeUtils.dateAndTimeFromValue(l); long[] a = DateTimeUtils.dateAndTimeFromValue(l);
long[] b = DateTimeUtils.dateAndTimeFromValue(r); long[] b = DateTimeUtils.dateAndTimeFromValue(r);
BigInteger bi1 = BigInteger.valueOf(a[0]).multiply(BigInteger.valueOf(DateTimeUtils.NANOS_PER_DAY)) BigInteger bi1 = BigInteger.valueOf(a[0]).multiply(NANOS_PER_DAY_BI)
.add(BigInteger.valueOf(a[1])); .add(BigInteger.valueOf(a[1]));
BigInteger bi2 = BigInteger.valueOf(b[0]).multiply(BigInteger.valueOf(DateTimeUtils.NANOS_PER_DAY)) BigInteger bi2 = BigInteger.valueOf(b[0]).multiply(NANOS_PER_DAY_BI)
.add(BigInteger.valueOf(b[1])); .add(BigInteger.valueOf(b[1]));
BigInteger diff = bi1.subtract(bi2); BigInteger diff = bi1.subtract(bi2);
if (lType == Value.TIMESTAMP_TZ || rType == Value.TIMESTAMP_TZ) { if (lType == Value.TIMESTAMP_TZ || rType == Value.TIMESTAMP_TZ) {
...@@ -179,7 +182,7 @@ public class IntervalOperation extends Expression { ...@@ -179,7 +182,7 @@ public class IntervalOperation extends Expression {
diff = diff.add(BigInteger.valueOf((((ValueTimestampTimeZone) r).getTimeZoneOffsetMins() diff = diff.add(BigInteger.valueOf((((ValueTimestampTimeZone) r).getTimeZoneOffsetMins()
- ((ValueTimestampTimeZone) l).getTimeZoneOffsetMins()) * 60_000_000_000L)); - ((ValueTimestampTimeZone) l).getTimeZoneOffsetMins()) * 60_000_000_000L));
} }
return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.DAY_TO_SECOND, diff); return IntervalUtils.intervalFromAbsolute(IntervalQualifier.DAY_TO_SECOND, diff);
} }
} }
throw DbException.throwInternalError("type=" + opType); throw DbException.throwInternalError("type=" + opType);
...@@ -192,9 +195,9 @@ public class IntervalOperation extends Expression { ...@@ -192,9 +195,9 @@ public class IntervalOperation extends Expression {
throw DbException.throwInternalError("type=" + rType); throw DbException.throwInternalError("type=" + rType);
} }
BigInteger a1 = BigInteger.valueOf(((ValueTime) l).getNanos()); BigInteger a1 = BigInteger.valueOf(((ValueTime) l).getNanos());
BigInteger a2 = DateTimeUtils.intervalToAbsolute((ValueInterval) r); BigInteger a2 = IntervalUtils.intervalToAbsolute((ValueInterval) r);
BigInteger n = opType == IntervalOpType.DATETIME_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2); BigInteger n = opType == IntervalOpType.DATETIME_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2);
if (n.signum() < 0 || n.compareTo(BigInteger.valueOf(DateTimeUtils.NANOS_PER_DAY)) >= 0) { if (n.signum() < 0 || n.compareTo(NANOS_PER_DAY_BI) >= 0) {
throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, n.toString()); throw DbException.get(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1, n.toString());
} }
return ValueTime.fromNanos(n.longValue()); return ValueTime.fromNanos(n.longValue());
...@@ -203,24 +206,24 @@ public class IntervalOperation extends Expression { ...@@ -203,24 +206,24 @@ public class IntervalOperation extends Expression {
case Value.TIMESTAMP: case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ: case Value.TIMESTAMP_TZ:
if (DataType.isYearMonthIntervalType(rType)) { if (DataType.isYearMonthIntervalType(rType)) {
long m = DateTimeUtils.intervalToAbsolute((ValueInterval) r).longValue(); long m = IntervalUtils.intervalToAbsolute((ValueInterval) r).longValue();
if (opType == IntervalOpType.DATETIME_MINUS_INTERVAL) { if (opType == IntervalOpType.DATETIME_MINUS_INTERVAL) {
m = -m; m = -m;
} }
return DateTimeFunctions.dateadd("MONTH", m, l); return DateTimeFunctions.dateadd("MONTH", m, l);
} else { } else {
BigInteger a2 = DateTimeUtils.intervalToAbsolute((ValueInterval) r); BigInteger a2 = IntervalUtils.intervalToAbsolute((ValueInterval) r);
if (lType == Value.DATE) { if (lType == Value.DATE) {
BigInteger a1 = BigInteger BigInteger a1 = BigInteger
.valueOf(DateTimeUtils.absoluteDayFromDateValue(((ValueDate) l).getDateValue())); .valueOf(DateTimeUtils.absoluteDayFromDateValue(((ValueDate) l).getDateValue()));
a2 = a2.divide(BigInteger.valueOf(DateTimeUtils.NANOS_PER_DAY)); a2 = a2.divide(NANOS_PER_DAY_BI);
BigInteger n = opType == IntervalOpType.DATETIME_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2); BigInteger n = opType == IntervalOpType.DATETIME_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2);
return ValueDate.fromDateValue(DateTimeUtils.dateValueFromAbsoluteDay(n.longValue())); return ValueDate.fromDateValue(DateTimeUtils.dateValueFromAbsoluteDay(n.longValue()));
} else { } else {
long[] a = DateTimeUtils.dateAndTimeFromValue(l); long[] a = DateTimeUtils.dateAndTimeFromValue(l);
long absoluteDay = DateTimeUtils.absoluteDayFromDateValue(a[0]); long absoluteDay = DateTimeUtils.absoluteDayFromDateValue(a[0]);
long timeNanos = a[1]; long timeNanos = a[1];
BigInteger[] dr = a2.divideAndRemainder(BigInteger.valueOf(DateTimeUtils.NANOS_PER_DAY)); BigInteger[] dr = a2.divideAndRemainder(NANOS_PER_DAY_BI);
if (opType == IntervalOpType.DATETIME_PLUS_INTERVAL) { if (opType == IntervalOpType.DATETIME_PLUS_INTERVAL) {
absoluteDay += dr[0].longValue(); absoluteDay += dr[0].longValue();
timeNanos += dr[1].longValue(); timeNanos += dr[1].longValue();
......
...@@ -85,8 +85,8 @@ public class Parameter extends Expression implements ParameterInterface { ...@@ -85,8 +85,8 @@ public class Parameter extends Expression implements ParameterInterface {
@Override @Override
public Expression optimize(Session session) { public Expression optimize(Session session) {
if (session.getDatabase().getMode().treatEmptyStringsAsNull) { if (session.getDatabase().getMode().treatEmptyStringsAsNull) {
if (value instanceof ValueString) { if (value instanceof ValueString && value.getString().isEmpty()) {
value = ValueString.get(value.getString(), true); value = ValueNull.INSTANCE;
} }
} }
return this; return this;
......
...@@ -589,13 +589,13 @@ public class ValueDataType implements DataType { ...@@ -589,13 +589,13 @@ public class ValueDataType implements DataType {
ordinal < 5 ? 0 : readVarLong(buff)); ordinal < 5 ? 0 : readVarLong(buff));
} }
case FLOAT_0_1: case FLOAT_0_1:
return ValueFloat.get(0); return ValueFloat.ZERO;
case FLOAT_0_1 + 1: case FLOAT_0_1 + 1:
return ValueFloat.get(1); return ValueFloat.ONE;
case DOUBLE_0_1: case DOUBLE_0_1:
return ValueDouble.get(0); return ValueDouble.ZERO;
case DOUBLE_0_1 + 1: case DOUBLE_0_1 + 1:
return ValueDouble.get(1); return ValueDouble.ONE;
case Value.DOUBLE: case Value.DOUBLE:
return ValueDouble.get(Double.longBitsToDouble( return ValueDouble.get(Double.longBitsToDouble(
Long.reverse(readVarLong(buff)))); Long.reverse(readVarLong(buff))));
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
*/ */
package org.h2.schema; package org.h2.schema;
import java.math.BigInteger;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.engine.DbObject; import org.h2.engine.DbObject;
import org.h2.engine.Session; import org.h2.engine.Session;
...@@ -139,17 +138,14 @@ public class Sequence extends SchemaObjectBase { ...@@ -139,17 +138,14 @@ public class Sequence extends SchemaObjectBase {
* @param maxValue the prospective max value * @param maxValue the prospective max value
* @param increment the prospective increment * @param increment the prospective increment
*/ */
private static boolean isValid(long value, long minValue, long maxValue, private static boolean isValid(long value, long minValue, long maxValue, long increment) {
long increment) {
return minValue <= value && return minValue <= value &&
maxValue >= value && maxValue >= value &&
maxValue > minValue && maxValue > minValue &&
increment != 0 && increment != 0 &&
// Math.abs(increment) < maxValue - minValue // Math.abs(increment) <= maxValue - minValue
// use BigInteger to avoid overflows when maxValue and minValue // Can use Long.compareUnsigned() on Java 8
// are really big Math.abs(increment) + Long.MIN_VALUE <= maxValue - minValue + Long.MIN_VALUE;
BigInteger.valueOf(increment).abs().compareTo(
BigInteger.valueOf(maxValue).subtract(BigInteger.valueOf(minValue))) < 0;
} }
private static long getDefaultMinValue(Long startValue, long increment) { private static long getDefaultMinValue(Long startValue, long increment) {
......
...@@ -809,13 +809,13 @@ public class Data { ...@@ -809,13 +809,13 @@ public class Data {
case Value.STRING_FIXED: case Value.STRING_FIXED:
return ValueStringFixed.get(readString()); return ValueStringFixed.get(readString());
case FLOAT_0_1: case FLOAT_0_1:
return ValueFloat.get(0); return ValueFloat.ZERO;
case FLOAT_0_1 + 1: case FLOAT_0_1 + 1:
return ValueFloat.get(1); return ValueFloat.ONE;
case DOUBLE_0_1: case DOUBLE_0_1:
return ValueDouble.get(0); return ValueDouble.ZERO;
case DOUBLE_0_1 + 1: case DOUBLE_0_1 + 1:
return ValueDouble.get(1); return ValueDouble.ONE;
case Value.DOUBLE: case Value.DOUBLE:
return ValueDouble.get(Double.longBitsToDouble( return ValueDouble.get(Double.longBitsToDouble(
Long.reverse(readVarLong()))); Long.reverse(readVarLong())));
......
...@@ -355,7 +355,7 @@ public final class DateTimeFunctions { ...@@ -355,7 +355,7 @@ public final class DateTimeFunctions {
bd = bd.negate(); bd = bd.negate();
} }
} else { } else {
bd = new BigDecimal(DateTimeUtils.intervalToAbsolute(interval)) bd = new BigDecimal(IntervalUtils.intervalToAbsolute(interval))
.divide(BigDecimal.valueOf(NANOS_PER_SECOND)); .divide(BigDecimal.valueOf(NANOS_PER_SECOND));
} }
return ValueDecimal.get(bd); return ValueDecimal.get(bd);
...@@ -634,33 +634,33 @@ public final class DateTimeFunctions { ...@@ -634,33 +634,33 @@ public final class DateTimeFunctions {
long v; long v;
switch (field) { switch (field) {
case YEAR: case YEAR:
v = DateTimeUtils.yearsFromInterval(qualifier, negative, leading, remaining); v = IntervalUtils.yearsFromInterval(qualifier, negative, leading, remaining);
break; break;
case MONTH: case MONTH:
v = DateTimeUtils.monthsFromInterval(qualifier, negative, leading, remaining); v = IntervalUtils.monthsFromInterval(qualifier, negative, leading, remaining);
break; break;
case DAY_OF_MONTH: case DAY_OF_MONTH:
case DAY_OF_WEEK: case DAY_OF_WEEK:
case DAY_OF_YEAR: case DAY_OF_YEAR:
v = DateTimeUtils.daysFromInterval(qualifier, negative, leading, remaining); v = IntervalUtils.daysFromInterval(qualifier, negative, leading, remaining);
break; break;
case HOUR: case HOUR:
v = DateTimeUtils.hoursFromInterval(qualifier, negative, leading, remaining); v = IntervalUtils.hoursFromInterval(qualifier, negative, leading, remaining);
break; break;
case MINUTE: case MINUTE:
v = DateTimeUtils.minutesFromInterval(qualifier, negative, leading, remaining); v = IntervalUtils.minutesFromInterval(qualifier, negative, leading, remaining);
break; break;
case SECOND: case SECOND:
v = DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining) / NANOS_PER_SECOND; v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / NANOS_PER_SECOND;
break; break;
case MILLISECOND: case MILLISECOND:
v = DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000_000 % 1_000; v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000_000 % 1_000;
break; break;
case MICROSECOND: case MICROSECOND:
v = DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000 % 1_000_000; v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000 % 1_000_000;
break; break;
case NANOSECOND: case NANOSECOND:
v = DateTimeUtils.nanosFromInterval(qualifier, negative, leading, remaining) % NANOS_PER_SECOND; v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) % NANOS_PER_SECOND;
break; break;
default: default:
throw DbException.getUnsupportedException("getDatePart(" + date + ", " + field + ')'); throw DbException.getUnsupportedException("getDatePart(" + date + ", " + field + ')');
......
差异被折叠。
...@@ -397,7 +397,7 @@ public class LocalDateTimeUtils { ...@@ -397,7 +397,7 @@ public class LocalDateTimeUtils {
if (DataType.isYearMonthIntervalType(value.getType())) { if (DataType.isYearMonthIntervalType(value.getType())) {
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, (Throwable) null, value.getString()); throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, (Throwable) null, value.getString());
} }
BigInteger[] dr = DateTimeUtils.intervalToAbsolute((ValueInterval) value) BigInteger[] dr = IntervalUtils.intervalToAbsolute((ValueInterval) value)
.divideAndRemainder(BigInteger.valueOf(1_000_000_000)); .divideAndRemainder(BigInteger.valueOf(1_000_000_000));
try { try {
return DURATION_OF_SECONDS.invoke(null, dr[0].longValue(), dr[1].longValue()); return DURATION_OF_SECONDS.invoke(null, dr[0].longValue(), dr[1].longValue());
......
...@@ -28,6 +28,7 @@ import org.h2.store.DataHandler; ...@@ -28,6 +28,7 @@ import org.h2.store.DataHandler;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.Bits; import org.h2.util.Bits;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.IntervalUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
...@@ -963,7 +964,7 @@ public abstract class Value { ...@@ -963,7 +964,7 @@ public abstract class Value {
private ValueDouble convertToDouble() { private ValueDouble convertToDouble() {
switch (getType()) { switch (getType()) {
case BOOLEAN: case BOOLEAN:
return ValueDouble.get(getBoolean() ? 1 : 0); return getBoolean() ? ValueDouble.ONE : ValueDouble.ZERO;
case BYTE: case BYTE:
case SHORT: case SHORT:
case INT: case INT:
...@@ -984,7 +985,7 @@ public abstract class Value { ...@@ -984,7 +985,7 @@ public abstract class Value {
private ValueFloat convertToFloat() { private ValueFloat convertToFloat() {
switch (getType()) { switch (getType()) {
case BOOLEAN: case BOOLEAN:
return ValueFloat.get(getBoolean() ? 1 : 0); return getBoolean() ? ValueFloat.ONE : ValueFloat.ZERO;
case BYTE: case BYTE:
case SHORT: case SHORT:
case INT: case INT:
...@@ -1234,7 +1235,7 @@ public abstract class Value { ...@@ -1234,7 +1235,7 @@ public abstract class Value {
case Value.STRING_FIXED: { case Value.STRING_FIXED: {
String s = getString(); String s = getString();
try { try {
return (ValueInterval) DateTimeUtils return (ValueInterval) IntervalUtils
.parseFormattedInterval(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR), s) .parseFormattedInterval(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR), s)
.convertTo(targetType); .convertTo(targetType);
} catch (Exception e) { } catch (Exception e) {
...@@ -1244,8 +1245,8 @@ public abstract class Value { ...@@ -1244,8 +1245,8 @@ public abstract class Value {
case Value.INTERVAL_YEAR: case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH: case Value.INTERVAL_MONTH:
case Value.INTERVAL_YEAR_TO_MONTH: case Value.INTERVAL_YEAR_TO_MONTH:
return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR), return IntervalUtils.intervalFromAbsolute(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR),
DateTimeUtils.intervalToAbsolute((ValueInterval) this)); IntervalUtils.intervalToAbsolute((ValueInterval) this));
} }
throw getDataConversionError(targetType); throw getDataConversionError(targetType);
} }
...@@ -1257,7 +1258,7 @@ public abstract class Value { ...@@ -1257,7 +1258,7 @@ public abstract class Value {
case Value.STRING_FIXED: { case Value.STRING_FIXED: {
String s = getString(); String s = getString();
try { try {
return (ValueInterval) DateTimeUtils return (ValueInterval) IntervalUtils
.parseFormattedInterval(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR), s) .parseFormattedInterval(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR), s)
.convertTo(targetType); .convertTo(targetType);
} catch (Exception e) { } catch (Exception e) {
...@@ -1274,8 +1275,8 @@ public abstract class Value { ...@@ -1274,8 +1275,8 @@ public abstract class Value {
case Value.INTERVAL_HOUR_TO_MINUTE: case Value.INTERVAL_HOUR_TO_MINUTE:
case Value.INTERVAL_HOUR_TO_SECOND: case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND: case Value.INTERVAL_MINUTE_TO_SECOND:
return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR), return IntervalUtils.intervalFromAbsolute(IntervalQualifier.valueOf(targetType - Value.INTERVAL_YEAR),
DateTimeUtils.intervalToAbsolute((ValueInterval) this)); IntervalUtils.intervalToAbsolute((ValueInterval) this));
} }
throw getDataConversionError(targetType); throw getDataConversionError(targetType);
} }
......
...@@ -28,12 +28,20 @@ public class ValueDouble extends Value { ...@@ -28,12 +28,20 @@ public class ValueDouble extends Value {
public static final int DISPLAY_SIZE = 24; public static final int DISPLAY_SIZE = 24;
/** /**
* Double.doubleToLongBits(0.0) * Double.doubleToLongBits(0d)
*/ */
public static final long ZERO_BITS = Double.doubleToLongBits(0.0); public static final long ZERO_BITS = 0L;
/**
* The value 0.
*/
public static final ValueDouble ZERO = new ValueDouble(0d);
/**
* The value 1.
*/
public static final ValueDouble ONE = new ValueDouble(1d);
private static final ValueDouble ZERO = new ValueDouble(0.0);
private static final ValueDouble ONE = new ValueDouble(1.0);
private static final ValueDouble NAN = new ValueDouble(Double.NaN); private static final ValueDouble NAN = new ValueDouble(Double.NaN);
private final double value; private final double value;
...@@ -45,24 +53,24 @@ public class ValueDouble extends Value { ...@@ -45,24 +53,24 @@ public class ValueDouble extends Value {
@Override @Override
public Value add(Value v) { public Value add(Value v) {
ValueDouble v2 = (ValueDouble) v; ValueDouble v2 = (ValueDouble) v;
return ValueDouble.get(value + v2.value); return get(value + v2.value);
} }
@Override @Override
public Value subtract(Value v) { public Value subtract(Value v) {
ValueDouble v2 = (ValueDouble) v; ValueDouble v2 = (ValueDouble) v;
return ValueDouble.get(value - v2.value); return get(value - v2.value);
} }
@Override @Override
public Value negate() { public Value negate() {
return ValueDouble.get(-value); return get(-value);
} }
@Override @Override
public Value multiply(Value v) { public Value multiply(Value v) {
ValueDouble v2 = (ValueDouble) v; ValueDouble v2 = (ValueDouble) v;
return ValueDouble.get(value * v2.value); return get(value * v2.value);
} }
@Override @Override
...@@ -71,7 +79,7 @@ public class ValueDouble extends Value { ...@@ -71,7 +79,7 @@ public class ValueDouble extends Value {
if (v2.value == 0.0) { if (v2.value == 0.0) {
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
} }
return ValueDouble.get(value / v2.value); return get(value / v2.value);
} }
@Override @Override
...@@ -80,7 +88,7 @@ public class ValueDouble extends Value { ...@@ -80,7 +88,7 @@ public class ValueDouble extends Value {
if (other.value == 0) { if (other.value == 0) {
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
} }
return ValueDouble.get(value % other.value); return get(value % other.value);
} }
@Override @Override
......
...@@ -16,11 +16,6 @@ import org.h2.message.DbException; ...@@ -16,11 +16,6 @@ import org.h2.message.DbException;
*/ */
public class ValueFloat extends Value { public class ValueFloat extends Value {
/**
* Float.floatToIntBits(0.0F).
*/
public static final int ZERO_BITS = Float.floatToIntBits(0.0F);
/** /**
* The precision in digits. * The precision in digits.
*/ */
...@@ -32,8 +27,21 @@ public class ValueFloat extends Value { ...@@ -32,8 +27,21 @@ public class ValueFloat extends Value {
*/ */
static final int DISPLAY_SIZE = 15; static final int DISPLAY_SIZE = 15;
private static final ValueFloat ZERO = new ValueFloat(0.0F); /**
private static final ValueFloat ONE = new ValueFloat(1.0F); * Float.floatToIntBits(0f).
*/
public static final int ZERO_BITS = 0;
/**
* The value 0.
*/
public static final ValueFloat ZERO = new ValueFloat(0f);
/**
* The value 1.
*/
public static final ValueFloat ONE = new ValueFloat(1f);
private static final ValueFloat NAN = new ValueFloat(Float.NaN); private static final ValueFloat NAN = new ValueFloat(Float.NaN);
private final float value; private final float value;
...@@ -45,24 +53,24 @@ public class ValueFloat extends Value { ...@@ -45,24 +53,24 @@ public class ValueFloat extends Value {
@Override @Override
public Value add(Value v) { public Value add(Value v) {
ValueFloat v2 = (ValueFloat) v; ValueFloat v2 = (ValueFloat) v;
return ValueFloat.get(value + v2.value); return get(value + v2.value);
} }
@Override @Override
public Value subtract(Value v) { public Value subtract(Value v) {
ValueFloat v2 = (ValueFloat) v; ValueFloat v2 = (ValueFloat) v;
return ValueFloat.get(value - v2.value); return get(value - v2.value);
} }
@Override @Override
public Value negate() { public Value negate() {
return ValueFloat.get(-value); return get(-value);
} }
@Override @Override
public Value multiply(Value v) { public Value multiply(Value v) {
ValueFloat v2 = (ValueFloat) v; ValueFloat v2 = (ValueFloat) v;
return ValueFloat.get(value * v2.value); return get(value * v2.value);
} }
@Override @Override
...@@ -71,7 +79,7 @@ public class ValueFloat extends Value { ...@@ -71,7 +79,7 @@ public class ValueFloat extends Value {
if (v2.value == 0.0) { if (v2.value == 0.0) {
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
} }
return ValueFloat.get(value / v2.value); return get(value / v2.value);
} }
@Override @Override
...@@ -80,7 +88,7 @@ public class ValueFloat extends Value { ...@@ -80,7 +88,7 @@ public class ValueFloat extends Value {
if (other.value == 0) { if (other.value == 0) {
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
} }
return ValueFloat.get(value % other.value); return get(value % other.value);
} }
@Override @Override
......
...@@ -12,6 +12,7 @@ import org.h2.api.Interval; ...@@ -12,6 +12,7 @@ import org.h2.api.Interval;
import org.h2.api.IntervalQualifier; import org.h2.api.IntervalQualifier;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.IntervalUtils;
/** /**
* Implementation of the INTERVAL data type. * Implementation of the INTERVAL data type.
...@@ -58,7 +59,7 @@ public class ValueInterval extends Value { ...@@ -58,7 +59,7 @@ public class ValueInterval extends Value {
* @return interval value * @return interval value
*/ */
public static ValueInterval from(IntervalQualifier qualifier, boolean negative, long leading, long remaining) { public static ValueInterval from(IntervalQualifier qualifier, boolean negative, long leading, long remaining) {
negative = DateTimeUtils.validateInterval(qualifier, negative, leading, remaining); negative = IntervalUtils.validateInterval(qualifier, negative, leading, remaining);
return (ValueInterval) Value return (ValueInterval) Value
.cache(new ValueInterval(qualifier.ordinal() + INTERVAL_YEAR, negative, leading, remaining)); .cache(new ValueInterval(qualifier.ordinal() + INTERVAL_YEAR, negative, leading, remaining));
} }
...@@ -203,7 +204,7 @@ public class ValueInterval extends Value { ...@@ -203,7 +204,7 @@ public class ValueInterval extends Value {
@Override @Override
public String getString() { public String getString() {
return DateTimeUtils.intervalToString(getQualifier(), negative, leading, remaining); return IntervalUtils.intervalToString(getQualifier(), negative, leading, remaining);
} }
@Override @Override
...@@ -298,14 +299,14 @@ public class ValueInterval extends Value { ...@@ -298,14 +299,14 @@ public class ValueInterval extends Value {
@Override @Override
public Value add(Value v) { public Value add(Value v) {
return DateTimeUtils.intervalFromAbsolute(getQualifier(), return IntervalUtils.intervalFromAbsolute(getQualifier(),
DateTimeUtils.intervalToAbsolute(this).add(DateTimeUtils.intervalToAbsolute((ValueInterval) v))); IntervalUtils.intervalToAbsolute(this).add(IntervalUtils.intervalToAbsolute((ValueInterval) v)));
} }
@Override @Override
public Value subtract(Value v) { public Value subtract(Value v) {
return DateTimeUtils.intervalFromAbsolute(getQualifier(), return IntervalUtils.intervalFromAbsolute(getQualifier(),
DateTimeUtils.intervalToAbsolute(this).subtract(DateTimeUtils.intervalToAbsolute((ValueInterval) v))); IntervalUtils.intervalToAbsolute(this).subtract(IntervalUtils.intervalToAbsolute((ValueInterval) v)));
} }
@Override @Override
......
...@@ -125,7 +125,7 @@ public class TestScript extends TestDb { ...@@ -125,7 +125,7 @@ public class TestScript extends TestDb {
testScript("datatypes/" + s + ".sql"); testScript("datatypes/" + s + ".sql");
} }
for (String s : new String[] { "alterTableAdd", "alterTableDropColumn", "alterTableRename", for (String s : new String[] { "alterTableAdd", "alterTableDropColumn", "alterTableRename",
"createAlias", "createSynonym", "createTable", "createTrigger", "createView", "createAlias", "createSequence", "createSynonym", "createTable", "createTrigger", "createView",
"dropDomain", "dropIndex", "dropSchema", "truncateTable" }) { "dropDomain", "dropIndex", "dropSchema", "truncateTable" }) {
testScript("ddl/" + s + ".sql"); testScript("ddl/" + s + ".sql");
} }
......
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
CREATE SEQUENCE SEQ START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 1;
> ok
DROP SEQUENCE SEQ;
> ok
CREATE SEQUENCE SEQ START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 0;
> exception SEQUENCE_ATTRIBUTES_INVALID
CREATE SEQUENCE SEQ START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 0;
> exception SEQUENCE_ATTRIBUTES_INVALID
CREATE SEQUENCE SEQ START WITH 0 INCREMENT BY 0 MINVALUE 0 MAXVALUE 1;
> exception SEQUENCE_ATTRIBUTES_INVALID
CREATE SEQUENCE SEQ START WITH 1 INCREMENT BY 1 MINVALUE 2 MAXVALUE 10;
> exception SEQUENCE_ATTRIBUTES_INVALID
CREATE SEQUENCE SEQ START WITH 20 INCREMENT BY 1 MINVALUE 1 MAXVALUE 10;
> exception SEQUENCE_ATTRIBUTES_INVALID
CREATE SEQUENCE SEQ START WITH 0 INCREMENT BY 9223372036854775807 MINVALUE -9223372036854775808 MAXVALUE 9223372036854775807;
> ok
DROP SEQUENCE SEQ;
> ok
CREATE SEQUENCE SEQ START WITH 0 INCREMENT BY -9223372036854775808 MINVALUE -9223372036854775808 MAXVALUE 9223372036854775807;
> ok
DROP SEQUENCE SEQ;
> ok
CREATE SEQUENCE SEQ START WITH 0 INCREMENT BY -9223372036854775808 MINVALUE -1 MAXVALUE 9223372036854775807;
> ok
DROP SEQUENCE SEQ;
> ok
CREATE SEQUENCE SEQ START WITH 0 INCREMENT BY -9223372036854775808 MINVALUE 0 MAXVALUE 9223372036854775807;
> exception SEQUENCE_ATTRIBUTES_INVALID
...@@ -15,11 +15,12 @@ import java.util.TimeZone; ...@@ -15,11 +15,12 @@ import java.util.TimeZone;
import org.h2.api.IntervalQualifier; import org.h2.api.IntervalQualifier;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.util.IntervalUtils;
import org.h2.value.ValueInterval; import org.h2.value.ValueInterval;
import org.h2.value.ValueTimestamp; import org.h2.value.ValueTimestamp;
/** /**
* Unit tests for the DateTimeUtils class * Unit tests for the DateTimeUtils and IntervalUtils classes.
*/ */
public class TestDateTimeUtils extends TestBase { public class TestDateTimeUtils extends TestBase {
...@@ -257,7 +258,7 @@ public class TestDateTimeUtils extends TestBase { ...@@ -257,7 +258,7 @@ public class TestDateTimeUtils extends TestBase {
private void testParseIntervalImpl(IntervalQualifier qualifier, boolean negative, long leading, long remaining, private void testParseIntervalImpl(IntervalQualifier qualifier, boolean negative, long leading, long remaining,
String s, String full) { String s, String full) {
ValueInterval expected = ValueInterval.from(qualifier, negative, leading, remaining); ValueInterval expected = ValueInterval.from(qualifier, negative, leading, remaining);
assertEquals(expected, DateTimeUtils.parseInterval(qualifier, negative, s)); assertEquals(expected, IntervalUtils.parseInterval(qualifier, negative, s));
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
b.append("INTERVAL ").append('\''); b.append("INTERVAL ").append('\'');
if (negative) { if (negative) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论