From 4e07255cf295d683868f9abcbee6db2a8af5f5d6 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 17 May 2023 14:19:39 +0200 Subject: [PATCH 01/42] Native array size. --- .../arrays/ArraySizeFunctionIterator.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/org/rumbledb/runtime/functions/arrays/ArraySizeFunctionIterator.java b/src/main/java/org/rumbledb/runtime/functions/arrays/ArraySizeFunctionIterator.java index 8b96cdadf3..eed1500e60 100644 --- a/src/main/java/org/rumbledb/runtime/functions/arrays/ArraySizeFunctionIterator.java +++ b/src/main/java/org/rumbledb/runtime/functions/arrays/ArraySizeFunctionIterator.java @@ -26,6 +26,9 @@ import org.rumbledb.items.ItemFactory; import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.RuntimeIterator; +import org.rumbledb.runtime.flwor.NativeClauseContext; +import org.rumbledb.types.BuiltinTypesCatalogue; +import org.rumbledb.types.SequenceType; import java.util.List; @@ -50,4 +53,16 @@ public Item materializeFirstItemOrNull(DynamicContext context) { return ItemFactory.getInstance().createIntItem(array.getSize()); } + @Override + public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { + NativeClauseContext nativeChildQuery = this.children.get(0).generateNativeQuery(nativeClauseContext); + if (nativeChildQuery != NativeClauseContext.NoNativeQuery) { + return new NativeClauseContext( + nativeClauseContext, + "SIZE (" + nativeChildQuery.getResultingQuery() + ")", + new SequenceType(BuiltinTypesCatalogue.integerItem, SequenceType.Arity.One) + ); + } + return NativeClauseContext.NoNativeQuery; + } } From fc20a8f35ca03aa5844d6eb31d5c50c80314dd7a Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 17 May 2023 14:42:58 +0200 Subject: [PATCH 02/42] Use static types in native SQL generation. --- .../java/org/rumbledb/items/DecimalItem.java | 3 +- .../java/org/rumbledb/items/DoubleItem.java | 3 +- .../java/org/rumbledb/items/FloatItem.java | 3 +- src/main/java/org/rumbledb/items/IntItem.java | 3 +- .../java/org/rumbledb/items/IntegerItem.java | 3 +- .../java/org/rumbledb/items/StringItem.java | 3 +- .../AdditiveOperationIterator.java | 59 ++++++------------ .../MultiplicativeOperationIterator.java | 62 +++++++------------ .../arithmetics/UnaryOperationIterator.java | 8 +-- .../AtMostOneItemIfRuntimeIterator.java | 8 +-- .../runtime/flwor/NativeClauseContext.java | 16 +---- .../clauses/OrderByClauseSparkIterator.java | 6 +- .../arrays/ArraySizeFunctionIterator.java | 5 +- .../numerics/FloorFunctionIterator.java | 4 +- .../numerics/RoundFunctionIterator.java | 4 +- .../runtime/logics/AndOperationIterator.java | 6 +- .../runtime/logics/NotOperationIterator.java | 4 +- .../runtime/logics/OrOperationIterator.java | 6 +- .../runtime/misc/ComparisonIterator.java | 27 ++++---- .../navigation/ArrayLookupIterator.java | 9 +-- .../navigation/ArrayUnboxingIterator.java | 9 +-- .../navigation/ObjectLookupIterator.java | 12 +--- .../primary/ContextExpressionIterator.java | 7 +-- .../primary/DecimalRuntimeIterator.java | 4 +- .../primary/DoubleRuntimeIterator.java | 4 +- .../primary/IntegerRuntimeIterator.java | 4 +- .../primary/StringRuntimeIterator.java | 4 +- .../primary/VariableReferenceIterator.java | 8 +-- .../AtMostOneItemTypePromotionIterator.java | 2 +- 29 files changed, 94 insertions(+), 202 deletions(-) diff --git a/src/main/java/org/rumbledb/items/DecimalItem.java b/src/main/java/org/rumbledb/items/DecimalItem.java index bc140b4110..149dddf32e 100644 --- a/src/main/java/org/rumbledb/items/DecimalItem.java +++ b/src/main/java/org/rumbledb/items/DecimalItem.java @@ -30,7 +30,6 @@ import org.rumbledb.types.BuiltinTypesCatalogue; import org.rumbledb.runtime.misc.ComparisonIterator; import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; import java.math.BigDecimal; import java.math.BigInteger; @@ -133,7 +132,7 @@ public ItemType getDynamicType() { @Override public NativeClauseContext generateNativeQuery(NativeClauseContext context) { - return new NativeClauseContext(context, this.value.toString(), SequenceType.DECIMAL); + return new NativeClauseContext(context, "CAST (" + this.value + "BD AS DECIMAL(38, 19))"); } public boolean isNumeric() { diff --git a/src/main/java/org/rumbledb/items/DoubleItem.java b/src/main/java/org/rumbledb/items/DoubleItem.java index fab38f6247..0cf929f554 100644 --- a/src/main/java/org/rumbledb/items/DoubleItem.java +++ b/src/main/java/org/rumbledb/items/DoubleItem.java @@ -33,7 +33,6 @@ import org.rumbledb.types.BuiltinTypesCatalogue; import org.rumbledb.runtime.misc.ComparisonIterator; import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; import java.math.BigDecimal; import java.math.BigInteger; @@ -174,7 +173,7 @@ public ItemType getDynamicType() { @Override public NativeClauseContext generateNativeQuery(NativeClauseContext context) { - return new NativeClauseContext(context, "" + this.value, SequenceType.DOUBLE); + return new NativeClauseContext(context, "CAST (" + this.value + "D AS DOUBLE)"); } @Override diff --git a/src/main/java/org/rumbledb/items/FloatItem.java b/src/main/java/org/rumbledb/items/FloatItem.java index d9dca106c3..a2e04d7fda 100644 --- a/src/main/java/org/rumbledb/items/FloatItem.java +++ b/src/main/java/org/rumbledb/items/FloatItem.java @@ -33,7 +33,6 @@ import org.rumbledb.runtime.flwor.NativeClauseContext; import org.rumbledb.runtime.misc.ComparisonIterator; import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; import java.math.BigDecimal; import java.math.BigInteger; @@ -188,6 +187,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext context) { if (Float.isNaN(this.value)) { return NativeClauseContext.NoNativeQuery; } - return new NativeClauseContext(context, "CAST (" + this.value + "D AS FLOAT)", SequenceType.FLOAT); + return new NativeClauseContext(context, "CAST (" + this.value + "F AS FLOAT)"); } } diff --git a/src/main/java/org/rumbledb/items/IntItem.java b/src/main/java/org/rumbledb/items/IntItem.java index 1998a05216..0a310e516d 100644 --- a/src/main/java/org/rumbledb/items/IntItem.java +++ b/src/main/java/org/rumbledb/items/IntItem.java @@ -31,7 +31,6 @@ import org.rumbledb.types.BuiltinTypesCatalogue; import org.rumbledb.runtime.misc.ComparisonIterator; import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; import java.math.BigDecimal; import java.math.BigInteger; @@ -151,7 +150,7 @@ public ItemType getDynamicType() { @Override public NativeClauseContext generateNativeQuery(NativeClauseContext context) { - return new NativeClauseContext(context, "" + this.value, SequenceType.INT); + return new NativeClauseContext(context, "CAST (" + this.value + " AS INT)"); } public boolean isNumeric() { diff --git a/src/main/java/org/rumbledb/items/IntegerItem.java b/src/main/java/org/rumbledb/items/IntegerItem.java index 0ca1de1822..b1be55209e 100644 --- a/src/main/java/org/rumbledb/items/IntegerItem.java +++ b/src/main/java/org/rumbledb/items/IntegerItem.java @@ -30,7 +30,6 @@ import org.rumbledb.types.BuiltinTypesCatalogue; import org.rumbledb.runtime.misc.ComparisonIterator; import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; import java.math.BigDecimal; import java.math.BigInteger; @@ -140,7 +139,7 @@ public ItemType getDynamicType() { @Override public NativeClauseContext generateNativeQuery(NativeClauseContext context) { - return new NativeClauseContext(context, this.value.toString(), SequenceType.INTEGER); + return new NativeClauseContext(context, "CAST (" + this.value + "BD AS DECIMAL(38,0))"); } public boolean isNumeric() { diff --git a/src/main/java/org/rumbledb/items/StringItem.java b/src/main/java/org/rumbledb/items/StringItem.java index 130a929bc0..ec08ffb5eb 100644 --- a/src/main/java/org/rumbledb/items/StringItem.java +++ b/src/main/java/org/rumbledb/items/StringItem.java @@ -31,7 +31,6 @@ import org.rumbledb.types.BuiltinTypesCatalogue; import org.rumbledb.runtime.misc.ComparisonIterator; import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; import java.math.BigDecimal; import java.math.BigInteger; @@ -147,7 +146,7 @@ public ItemType getDynamicType() { @Override public NativeClauseContext generateNativeQuery(NativeClauseContext context) { - return new NativeClauseContext(context, '"' + this.value + '"', SequenceType.STRING); + return new NativeClauseContext(context, '"' + this.value + '"'); } @Override diff --git a/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java b/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java index 910ce3bc86..4ccb01259d 100644 --- a/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java +++ b/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java @@ -428,88 +428,71 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (leftResult == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!leftResult.getResultingType().getArity().equals(Arity.One)) { + if (!this.leftIterator.getStaticType().getArity().equals(Arity.One)) { return NativeClauseContext.NoNativeQuery; } NativeClauseContext rightResult = this.rightIterator.generateNativeQuery(nativeClauseContext); if (rightResult == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!rightResult.getResultingType().getArity().equals(Arity.One)) { + if (!this.rightIterator.getStaticType().getArity().equals(Arity.One)) { return NativeClauseContext.NoNativeQuery; } ItemType resultType = null; String leftQuery = leftResult.getResultingQuery(); String rightQuery = rightResult.getResultingQuery(); if ( - leftResult.getResultingType().isSubtypeOf(SequenceType.DOUBLE_QM) + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) && - rightResult.getResultingType().getItemType().isNumeric() + this.rightIterator.getStaticType().getItemType().isNumeric() ) { - if (!rightResult.getResultingType().isSubtypeOf(SequenceType.DOUBLE_QM)) { + if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { rightQuery = "(CAST (" + rightQuery + " AS DOUBLE))"; } resultType = BuiltinTypesCatalogue.doubleItem; } else if ( - rightResult.getResultingType().isSubtypeOf(SequenceType.DOUBLE_QM) + this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) && - leftResult.getResultingType().getItemType().isNumeric() + this.leftIterator.getStaticType().getItemType().isNumeric() ) { - if (!leftResult.getResultingType().isSubtypeOf(SequenceType.DOUBLE_QM)) { + if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { leftQuery = "(CAST (" + leftQuery + " AS DOUBLE))"; } resultType = BuiltinTypesCatalogue.doubleItem; } else if ( - leftResult.getResultingType().isSubtypeOf(SequenceType.FLOAT_QM) + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) && - rightResult.getResultingType().getItemType().isNumeric() + this.rightIterator.getStaticType().getItemType().isNumeric() ) { - if (!rightResult.getResultingType().isSubtypeOf(SequenceType.FLOAT_QM)) { + if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { rightQuery = "(CAST (" + rightQuery + " AS FLOAT))"; } resultType = BuiltinTypesCatalogue.floatItem; } else if ( - rightResult.getResultingType().isSubtypeOf(SequenceType.FLOAT_QM) + this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) && - leftResult.getResultingType().getItemType().isNumeric() + this.leftIterator.getStaticType().getItemType().isNumeric() ) { - if (!leftResult.getResultingType().isSubtypeOf(SequenceType.FLOAT_QM)) { + if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { leftQuery = "(CAST (" + leftQuery + " AS FLOAT))"; } resultType = BuiltinTypesCatalogue.floatItem; } else if ( - leftResult.getResultingType().isSubtypeOf(SequenceType.INTEGER_QM) + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) && - rightResult.getResultingType().isSubtypeOf(SequenceType.INTEGER_QM) + this.rightIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) ) { resultType = BuiltinTypesCatalogue.integerItem; } else if ( - leftResult.getResultingType().isSubtypeOf(SequenceType.DECIMAL_QM) + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) && - rightResult.getResultingType().isSubtypeOf(SequenceType.DECIMAL_QM) + this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) ) { resultType = BuiltinTypesCatalogue.decimalItem; } else { return NativeClauseContext.NoNativeQuery; } - SequenceType.Arity resultingArity = - leftResult.getResultingType() - .getArity() - .multiplyWith( - rightResult.getResultingType().getArity() - ); - - if (resultingArity.equals(Arity.OneOrMore) || resultingArity.equals(Arity.ZeroOrMore)) { - throw new UnexpectedTypeException( - " \"+\": operation not possible with parameters of type \"" - + this.left.getDynamicType().toString() - + "\" and \"" - + this.right.getDynamicType().toString() - + "\"", - getMetadata() - ); - } if (this.isMinus) { String resultingQuery = "( " + leftQuery @@ -518,8 +501,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC + " )"; return new NativeClauseContext( nativeClauseContext, - resultingQuery, - new SequenceType(resultType, resultingArity) + resultingQuery ); } else { String resultingQuery = "( " @@ -529,8 +511,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC + " )"; return new NativeClauseContext( nativeClauseContext, - resultingQuery, - new SequenceType(resultType, resultingArity) + resultingQuery ); } } diff --git a/src/main/java/org/rumbledb/runtime/arithmetics/MultiplicativeOperationIterator.java b/src/main/java/org/rumbledb/runtime/arithmetics/MultiplicativeOperationIterator.java index 2d99afdc8d..42ae4d2b58 100644 --- a/src/main/java/org/rumbledb/runtime/arithmetics/MultiplicativeOperationIterator.java +++ b/src/main/java/org/rumbledb/runtime/arithmetics/MultiplicativeOperationIterator.java @@ -551,59 +551,59 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (leftResult == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!leftResult.getResultingType().getArity().equals(Arity.One)) { + if (!this.leftIterator.getStaticType().getArity().equals(Arity.One)) { return NativeClauseContext.NoNativeQuery; } NativeClauseContext rightResult = this.rightIterator.generateNativeQuery(nativeClauseContext); if (rightResult == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!rightResult.getResultingType().getArity().equals(Arity.One)) { + if (!this.rightIterator.getStaticType().getArity().equals(Arity.One)) { return NativeClauseContext.NoNativeQuery; } ItemType resultType = null; String leftQuery = leftResult.getResultingQuery(); String rightQuery = rightResult.getResultingQuery(); if ( - leftResult.getResultingType().isSubtypeOf(SequenceType.DOUBLE_QM) + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) && - rightResult.getResultingType().getItemType().isNumeric() + this.rightIterator.getStaticType().getItemType().isNumeric() ) { - if (!rightResult.getResultingType().isSubtypeOf(SequenceType.DOUBLE_QM)) { + if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { rightQuery = "(CAST (" + rightQuery + " AS DOUBLE))"; } resultType = BuiltinTypesCatalogue.doubleItem; } else if ( - rightResult.getResultingType().isSubtypeOf(SequenceType.DOUBLE_QM) + this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) && - leftResult.getResultingType().getItemType().isNumeric() + this.leftIterator.getStaticType().getItemType().isNumeric() ) { - if (!leftResult.getResultingType().isSubtypeOf(SequenceType.DOUBLE_QM)) { + if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { leftQuery = "(CAST (" + leftQuery + " AS DOUBLE))"; } resultType = BuiltinTypesCatalogue.doubleItem; } else if ( - leftResult.getResultingType().isSubtypeOf(SequenceType.FLOAT_QM) + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) && - rightResult.getResultingType().getItemType().isNumeric() + this.rightIterator.getStaticType().getItemType().isNumeric() ) { - if (!rightResult.getResultingType().isSubtypeOf(SequenceType.FLOAT_QM)) { + if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { rightQuery = "(CAST (" + rightQuery + " AS FLOAT))"; } resultType = BuiltinTypesCatalogue.floatItem; } else if ( - rightResult.getResultingType().isSubtypeOf(SequenceType.FLOAT_QM) + this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) && - leftResult.getResultingType().getItemType().isNumeric() + this.leftIterator.getStaticType().getItemType().isNumeric() ) { - if (!leftResult.getResultingType().isSubtypeOf(SequenceType.FLOAT_QM)) { + if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { leftQuery = "(CAST (" + leftQuery + " AS FLOAT))"; } resultType = BuiltinTypesCatalogue.floatItem; } else if ( - leftResult.getResultingType().isSubtypeOf(SequenceType.INTEGER_QM) + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) && - rightResult.getResultingType().isSubtypeOf(SequenceType.INTEGER_QM) + this.rightIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) ) { if (this.multiplicativeOperator.equals(MultiplicativeExpression.MultiplicativeOperator.DIV)) { resultType = BuiltinTypesCatalogue.decimalItem; @@ -611,9 +611,9 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC resultType = BuiltinTypesCatalogue.integerItem; } } else if ( - leftResult.getResultingType().isSubtypeOf(SequenceType.DECIMAL_QM) + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) && - rightResult.getResultingType().isSubtypeOf(SequenceType.DECIMAL_QM) + this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) ) { resultType = BuiltinTypesCatalogue.decimalItem; } else { @@ -621,23 +621,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC } String resultingQuery = null; - SequenceType.Arity resultingArity = - leftResult.getResultingType() - .getArity() - .multiplyWith( - rightResult.getResultingType().getArity() - ); - - if (resultingArity.equals(Arity.OneOrMore) || resultingArity.equals(Arity.ZeroOrMore)) { - throw new UnexpectedTypeException( - " \"+\": operation not possible with parameters of type \"" - + this.left.getDynamicType().toString() - + "\" and \"" - + this.right.getDynamicType().toString() - + "\"", - getMetadata() - ); - } switch (this.multiplicativeOperator) { case MUL: resultingQuery = "( " @@ -647,8 +630,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC + " )"; return new NativeClauseContext( nativeClauseContext, - resultingQuery, - new SequenceType(resultType, resultingArity) + resultingQuery ); case DIV: resultingQuery = "( " @@ -658,8 +640,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC + " )"; return new NativeClauseContext( nativeClauseContext, - resultingQuery, - new SequenceType(resultType, resultingArity) + resultingQuery ); case MOD: resultingQuery = "( " @@ -669,8 +650,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC + " )"; return new NativeClauseContext( nativeClauseContext, - resultingQuery, - new SequenceType(resultType, resultingArity) + resultingQuery ); default: return NativeClauseContext.NoNativeQuery; diff --git a/src/main/java/org/rumbledb/runtime/arithmetics/UnaryOperationIterator.java b/src/main/java/org/rumbledb/runtime/arithmetics/UnaryOperationIterator.java index dc1c3f5719..b4ba2ed2d6 100644 --- a/src/main/java/org/rumbledb/runtime/arithmetics/UnaryOperationIterator.java +++ b/src/main/java/org/rumbledb/runtime/arithmetics/UnaryOperationIterator.java @@ -29,7 +29,6 @@ import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.RuntimeIterator; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.SequenceType; import org.rumbledb.types.SequenceType.Arity; import java.math.BigDecimal; @@ -102,22 +101,21 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (leftResult == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!leftResult.getResultingType().getArity().equals(Arity.One)) { + if (!this.child.getStaticType().getArity().equals(Arity.One)) { return NativeClauseContext.NoNativeQuery; } String leftQuery = leftResult.getResultingQuery(); - SequenceType resultType = leftResult.getResultingType(); if (this.negated) { String resultingQuery = "( " + " - " + leftQuery + " )"; - return new NativeClauseContext(nativeClauseContext, resultingQuery, resultType); + return new NativeClauseContext(nativeClauseContext, resultingQuery); } else { String resultingQuery = "( " + leftQuery + " )"; - return new NativeClauseContext(nativeClauseContext, resultingQuery, resultType); + return new NativeClauseContext(nativeClauseContext, resultingQuery); } } } diff --git a/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java index ccbb99b22e..1fc7cb348d 100644 --- a/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java @@ -70,13 +70,13 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC ) { return NativeClauseContext.NoNativeQuery; } - if (!conditionResult.getResultingType().equals(SequenceType.BOOLEAN)) { + if (!this.children.get(0).getStaticType().equals(SequenceType.BOOLEAN)) { return NativeClauseContext.NoNativeQuery; } - if (!thenResult.getResultingType().equals(SequenceType.FLOAT)) { + if (!this.children.get(1).getStaticType().equals(SequenceType.FLOAT)) { return NativeClauseContext.NoNativeQuery; } - if (!elseResult.getResultingType().equals(SequenceType.FLOAT)) { + if (!this.children.get(2).getStaticType().equals(SequenceType.FLOAT)) { return NativeClauseContext.NoNativeQuery; } String resultingQuery = "( " @@ -87,6 +87,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC + ", " + elseResult.getResultingQuery() + " ) )"; - return new NativeClauseContext(nativeClauseContext, resultingQuery, SequenceType.FLOAT); + return new NativeClauseContext(nativeClauseContext, resultingQuery); } } diff --git a/src/main/java/org/rumbledb/runtime/flwor/NativeClauseContext.java b/src/main/java/org/rumbledb/runtime/flwor/NativeClauseContext.java index 1fa4e36a76..a0471d4cda 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/NativeClauseContext.java +++ b/src/main/java/org/rumbledb/runtime/flwor/NativeClauseContext.java @@ -4,7 +4,6 @@ import org.apache.spark.sql.types.StructType; import org.rumbledb.context.DynamicContext; import org.rumbledb.expressions.flowr.FLWOR_CLAUSES; -import org.rumbledb.types.SequenceType; import java.util.ArrayList; import java.util.List; @@ -20,7 +19,6 @@ public class NativeClauseContext { private DynamicContext context; private String resultingQuery; private List lateralViewPart; // used in array unboxing to generate the correct lateral view - private SequenceType resultingType; private NativeClauseContext() { } @@ -31,7 +29,6 @@ public NativeClauseContext(FLWOR_CLAUSES clauseType, StructType schema, DynamicC this.context = context; this.resultingQuery = ""; this.lateralViewPart = new ArrayList<>(); - this.resultingType = null; } public NativeClauseContext(NativeClauseContext parent) { @@ -40,16 +37,14 @@ public NativeClauseContext(NativeClauseContext parent) { this.context = parent.context; this.resultingQuery = parent.resultingQuery; this.lateralViewPart = parent.lateralViewPart; - this.resultingType = parent.resultingType; } - public NativeClauseContext(NativeClauseContext parent, String newResultingQuery, SequenceType resultingType) { + public NativeClauseContext(NativeClauseContext parent, String newResultingQuery) { this.clauseType = parent.clauseType; this.schema = parent.schema; this.context = parent.context; this.resultingQuery = newResultingQuery; this.lateralViewPart = parent.lateralViewPart; - this.resultingType = resultingType; } public FLWOR_CLAUSES getClauseType() { @@ -80,14 +75,6 @@ public List getLateralViewPart() { return this.lateralViewPart; } - public SequenceType getResultingType() { - return this.resultingType; - } - - public void setResultingType(SequenceType resultingType) { - this.resultingType = resultingType; - } - public String toString() { StringBuffer sb = new StringBuffer(); sb.append("Native clause context.\n"); @@ -95,7 +82,6 @@ public String toString() { sb.append("Clause type: " + this.clauseType + "\n"); sb.append("Schema: " + this.schema.toString() + "\n"); sb.append("Context: " + this.context.toString() + "\n"); - sb.append("Resulting type: " + this.resultingType + "\n"); sb.append("Lateral views:\n"); for (String s : this.lateralViewPart) { sb.append(s + "\n"); diff --git a/src/main/java/org/rumbledb/runtime/flwor/clauses/OrderByClauseSparkIterator.java b/src/main/java/org/rumbledb/runtime/flwor/clauses/OrderByClauseSparkIterator.java index 1d17221bf0..4aa5c72b2f 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/clauses/OrderByClauseSparkIterator.java +++ b/src/main/java/org/rumbledb/runtime/flwor/clauses/OrderByClauseSparkIterator.java @@ -544,7 +544,7 @@ public static FlworDataFrame tryNativeQuery( return null; } // For now we are conservative and do not support arities other than one. - if (!nativeQuery.getResultingType().getArity().equals(Arity.One)) { + if (!orderIterator.getIterator().getStaticType().getArity().equals(Arity.One)) { return null; } orderSql.append(orderSeparator); @@ -554,8 +554,8 @@ public static FlworDataFrame tryNativeQuery( // because of meaning mismatch between sparksql (where it is supposed to order by the i-th col) // and jsoniq (order by a costant, so no actual ordering is performed) if ( - (nativeQuery.getResultingType().isSubtypeOf(SequenceType.INTEGER_QM) - || nativeQuery.getResultingType().isSubtypeOf(SequenceType.INT_QM)) + (orderIterator.getIterator().getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) + || orderIterator.getIterator().getStaticType().isSubtypeOf(SequenceType.INT_QM)) && nativeQuery.getResultingQuery().matches("\\s*-?\\s*\\d+\\s*") ) { orderSql.append('"'); diff --git a/src/main/java/org/rumbledb/runtime/functions/arrays/ArraySizeFunctionIterator.java b/src/main/java/org/rumbledb/runtime/functions/arrays/ArraySizeFunctionIterator.java index eed1500e60..71d3fcf1a2 100644 --- a/src/main/java/org/rumbledb/runtime/functions/arrays/ArraySizeFunctionIterator.java +++ b/src/main/java/org/rumbledb/runtime/functions/arrays/ArraySizeFunctionIterator.java @@ -27,8 +27,6 @@ import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.RuntimeIterator; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.BuiltinTypesCatalogue; -import org.rumbledb.types.SequenceType; import java.util.List; @@ -59,8 +57,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (nativeChildQuery != NativeClauseContext.NoNativeQuery) { return new NativeClauseContext( nativeClauseContext, - "SIZE (" + nativeChildQuery.getResultingQuery() + ")", - new SequenceType(BuiltinTypesCatalogue.integerItem, SequenceType.Arity.One) + "SIZE (" + nativeChildQuery.getResultingQuery() + ")" ); } return NativeClauseContext.NoNativeQuery; diff --git a/src/main/java/org/rumbledb/runtime/functions/numerics/FloorFunctionIterator.java b/src/main/java/org/rumbledb/runtime/functions/numerics/FloorFunctionIterator.java index 1bac2688eb..4b96c7b52d 100644 --- a/src/main/java/org/rumbledb/runtime/functions/numerics/FloorFunctionIterator.java +++ b/src/main/java/org/rumbledb/runtime/functions/numerics/FloorFunctionIterator.java @@ -103,7 +103,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (value == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!value.getResultingType().equals(SequenceType.FLOAT)) { + if (!this.children.get(0).getStaticType().equals(SequenceType.FLOAT)) { return NativeClauseContext.NoNativeQuery; } String resultingQuery = "( CAST (" @@ -111,7 +111,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC + value.getResultingQuery() + " ) AS FLOAT)" + " )"; - return new NativeClauseContext(nativeClauseContext, resultingQuery, SequenceType.FLOAT); + return new NativeClauseContext(nativeClauseContext, resultingQuery); } diff --git a/src/main/java/org/rumbledb/runtime/functions/numerics/RoundFunctionIterator.java b/src/main/java/org/rumbledb/runtime/functions/numerics/RoundFunctionIterator.java index 319112b53f..bc80162199 100644 --- a/src/main/java/org/rumbledb/runtime/functions/numerics/RoundFunctionIterator.java +++ b/src/main/java/org/rumbledb/runtime/functions/numerics/RoundFunctionIterator.java @@ -143,7 +143,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (value == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!value.getResultingType().equals(SequenceType.FLOAT)) { + if (!this.children.get(0).getStaticType().equals(SequenceType.FLOAT)) { return NativeClauseContext.NoNativeQuery; } String resultingQuery = "( CAST (" @@ -151,6 +151,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC + value.getResultingQuery() + " ) AS FLOAT)" + " )"; - return new NativeClauseContext(nativeClauseContext, resultingQuery, SequenceType.FLOAT); + return new NativeClauseContext(nativeClauseContext, resultingQuery); } } diff --git a/src/main/java/org/rumbledb/runtime/logics/AndOperationIterator.java b/src/main/java/org/rumbledb/runtime/logics/AndOperationIterator.java index c50d9663b1..f8fd6428e7 100644 --- a/src/main/java/org/rumbledb/runtime/logics/AndOperationIterator.java +++ b/src/main/java/org/rumbledb/runtime/logics/AndOperationIterator.java @@ -76,10 +76,10 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (rightResult == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!leftResult.getResultingType().equals(SequenceType.BOOLEAN)) { + if (!this.leftIterator.getStaticType().equals(SequenceType.BOOLEAN)) { return NativeClauseContext.NoNativeQuery; } - if (!rightResult.getResultingType().equals(SequenceType.BOOLEAN)) { + if (!this.leftIterator.getStaticType().equals(SequenceType.BOOLEAN)) { return NativeClauseContext.NoNativeQuery; } @@ -88,6 +88,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC + " AND " + rightResult.getResultingQuery() + " )"; - return new NativeClauseContext(nativeClauseContext, resultingQuery, SequenceType.BOOLEAN); + return new NativeClauseContext(nativeClauseContext, resultingQuery); } } diff --git a/src/main/java/org/rumbledb/runtime/logics/NotOperationIterator.java b/src/main/java/org/rumbledb/runtime/logics/NotOperationIterator.java index 4fff1fefcb..215a376a70 100644 --- a/src/main/java/org/rumbledb/runtime/logics/NotOperationIterator.java +++ b/src/main/java/org/rumbledb/runtime/logics/NotOperationIterator.java @@ -56,11 +56,11 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (childResult == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!childResult.getResultingType().equals(SequenceType.BOOLEAN)) { + if (!this.child.getStaticType().equals(SequenceType.BOOLEAN)) { return NativeClauseContext.NoNativeQuery; } String resultingQuery = "( NOT " + childResult.getResultingQuery() + " )"; - return new NativeClauseContext(nativeClauseContext, resultingQuery, SequenceType.BOOLEAN); + return new NativeClauseContext(nativeClauseContext, resultingQuery); } } diff --git a/src/main/java/org/rumbledb/runtime/logics/OrOperationIterator.java b/src/main/java/org/rumbledb/runtime/logics/OrOperationIterator.java index 08db3868eb..f308a6d20f 100644 --- a/src/main/java/org/rumbledb/runtime/logics/OrOperationIterator.java +++ b/src/main/java/org/rumbledb/runtime/logics/OrOperationIterator.java @@ -66,14 +66,14 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (rightResult == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!leftResult.getResultingType().equals(SequenceType.BOOLEAN)) { + if (!this.leftIterator.getStaticType().equals(SequenceType.BOOLEAN)) { return NativeClauseContext.NoNativeQuery; } - if (!rightResult.getResultingType().equals(SequenceType.BOOLEAN)) { + if (!this.leftIterator.getStaticType().equals(SequenceType.BOOLEAN)) { return NativeClauseContext.NoNativeQuery; } String resultingQuery = "( " + leftResult.getResultingQuery() + " OR " + rightResult.getResultingQuery() + " )"; - return new NativeClauseContext(nativeClauseContext, resultingQuery, SequenceType.BOOLEAN); + return new NativeClauseContext(nativeClauseContext, resultingQuery); } } diff --git a/src/main/java/org/rumbledb/runtime/misc/ComparisonIterator.java b/src/main/java/org/rumbledb/runtime/misc/ComparisonIterator.java index b4bfd4578a..155dcdbd25 100644 --- a/src/main/java/org/rumbledb/runtime/misc/ComparisonIterator.java +++ b/src/main/java/org/rumbledb/runtime/misc/ComparisonIterator.java @@ -38,7 +38,6 @@ import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.RuntimeIterator; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.BuiltinTypesCatalogue; import org.rumbledb.types.SequenceType; import org.rumbledb.types.SequenceType.Arity; @@ -511,31 +510,33 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (leftResult == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!leftResult.getResultingType().getArity().equals(Arity.One)) { + if (!this.leftIterator.getStaticType().getArity().equals(Arity.One)) { return NativeClauseContext.NoNativeQuery; } NativeClauseContext rightResult = this.rightIterator.generateNativeQuery(nativeClauseContext); if (rightResult == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - if (!rightResult.getResultingType().getArity().equals(Arity.One)) { + if (!this.rightIterator.getStaticType().getArity().equals(Arity.One)) { return NativeClauseContext.NoNativeQuery; } if ( - SequenceType.Arity.OneOrMore.isSubtypeOf(leftResult.getResultingType().getArity()) + SequenceType.Arity.OneOrMore.isSubtypeOf(this.leftIterator.getStaticType().getArity()) || - SequenceType.Arity.OneOrMore.isSubtypeOf(rightResult.getResultingType().getArity()) + SequenceType.Arity.OneOrMore.isSubtypeOf(this.rightIterator.getStaticType().getArity()) ) { return NativeClauseContext.NoNativeQuery; } // TODO: once done type system do proper comparison if ( - !(leftResult.getResultingType() != null - && rightResult.getResultingType() != null - && leftResult.getResultingType().getItemType().isNumeric() - && rightResult.getResultingType().getItemType().isNumeric() - || leftResult.getResultingType().getItemType().equals(rightResult.getResultingType().getItemType())) + !(this.leftIterator.getStaticType() != null + && this.rightIterator.getStaticType() != null + && this.leftIterator.getStaticType().getItemType().isNumeric() + && this.rightIterator.getStaticType().getItemType().isNumeric() + || this.leftIterator.getStaticType() + .getItemType() + .equals(this.rightIterator.getStaticType().getItemType())) ) { return NativeClauseContext.NoNativeQuery; } @@ -563,14 +564,10 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC default: return NativeClauseContext.NoNativeQuery; } - SequenceType.Arity resultingArity = leftResult.getResultingType() - .getArity() - .multiplyWith(rightResult.getResultingType().getArity()); String query = "( " + leftResult.getResultingQuery() + operator + rightResult.getResultingQuery() + " )"; return new NativeClauseContext( nativeClauseContext, - query, - new SequenceType(BuiltinTypesCatalogue.booleanItem, resultingArity) + query ); } else { return NativeClauseContext.NoNativeQuery; diff --git a/src/main/java/org/rumbledb/runtime/navigation/ArrayLookupIterator.java b/src/main/java/org/rumbledb/runtime/navigation/ArrayLookupIterator.java index 5aec4653a3..5610ff83ed 100644 --- a/src/main/java/org/rumbledb/runtime/navigation/ArrayLookupIterator.java +++ b/src/main/java/org/rumbledb/runtime/navigation/ArrayLookupIterator.java @@ -39,7 +39,6 @@ import org.rumbledb.runtime.flwor.FlworDataFrameUtils; import org.rumbledb.runtime.flwor.NativeClauseContext; import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; import sparksoniq.spark.SparkSessionManager; @@ -186,7 +185,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC initLookupPosition(newContext.getContext()); - ItemType resultType = newContext.getResultingType().getItemType(); + ItemType resultType = this.iterator.getStaticType().getItemType(); if (!(resultType.isArrayItemType())) { if (getConfiguration().doStaticAnalysis()) { throw new UnexpectedStaticTypeException( @@ -221,12 +220,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC ); return NativeClauseContext.NoNativeQuery; } - newContext.setResultingType( - new SequenceType( - resultType.getArrayContentFacet(), - SequenceType.Arity.OneOrZero - ) - ); newContext.setSchema(((ArrayType) newContext.getSchema()).elementType()); newContext.setResultingQuery(newContext.getResultingQuery() + "[" + (this.lookup - 1) + "]"); } diff --git a/src/main/java/org/rumbledb/runtime/navigation/ArrayUnboxingIterator.java b/src/main/java/org/rumbledb/runtime/navigation/ArrayUnboxingIterator.java index ba12801d73..5a0ad7359b 100644 --- a/src/main/java/org/rumbledb/runtime/navigation/ArrayUnboxingIterator.java +++ b/src/main/java/org/rumbledb/runtime/navigation/ArrayUnboxingIterator.java @@ -38,7 +38,6 @@ import org.rumbledb.runtime.flwor.FlworDataFrameUtils; import org.rumbledb.runtime.flwor.NativeClauseContext; import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; import sparksoniq.spark.SparkSessionManager; @@ -139,7 +138,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (newContext == NativeClauseContext.NoNativeQuery) { return NativeClauseContext.NoNativeQuery; } - ItemType newContextType = newContext.getResultingType().getItemType(); + ItemType newContextType = this.iterator.getStaticType().getItemType(); if (!newContextType.isArrayItemType()) { // let control to UDF when what we are unboxing is not an array if (getConfiguration().doStaticAnalysis()) { @@ -157,12 +156,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC ); return NativeClauseContext.NoNativeQuery; } - newContext.setResultingType( - new SequenceType( - newContextType.getArrayContentFacet(), - SequenceType.Arity.ZeroOrMore - ) - ); List lateralViewPart = newContext.getLateralViewPart(); if (lateralViewPart.size() == 0) { diff --git a/src/main/java/org/rumbledb/runtime/navigation/ObjectLookupIterator.java b/src/main/java/org/rumbledb/runtime/navigation/ObjectLookupIterator.java index 1237e01e03..1181764420 100644 --- a/src/main/java/org/rumbledb/runtime/navigation/ObjectLookupIterator.java +++ b/src/main/java/org/rumbledb/runtime/navigation/ObjectLookupIterator.java @@ -44,9 +44,6 @@ import org.rumbledb.types.BuiltinTypesCatalogue; import org.rumbledb.types.FieldDescriptor; import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; -import org.rumbledb.types.SequenceType.Arity; -import org.rumbledb.types.TypeMappings; import sparksoniq.spark.SparkSessionManager; @@ -254,14 +251,12 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (outerContextSchema instanceof StructType) { newContext = new NativeClauseContext( nativeClauseContext, - null, - nativeClauseContext.getResultingType() + null ); } else { newContext = new NativeClauseContext( nativeClauseContext, - SparkSessionManager.atomicJSONiqItemColumnName, - nativeClauseContext.getResultingType() + SparkSessionManager.atomicJSONiqItemColumnName ); } } else { @@ -306,9 +301,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC } StructField field = structSchema.fields()[structSchema.fieldIndex(key)]; newContext.setSchema(field.dataType()); - newContext.setResultingType( - new SequenceType(TypeMappings.getItemTypeFromDataFrameDataType(field.dataType()), Arity.One) - ); } else { if (this.children.get(1) instanceof StringRuntimeIterator) { LogManager.getLogger("ObjectLookupIterator") diff --git a/src/main/java/org/rumbledb/runtime/primary/ContextExpressionIterator.java b/src/main/java/org/rumbledb/runtime/primary/ContextExpressionIterator.java index d0214264ee..4fc866ce23 100644 --- a/src/main/java/org/rumbledb/runtime/primary/ContextExpressionIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/ContextExpressionIterator.java @@ -30,9 +30,6 @@ import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.flwor.FlworDataFrameUtils; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; -import org.rumbledb.types.TypeMappings; import sparksoniq.spark.SparkSessionManager; @@ -87,11 +84,9 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC SparkSessionManager.atomicJSONiqItemColumnName )]; DataType fieldType = field.dataType(); - ItemType variableType = TypeMappings.getItemTypeFromDataFrameDataType(fieldType); return new NativeClauseContext( nativeClauseContext, - "`" + SparkSessionManager.atomicJSONiqItemColumnName + "`", - new SequenceType(variableType, SequenceType.Arity.One) + "`" + SparkSessionManager.atomicJSONiqItemColumnName + "`" ); } } diff --git a/src/main/java/org/rumbledb/runtime/primary/DecimalRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/primary/DecimalRuntimeIterator.java index 5f4454bdea..15a7e2f999 100644 --- a/src/main/java/org/rumbledb/runtime/primary/DecimalRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/DecimalRuntimeIterator.java @@ -25,7 +25,6 @@ import org.rumbledb.context.RuntimeStaticContext; import org.rumbledb.items.ItemFactory; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.SequenceType; import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import java.math.BigDecimal; @@ -50,8 +49,7 @@ public Item materializeFirstItemOrNull(DynamicContext context) { public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { return new NativeClauseContext( nativeClauseContext, - "" + this.item.getDecimalValue(), - SequenceType.DECIMAL + "CAST (" + this.item.getDecimalValue() + " AS DECIMAL(38, 19))" ); } } diff --git a/src/main/java/org/rumbledb/runtime/primary/DoubleRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/primary/DoubleRuntimeIterator.java index d1600fb4c7..ddc021eb07 100644 --- a/src/main/java/org/rumbledb/runtime/primary/DoubleRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/DoubleRuntimeIterator.java @@ -25,7 +25,6 @@ import org.rumbledb.context.RuntimeStaticContext; import org.rumbledb.items.ItemFactory; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.SequenceType; import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; public class DoubleRuntimeIterator extends AtMostOneItemLocalRuntimeIterator { @@ -49,8 +48,7 @@ public Item materializeFirstItemOrNull(DynamicContext context) { public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { return new NativeClauseContext( nativeClauseContext, - "" + this.item.getDoubleValue(), - SequenceType.DOUBLE + "CAST (" + item.getDoubleValue() + "D AS DOUBLE)" ); } } diff --git a/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java index 9c96c48fe4..93a6a1e593 100644 --- a/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java @@ -25,7 +25,6 @@ import org.rumbledb.context.RuntimeStaticContext; import org.rumbledb.items.ItemFactory; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.SequenceType; import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; public class IntegerRuntimeIterator extends AtMostOneItemLocalRuntimeIterator { @@ -51,8 +50,7 @@ public Item materializeFirstItemOrNull(DynamicContext context) { public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { return new NativeClauseContext( nativeClauseContext, - "" + this.item.getIntValue(), - SequenceType.INTEGER + "CAST (" + item.getIntValue() + "D AS DECIMAL(38, 0))" ); } } diff --git a/src/main/java/org/rumbledb/runtime/primary/StringRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/primary/StringRuntimeIterator.java index 60564d1751..35306dfd97 100644 --- a/src/main/java/org/rumbledb/runtime/primary/StringRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/StringRuntimeIterator.java @@ -27,7 +27,6 @@ import org.rumbledb.items.ItemFactory; import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.SequenceType; public class StringRuntimeIterator extends AtMostOneItemLocalRuntimeIterator { @@ -48,8 +47,7 @@ public Item materializeFirstItemOrNull(DynamicContext context) { public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { return new NativeClauseContext( nativeClauseContext, - '"' + this.item.getStringValue() + '"', - SequenceType.STRING + '"' + this.item.getStringValue() + '"' ); } } diff --git a/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java b/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java index 4abd9b8567..1d4e8cc96a 100644 --- a/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java @@ -35,10 +35,6 @@ import org.rumbledb.runtime.RuntimeIterator; import org.rumbledb.runtime.flwor.FlworDataFrameUtils; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.ItemType; -import org.rumbledb.types.SequenceType; -import org.rumbledb.types.SequenceType.Arity; -import org.rumbledb.types.TypeMappings; import java.util.List; import java.util.Map; @@ -105,11 +101,9 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC String escapedName = name.replace("`", FlworDataFrameUtils.backtickEscape); StructField field = structSchema.fields()[structSchema.fieldIndex(escapedName)]; DataType fieldType = field.dataType(); - ItemType variableType = TypeMappings.getItemTypeFromDataFrameDataType(fieldType); NativeClauseContext newContext = new NativeClauseContext( nativeClauseContext, - "`" + escapedName + "`", - new SequenceType(variableType, Arity.One) + "`" + escapedName + "`" ); newContext.setSchema(fieldType); return newContext; diff --git a/src/main/java/org/rumbledb/runtime/typing/AtMostOneItemTypePromotionIterator.java b/src/main/java/org/rumbledb/runtime/typing/AtMostOneItemTypePromotionIterator.java index 751eac69a5..78ff7b5184 100644 --- a/src/main/java/org/rumbledb/runtime/typing/AtMostOneItemTypePromotionIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/AtMostOneItemTypePromotionIterator.java @@ -124,7 +124,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (value.equals(NativeClauseContext.NoNativeQuery)) { return NativeClauseContext.NoNativeQuery; } - if (!value.getResultingType().equals(SequenceType.FLOAT)) { + if (!this.children.get(0).getStaticType().equals(SequenceType.FLOAT)) { return NativeClauseContext.NoNativeQuery; } if ( From 93aaaf3796a837651754940167df93ea2f5fd975 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 17 May 2023 15:20:37 +0200 Subject: [PATCH 03/42] Native casts. --- .../rumbledb/compiler/InferTypeVisitor.java | 6 +++ .../flwor/clauses/LetClauseSparkIterator.java | 7 +++ .../primary/DecimalRuntimeIterator.java | 2 +- .../primary/DoubleRuntimeIterator.java | 2 +- .../primary/IntegerRuntimeIterator.java | 2 +- .../rumbledb/runtime/typing/CastIterator.java | 47 +++++++++++++++++++ 6 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/rumbledb/compiler/InferTypeVisitor.java b/src/main/java/org/rumbledb/compiler/InferTypeVisitor.java index dae00d244d..67ad4be849 100644 --- a/src/main/java/org/rumbledb/compiler/InferTypeVisitor.java +++ b/src/main/java/org/rumbledb/compiler/InferTypeVisitor.java @@ -91,6 +91,7 @@ import org.rumbledb.types.ItemType; import org.rumbledb.types.ItemTypeFactory; import org.rumbledb.types.SequenceType; +import org.rumbledb.types.SequenceType.Arity; import sparksoniq.spark.SparkSessionManager; @@ -631,6 +632,11 @@ public StaticContext visitCastExpression(CastExpression expression, StaticContex expression.getMetadata() ); } + if(expressionSequenceType.getArity().equals(Arity.One)) + { + expression.setStaticSequenceType(new SequenceType(castedSequenceType.getItemType(), Arity.One)); + return argument; + } expression.setStaticSequenceType(castedSequenceType); return argument; diff --git a/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java b/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java index 8f4e9c098b..9a45ccfd36 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java +++ b/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java @@ -833,6 +833,13 @@ public static Dataset tryNativeQuery( ); String selectSQL = FlworDataFrameUtils.getSQLColumnProjection(allColumns, true); String input = FlworDataFrameUtils.createTempView(dataFrame); + System.err.println(String.format( + "select %s %s as `%s` from %s", + selectSQL, + nativeQuery.getResultingQuery(), + newVariableName, + input + )); return dataFrame.sparkSession() .sql( String.format( diff --git a/src/main/java/org/rumbledb/runtime/primary/DecimalRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/primary/DecimalRuntimeIterator.java index 15a7e2f999..3dc235fe7d 100644 --- a/src/main/java/org/rumbledb/runtime/primary/DecimalRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/DecimalRuntimeIterator.java @@ -49,7 +49,7 @@ public Item materializeFirstItemOrNull(DynamicContext context) { public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { return new NativeClauseContext( nativeClauseContext, - "CAST (" + this.item.getDecimalValue() + " AS DECIMAL(38, 19))" + "CAST (" + this.item.getDecimalValue() + "BD AS DECIMAL(38, 19))" ); } } diff --git a/src/main/java/org/rumbledb/runtime/primary/DoubleRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/primary/DoubleRuntimeIterator.java index ddc021eb07..02b35e4733 100644 --- a/src/main/java/org/rumbledb/runtime/primary/DoubleRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/DoubleRuntimeIterator.java @@ -48,7 +48,7 @@ public Item materializeFirstItemOrNull(DynamicContext context) { public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { return new NativeClauseContext( nativeClauseContext, - "CAST (" + item.getDoubleValue() + "D AS DOUBLE)" + "CAST (" + this.item.getDoubleValue() + "D AS DOUBLE)" ); } } diff --git a/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java index 93a6a1e593..f38373a950 100644 --- a/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java @@ -50,7 +50,7 @@ public Item materializeFirstItemOrNull(DynamicContext context) { public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { return new NativeClauseContext( nativeClauseContext, - "CAST (" + item.getIntValue() + "D AS DECIMAL(38, 0))" + "CAST (" + this.item.getIntValue() + "BD AS DECIMAL(38, 0))" ); } } diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index 082b9fb499..1a382be2f5 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -12,6 +12,7 @@ import org.rumbledb.items.ItemFactory; import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.RuntimeIterator; +import org.rumbledb.runtime.flwor.NativeClauseContext; import org.rumbledb.types.BuiltinTypesCatalogue; import org.rumbledb.types.ItemType; import org.rumbledb.types.SequenceType; @@ -799,5 +800,51 @@ public static boolean checkFacetsDuration(Item item, ItemType targetType) { return true; } + + @Override + public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { + NativeClauseContext value = this.children.get(0).generateNativeQuery(nativeClauseContext); + if (value.equals(NativeClauseContext.NoNativeQuery)) { + return NativeClauseContext.NoNativeQuery; + } + if(this.children.get(0).getStaticType().getArity() != Arity.One) + { + return NativeClauseContext.NoNativeQuery; + } + String resultingQuery = " (CAST ("+value.getResultingQuery()+" AS "; + if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) + { + resultingQuery = resultingQuery + "INT"; + System.err.println("Int"); + } else if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.integerItem)) + { + resultingQuery = resultingQuery + "DECIMAL(38,0)"; + System.err.println("Int"); + } else if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.decimalItem)) + { + resultingQuery = resultingQuery + "DECIMAL(38,19)"; + System.err.println("Int"); + } else if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.doubleItem)) + { + resultingQuery = resultingQuery + "DOUBLE"; + System.err.println("Int"); + } else if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.floatItem)) + { + resultingQuery = resultingQuery + "FLOAT"; + System.err.println("Int"); + } else if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) + { + resultingQuery = resultingQuery + "INT"; + System.err.println("Int"); + } else { + return NativeClauseContext.NoNativeQuery; + } + resultingQuery = resultingQuery + ")) "; + System.err.println("Return"); + return new NativeClauseContext( + nativeClauseContext, + resultingQuery + ); + } } From 588d0dec7788d14206582474720ad3fcfe017737 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 17 May 2023 15:29:43 +0200 Subject: [PATCH 04/42] Add tests. --- .../test_files/runtime-native-flwor/literals/decimal.jq | 6 ++++++ .../test_files/runtime-native-flwor/literals/double.jq | 6 ++++++ .../test_files/runtime-native-flwor/literals/float.jq | 5 +++++ .../test_files/runtime-native-flwor/literals/int.jq | 5 +++++ .../test_files/runtime-native-flwor/literals/integer.jq | 6 ++++++ 5 files changed, 28 insertions(+) create mode 100644 src/test/resources/test_files/runtime-native-flwor/literals/decimal.jq create mode 100644 src/test/resources/test_files/runtime-native-flwor/literals/double.jq create mode 100644 src/test/resources/test_files/runtime-native-flwor/literals/float.jq create mode 100644 src/test/resources/test_files/runtime-native-flwor/literals/int.jq create mode 100644 src/test/resources/test_files/runtime-native-flwor/literals/integer.jq diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/decimal.jq b/src/test/resources/test_files/runtime-native-flwor/literals/decimal.jq new file mode 100644 index 0000000000..9689d5496f --- /dev/null +++ b/src/test/resources/test_files/runtime-native-flwor/literals/decimal.jq @@ -0,0 +1,6 @@ +(:JIQS: ShouldRun; Output="(123456792.4123456789, 123456793.4123456789, 123456794.4123456789, 123456795.4123456789, 123456796.4123456789, 123456797.4123456789, 123456798.4123456789, 123456799.4123456789, 123456800.4123456789, 123456801.4123456789, 123456802.4123456789, 123456803.4123456789, 123456804.4123456789, 123456805.4123456789, 123456806.4123456789, 123456807.4123456789, 123456808.4123456789, 123456809.4123456789, 123456810.4123456789, 123456811.4123456789, 123456812.4123456789, 123456813.4123456789, 123456814.4123456789, 123456815.4123456789, 123456816.4123456789, 123456817.4123456789, 123456818.4123456789, 123456819.4123456789, 123456820.4123456789, 123456821.4123456789, 123456822.4123456789, 123456823.4123456789, 123456824.4123456789, 123456825.4123456789, 123456826.4123456789, 123456827.4123456789, 123456828.4123456789, 123456829.4123456789, 123456830.4123456789, 123456831.4123456789, 123456832.4123456789, 123456833.4123456789, 123456834.4123456789, 123456835.4123456789, 123456836.4123456789, 123456837.4123456789, 123456838.4123456789, 123456839.4123456789, 123456840.4123456789, 123456841.4123456789, 123456842.4123456789, 123456843.4123456789, 123456844.4123456789, 123456845.4123456789, 123456846.4123456789, 123456847.4123456789, 123456848.4123456789, 123456849.4123456789, 123456850.4123456789, 123456851.4123456789, 123456852.4123456789, 123456853.4123456789, 123456854.4123456789, 123456855.4123456789, 123456856.4123456789, 123456857.4123456789, 123456858.4123456789, 123456859.4123456789, 123456860.4123456789, 123456861.4123456789, 123456862.4123456789, 123456863.4123456789, 123456864.4123456789, 123456865.4123456789, 123456866.4123456789, 123456867.4123456789, 123456868.4123456789, 123456869.4123456789, 123456870.4123456789, 123456871.4123456789, 123456872.4123456789, 123456873.4123456789, 123456874.4123456789, 123456875.4123456789, 123456876.4123456789, 123456877.4123456789, 123456878.4123456789, 123456879.4123456789, 123456880.4123456789, 123456881.4123456789, 123456882.4123456789, 123456883.4123456789, 123456884.4123456789, 123456885.4123456789, 123456886.4123456789, 123456887.4123456789, 123456888.4123456789, 123456889.4123456789, 123456890.4123456789, 123456891.4123456789)" :) +for $i in parallelize((1 to 100) ! ($$ cast as decimal), 10) +let $a := decimal(2.4) +let $b := 123456789.0123456789 +let $j := $i + $a + $b +return $j diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/double.jq b/src/test/resources/test_files/runtime-native-flwor/literals/double.jq new file mode 100644 index 0000000000..66748e6d62 --- /dev/null +++ b/src/test/resources/test_files/runtime-native-flwor/literals/double.jq @@ -0,0 +1,6 @@ +(:JIQS: ShouldRun; Output="(31003, 31004, 31005, 31006, 31007, 31008, 31009, 31010, 31011, 31012, 31013, 31014, 31015, 31016, 31017, 31018, 31019, 31020, 31021, 31022, 31023, 31024, 31025, 31026, 31027, 31028, 31029, 31030, 31031, 31032, 31033, 31034, 31035, 31036, 31037, 31038, 31039, 31040, 31041, 31042, 31043, 31044, 31045, 31046, 31047, 31048, 31049, 31050, 31051, 31052, 31053, 31054, 31055, 31056, 31057, 31058, 31059, 31060, 31061, 31062, 31063, 31064, 31065, 31066, 31067, 31068, 31069, 31070, 31071, 31072, 31073, 31074, 31075, 31076, 31077, 31078, 31079, 31080, 31081, 31082, 31083, 31084, 31085, 31086, 31087, 31088, 31089, 31090, 31091, 31092, 31093, 31094, 31095, 31096, 31097, 31098, 31099, 31100, 31101, 31102)" :) +for $i in parallelize((1 to 100) ! ($$ cast as double), 10) +let $a := double(2) +let $b := 3.1e4 +let $j := $i + $a + $b +return $j diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/float.jq b/src/test/resources/test_files/runtime-native-flwor/literals/float.jq new file mode 100644 index 0000000000..1ea3116006 --- /dev/null +++ b/src/test/resources/test_files/runtime-native-flwor/literals/float.jq @@ -0,0 +1,5 @@ +(:JIQS: ShouldRun; Output="(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102)" :) +for $i in parallelize((1 to 100) ! ($$ cast as float), 10) +let $a := float(2) +let $j := $i + $a +return $j diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/int.jq b/src/test/resources/test_files/runtime-native-flwor/literals/int.jq new file mode 100644 index 0000000000..0865437938 --- /dev/null +++ b/src/test/resources/test_files/runtime-native-flwor/literals/int.jq @@ -0,0 +1,5 @@ +(:JIQS: ShouldRun; Output="(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102)" :) +for $i as int in parallelize((1 to 100) ! ($$ cast as int), 10) +let $a := int(2) +let $j := $i + $a +return $j diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/integer.jq b/src/test/resources/test_files/runtime-native-flwor/literals/integer.jq new file mode 100644 index 0000000000..01236e0f37 --- /dev/null +++ b/src/test/resources/test_files/runtime-native-flwor/literals/integer.jq @@ -0,0 +1,6 @@ +(:JIQS: ShouldRun; Output="(1234567890123456792, 1234567890123456793, 1234567890123456794, 1234567890123456795, 1234567890123456796, 1234567890123456797, 1234567890123456798, 1234567890123456799, 1234567890123456800, 1234567890123456801, 1234567890123456802, 1234567890123456803, 1234567890123456804, 1234567890123456805, 1234567890123456806, 1234567890123456807, 1234567890123456808, 1234567890123456809, 1234567890123456810, 1234567890123456811, 1234567890123456812, 1234567890123456813, 1234567890123456814, 1234567890123456815, 1234567890123456816, 1234567890123456817, 1234567890123456818, 1234567890123456819, 1234567890123456820, 1234567890123456821, 1234567890123456822, 1234567890123456823, 1234567890123456824, 1234567890123456825, 1234567890123456826, 1234567890123456827, 1234567890123456828, 1234567890123456829, 1234567890123456830, 1234567890123456831, 1234567890123456832, 1234567890123456833, 1234567890123456834, 1234567890123456835, 1234567890123456836, 1234567890123456837, 1234567890123456838, 1234567890123456839, 1234567890123456840, 1234567890123456841, 1234567890123456842, 1234567890123456843, 1234567890123456844, 1234567890123456845, 1234567890123456846, 1234567890123456847, 1234567890123456848, 1234567890123456849, 1234567890123456850, 1234567890123456851, 1234567890123456852, 1234567890123456853, 1234567890123456854, 1234567890123456855, 1234567890123456856, 1234567890123456857, 1234567890123456858, 1234567890123456859, 1234567890123456860, 1234567890123456861, 1234567890123456862, 1234567890123456863, 1234567890123456864, 1234567890123456865, 1234567890123456866, 1234567890123456867, 1234567890123456868, 1234567890123456869, 1234567890123456870, 1234567890123456871, 1234567890123456872, 1234567890123456873, 1234567890123456874, 1234567890123456875, 1234567890123456876, 1234567890123456877, 1234567890123456878, 1234567890123456879, 1234567890123456880, 1234567890123456881, 1234567890123456882, 1234567890123456883, 1234567890123456884, 1234567890123456885, 1234567890123456886, 1234567890123456887, 1234567890123456888, 1234567890123456889, 1234567890123456890, 1234567890123456891)" :) +for $i in parallelize((1 to 100) ! ($$ cast as integer), 10) +let $a := integer(2) +let $b := 1234567890123456789 +let $j := $i + integer(2) + 1234567890123456789 +return $j From 1c7f483ea6e05c16c727f90aeddf2f992662ea8b Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 17 May 2023 15:29:46 +0200 Subject: [PATCH 05/42] Add tests. --- .../rumbledb/compiler/InferTypeVisitor.java | 3 +- .../flwor/clauses/LetClauseSparkIterator.java | 16 ++++---- .../rumbledb/runtime/typing/CastIterator.java | 37 ++++++++----------- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/rumbledb/compiler/InferTypeVisitor.java b/src/main/java/org/rumbledb/compiler/InferTypeVisitor.java index 67ad4be849..9dc47c4640 100644 --- a/src/main/java/org/rumbledb/compiler/InferTypeVisitor.java +++ b/src/main/java/org/rumbledb/compiler/InferTypeVisitor.java @@ -632,8 +632,7 @@ public StaticContext visitCastExpression(CastExpression expression, StaticContex expression.getMetadata() ); } - if(expressionSequenceType.getArity().equals(Arity.One)) - { + if (expressionSequenceType.getArity().equals(Arity.One)) { expression.setStaticSequenceType(new SequenceType(castedSequenceType.getItemType(), Arity.One)); return argument; } diff --git a/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java b/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java index 9a45ccfd36..533e46ef1f 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java +++ b/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java @@ -833,13 +833,15 @@ public static Dataset tryNativeQuery( ); String selectSQL = FlworDataFrameUtils.getSQLColumnProjection(allColumns, true); String input = FlworDataFrameUtils.createTempView(dataFrame); - System.err.println(String.format( - "select %s %s as `%s` from %s", - selectSQL, - nativeQuery.getResultingQuery(), - newVariableName, - input - )); + System.err.println( + String.format( + "select %s %s as `%s` from %s", + selectSQL, + nativeQuery.getResultingQuery(), + newVariableName, + input + ) + ); return dataFrame.sparkSession() .sql( String.format( diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index 1a382be2f5..82ac71d5d6 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -807,34 +807,27 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (value.equals(NativeClauseContext.NoNativeQuery)) { return NativeClauseContext.NoNativeQuery; } - if(this.children.get(0).getStaticType().getArity() != Arity.One) - { + if (this.children.get(0).getStaticType().getArity() != Arity.One) { return NativeClauseContext.NoNativeQuery; } - String resultingQuery = " (CAST ("+value.getResultingQuery()+" AS "; - if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) - { - resultingQuery = resultingQuery + "INT"; + String resultingQuery = " (CAST (" + value.getResultingQuery() + " AS "; + if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) { + resultingQuery = resultingQuery + "INT"; System.err.println("Int"); - } else if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.integerItem)) - { - resultingQuery = resultingQuery + "DECIMAL(38,0)"; + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.integerItem)) { + resultingQuery = resultingQuery + "DECIMAL(38,0)"; System.err.println("Int"); - } else if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.decimalItem)) - { - resultingQuery = resultingQuery + "DECIMAL(38,19)"; + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.decimalItem)) { + resultingQuery = resultingQuery + "DECIMAL(38,19)"; System.err.println("Int"); - } else if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.doubleItem)) - { - resultingQuery = resultingQuery + "DOUBLE"; + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.doubleItem)) { + resultingQuery = resultingQuery + "DOUBLE"; System.err.println("Int"); - } else if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.floatItem)) - { - resultingQuery = resultingQuery + "FLOAT"; + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.floatItem)) { + resultingQuery = resultingQuery + "FLOAT"; System.err.println("Int"); - } else if(this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) - { - resultingQuery = resultingQuery + "INT"; + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) { + resultingQuery = resultingQuery + "INT"; System.err.println("Int"); } else { return NativeClauseContext.NoNativeQuery; @@ -844,7 +837,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return new NativeClauseContext( nativeClauseContext, resultingQuery - ); + ); } } From 9d47bce26004e3b2cf6afcba98bbde3aa7eac27b Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 17 May 2023 15:40:36 +0200 Subject: [PATCH 06/42] Fix test. --- src/test/resources/test_files/static-typing/typing/cast2.jq | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/test_files/static-typing/typing/cast2.jq b/src/test/resources/test_files/static-typing/typing/cast2.jq index 1aa54f5010..baf9de637a 100644 --- a/src/test/resources/test_files/static-typing/typing/cast2.jq +++ b/src/test/resources/test_files/static-typing/typing/cast2.jq @@ -3,4 +3,4 @@ declare variable $date := "2020-02-12" cast as date; declare variable $dt := "2015-05-03T13:20:00" cast as dateTime; declare variable $hexb := "0cd7" cast as hexBinary; declare variable $base64b := "DNc=" cast as base64Binary; -($base64b cast as hexBinary) is statically hexBinary, ($hexb cast as base64Binary) is statically base64Binary, ($dt cast as date) is statically date, ($dt cast as time) is statically time, ($date cast as dateTime) is statically dateTime, (null cast as null) is statically null, (3 cast as integer?) is statically integer? \ No newline at end of file +($base64b cast as hexBinary) is statically hexBinary, ($hexb cast as base64Binary) is statically base64Binary, ($dt cast as date) is statically date, ($dt cast as time) is statically time, ($date cast as dateTime) is statically dateTime, (null cast as null) is statically null, (3 cast as integer?) is statically integer \ No newline at end of file From 5002b758db454e33ade4117ca7ec7e52c604213d Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Mon, 22 May 2023 16:02:13 +0200 Subject: [PATCH 07/42] More native casts. --- .../org/rumbledb/items/AnnotatedItem.java | 3 + .../rumbledb/items/parsing/ItemParser.java | 2 +- .../runtime/flwor/udfs/DataFrameContext.java | 4 ++ .../rumbledb/runtime/typing/CastIterator.java | 61 ++++++++++++++----- 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/rumbledb/items/AnnotatedItem.java b/src/main/java/org/rumbledb/items/AnnotatedItem.java index 5d110a4014..583b42f6be 100644 --- a/src/main/java/org/rumbledb/items/AnnotatedItem.java +++ b/src/main/java/org/rumbledb/items/AnnotatedItem.java @@ -40,6 +40,9 @@ public AnnotatedItem() { public AnnotatedItem(Item itemToAnnotate, ItemType type) { this.itemToAnnotate = itemToAnnotate; this.type = type; + if (type == null) { + throw new OurBadException("It it not possible to annotate an item without a type."); + } if (type.getName() == null) { throw new OurBadException("It it not possible to annotate an item with an anonymous type."); } diff --git a/src/main/java/org/rumbledb/items/parsing/ItemParser.java b/src/main/java/org/rumbledb/items/parsing/ItemParser.java index 0229a79551..e27256f5e9 100644 --- a/src/main/java/org/rumbledb/items/parsing/ItemParser.java +++ b/src/main/java/org/rumbledb/items/parsing/ItemParser.java @@ -462,7 +462,7 @@ private static Item convertValueToItem( value = (Timestamp) o; } Instant instant = value.toInstant(); - DateTime dt = new DateTime(instant); + DateTime dt = new DateTime(instant.toEpochMilli()); Item item = ItemFactory.getInstance().createDateTimeItem(dt, false); if (itemType == null || itemType.equals(BuiltinTypesCatalogue.dateTimeStampItem)) { return item; diff --git a/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java b/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java index 790be30d76..b869df7ba6 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java +++ b/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java @@ -37,6 +37,7 @@ import org.rumbledb.runtime.flwor.FlworDataFrameColumn; import org.rumbledb.runtime.flwor.FlworDataFrameUtils; import org.rumbledb.types.ItemType; +import org.rumbledb.types.TypeMappings; import java.io.IOException; import java.io.Serializable; @@ -252,6 +253,9 @@ private List readColumnAsSequenceOfItems(Row row, ItemType itemType, int c return items; } } + if (itemType == null) { + itemType = TypeMappings.getItemTypeFromDataFrameDataType(dt); + } Item item = ItemParser.convertValueToItem(o, dt, ExceptionMetadata.EMPTY_METADATA, itemType); return Collections.singletonList(item); } diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index 82ac71d5d6..432070d9d5 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -810,29 +810,60 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (this.children.get(0).getStaticType().getArity() != Arity.One) { return NativeClauseContext.NoNativeQuery; } - String resultingQuery = " (CAST (" + value.getResultingQuery() + " AS "; - if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) { - resultingQuery = resultingQuery + "INT"; + String resultingQuery = ""; + if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.booleanItem)) { + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "BOOLEAN" + ")) "; + System.err.println("Boolean"); + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.byteItem)) { + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "BYTE" + ")) "; + System.err.println("Byte"); + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.shortItem)) { + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "SHORT" + ")) "; + System.err.println("Short"); + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) { + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "INT" + ")) "; System.err.println("Int"); + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.longItem)) { + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "LONG" + ")) "; + System.err.println("Long"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.integerItem)) { - resultingQuery = resultingQuery + "DECIMAL(38,0)"; - System.err.println("Int"); + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DECIMAL(38,0)" + ")) "; + System.err.println("Integer"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.decimalItem)) { - resultingQuery = resultingQuery + "DECIMAL(38,19)"; - System.err.println("Int"); + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DECIMAL(38,19)" + ")) "; + System.err.println("Decimal"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.doubleItem)) { - resultingQuery = resultingQuery + "DOUBLE"; - System.err.println("Int"); + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DOUBLE" + ")) "; + System.err.println("Double"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.floatItem)) { - resultingQuery = resultingQuery + "FLOAT"; - System.err.println("Int"); - } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) { - resultingQuery = resultingQuery + "INT"; - System.err.println("Int"); + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "FLOAT" + ")) "; + System.err.println("Float"); + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.stringItem)) { + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; + System.err.println("String"); + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.anyURIItem)) { + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; + System.err.println("anyURI"); + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.dateItem)) { + if (getConfiguration().dateWithTimezone()) { + return NativeClauseContext.NoNativeQuery; + } + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DATE" + ")) "; + System.err.println("Date"); + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem)) { + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "TIMESTAMP" + ")) "; + System.err.println("Timestamp"); + /* + * } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.hexBinaryItem)) { + * resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "BINARY" + ")) "; + * System.err.println("HexBinary"); + */ + } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.nullItem)) { + resultingQuery = " NULL "; + System.err.println("Null"); } else { return NativeClauseContext.NoNativeQuery; } - resultingQuery = resultingQuery + ")) "; System.err.println("Return"); return new NativeClauseContext( nativeClauseContext, From fe647f82fb4eb1be3f06421c5377da90b33e4ccc Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Tue, 23 May 2023 14:32:41 +0200 Subject: [PATCH 08/42] Fix warnings. --- pom.xml | 6 +- .../org/rumbledb/api/SequenceOfItems.java | 41 +++++- src/main/java/org/rumbledb/cli/Main.java | 52 +++++--- .../AdditiveOperationIterator.java | 9 -- .../MultiplicativeOperationIterator.java | 13 -- .../primary/ContextExpressionIterator.java | 5 - .../rumbledb/runtime/typing/CastIterator.java | 125 +++++++++++++++++- .../org/rumbledb/server/MainPageHandler.java | 1 - .../rumbledb/server/RumbleHttpHandler.java | 1 - .../org/rumbledb/server/RumbleServer.java | 1 - .../rumbledb/server/ValidatorPageHandler.java | 1 - .../org/rumbledb/shell/RumbleJLineShell.java | 18 +-- .../sparksoniq/spark/SparkSessionManager.java | 1 + src/test/java/iq/RuntimeTests.java | 1 + .../java/iq/base/AnnotationsTestsBase.java | 9 ++ 15 files changed, 209 insertions(+), 75 deletions(-) diff --git a/pom.xml b/pom.xml index 423a700afc..308e8c7afd 100644 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.11.0 eclipse @@ -78,7 +78,7 @@ org.apache.maven.plugins maven-assembly-plugin - 3.1.1 + 3.6.0 @@ -167,7 +167,7 @@ com.diffplug.spotless spotless-maven-plugin - 1.26.0 + 2.36.0 verify diff --git a/src/main/java/org/rumbledb/api/SequenceOfItems.java b/src/main/java/org/rumbledb/api/SequenceOfItems.java index c8cec7beca..823938e013 100644 --- a/src/main/java/org/rumbledb/api/SequenceOfItems.java +++ b/src/main/java/org/rumbledb/api/SequenceOfItems.java @@ -7,6 +7,9 @@ import org.apache.spark.sql.Row; import org.rumbledb.config.RumbleRuntimeConfiguration; import org.rumbledb.context.DynamicContext; +import org.rumbledb.exceptions.ExceptionMetadata; +import org.rumbledb.exceptions.RumbleException; +import org.rumbledb.exceptions.UnexpectedTypeException; import org.rumbledb.runtime.RuntimeIterator; import sparksoniq.spark.SparkSessionManager; @@ -78,7 +81,17 @@ public void close() { * @return true if there are more items, false otherwise. */ public boolean hasNext() { - return this.iterator.hasNext(); + try { + return this.iterator.hasNext(); + } catch (NumberFormatException e) { + RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + ex.initCause(e); + throw ex; + } catch (UnsupportedOperationException e) { + RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + ex.initCause(e); + throw ex; + } } /** @@ -119,7 +132,17 @@ public JavaRDD getAsRDD() { if (this.isOpen) { throw new RuntimeException("Cannot obtain an RDD if the iterator is open."); } - return this.iterator.getRDD(this.dynamicContext); + try { + return this.iterator.getRDD(this.dynamicContext); + } catch (NumberFormatException e) { + RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + ex.initCause(e); + throw ex; + } catch (UnsupportedOperationException e) { + RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + ex.initCause(e); + throw ex; + } } /** @@ -176,8 +199,18 @@ public long populateList(List resultList) { public long populateListWithWarningOnlyIfCapReached(List resultList) { if (this.availableAsRDD()) { - JavaRDD rdd = this.iterator.getRDD(this.dynamicContext); - return SparkSessionManager.collectRDDwithLimitWarningOnly(rdd, resultList); + try { + JavaRDD rdd = this.iterator.getRDD(this.dynamicContext); + return SparkSessionManager.collectRDDwithLimitWarningOnly(rdd, resultList); + } catch (NumberFormatException e) { + RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + ex.initCause(e); + throw ex; + } catch (UnsupportedOperationException e) { + RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + ex.initCause(e); + throw ex; + } } else { return populateList(resultList); } diff --git a/src/main/java/org/rumbledb/cli/Main.java b/src/main/java/org/rumbledb/cli/Main.java index 436a14dc69..29562892bb 100644 --- a/src/main/java/org/rumbledb/cli/Main.java +++ b/src/main/java/org/rumbledb/cli/Main.java @@ -25,8 +25,10 @@ import org.apache.commons.io.IOUtils; import org.apache.spark.SparkException; import org.rumbledb.config.RumbleRuntimeConfiguration; +import org.rumbledb.exceptions.ExceptionMetadata; import org.rumbledb.exceptions.OurBadException; import org.rumbledb.exceptions.RumbleException; +import org.rumbledb.exceptions.UnexpectedTypeException; import org.rumbledb.server.RumbleServer; import org.rumbledb.shell.RumbleJLineShell; @@ -81,29 +83,22 @@ public static void main(String[] args) throws IOException { } private static void handleException(Throwable ex, boolean showErrorInfo) { + ex = unboxException(ex); if (ex != null) { - if (ex instanceof SparkException) { - Throwable sparkExceptionCause = ex.getCause(); - if (sparkExceptionCause != null) { - handleException(sparkExceptionCause, showErrorInfo); - } else { - if (showErrorInfo) { - ex.printStackTrace(); - } - handleException( - new OurBadException( - "There was a problem with Spark, but Spark did not provide any cause or stracktrace. The message from Spark is: " - + ex.getMessage() - ), - showErrorInfo - ); - } - } else if (ex instanceof RumbleException && !(ex instanceof OurBadException)) { + if (ex instanceof RumbleException && !(ex instanceof OurBadException)) { System.err.println("⚠️ ️" + ex.getMessage()); if (showErrorInfo) { ex.printStackTrace(); } System.exit(42); + } else if (ex instanceof NumberFormatException) { + handleException( + new UnexpectedTypeException( + "A cast failed. However, since this happened in a native Spark SQL execution, we cannot show you where. You can get more information on this type error by retrying with --native-execution no.", + ExceptionMetadata.EMPTY_METADATA + ), + showErrorInfo + ); } else if (ex instanceof OutOfMemoryError) { System.err.println( "⚠️ Java went out of memory." @@ -174,6 +169,29 @@ private static void handleException(Throwable ex, boolean showErrorInfo) { } } + public static Throwable unboxException(Throwable ex) { + if (ex != null) { + if (ex instanceof SparkException) { + Throwable sparkExceptionCause = ex.getCause(); + if (sparkExceptionCause != null) { + return sparkExceptionCause; + } + return new OurBadException( + "There was a problem with Spark, but Spark did not provide any cause or stracktrace. The message from Spark is: " + + ex.getMessage() + ); + } + if (ex instanceof NumberFormatException) { + return new UnexpectedTypeException( + "A cast failed. However, since this happened in a native Spark SQL execution, we cannot show you where. You can get more information on this type error by retrying with --native-execution no.", + ExceptionMetadata.EMPTY_METADATA + ); + } + return ex; + } + return new OurBadException("A null exception was returned."); + } + private static void runQueryExecutor(RumbleRuntimeConfiguration sparksoniqConf) throws IOException { JsoniqQueryExecutor translator = new JsoniqQueryExecutor(sparksoniqConf); translator.runQuery(); diff --git a/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java b/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java index 4ccb01259d..3fc8cb51bf 100644 --- a/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java +++ b/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java @@ -37,8 +37,6 @@ import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.RuntimeIterator; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.BuiltinTypesCatalogue; -import org.rumbledb.types.ItemType; import org.rumbledb.types.SequenceType; import org.rumbledb.types.SequenceType.Arity; @@ -438,7 +436,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!this.rightIterator.getStaticType().getArity().equals(Arity.One)) { return NativeClauseContext.NoNativeQuery; } - ItemType resultType = null; String leftQuery = leftResult.getResultingQuery(); String rightQuery = rightResult.getResultingQuery(); if ( @@ -449,7 +446,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { rightQuery = "(CAST (" + rightQuery + " AS DOUBLE))"; } - resultType = BuiltinTypesCatalogue.doubleItem; } else if ( this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) && @@ -458,7 +454,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { leftQuery = "(CAST (" + leftQuery + " AS DOUBLE))"; } - resultType = BuiltinTypesCatalogue.doubleItem; } else if ( this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) && @@ -467,7 +462,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { rightQuery = "(CAST (" + rightQuery + " AS FLOAT))"; } - resultType = BuiltinTypesCatalogue.floatItem; } else if ( this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) && @@ -476,19 +470,16 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { leftQuery = "(CAST (" + leftQuery + " AS FLOAT))"; } - resultType = BuiltinTypesCatalogue.floatItem; } else if ( this.leftIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) && this.rightIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) ) { - resultType = BuiltinTypesCatalogue.integerItem; } else if ( this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) && this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) ) { - resultType = BuiltinTypesCatalogue.decimalItem; } else { return NativeClauseContext.NoNativeQuery; } diff --git a/src/main/java/org/rumbledb/runtime/arithmetics/MultiplicativeOperationIterator.java b/src/main/java/org/rumbledb/runtime/arithmetics/MultiplicativeOperationIterator.java index 42ae4d2b58..212950eae2 100644 --- a/src/main/java/org/rumbledb/runtime/arithmetics/MultiplicativeOperationIterator.java +++ b/src/main/java/org/rumbledb/runtime/arithmetics/MultiplicativeOperationIterator.java @@ -39,8 +39,6 @@ import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.RuntimeIterator; import org.rumbledb.runtime.flwor.NativeClauseContext; -import org.rumbledb.types.BuiltinTypesCatalogue; -import org.rumbledb.types.ItemType; import org.rumbledb.types.SequenceType; import org.rumbledb.types.SequenceType.Arity; @@ -561,7 +559,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!this.rightIterator.getStaticType().getArity().equals(Arity.One)) { return NativeClauseContext.NoNativeQuery; } - ItemType resultType = null; String leftQuery = leftResult.getResultingQuery(); String rightQuery = rightResult.getResultingQuery(); if ( @@ -572,7 +569,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { rightQuery = "(CAST (" + rightQuery + " AS DOUBLE))"; } - resultType = BuiltinTypesCatalogue.doubleItem; } else if ( this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) && @@ -581,7 +577,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { leftQuery = "(CAST (" + leftQuery + " AS DOUBLE))"; } - resultType = BuiltinTypesCatalogue.doubleItem; } else if ( this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) && @@ -590,7 +585,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { rightQuery = "(CAST (" + rightQuery + " AS FLOAT))"; } - resultType = BuiltinTypesCatalogue.floatItem; } else if ( this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) && @@ -599,23 +593,16 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { leftQuery = "(CAST (" + leftQuery + " AS FLOAT))"; } - resultType = BuiltinTypesCatalogue.floatItem; } else if ( this.leftIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) && this.rightIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) ) { - if (this.multiplicativeOperator.equals(MultiplicativeExpression.MultiplicativeOperator.DIV)) { - resultType = BuiltinTypesCatalogue.decimalItem; - } else { - resultType = BuiltinTypesCatalogue.integerItem; - } } else if ( this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) && this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) ) { - resultType = BuiltinTypesCatalogue.decimalItem; } else { return NativeClauseContext.NoNativeQuery; } diff --git a/src/main/java/org/rumbledb/runtime/primary/ContextExpressionIterator.java b/src/main/java/org/rumbledb/runtime/primary/ContextExpressionIterator.java index 4fc866ce23..14e6feabf9 100644 --- a/src/main/java/org/rumbledb/runtime/primary/ContextExpressionIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/ContextExpressionIterator.java @@ -21,7 +21,6 @@ package org.rumbledb.runtime.primary; import org.apache.spark.sql.types.DataType; -import org.apache.spark.sql.types.StructField; import org.apache.spark.sql.types.StructType; import org.rumbledb.api.Item; import org.rumbledb.context.DynamicContext; @@ -80,10 +79,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC if (!FlworDataFrameUtils.isVariableAvailableAsNativeItem(structSchema, Name.CONTEXT_ITEM)) { return NativeClauseContext.NoNativeQuery; } - StructField field = structSchema.fields()[structSchema.fieldIndex( - SparkSessionManager.atomicJSONiqItemColumnName - )]; - DataType fieldType = field.dataType(); return new NativeClauseContext( nativeClauseContext, "`" + SparkSessionManager.atomicJSONiqItemColumnName + "`" diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index 432070d9d5..dc410707ad 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -812,30 +812,138 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC } String resultingQuery = ""; if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.booleanItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.doubleItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.floatItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .isSubtypeOf(BuiltinTypesCatalogue.decimalItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "BOOLEAN" + ")) "; System.err.println("Boolean"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.byteItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.doubleItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.floatItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .isSubtypeOf(BuiltinTypesCatalogue.decimalItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "BYTE" + ")) "; System.err.println("Byte"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.shortItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.doubleItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.floatItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .isSubtypeOf(BuiltinTypesCatalogue.decimalItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "SHORT" + ")) "; System.err.println("Short"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.doubleItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.floatItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .isSubtypeOf(BuiltinTypesCatalogue.decimalItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "INT" + ")) "; System.err.println("Int"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.longItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.doubleItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.floatItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .isSubtypeOf(BuiltinTypesCatalogue.decimalItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "LONG" + ")) "; System.err.println("Long"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.integerItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.doubleItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.floatItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .isSubtypeOf(BuiltinTypesCatalogue.decimalItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DECIMAL(38,0)" + ")) "; System.err.println("Integer"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.decimalItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.doubleItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.floatItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .isSubtypeOf(BuiltinTypesCatalogue.decimalItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DECIMAL(38,19)" + ")) "; System.err.println("Decimal"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.doubleItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.doubleItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.floatItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .isSubtypeOf(BuiltinTypesCatalogue.decimalItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DOUBLE" + ")) "; System.err.println("Double"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.floatItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.doubleItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.floatItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .isSubtypeOf(BuiltinTypesCatalogue.decimalItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "FLOAT" + ")) "; System.err.println("Float"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.stringItem)) { @@ -845,12 +953,26 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; System.err.println("anyURI"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.dateItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeItem) + ) { + return NativeClauseContext.NoNativeQuery; + } if (getConfiguration().dateWithTimezone()) { return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DATE" + ")) "; System.err.println("Date"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "TIMESTAMP" + ")) "; System.err.println("Timestamp"); /* @@ -858,9 +980,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC * resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "BINARY" + ")) "; * System.err.println("HexBinary"); */ - } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.nullItem)) { - resultingQuery = " NULL "; - System.err.println("Null"); } else { return NativeClauseContext.NoNativeQuery; } diff --git a/src/main/java/org/rumbledb/server/MainPageHandler.java b/src/main/java/org/rumbledb/server/MainPageHandler.java index 39045d7329..c1458d4c0c 100644 --- a/src/main/java/org/rumbledb/server/MainPageHandler.java +++ b/src/main/java/org/rumbledb/server/MainPageHandler.java @@ -9,7 +9,6 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; -@SuppressWarnings("restriction") public class MainPageHandler implements HttpHandler { public MainPageHandler() { diff --git a/src/main/java/org/rumbledb/server/RumbleHttpHandler.java b/src/main/java/org/rumbledb/server/RumbleHttpHandler.java index 5899742ed1..fa7ac8565d 100644 --- a/src/main/java/org/rumbledb/server/RumbleHttpHandler.java +++ b/src/main/java/org/rumbledb/server/RumbleHttpHandler.java @@ -28,7 +28,6 @@ import sparksoniq.spark.SparkSessionManager; -@SuppressWarnings("restriction") public class RumbleHttpHandler implements HttpHandler { private RumbleRuntimeConfiguration rumbleRuntimeConfiguration; diff --git a/src/main/java/org/rumbledb/server/RumbleServer.java b/src/main/java/org/rumbledb/server/RumbleServer.java index 2cf3797bc6..23fc13444e 100644 --- a/src/main/java/org/rumbledb/server/RumbleServer.java +++ b/src/main/java/org/rumbledb/server/RumbleServer.java @@ -10,7 +10,6 @@ import com.sun.net.httpserver.HttpContext; import com.sun.net.httpserver.HttpServer; -@SuppressWarnings("restriction") public class RumbleServer { private RumbleRuntimeConfiguration rumbleRuntimeConfiguration; diff --git a/src/main/java/org/rumbledb/server/ValidatorPageHandler.java b/src/main/java/org/rumbledb/server/ValidatorPageHandler.java index 8a98082802..9ecc9624a5 100644 --- a/src/main/java/org/rumbledb/server/ValidatorPageHandler.java +++ b/src/main/java/org/rumbledb/server/ValidatorPageHandler.java @@ -8,7 +8,6 @@ import java.io.IOException; import java.io.OutputStream; -@SuppressWarnings("restriction") public class ValidatorPageHandler implements HttpHandler { public ValidatorPageHandler() { diff --git a/src/main/java/org/rumbledb/shell/RumbleJLineShell.java b/src/main/java/org/rumbledb/shell/RumbleJLineShell.java index b6429b4420..37acb70294 100644 --- a/src/main/java/org/rumbledb/shell/RumbleJLineShell.java +++ b/src/main/java/org/rumbledb/shell/RumbleJLineShell.java @@ -22,7 +22,6 @@ import javassist.CannotCompileException; import org.apache.commons.io.IOUtils; -import org.apache.spark.SparkException; import org.jline.reader.EndOfFileException; import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; @@ -160,25 +159,10 @@ private void initialize() throws IOException { } private void handleException(Throwable ex, boolean showErrorInfo) { + ex = Main.unboxException(ex); if (ex != null) { if (ex instanceof EndOfFileException) { this.currentLine = RumbleJLineShell.EXIT_COMMAND; - } else if (ex instanceof SparkException) { - Throwable sparkExceptionCause = ex.getCause(); - if (sparkExceptionCause != null) { - handleException(sparkExceptionCause, showErrorInfo); - } else { - if (showErrorInfo) { - ex.printStackTrace(); - } - handleException( - new OurBadException( - "There was a problem with Spark, but Spark did not provide any cause or stracktrace. The message from Spark is: " - + ex.getMessage() - ), - showErrorInfo - ); - } } else if (ex instanceof RumbleException && !(ex instanceof OurBadException)) { System.err.println("⚠️ ️" + ex.getMessage()); if (showErrorInfo) { diff --git a/src/main/java/sparksoniq/spark/SparkSessionManager.java b/src/main/java/sparksoniq/spark/SparkSessionManager.java index 17923d698a..4e629433c1 100644 --- a/src/main/java/sparksoniq/spark/SparkSessionManager.java +++ b/src/main/java/sparksoniq/spark/SparkSessionManager.java @@ -126,6 +126,7 @@ private void setDefaultConfiguration() { this.configuration.setAppName(APP_NAME); } this.configuration.set("spark.sql.crossJoin.enabled", "true"); // enables cartesian product + this.configuration.set("spark.sql.ansi.enabled", "true"); // enables throwing errors in SPark SQL if (!this.configuration.contains("spark.master")) { this.configuration.set("spark.master", "local[*]"); } diff --git a/src/test/java/iq/RuntimeTests.java b/src/test/java/iq/RuntimeTests.java index 97f95ca3b1..ef3d00a6d4 100644 --- a/src/test/java/iq/RuntimeTests.java +++ b/src/test/java/iq/RuntimeTests.java @@ -81,6 +81,7 @@ public static void setupSparkSession() { sparkConfiguration.set("spark.executor.extraClassPath", "lib/"); sparkConfiguration.set("spark.driver.extraClassPath", "lib/"); sparkConfiguration.set("spark.sql.crossJoin.enabled", "true"); // enables cartesian product + sparkConfiguration.set("spark.sql.ansi.enabled", "true"); // prevents spark from failing to start on MacOS when disconnected from the internet sparkConfiguration.set("spark.driver.host", "127.0.0.1"); diff --git a/src/test/java/iq/base/AnnotationsTestsBase.java b/src/test/java/iq/base/AnnotationsTestsBase.java index 30d4353d19..dcfb4cf1d7 100644 --- a/src/test/java/iq/base/AnnotationsTestsBase.java +++ b/src/test/java/iq/base/AnnotationsTestsBase.java @@ -195,8 +195,17 @@ protected void testAnnotations(String path, RumbleRuntimeConfiguration configura ) { try { checkExpectedOutput(this.currentAnnotation.getOutput(), sequence); + } catch (RumbleException exception) { + String errorOutput = exception.getMessage(); + checkErrorCode( + errorOutput, + this.currentAnnotation.getErrorCode(), + this.currentAnnotation.getErrorMetadata() + ); + return; } catch (Exception exception) { String errorOutput = exception.getMessage(); + exception.printStackTrace(); checkErrorCode( errorOutput, this.currentAnnotation.getErrorCode(), From 832c210c28f66daedac4da8d9129f414505a7165 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Tue, 23 May 2023 15:05:57 +0200 Subject: [PATCH 09/42] Add tests. --- .../rumbledb/runtime/primary/BooleanRuntimeIterator.java | 9 +++++++++ .../java/org/rumbledb/runtime/typing/CastIterator.java | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/rumbledb/runtime/primary/BooleanRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/primary/BooleanRuntimeIterator.java index e60eac2b6a..5c0409d64b 100644 --- a/src/main/java/org/rumbledb/runtime/primary/BooleanRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/BooleanRuntimeIterator.java @@ -25,6 +25,7 @@ import org.rumbledb.context.RuntimeStaticContext; import org.rumbledb.items.ItemFactory; import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; +import org.rumbledb.runtime.flwor.NativeClauseContext; public class BooleanRuntimeIterator extends AtMostOneItemLocalRuntimeIterator { @@ -41,5 +42,13 @@ public BooleanRuntimeIterator(boolean value, RuntimeStaticContext staticContext) public Item materializeFirstItemOrNull(DynamicContext context) { return this.item; } + + @Override + public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { + return new NativeClauseContext( + nativeClauseContext, + item.getBooleanValue() ? "(TRUE)" : "(FALSE)" + ); + } } diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index dc410707ad..55821ff549 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -258,7 +258,7 @@ public static Item castItemToType(Item item, ItemType targetType, ExceptionMetad return result; } if (targetType.isSubtypeOf(BuiltinTypesCatalogue.intItem)) { - result = ItemFactory.getInstance().createIntItem(item.castToIntValue()); + result = ItemFactory.getInstance().createIntItem(result.castToIntValue()); if (targetType.equals(BuiltinTypesCatalogue.intItem)) { return result; } From 20e1857dba9020d414e959a289d492adecdf0e10 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 24 May 2023 08:25:20 +0200 Subject: [PATCH 10/42] Add tests. --- .../literals/castError1.jq | 5 + .../literals/castError2.jq | 4 + .../runtime-native-flwor/literals/types.jq | 128 ++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq create mode 100644 src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq create mode 100644 src/test/resources/test_files/runtime-native-flwor/literals/types.jq diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq b/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq new file mode 100644 index 0000000000..ddfeb87883 --- /dev/null +++ b/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq @@ -0,0 +1,5 @@ +(:JIQS: ShouldCrash; ErrorCode="FORG0006"; ErrorMetadata="LINE:4:COLUMN:18:" :) +for $i in parallelize((1 to 10), 10) +let $a := date("2023-01-01") +let $b := boolean($a) +return $b diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq b/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq new file mode 100644 index 0000000000..cc9edce41d --- /dev/null +++ b/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq @@ -0,0 +1,4 @@ +(:JIQS: ShouldCrash; ErrorCode="XPTY0004"; ErrorMetadata="LINE:1:COLUMN:0:" :) +for $i in parallelize((1 to 10), 10) +let $a := xs:boolean("falsea") +return $a diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq new file mode 100644 index 0000000000..c1c194ae1b --- /dev/null +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq @@ -0,0 +1,128 @@ +(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "float" : 1.23546789E13, "string" : "123465789", "anyURI" : "123465789", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "float" : 1.23546789E13, "string" : "123465789", "anyURI" : "123465789", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "hexBinary" : "1ABF", "null" : null })" :) +for $i in parallelize((1 to 2), 10) +let $boolean := boolean("false") +let $boolean := boolean(anyURI("false")) +let $boolean2 := boolean(integer(0)) +let $boolean3 := boolean(decimal(0)) +let $boolean4 := boolean(double(0)) +let $boolean5 := boolean(float(0)) +let $boolean6 := boolean("true") +let $boolean6 := boolean(anyURI("true")) +let $boolean7 := boolean(integer(1)) +let $boolean8 := boolean(decimal(1)) +let $boolean9 := boolean(double(1)) +let $boolean10 := boolean(float(1)) +let $byte := byte("12") +let $byte2 := byte(true) +let $byte3 := byte(false) +let $byte4 := byte(double(1)) +let $byte5 := byte(float(2)) +let $byte6 := byte(decimal(3)) +let $short := short("12345") +let $short2 := short(true) +let $short3 := short(false) +let $short4 := short(double(1)) +let $short5 := short(float(2)) +let $short6 := short(decimal(3)) +let $int := int($short) +let $int2 := int("122345") +let $int3 := int(true) +let $int4 := int(false) +let $int5 := int(double(1)) +let $int6 := int(float(2)) +let $int7 := int(decimal(3)) +let $long := long($int) +let $long2 := long($short) +let $long3 := long("123546789000000") +let $long4 := long(true) +let $long5 := long(false) +let $long6 := long(double(1)) +let $long7 := long(float(2)) +let $long8 := long(decimal(3)) +let $integer := integer("1235467890000001230498761230948") +let $integer2 := integer($int) +let $integer3 := integer(true) +let $integer4 := integer(false) +let $integer5 := integer(double(1)) +let $integer6 := integer(float(2)) +let $integer7 := integer(decimal(3)) +let $decimal := decimal("12354678900000.01230498761230948") +let $decimal2 := decimal($byte) +let $decimal3 := decimal($short) +let $decimal4 := decimal($int) +let $decimal5 := decimal(integer("12354678900000")) +let $decimal6 := decimal(true) +let $decimal7 := decimal(false) +let $decimal8 := decimal(double(1)) +let $decimal9 := decimal(float(2)) +let $double := double($decimal) +let $float := float($decimal) +let $string := string(123465789) +let $anyURI := anyURI(123465789) +let $date := date("2023-05-12") +let $dateTimeStamp := dateTimeStamp("2023-05-12T12:34:56") +let $hexBinary := hexBinary("1ABF") +let $null := "null" cast as null +return { + "boolean" : $boolean, + "boolean2" : $boolean2, + "boolean3" : $boolean3, + "boolean4" : $boolean4, + "boolean5" : $boolean5, + "boolean6" : $boolean6, + "boolean7" : $boolean7, + "boolean8" : $boolean8, + "boolean9" : $boolean9, + "boolean10" : $boolean10, + "byte" : $byte, + "byte2" : $byte2, + "byte3" : $byte3, + "byte4" : $byte4, + "byte5" : $byte5, + "byte6" : $byte6, + "short" : $short, + "short2" : $short2, + "short3" : $short3, + "short4" : $short4, + "short5" : $short5, + "short6" : $short6, + "int" : $int, + "int2" : $int2, + "int3" : $int3, + "int4" : $int4, + "int5" : $int5, + "int6" : $int6, + "int7" : $int7, + "long" : $long, + "long2" : $long2, + "long3" : $long3, + "long4" : $long4, + "long5" : $long5, + "long6" : $long6, + "long7" : $long7, + "long8" : $long8, + "integer" : $integer, + "integer2" : $integer2, + "integer3" : $integer3, + "integer4" : $integer4, + "integer5" : $integer5, + "integer6" : $integer6, + "integer7" : $integer7, + "decimal" : $decimal, + "decimal2" : $decimal2, + "decimal3" : $decimal3, + "decimal4" : $decimal4, + "decimal5" : $decimal5, + "decimal6" : $decimal6, + "decimal7" : $decimal7, + "decimal8" : $decimal8, + "decimal9" : $decimal9, + "double" : $double, + "float" : $float, + "string": $string, + "anyURI" : $anyURI, + "date" : $date, + "dateTimeStamp" : $dateTimeStamp, + "hexBinary" : $hexBinary, + "null" : $null +} From b616830f9655a009a72bab1b9125eef9751182c7 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 24 May 2023 08:28:17 +0200 Subject: [PATCH 11/42] Add tests --- .../runtime-native-flwor/literals/types.jq | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq index c1c194ae1b..e51726fa24 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "float" : 1.23546789E13, "string" : "123465789", "anyURI" : "123465789", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "float" : 1.23546789E13, "string" : "123465789", "anyURI" : "123465789", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "hexBinary" : "1ABF", "null" : null })" :) +(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "anyURI" : "123465789", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "anyURI" : "123465789", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "hexBinary" : "1ABF", "null" : null })" :) for $i in parallelize((1 to 2), 10) let $boolean := boolean("false") let $boolean := boolean(anyURI("false")) @@ -56,7 +56,25 @@ let $decimal7 := decimal(false) let $decimal8 := decimal(double(1)) let $decimal9 := decimal(float(2)) let $double := double($decimal) +let $double2 := double("1234.345e-2345") +let $double3 := double($byte) +let $double4 := double($short) +let $double5 := double($int) +let $double6 := double($long) +let $double7 := double($integer) +let $double8 := double($decimal) +let $double9 := double(true) +let $double10 := double(false) let $float := float($decimal) +let $float2 := float("1234.345e-2345") +let $float3 := float($byte) +let $float4 := float($short) +let $float5 := float($int) +let $float6 := float($long) +let $float7 := float($integer) +let $float8 := float($decimal) +let $float9 := float(true) +let $float10 := float(false) let $string := string(123465789) let $anyURI := anyURI(123465789) let $date := date("2023-05-12") @@ -118,7 +136,25 @@ return { "decimal8" : $decimal8, "decimal9" : $decimal9, "double" : $double, + "double2" : $double2, + "double3" : $double3, + "double4" : $double4, + "double5" : $double5, + "double6" : $double6, + "double7" : $double7, + "double8" : $double8, + "double9" : $double9, + "double10" : $double10, "float" : $float, + "float2" : $float2, + "float3" : $float3, + "float4" : $float4, + "float5" : $float5, + "float6" : $float6, + "float7" : $float7, + "float8" : $float8, + "float9" : $float9, + "float10" : $float10, "string": $string, "anyURI" : $anyURI, "date" : $date, From cfbabeb9190124d91c3c5445c75e44e5e4a1c5f6 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 24 May 2023 08:40:23 +0200 Subject: [PATCH 12/42] Add tests. --- .../rumbledb/runtime/typing/CastIterator.java | 8 +++++ .../runtime-native-flwor/literals/types.jq | 32 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index 55821ff549..ccd6f106e0 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -950,6 +950,12 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; System.err.println("String"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.anyURIItem)) { + if ( + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.anyURIItem) + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; System.err.println("anyURI"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.dateItem)) { @@ -957,6 +963,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateItem) && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem) ) { return NativeClauseContext.NoNativeQuery; } @@ -970,6 +977,7 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateItem) && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeItem) + && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem) ) { return NativeClauseContext.NoNativeQuery; } diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq index e51726fa24..a4133ec853 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "anyURI" : "123465789", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "anyURI" : "123465789", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "hexBinary" : "1ABF", "null" : null })" :) +(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 21:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "dateTimeStamp2" : "2023-05-12T00:00:00.000+0", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 21:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "dateTimeStamp2" : "2023-05-12T00:00:00.000+0", "hexBinary" : "1ABF", "null" : null })" :) for $i in parallelize((1 to 2), 10) let $boolean := boolean("false") let $boolean := boolean(anyURI("false")) @@ -76,9 +76,22 @@ let $float8 := float($decimal) let $float9 := float(true) let $float10 := float(false) let $string := string(123465789) -let $anyURI := anyURI(123465789) +let $string2 := string($byte) +let $string3 := string($short) +let $string4 := string($int) +let $string5 := string($long) +let $string6 := string($integer) +let $string7 := string($decimal) +let $string8 := string($double) +let $string9 := string($float) +let $string10 := string(date("2023-05-12")) +let $string11 := string(dateTimeStamp("2023-05-12T12:34:56-07:00")) +let $string12 := string(hexBinary("1ABF")) +let $anyURI := anyURI($string) let $date := date("2023-05-12") -let $dateTimeStamp := dateTimeStamp("2023-05-12T12:34:56") +let $date2 := date(dateTimeStamp("2023-05-12T12:34:56Z")) +let $dateTimeStamp := dateTimeStamp("2023-05-12T12:34:56+02:00") +let $dateTimeStamp2 := dateTimeStamp($date) let $hexBinary := hexBinary("1ABF") let $null := "null" cast as null return { @@ -156,9 +169,22 @@ return { "float9" : $float9, "float10" : $float10, "string": $string, + "string2": $string2, + "string3": $string3, + "string4": $string4, + "string5": $string5, + "string6": $string6, + "string7": $string7, + "string8": $string8, + "string9": $string9, + "string10": $string10, + "string11": $string11, + "string12": $string12, "anyURI" : $anyURI, "date" : $date, + "date2" : $date2, "dateTimeStamp" : $dateTimeStamp, + "dateTimeStamp2" : $dateTimeStamp2, "hexBinary" : $hexBinary, "null" : $null } From b7147e724e140437bb6ccba76c639e66ed8f55fd Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 24 May 2023 09:19:24 +0200 Subject: [PATCH 13/42] Spotless. --- .../rumbledb/runtime/typing/CastIterator.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index ccd6f106e0..7e4560850c 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -951,11 +951,11 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC System.err.println("String"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.anyURIItem)) { if ( - !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) + !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.anyURIItem) - ) { - return NativeClauseContext.NoNativeQuery; - } + ) { + return NativeClauseContext.NoNativeQuery; + } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; System.err.println("anyURI"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.dateItem)) { @@ -963,7 +963,10 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateItem) && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeItem) - && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .equals(BuiltinTypesCatalogue.dateTimeStampItem) ) { return NativeClauseContext.NoNativeQuery; } @@ -977,7 +980,10 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateItem) && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeItem) - && !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem) + && !this.children.get(0) + .getStaticType() + .getItemType() + .equals(BuiltinTypesCatalogue.dateTimeStampItem) ) { return NativeClauseContext.NoNativeQuery; } From 674d675c2b27627f0aea92cd8487707480fd4648 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 24 May 2023 09:32:30 +0200 Subject: [PATCH 14/42] Fix test. --- .../resources/test_files/runtime-native-flwor/literals/types.jq | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq index a4133ec853..844daac6ff 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 21:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "dateTimeStamp2" : "2023-05-12T00:00:00.000+0", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 21:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "dateTimeStamp2" : "2023-05-12T00:00:00.000+0", "hexBinary" : "1ABF", "null" : null })" :) +(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 19:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T10:34:56.", "dateTimeStamp2" : "2023-05-12T00:00:00.", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 19:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T10:34:56.", "dateTimeStamp2" : "2023-05-12T00:00:00.", "hexBinary" : "1ABF", "null" : null })" :) for $i in parallelize((1 to 2), 10) let $boolean := boolean("false") let $boolean := boolean(anyURI("false")) From ce1efd732712f2f01c6a09885c865510a2056d37 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 24 May 2023 13:41:48 +0200 Subject: [PATCH 15/42] Fix error codes and boolean cast. --- .../java/org/rumbledb/api/SequenceOfItems.java | 5 +++-- .../runtime/primary/BooleanRuntimeIterator.java | 2 +- .../org/rumbledb/runtime/typing/CastIterator.java | 14 ++++++++++++-- .../runtime-native-flwor/literals/castError1.jq | 2 +- .../runtime-native-flwor/literals/castError2.jq | 2 +- .../runtime-native-flwor/literals/types.jq | 2 +- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/rumbledb/api/SequenceOfItems.java b/src/main/java/org/rumbledb/api/SequenceOfItems.java index 823938e013..a2b8937cdf 100644 --- a/src/main/java/org/rumbledb/api/SequenceOfItems.java +++ b/src/main/java/org/rumbledb/api/SequenceOfItems.java @@ -7,6 +7,7 @@ import org.apache.spark.sql.Row; import org.rumbledb.config.RumbleRuntimeConfiguration; import org.rumbledb.context.DynamicContext; +import org.rumbledb.exceptions.CastException; import org.rumbledb.exceptions.ExceptionMetadata; import org.rumbledb.exceptions.RumbleException; import org.rumbledb.exceptions.UnexpectedTypeException; @@ -84,11 +85,11 @@ public boolean hasNext() { try { return this.iterator.hasNext(); } catch (NumberFormatException e) { - RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + RumbleException ex = new CastException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); ex.initCause(e); throw ex; } catch (UnsupportedOperationException e) { - RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + RumbleException ex = new CastException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); ex.initCause(e); throw ex; } diff --git a/src/main/java/org/rumbledb/runtime/primary/BooleanRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/primary/BooleanRuntimeIterator.java index 5c0409d64b..e98311595f 100644 --- a/src/main/java/org/rumbledb/runtime/primary/BooleanRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/BooleanRuntimeIterator.java @@ -47,7 +47,7 @@ public Item materializeFirstItemOrNull(DynamicContext context) { public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { return new NativeClauseContext( nativeClauseContext, - item.getBooleanValue() ? "(TRUE)" : "(FALSE)" + this.item.getBooleanValue() ? "(TRUE)" : "(FALSE)" ); } } diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index 7e4560850c..4c583e005a 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -144,8 +144,18 @@ public static Item castItemToType(Item item, ItemType targetType, ExceptionMetad if (StringUtils.isNumeric(item.getStringValue())) { result = ItemFactory.getInstance().createBooleanItem(item.castToIntValue() != 0); } else { - result = ItemFactory.getInstance() - .createBooleanItem(Boolean.parseBoolean(item.getStringValue().trim())); + switch (item.getStringValue()) { + case "true": + case "1": + result = ItemFactory.getInstance().createBooleanItem(true); + break; + case "false": + case "0": + result = ItemFactory.getInstance().createBooleanItem(false); + break; + default: + return null; + } } } else if (item.isInt()) { result = ItemFactory.getInstance().createBooleanItem(item.getIntValue() != 0); diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq b/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq index ddfeb87883..5b7f7b394f 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldCrash; ErrorCode="FORG0006"; ErrorMetadata="LINE:4:COLUMN:18:" :) +(:JIQS: ShouldCrash; ErrorCode="XPTY0004"; ErrorMetadata="LINE:4:COLUMN:18:" :) for $i in parallelize((1 to 10), 10) let $a := date("2023-01-01") let $b := boolean($a) diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq b/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq index cc9edce41d..26e2c32e29 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldCrash; ErrorCode="XPTY0004"; ErrorMetadata="LINE:1:COLUMN:0:" :) +(:JIQS: ShouldCrash; ErrorCode="FORG0001"; ErrorMetadata="LINE:1:COLUMN:0:" :) for $i in parallelize((1 to 10), 10) let $a := xs:boolean("falsea") return $a diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq index 844daac6ff..a4133ec853 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 19:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T10:34:56.", "dateTimeStamp2" : "2023-05-12T00:00:00.", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 19:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T10:34:56.", "dateTimeStamp2" : "2023-05-12T00:00:00.", "hexBinary" : "1ABF", "null" : null })" :) +(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 21:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "dateTimeStamp2" : "2023-05-12T00:00:00.000+0", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 21:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "dateTimeStamp2" : "2023-05-12T00:00:00.000+0", "hexBinary" : "1ABF", "null" : null })" :) for $i in parallelize((1 to 2), 10) let $boolean := boolean("false") let $boolean := boolean(anyURI("false")) From b88cb7237802f99cee0d0cb94f701b0cd2569750 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 24 May 2023 13:43:58 +0200 Subject: [PATCH 16/42] Fix test. --- .../test_files/runtime-native-flwor/literals/castError1.jq | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq b/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq index 5b7f7b394f..bab0e733c3 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/castError1.jq @@ -1,5 +1,5 @@ -(:JIQS: ShouldCrash; ErrorCode="XPTY0004"; ErrorMetadata="LINE:4:COLUMN:18:" :) +(:JIQS: ShouldCrash; ErrorCode="XPTY0004"; ErrorMetadata="LINE:4:COLUMN:10:" :) for $i in parallelize((1 to 10), 10) let $a := date("2023-01-01") -let $b := boolean($a) +let $b := xs:boolean($a) return $b From 03eb3b683e82a22dc20e2adb1384d6c45b3417af Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 24 May 2023 14:40:45 +0200 Subject: [PATCH 17/42] Fix tests. --- .../java/org/rumbledb/items/DateTimeItem.java | 15 ++++++++++----- .../org/rumbledb/items/DateTimeStampItem.java | 13 +++++++++---- .../runtime-native-flwor/literals/castError2.jq | 2 +- .../runtime-native-flwor/literals/types.jq | 2 +- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/rumbledb/items/DateTimeItem.java b/src/main/java/org/rumbledb/items/DateTimeItem.java index 30d4b41541..3794b8bbc4 100644 --- a/src/main/java/org/rumbledb/items/DateTimeItem.java +++ b/src/main/java/org/rumbledb/items/DateTimeItem.java @@ -95,13 +95,18 @@ public DateTime getDateTimeValue() { @Override public String getStringValue() { - String value = this.value.toString(); - String zoneString = this.value.getZone() == DateTimeZone.UTC - ? "Z" - : this.value.getZone().toString().equals(DateTimeZone.getDefault().toString()) + String value = this.value.toString(); + if(this.value.getZone() == DateTimeZone.UTC) + { + value = value.substring(0, value.length() - 1); + value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; + String zoneString = "Z"; + return value + zoneString; + } + String zoneString = this.value.getZone().toString().equals(DateTimeZone.getDefault().toString()) ? "" : value.substring(value.length() - 6); - value = value.substring(0, value.length() - zoneString.length()); + value = value.substring(0, value.length() - this.value.getZone().toString().length()); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; return value + (this.hasTimeZone ? zoneString : ""); } diff --git a/src/main/java/org/rumbledb/items/DateTimeStampItem.java b/src/main/java/org/rumbledb/items/DateTimeStampItem.java index ac833dce67..3c842910cc 100644 --- a/src/main/java/org/rumbledb/items/DateTimeStampItem.java +++ b/src/main/java/org/rumbledb/items/DateTimeStampItem.java @@ -61,12 +61,17 @@ public DateTime getDateTimeValue() { @Override public String getStringValue() { String value = this.value.toString(); - String zoneString = this.value.getZone() == DateTimeZone.UTC - ? "Z" - : this.value.getZone().toString().equals(DateTimeZone.getDefault().toString()) + if(this.value.getZone() == DateTimeZone.UTC) + { + value = value.substring(0, value.length() - 1); + value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; + String zoneString = "Z"; + return value + zoneString; + } + String zoneString = this.value.getZone().toString().equals(DateTimeZone.getDefault().toString()) ? "" : value.substring(value.length() - 6); - value = value.substring(0, value.length() - zoneString.length()); + value = value.substring(0, value.length() - this.value.getZone().toString().length()); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; return value + zoneString; } diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq b/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq index 26e2c32e29..0cb0698883 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/castError2.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldCrash; ErrorCode="FORG0001"; ErrorMetadata="LINE:1:COLUMN:0:" :) +(:JIQS: ShouldCrash; ErrorCode="FORG0001" :) for $i in parallelize((1 to 10), 10) let $a := xs:boolean("falsea") return $a diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq index a4133ec853..9bda6f9788 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 21:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "dateTimeStamp2" : "2023-05-12T00:00:00.000+0", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.0123049876123094800", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12 21:34:56", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56.000+0", "dateTimeStamp2" : "2023-05-12T00:00:00.000+0", "hexBinary" : "1ABF", "null" : null })" :) +(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.01230498761230948", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.01230498761230948", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z", "hexBinary" : "1ABF", "null" : null })" :) for $i in parallelize((1 to 2), 10) let $boolean := boolean("false") let $boolean := boolean(anyURI("false")) From ec877cf636adaa8afb6b2e27660daf92556c6ad2 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 24 May 2023 15:59:43 +0200 Subject: [PATCH 18/42] Fix serialization of time stamps. --- .../org/rumbledb/items/DateTimeStampItem.java | 13 +++++++++---- .../org/rumbledb/items/parsing/ItemParser.java | 2 +- .../flwor/clauses/ReturnClauseSparkIterator.java | 2 ++ .../runtime/flwor/udfs/DataFrameContext.java | 5 +++++ .../rumbledb/runtime/typing/CastIterator.java | 16 +++++++++++++++- .../java/org/rumbledb/types/TypeMappings.java | 2 +- 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/rumbledb/items/DateTimeStampItem.java b/src/main/java/org/rumbledb/items/DateTimeStampItem.java index 3c842910cc..82cc5d90cb 100644 --- a/src/main/java/org/rumbledb/items/DateTimeStampItem.java +++ b/src/main/java/org/rumbledb/items/DateTimeStampItem.java @@ -68,11 +68,16 @@ public String getStringValue() { String zoneString = "Z"; return value + zoneString; } - String zoneString = this.value.getZone().toString().equals(DateTimeZone.getDefault().toString()) - ? "" - : value.substring(value.length() - 6); - value = value.substring(0, value.length() - this.value.getZone().toString().length()); + if(this.value.getZone().toString().equals(DateTimeZone.getDefault().toString())) + { + value = value.substring(0, value.length() - 6); + value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; + String zoneString = ""; + return value + zoneString; + } + value = value.substring(0, value.length() - 6); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; + String zoneString = value.substring(value.length() - 6); return value + zoneString; } diff --git a/src/main/java/org/rumbledb/items/parsing/ItemParser.java b/src/main/java/org/rumbledb/items/parsing/ItemParser.java index e27256f5e9..d23eb7b83d 100644 --- a/src/main/java/org/rumbledb/items/parsing/ItemParser.java +++ b/src/main/java/org/rumbledb/items/parsing/ItemParser.java @@ -463,7 +463,7 @@ private static Item convertValueToItem( } Instant instant = value.toInstant(); DateTime dt = new DateTime(instant.toEpochMilli()); - Item item = ItemFactory.getInstance().createDateTimeItem(dt, false); + Item item = ItemFactory.getInstance().createDateTimeStampItem(dt, false); if (itemType == null || itemType.equals(BuiltinTypesCatalogue.dateTimeStampItem)) { return item; } else { diff --git a/src/main/java/org/rumbledb/runtime/flwor/clauses/ReturnClauseSparkIterator.java b/src/main/java/org/rumbledb/runtime/flwor/clauses/ReturnClauseSparkIterator.java index 21419ec60d..8e487afea5 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/clauses/ReturnClauseSparkIterator.java +++ b/src/main/java/org/rumbledb/runtime/flwor/clauses/ReturnClauseSparkIterator.java @@ -109,6 +109,8 @@ public JavaRDD getRDDAux(DynamicContext context) { return result; } Dataset df = this.child.getDataFrame(context).getDataFrame(); + df.show(); + df.printSchema(); StructType oldSchema = df.schema(); List UDFcolumns = FlworDataFrameUtils.getColumns( oldSchema, diff --git a/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java b/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java index b869df7ba6..3fbfe74177 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java +++ b/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java @@ -129,6 +129,11 @@ public void setFromRow(Row row, ItemType itemType) { } if (!column.isCount()) { List i = readColumnAsSequenceOfItems(row, itemType, columnIndex); + for(Item j : i) + { + System.err.println(j.getDynamicType()); + System.err.println(j.serialize()); + } this.context.getVariableValues() .addVariableValue( column.getVariableName(), diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index 4c583e005a..a27d66fe1f 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -957,7 +957,21 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "FLOAT" + ")) "; System.err.println("Float"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.stringItem)) { - resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; + if(this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem)) + { + resultingQuery = " (date_format(" + value.getResultingQuery() + ", \"yyyy-MM-dd'T'HH:mm:ss.SZZZZZ\")) "; + } else if(this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateItem)) + { + if (getConfiguration().dateWithTimezone()) { + return NativeClauseContext.NoNativeQuery; + } + resultingQuery = " (date_format(" + value.getResultingQuery() + ", 'yyyy-MM-dd')) "; + } else if(this.children.get(0).getStaticType().getItemType().isSubtypeOf(BuiltinTypesCatalogue.decimalItem)) + { + resultingQuery = " (format_number(" + value.getResultingQuery() + ", '#.###################')) "; + } else { + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; + } System.err.println("String"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.anyURIItem)) { if ( diff --git a/src/main/java/org/rumbledb/types/TypeMappings.java b/src/main/java/org/rumbledb/types/TypeMappings.java index e37bb117a6..74310c13bb 100644 --- a/src/main/java/org/rumbledb/types/TypeMappings.java +++ b/src/main/java/org/rumbledb/types/TypeMappings.java @@ -103,7 +103,7 @@ public static ItemType getItemTypeFromDataFrameDataType(DataType dataType) { return BuiltinTypesCatalogue.dateItem; } if (DataTypes.TimestampType.equals(dataType)) { - return BuiltinTypesCatalogue.dateTimeItem; + return BuiltinTypesCatalogue.dateTimeStampItem; } if (DataTypes.BinaryType.equals(dataType)) { return BuiltinTypesCatalogue.hexBinaryItem; From 69b8f1e628f5f3e44f988743dbd61604b4339a82 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Fri, 7 Jul 2023 14:11:54 +0200 Subject: [PATCH 19/42] Spotless. --- .../java/org/rumbledb/items/DateTimeItem.java | 9 +++---- .../org/rumbledb/items/DateTimeStampItem.java | 6 ++--- .../runtime/flwor/udfs/DataFrameContext.java | 7 +++--- .../rumbledb/runtime/typing/CastIterator.java | 25 +++++++++---------- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/rumbledb/items/DateTimeItem.java b/src/main/java/org/rumbledb/items/DateTimeItem.java index 3794b8bbc4..afe8b9d1d5 100644 --- a/src/main/java/org/rumbledb/items/DateTimeItem.java +++ b/src/main/java/org/rumbledb/items/DateTimeItem.java @@ -95,17 +95,16 @@ public DateTime getDateTimeValue() { @Override public String getStringValue() { - String value = this.value.toString(); - if(this.value.getZone() == DateTimeZone.UTC) - { + String value = this.value.toString(); + if (this.value.getZone() == DateTimeZone.UTC) { value = value.substring(0, value.length() - 1); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; String zoneString = "Z"; return value + zoneString; } String zoneString = this.value.getZone().toString().equals(DateTimeZone.getDefault().toString()) - ? "" - : value.substring(value.length() - 6); + ? "" + : value.substring(value.length() - 6); value = value.substring(0, value.length() - this.value.getZone().toString().length()); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; return value + (this.hasTimeZone ? zoneString : ""); diff --git a/src/main/java/org/rumbledb/items/DateTimeStampItem.java b/src/main/java/org/rumbledb/items/DateTimeStampItem.java index 82cc5d90cb..ffdfdd3b36 100644 --- a/src/main/java/org/rumbledb/items/DateTimeStampItem.java +++ b/src/main/java/org/rumbledb/items/DateTimeStampItem.java @@ -61,15 +61,13 @@ public DateTime getDateTimeValue() { @Override public String getStringValue() { String value = this.value.toString(); - if(this.value.getZone() == DateTimeZone.UTC) - { + if (this.value.getZone() == DateTimeZone.UTC) { value = value.substring(0, value.length() - 1); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; String zoneString = "Z"; return value + zoneString; } - if(this.value.getZone().toString().equals(DateTimeZone.getDefault().toString())) - { + if (this.value.getZone().toString().equals(DateTimeZone.getDefault().toString())) { value = value.substring(0, value.length() - 6); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; String zoneString = ""; diff --git a/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java b/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java index 3fbfe74177..e63dea776f 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java +++ b/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java @@ -129,10 +129,9 @@ public void setFromRow(Row row, ItemType itemType) { } if (!column.isCount()) { List i = readColumnAsSequenceOfItems(row, itemType, columnIndex); - for(Item j : i) - { - System.err.println(j.getDynamicType()); - System.err.println(j.serialize()); + for (Item j : i) { + System.err.println(j.getDynamicType()); + System.err.println(j.serialize()); } this.context.getVariableValues() .addVariableValue( diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index a27d66fe1f..ecbfba208e 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -957,21 +957,20 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "FLOAT" + ")) "; System.err.println("Float"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.stringItem)) { - if(this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem)) - { - resultingQuery = " (date_format(" + value.getResultingQuery() + ", \"yyyy-MM-dd'T'HH:mm:ss.SZZZZZ\")) "; - } else if(this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateItem)) - { - if (getConfiguration().dateWithTimezone()) { + if (this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem)) { + resultingQuery = " (date_format(" + value.getResultingQuery() + ", \"yyyy-MM-dd'T'HH:mm:ss.SZZZZZ\")) "; + } else if (this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateItem)) { + if (getConfiguration().dateWithTimezone()) { return NativeClauseContext.NoNativeQuery; } - resultingQuery = " (date_format(" + value.getResultingQuery() + ", 'yyyy-MM-dd')) "; - } else if(this.children.get(0).getStaticType().getItemType().isSubtypeOf(BuiltinTypesCatalogue.decimalItem)) - { - resultingQuery = " (format_number(" + value.getResultingQuery() + ", '#.###################')) "; - } else { - resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; - } + resultingQuery = " (date_format(" + value.getResultingQuery() + ", 'yyyy-MM-dd')) "; + } else if ( + this.children.get(0).getStaticType().getItemType().isSubtypeOf(BuiltinTypesCatalogue.decimalItem) + ) { + resultingQuery = " (format_number(" + value.getResultingQuery() + ", '#.###################')) "; + } else { + resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; + } System.err.println("String"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.anyURIItem)) { if ( From bbaf6bf9f2bd98666867ff83e9b3d128e0cfa4be Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Fri, 7 Jul 2023 14:26:28 +0200 Subject: [PATCH 20/42] Fix test. --- src/test/resources/test_files/runtime/Treat.jq | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/test_files/runtime/Treat.jq b/src/test/resources/test_files/runtime/Treat.jq index 06e9a8d4a7..dd1e98e138 100644 --- a/src/test/resources/test_files/runtime/Treat.jq +++ b/src/test/resources/test_files/runtime/Treat.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="(1, 2.14, 1, aa, 1, { "a" : "b" }, 1, 2, 3, false, null, [ 1, 2, 3 ], { "aa" : "bb" }, { "cc" : "dd" }, P3Y5M, P2Y4M, P10Y3M, P3DT5H6.001S, P21D, 2001-12-12T23:00:00, 2001-12-12-10:00, 13:20:30.555, AABBCC, 0FB80F+9, QQ==, mailto:rumble)" :) +(:JIQS: ShouldRun; Output="(1, 2.14, 1, aa, 1, { "a" : "b" }, 1, 2, 3, false, null, [ 1, 2, 3 ], { "aa" : "bb" }, { "cc" : "dd" }, P3Y5M, P2Y4M, P10Y3M, P3DT5H6.001S, P21D, 2001-12-12T23:00:00Z, 2001-12-12-10:00, 13:20:30.555, AABBCC, 0FB80F+9, QQ==, mailto:rumble)" :) 1 treat as integer, 2.14 treat as decimal, 1 treat as decimal, From 88c4a201194b0997d006a94c75238b5a2fb39f8a Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Fri, 7 Jul 2023 14:51:42 +0200 Subject: [PATCH 21/42] Fix tests. --- .../java/org/rumbledb/items/DateItem.java | 6 +++- .../java/org/rumbledb/items/DateTimeItem.java | 31 +++++++++++++------ .../java/org/rumbledb/items/TimeItem.java | 6 +++- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/rumbledb/items/DateItem.java b/src/main/java/org/rumbledb/items/DateItem.java index 6881e66052..4de170e81b 100644 --- a/src/main/java/org/rumbledb/items/DateItem.java +++ b/src/main/java/org/rumbledb/items/DateItem.java @@ -30,12 +30,16 @@ public DateItem() { DateItem(String dateTimeString) { this.value = DateTimeItem.parseDateTime(dateTimeString, BuiltinTypesCatalogue.dateItem); - if (!dateTimeString.endsWith("Z") && this.value.getZone() == DateTimeZone.getDefault()) { + if (doesLexicalValueHaveNoTimeZone(dateTimeString)) { this.hasTimeZone = false; this.value = this.value.withZoneRetainFields(DateTimeZone.UTC); } } + private static boolean doesLexicalValueHaveNoTimeZone(String dateTimeString) { + return DateTimeItem.DATE_NOTIMEZONE_PATTERN.matcher(dateTimeString).matches(); + } + @Override public boolean equals(Object otherItem) { if (otherItem instanceof Item) { diff --git a/src/main/java/org/rumbledb/items/DateTimeItem.java b/src/main/java/org/rumbledb/items/DateTimeItem.java index afe8b9d1d5..19279cd9a2 100644 --- a/src/main/java/org/rumbledb/items/DateTimeItem.java +++ b/src/main/java/org/rumbledb/items/DateTimeItem.java @@ -45,11 +45,17 @@ public class DateTimeItem implements Item { private static final String dateTimeStampLexicalRep = dateFrag + "T" + timeFrag + timezoneFrag; private static final String dateLexicalRep = "(" + dateFrag + "(" + timezoneFrag + ")?)"; private static final String timeLexicalRep = "(" + timeFrag + "(" + timezoneFrag + ")?)"; + private static final String dateTimeNoTimeZoneLexicalRep = dateFrag + "T" + timeFrag; + private static final String dateNoTimeZoneLexicalRep = dateFrag; + private static final String timeNoTimeZoneLexicalRep = timeFrag; - private static final Pattern dateTimePattern = Pattern.compile(dateTimeLexicalRep); - private static final Pattern dateTimeStampPattern = Pattern.compile(dateTimeStampLexicalRep); - private static final Pattern datePattern = Pattern.compile(dateLexicalRep); - private static final Pattern timePattern = Pattern.compile(timeLexicalRep); + public static final Pattern DATETIME_PATTERN = Pattern.compile(dateTimeLexicalRep); + public static final Pattern DATETIMESTAMP_PATTERN = Pattern.compile(dateTimeStampLexicalRep); + public static final Pattern DATE_PATTERN = Pattern.compile(dateLexicalRep); + public static final Pattern TIME_PATTERN = Pattern.compile(timeLexicalRep); + public static final Pattern DATETIME_NOTIMEZONE_PATTERN = Pattern.compile(dateTimeNoTimeZoneLexicalRep); + public static final Pattern DATE_NOTIMEZONE_PATTERN = Pattern.compile(dateNoTimeZoneLexicalRep); + public static final Pattern TIME_NOTIMEZONE_PATTERN = Pattern.compile(timeNoTimeZoneLexicalRep); private static final long serialVersionUID = 1L; @@ -68,12 +74,17 @@ public DateTimeItem() { DateTimeItem(String dateTimeString) { this.value = parseDateTime(dateTimeString, BuiltinTypesCatalogue.dateTimeItem); - if (!dateTimeString.endsWith("Z") && this.value.getZone() == DateTimeZone.getDefault()) { + if (doesLexicalValueHaveNoTimeZone(dateTimeString)) { + System.err.println("No time zone."); this.hasTimeZone = false; this.value = this.value.withZoneRetainFields(DateTimeZone.UTC); } } + private static boolean doesLexicalValueHaveNoTimeZone(String dateTimeString) { + return DATETIME_NOTIMEZONE_PATTERN.matcher(dateTimeString).matches(); + } + @Override public boolean equals(Object otherItem) { if (otherItem instanceof Item) { @@ -100,7 +111,7 @@ public String getStringValue() { value = value.substring(0, value.length() - 1); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; String zoneString = "Z"; - return value + zoneString; + return value + (this.hasTimeZone ? zoneString : ""); } String zoneString = this.value.getZone().toString().equals(DateTimeZone.getDefault().toString()) ? "" @@ -174,16 +185,16 @@ static DateTimeFormatter getDateTimeFormatter(ItemType dateTimeType) { static boolean checkInvalidDateTimeFormat(String dateTime, ItemType dateTimeType) { if (dateTimeType.equals(BuiltinTypesCatalogue.dateTimeStampItem)) { - return dateTimeStampPattern.matcher(dateTime).matches(); + return DATETIMESTAMP_PATTERN.matcher(dateTime).matches(); } if (dateTimeType.equals(BuiltinTypesCatalogue.dateTimeItem)) { - return dateTimePattern.matcher(dateTime).matches(); + return DATETIME_PATTERN.matcher(dateTime).matches(); } if (dateTimeType.equals(BuiltinTypesCatalogue.dateItem)) { - return datePattern.matcher(dateTime).matches(); + return DATE_PATTERN.matcher(dateTime).matches(); } if (dateTimeType.equals(BuiltinTypesCatalogue.timeItem)) { - return timePattern.matcher(dateTime).matches(); + return TIME_PATTERN.matcher(dateTime).matches(); } return false; } diff --git a/src/main/java/org/rumbledb/items/TimeItem.java b/src/main/java/org/rumbledb/items/TimeItem.java index 5b5ec05223..63cde4dd08 100644 --- a/src/main/java/org/rumbledb/items/TimeItem.java +++ b/src/main/java/org/rumbledb/items/TimeItem.java @@ -31,12 +31,16 @@ public TimeItem() { TimeItem(String dateTimeString) { this.value = DateTimeItem.parseDateTime(dateTimeString, BuiltinTypesCatalogue.timeItem); - if (!dateTimeString.endsWith("Z") && this.value.getZone() == DateTimeZone.getDefault()) { + if (doesLexicalValueHaveNoTimeZone(dateTimeString)) { this.hasTimeZone = false; this.value = this.value.withZoneRetainFields(DateTimeZone.UTC); } } + private static boolean doesLexicalValueHaveNoTimeZone(String dateTimeString) { + return DateTimeItem.TIME_NOTIMEZONE_PATTERN.matcher(dateTimeString).matches(); + } + @Override public boolean equals(Object otherItem) { if (otherItem instanceof Item) { From 52def99c4321ca89547a8106fdd42fc6b7818d2f Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Fri, 7 Jul 2023 15:36:34 +0200 Subject: [PATCH 22/42] Fix tests. --- .../java/org/rumbledb/items/DateTimeItem.java | 20 ++++++++++--------- .../org/rumbledb/items/DateTimeStampItem.java | 17 +++++----------- .../resources/test_files/runtime/Treat.jq | 2 +- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/rumbledb/items/DateTimeItem.java b/src/main/java/org/rumbledb/items/DateTimeItem.java index 19279cd9a2..6ef6783ea8 100644 --- a/src/main/java/org/rumbledb/items/DateTimeItem.java +++ b/src/main/java/org/rumbledb/items/DateTimeItem.java @@ -106,19 +106,21 @@ public DateTime getDateTimeValue() { @Override public String getStringValue() { - String value = this.value.toString(); + String originalValue = this.value.toString(); + if (!hasTimeZone()) { + String value = originalValue.substring(0, originalValue.length() - 1); + value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; + return value; + } if (this.value.getZone() == DateTimeZone.UTC) { - value = value.substring(0, value.length() - 1); + String value = originalValue.substring(0, originalValue.length() - 1); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; - String zoneString = "Z"; - return value + (this.hasTimeZone ? zoneString : ""); + return value + "Z"; } - String zoneString = this.value.getZone().toString().equals(DateTimeZone.getDefault().toString()) - ? "" - : value.substring(value.length() - 6); - value = value.substring(0, value.length() - this.value.getZone().toString().length()); + String zoneString = originalValue.substring(originalValue.length() - 6); + String value = originalValue.substring(0, originalValue.length() - 6); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; - return value + (this.hasTimeZone ? zoneString : ""); + return value + zoneString; } @Override diff --git a/src/main/java/org/rumbledb/items/DateTimeStampItem.java b/src/main/java/org/rumbledb/items/DateTimeStampItem.java index ffdfdd3b36..369380dee8 100644 --- a/src/main/java/org/rumbledb/items/DateTimeStampItem.java +++ b/src/main/java/org/rumbledb/items/DateTimeStampItem.java @@ -60,22 +60,15 @@ public DateTime getDateTimeValue() { @Override public String getStringValue() { - String value = this.value.toString(); + String originalValue = this.value.toString(); if (this.value.getZone() == DateTimeZone.UTC) { - value = value.substring(0, value.length() - 1); + String value = originalValue.substring(0, originalValue.length() - 1); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; - String zoneString = "Z"; - return value + zoneString; + return value + "Z"; } - if (this.value.getZone().toString().equals(DateTimeZone.getDefault().toString())) { - value = value.substring(0, value.length() - 6); - value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; - String zoneString = ""; - return value + zoneString; - } - value = value.substring(0, value.length() - 6); + String zoneString = originalValue.substring(originalValue.length() - 6); + String value = originalValue.substring(0, originalValue.length() - 6); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; - String zoneString = value.substring(value.length() - 6); return value + zoneString; } diff --git a/src/test/resources/test_files/runtime/Treat.jq b/src/test/resources/test_files/runtime/Treat.jq index dd1e98e138..06e9a8d4a7 100644 --- a/src/test/resources/test_files/runtime/Treat.jq +++ b/src/test/resources/test_files/runtime/Treat.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="(1, 2.14, 1, aa, 1, { "a" : "b" }, 1, 2, 3, false, null, [ 1, 2, 3 ], { "aa" : "bb" }, { "cc" : "dd" }, P3Y5M, P2Y4M, P10Y3M, P3DT5H6.001S, P21D, 2001-12-12T23:00:00Z, 2001-12-12-10:00, 13:20:30.555, AABBCC, 0FB80F+9, QQ==, mailto:rumble)" :) +(:JIQS: ShouldRun; Output="(1, 2.14, 1, aa, 1, { "a" : "b" }, 1, 2, 3, false, null, [ 1, 2, 3 ], { "aa" : "bb" }, { "cc" : "dd" }, P3Y5M, P2Y4M, P10Y3M, P3DT5H6.001S, P21D, 2001-12-12T23:00:00, 2001-12-12-10:00, 13:20:30.555, AABBCC, 0FB80F+9, QQ==, mailto:rumble)" :) 1 treat as integer, 2.14 treat as decimal, 1 treat as decimal, From f46ce378b843ef7ae88e46c6a82bd02f7e3616cf Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Fri, 7 Jul 2023 15:46:20 +0200 Subject: [PATCH 23/42] Fix tests. --- .../java/org/rumbledb/items/DateItem.java | 4 +++ .../java/org/rumbledb/items/DateTimeItem.java | 4 +++ .../java/org/rumbledb/items/TimeItem.java | 25 +++++++++++++++---- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/rumbledb/items/DateItem.java b/src/main/java/org/rumbledb/items/DateItem.java index 4de170e81b..56764d6237 100644 --- a/src/main/java/org/rumbledb/items/DateItem.java +++ b/src/main/java/org/rumbledb/items/DateItem.java @@ -26,6 +26,10 @@ public DateItem() { super(); this.value = value; this.hasTimeZone = hasTimeZone; + if (!hasTimeZone) { + this.value = this.value.withZoneRetainFields(DateTimeZone.UTC); + } + } DateItem(String dateTimeString) { diff --git a/src/main/java/org/rumbledb/items/DateTimeItem.java b/src/main/java/org/rumbledb/items/DateTimeItem.java index 6ef6783ea8..896f0978d3 100644 --- a/src/main/java/org/rumbledb/items/DateTimeItem.java +++ b/src/main/java/org/rumbledb/items/DateTimeItem.java @@ -70,6 +70,10 @@ public DateTimeItem() { super(); this.value = value; this.hasTimeZone = hasTimeZone; + if (!hasTimeZone) { + this.value = this.value.withZoneRetainFields(DateTimeZone.UTC); + } + } DateTimeItem(String dateTimeString) { diff --git a/src/main/java/org/rumbledb/items/TimeItem.java b/src/main/java/org/rumbledb/items/TimeItem.java index 63cde4dd08..d0f35fc747 100644 --- a/src/main/java/org/rumbledb/items/TimeItem.java +++ b/src/main/java/org/rumbledb/items/TimeItem.java @@ -27,6 +27,9 @@ public TimeItem() { super(); this.value = value; this.hasTimeZone = hasTimeZone; + if (!hasTimeZone) { + this.value = this.value.withZoneRetainFields(DateTimeZone.UTC); + } } TimeItem(String dateTimeString) { @@ -87,12 +90,24 @@ public int hashCode() { @Override public String getStringValue() { - String value = this.value.toString(); - String zoneString = this.value.getZone() == DateTimeZone.UTC ? "Z" : value.substring(value.length() - 6); - value = value.substring(0, value.length() - zoneString.length()); + String originalValue = this.value.toString(); + if (!hasTimeZone()) { + String value = originalValue.substring(0, originalValue.length() - 1); + value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; + value = value.substring(value.indexOf("T") + 1);; + return value; + } + if (this.value.getZone() == DateTimeZone.UTC) { + String value = originalValue.substring(0, originalValue.length() - 1); + value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; + value = value.substring(value.indexOf("T") + 1);; + return value + "Z"; + } + String zoneString = originalValue.substring(originalValue.length() - 6); + String value = originalValue.substring(0, originalValue.length() - 6); value = this.value.getMillisOfSecond() == 0 ? value.substring(0, value.length() - 4) : value; - int dateTimeSeparatorIndex = value.indexOf("T"); - return value.substring(dateTimeSeparatorIndex + 1) + (this.hasTimeZone ? zoneString : ""); + value = value.substring(value.indexOf("T") + 1);; + return value + zoneString; } @Override From 89fba0d30d1f53c949e30164491e1e0790f03a64 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 28 Feb 2024 18:02:06 +0100 Subject: [PATCH 24/42] Fix function serialization. --- .../java/org/rumbledb/items/FunctionItem.java | 53 +++++++++++-------- .../runtime/flwor/udfs/DataFrameContext.java | 10 ++-- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/rumbledb/items/FunctionItem.java b/src/main/java/org/rumbledb/items/FunctionItem.java index fa0cbc5b85..f254afdcdb 100644 --- a/src/main/java/org/rumbledb/items/FunctionItem.java +++ b/src/main/java/org/rumbledb/items/FunctionItem.java @@ -30,6 +30,7 @@ import java.util.List; import java.util.Map; +import org.apache.commons.lang3.SerializationUtils; import org.apache.spark.api.java.JavaRDD; import org.apache.spark.ml.Estimator; import org.apache.spark.ml.Transformer; @@ -230,21 +231,24 @@ public int hashCode() { public void write(Kryo kryo, Output output) { kryo.writeObject(output, this.identifier); kryo.writeObject(output, this.parameterNames); - kryo.writeObject(output, this.signature.getParameterTypes()); - kryo.writeObject(output, this.signature.getReturnType()); - // kryo.writeObject(output, this.bodyIterator); - kryo.writeObject(output, this.localVariablesInClosure); - kryo.writeObject(output, this.RDDVariablesInClosure); - kryo.writeObject(output, this.dataFrameVariablesInClosure); - kryo.writeObject(output, this.dynamicModuleContext); + try { + byte[] data = SerializationUtils.serialize(this.signature); + output.writeInt(data.length); + output.writeBytes(data); + } catch (Exception e) { + throw new OurBadException( + "Error serializing signature:" + e.getMessage() + ); + } + + // kryo.writeObject(output, this.localVariablesInClosure); + // kryo.writeObject(output, this.RDDVariablesInClosure); + // kryo.writeObject(output, this.dataFrameVariablesInClosure); + // kryo.writeObject(output, this.dynamicModuleContext); // convert RuntimeIterator to byte[] data try { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(this.bodyIterator); - oos.flush(); - byte[] data = bos.toByteArray(); + byte[] data = SerializationUtils.serialize(this.bodyIterator); output.writeInt(data.length); output.writeBytes(data); } catch (Exception e) { @@ -259,21 +263,26 @@ public void write(Kryo kryo, Output output) { public void read(Kryo kryo, Input input) { this.identifier = kryo.readObject(input, FunctionIdentifier.class); this.parameterNames = kryo.readObject(input, ArrayList.class); - List parameters = kryo.readObject(input, ArrayList.class); - SequenceType returnType = kryo.readObject(input, SequenceType.class); - this.signature = new FunctionSignature(parameters, returnType); + try { + int dataLength = input.readInt(); + byte[] data = input.readBytes(dataLength); + this.signature = SerializationUtils.deserialize(data); + } catch (Exception e) { + e.printStackTrace(); + throw new OurBadException( + "Error deserializing parameter types:" + e.getMessage() + ); + } // this.bodyIterator = kryo.readObject(input, RuntimeIterator.class); - this.localVariablesInClosure = kryo.readObject(input, HashMap.class); - this.RDDVariablesInClosure = kryo.readObject(input, HashMap.class); - this.dataFrameVariablesInClosure = kryo.readObject(input, HashMap.class); - this.dynamicModuleContext = kryo.readObject(input, DynamicContext.class); + // this.localVariablesInClosure = kryo.readObject(input, HashMap.class); + // this.RDDVariablesInClosure = kryo.readObject(input, HashMap.class); + // this.dataFrameVariablesInClosure = kryo.readObject(input, HashMap.class); + // this.dynamicModuleContext = kryo.readObject(input, DynamicContext.class); try { int dataLength = input.readInt(); byte[] data = input.readBytes(dataLength); - ByteArrayInputStream bis = new ByteArrayInputStream(data); - ObjectInputStream ois = new ObjectInputStream(bis); - this.bodyIterator = (RuntimeIterator) ois.readObject(); + this.bodyIterator = SerializationUtils.deserialize(data); } catch (Exception e) { throw new OurBadException( "Error converting functionItem-bodyRuntimeIterator to functionItem:" + e.getMessage() diff --git a/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java b/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java index e63dea776f..48fec8f27f 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java +++ b/src/main/java/org/rumbledb/runtime/flwor/udfs/DataFrameContext.java @@ -129,10 +129,12 @@ public void setFromRow(Row row, ItemType itemType) { } if (!column.isCount()) { List i = readColumnAsSequenceOfItems(row, itemType, columnIndex); - for (Item j : i) { - System.err.println(j.getDynamicType()); - System.err.println(j.serialize()); - } + /* + * for (Item j : i) { + * System.err.println(j.getDynamicType()); + * System.err.println(j.serialize()); + * } + */ this.context.getVariableValues() .addVariableValue( column.getVariableName(), From e93150993a76704e5ea6cf2bcbac2698e69ac8e6 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Thu, 29 Feb 2024 16:39:53 +0100 Subject: [PATCH 25/42] Kryo serialization for sequence types and signatures. --- .../java/org/rumbledb/items/FunctionItem.java | 38 +++++++------------ .../org/rumbledb/types/FunctionSignature.java | 27 ++++++++++++- .../java/org/rumbledb/types/SequenceType.java | 19 +++++++++- 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/rumbledb/items/FunctionItem.java b/src/main/java/org/rumbledb/items/FunctionItem.java index f254afdcdb..a2c98fb281 100644 --- a/src/main/java/org/rumbledb/items/FunctionItem.java +++ b/src/main/java/org/rumbledb/items/FunctionItem.java @@ -231,19 +231,18 @@ public int hashCode() { public void write(Kryo kryo, Output output) { kryo.writeObject(output, this.identifier); kryo.writeObject(output, this.parameterNames); - try { - byte[] data = SerializationUtils.serialize(this.signature); - output.writeInt(data.length); - output.writeBytes(data); - } catch (Exception e) { + kryo.writeObject(output, this.signature); + kryo.writeObject(output, this.localVariablesInClosure); + if (!this.RDDVariablesInClosure.isEmpty()) { throw new OurBadException( - "Error serializing signature:" + e.getMessage() + "We do not support serializing RDDs in function closures." + ); + } + if (!this.dataFrameVariablesInClosure.isEmpty()) { + throw new OurBadException( + "We do not support serializing DataFrames in function closures." ); } - - // kryo.writeObject(output, this.localVariablesInClosure); - // kryo.writeObject(output, this.RDDVariablesInClosure); - // kryo.writeObject(output, this.dataFrameVariablesInClosure); // kryo.writeObject(output, this.dynamicModuleContext); // convert RuntimeIterator to byte[] data @@ -263,21 +262,12 @@ public void write(Kryo kryo, Output output) { public void read(Kryo kryo, Input input) { this.identifier = kryo.readObject(input, FunctionIdentifier.class); this.parameterNames = kryo.readObject(input, ArrayList.class); - try { - int dataLength = input.readInt(); - byte[] data = input.readBytes(dataLength); - this.signature = SerializationUtils.deserialize(data); - } catch (Exception e) { - e.printStackTrace(); - throw new OurBadException( - "Error deserializing parameter types:" + e.getMessage() - ); - } - // this.bodyIterator = kryo.readObject(input, RuntimeIterator.class); - // this.localVariablesInClosure = kryo.readObject(input, HashMap.class); - // this.RDDVariablesInClosure = kryo.readObject(input, HashMap.class); - // this.dataFrameVariablesInClosure = kryo.readObject(input, HashMap.class); + this.signature = kryo.readObject(input, FunctionSignature.class); + this.localVariablesInClosure = kryo.readObject(input, HashMap.class); + this.RDDVariablesInClosure = new HashMap<>(); + this.dataFrameVariablesInClosure = new HashMap<>(); // this.dynamicModuleContext = kryo.readObject(input, DynamicContext.class); + // this.bodyIterator = kryo.readObject(input, RuntimeIterator.class); try { int dataLength = input.readInt(); diff --git a/src/main/java/org/rumbledb/types/FunctionSignature.java b/src/main/java/org/rumbledb/types/FunctionSignature.java index be9defb075..bc7ca85973 100644 --- a/src/main/java/org/rumbledb/types/FunctionSignature.java +++ b/src/main/java/org/rumbledb/types/FunctionSignature.java @@ -21,9 +21,15 @@ package org.rumbledb.types; import java.io.Serializable; +import java.util.ArrayList; import java.util.List; -public class FunctionSignature implements Serializable { +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.KryoSerializable; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +public class FunctionSignature implements Serializable, KryoSerializable { private List parameterTypes; private SequenceType returnType; private static final long serialVersionUID = 1L; @@ -36,6 +42,11 @@ public FunctionSignature( this.returnType = returnType; } + public FunctionSignature() { + this.parameterTypes = new ArrayList<>(); + this.returnType = SequenceType.ITEM_STAR; + } + public List getParameterTypes() { return this.parameterTypes; @@ -91,4 +102,18 @@ public String toString() { sb.append(this.returnType); return sb.toString(); } + + + @Override + public void write(Kryo kryo, Output output) { + kryo.writeObject(output, this.parameterTypes); + kryo.writeObject(output, this.returnType); + } + + @SuppressWarnings("unchecked") + @Override + public void read(Kryo kryo, Input input) { + this.parameterTypes = kryo.readObject(input, ArrayList.class); + this.returnType = kryo.readObject(input, SequenceType.class); + } } diff --git a/src/main/java/org/rumbledb/types/SequenceType.java b/src/main/java/org/rumbledb/types/SequenceType.java index 30df1cd33f..836c88e66e 100644 --- a/src/main/java/org/rumbledb/types/SequenceType.java +++ b/src/main/java/org/rumbledb/types/SequenceType.java @@ -27,12 +27,17 @@ import org.rumbledb.exceptions.ExceptionMetadata; import org.rumbledb.exceptions.OurBadException; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.KryoSerializable; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + import java.io.Serializable; import java.util.Arrays; import java.util.HashMap; import java.util.Map; -public class SequenceType implements Serializable { +public class SequenceType implements Serializable, KryoSerializable { private static final long serialVersionUID = 1L; private ItemType itemType; @@ -738,5 +743,17 @@ public static SequenceType createSequenceType(String userFriendlyName) { } + @Override + public void write(Kryo kryo, Output output) { + // kryo.writeObject(output, this.itemType); + kryo.writeObject(output, this.arity); + } + + @SuppressWarnings("unchecked") + @Override + public void read(Kryo kryo, Input input) { + // this.itemType = kryo.readObject(input, ItemType.class); + this.arity = kryo.readObject(input, Arity.class); + } } From 7696551b167a0307368e33b4cee0751102e25f17 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Fri, 1 Mar 2024 18:18:23 +0100 Subject: [PATCH 26/42] Add Kryo serialization for item* ItemType. --- .../java/org/rumbledb/items/FunctionItem.java | 41 ++++++++++--------- .../runtime/flwor/FlworDataFrameUtils.java | 2 + .../java/org/rumbledb/types/ItemItemType.java | 14 +++++++ .../java/org/rumbledb/types/ItemType.java | 22 +++++++++- .../java/org/rumbledb/types/SequenceType.java | 4 +- 5 files changed, 61 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/rumbledb/items/FunctionItem.java b/src/main/java/org/rumbledb/items/FunctionItem.java index a2c98fb281..4e0413abf0 100644 --- a/src/main/java/org/rumbledb/items/FunctionItem.java +++ b/src/main/java/org/rumbledb/items/FunctionItem.java @@ -30,7 +30,6 @@ import java.util.List; import java.util.Map; -import org.apache.commons.lang3.SerializationUtils; import org.apache.spark.api.java.JavaRDD; import org.apache.spark.ml.Estimator; import org.apache.spark.ml.Transformer; @@ -246,15 +245,17 @@ public void write(Kryo kryo, Output output) { // kryo.writeObject(output, this.dynamicModuleContext); // convert RuntimeIterator to byte[] data - try { - byte[] data = SerializationUtils.serialize(this.bodyIterator); - output.writeInt(data.length); - output.writeBytes(data); - } catch (Exception e) { - throw new OurBadException( - "Error converting functionItem-bodyRuntimeIterator to byte[]:" + e.getMessage() - ); - } + /* + * try { + * byte[] data = SerializationUtils.serialize(this.bodyIterator); + * output.writeInt(data.length); + * output.writeBytes(data); + * } catch (Exception e) { + * throw new OurBadException( + * "Error converting functionItem-bodyRuntimeIterator to byte[]:" + e.getMessage() + * ); + * } + */ } @SuppressWarnings("unchecked") @@ -269,15 +270,17 @@ public void read(Kryo kryo, Input input) { // this.dynamicModuleContext = kryo.readObject(input, DynamicContext.class); // this.bodyIterator = kryo.readObject(input, RuntimeIterator.class); - try { - int dataLength = input.readInt(); - byte[] data = input.readBytes(dataLength); - this.bodyIterator = SerializationUtils.deserialize(data); - } catch (Exception e) { - throw new OurBadException( - "Error converting functionItem-bodyRuntimeIterator to functionItem:" + e.getMessage() - ); - } + /* + * try { + * int dataLength = input.readInt(); + * byte[] data = input.readBytes(dataLength); + * this.bodyIterator = SerializationUtils.deserialize(data); + * } catch (Exception e) { + * throw new OurBadException( + * "Error converting functionItem-bodyRuntimeIterator to functionItem:" + e.getMessage() + * ); + * } + */ } @Override diff --git a/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java b/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java index 6144043968..776f010b8a 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java +++ b/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java @@ -80,6 +80,7 @@ import org.rumbledb.items.structured.JSoundDataFrame; import org.rumbledb.runtime.flwor.FlworDataFrameColumn.ColumnFormat; import org.rumbledb.types.ItemType; +import org.rumbledb.types.ItemItemType; import org.rumbledb.types.SequenceType; import com.esotericsoftware.kryo.Kryo; @@ -130,6 +131,7 @@ public static void registerKryoClassesKryo(Kryo kryo) { kryo.register(SequenceType.class); kryo.register(SequenceType.Arity.class); kryo.register(ItemType.class); + kryo.register(ItemItemType.class); kryo.register(ArrayList.class); diff --git a/src/main/java/org/rumbledb/types/ItemItemType.java b/src/main/java/org/rumbledb/types/ItemItemType.java index 83c31125af..cecb58174c 100644 --- a/src/main/java/org/rumbledb/types/ItemItemType.java +++ b/src/main/java/org/rumbledb/types/ItemItemType.java @@ -3,6 +3,10 @@ import org.rumbledb.config.RumbleRuntimeConfiguration; import org.rumbledb.context.Name; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + import java.util.Set; /** @@ -79,4 +83,14 @@ public boolean isResolved() { public boolean isCompatibleWithDataFrames(RumbleRuntimeConfiguration configuration) { return false; } + + @Override + public void write(Kryo kryo, Output output) { + kryo.writeObject(output, this.name); + } + + @Override + public void read(Kryo kryo, Input input) { + this.name = kryo.readObject(input, Name.class); + } } diff --git a/src/main/java/org/rumbledb/types/ItemType.java b/src/main/java/org/rumbledb/types/ItemType.java index 101c5978a6..15842e171f 100644 --- a/src/main/java/org/rumbledb/types/ItemType.java +++ b/src/main/java/org/rumbledb/types/ItemType.java @@ -27,13 +27,19 @@ import org.rumbledb.context.Name; import org.rumbledb.context.StaticContext; import org.rumbledb.exceptions.ExceptionMetadata; +import org.rumbledb.exceptions.OurBadException; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.KryoSerializable; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; import java.io.Serializable; import java.util.List; import java.util.Map; import java.util.Set; -public interface ItemType extends Serializable { +public interface ItemType extends Serializable, KryoSerializable { long serialVersionUID = 1L; @@ -464,4 +470,18 @@ default void resolve(DynamicContext context, ExceptionMetadata metadata) { default void resolve(StaticContext context, ExceptionMetadata metadata) { return; } + + @Override + default void write(Kryo kryo, Output output) { + throw new OurBadException( + "Kryo serialization for type " + this.getClass().getCanonicalName() + " not implemented." + ); + } + + @Override + default void read(Kryo kryo, Input input) { + throw new OurBadException( + "Kryo deserialization for type " + this.getClass().getCanonicalName() + " not implemented." + ); + } } diff --git a/src/main/java/org/rumbledb/types/SequenceType.java b/src/main/java/org/rumbledb/types/SequenceType.java index 836c88e66e..47db9f8d9e 100644 --- a/src/main/java/org/rumbledb/types/SequenceType.java +++ b/src/main/java/org/rumbledb/types/SequenceType.java @@ -745,14 +745,14 @@ public static SequenceType createSequenceType(String userFriendlyName) { @Override public void write(Kryo kryo, Output output) { - // kryo.writeObject(output, this.itemType); + kryo.writeClassAndObject(output, this.itemType); kryo.writeObject(output, this.arity); } @SuppressWarnings("unchecked") @Override public void read(Kryo kryo, Input input) { - // this.itemType = kryo.readObject(input, ItemType.class); + this.itemType = (ItemType) kryo.readClassAndObject(input); this.arity = kryo.readObject(input, Arity.class); } From 52adf34e447720270b6cdcda19c8b61fda9a29c6 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Tue, 5 Mar 2024 14:23:10 +0100 Subject: [PATCH 27/42] Fix test. --- .../org/rumbledb/context/DynamicContext.java | 8 + .../context/RuntimeStaticContext.java | 30 +- .../exceptions/ExceptionMetadata.java | 42 +- .../java/org/rumbledb/items/FunctionItem.java | 35 +- .../AtMostOneItemLocalRuntimeIterator.java | 19 + .../runtime/HybridRuntimeIterator.java | 6 + .../org/rumbledb/runtime/RuntimeIterator.java | 11 + .../AdditiveOperationIterator.java | 808 ++++++++---------- .../AtMostOneItemIfRuntimeIterator.java | 15 + .../runtime/flwor/FlworDataFrameUtils.java | 10 +- .../primary/VariableReferenceIterator.java | 23 + .../org/rumbledb/types/AtomicItemType.java | 26 + .../java/org/rumbledb/types/SequenceType.java | 1 - 13 files changed, 552 insertions(+), 482 deletions(-) diff --git a/src/main/java/org/rumbledb/context/DynamicContext.java b/src/main/java/org/rumbledb/context/DynamicContext.java index a7850a4076..a1075bf94a 100644 --- a/src/main/java/org/rumbledb/context/DynamicContext.java +++ b/src/main/java/org/rumbledb/context/DynamicContext.java @@ -121,13 +121,21 @@ public VariableValues getVariableValues() { @Override public void write(Kryo kryo, Output output) { kryo.writeObjectOrNull(output, this.parent, DynamicContext.class); + kryo.writeObject(output, this.conf); kryo.writeObject(output, this.variableValues); + // kryo.writeObject(output, this.namedFunctions); + kryo.writeObject(output, this.inScopeSchemaTypes); + kryo.writeObject(output, this.currentDateTime.getMillis()); } @Override public void read(Kryo kryo, Input input) { this.parent = kryo.readObjectOrNull(input, DynamicContext.class); + this.conf = kryo.readObject(input, RumbleRuntimeConfiguration.class); this.variableValues = kryo.readObject(input, VariableValues.class); + this.namedFunctions = new NamedFunctions(this.conf); + this.inScopeSchemaTypes= kryo.readObject(input, InScopeSchemaTypes.class); + this.currentDateTime = new DateTime(kryo.readObject(input, Long.class)); } public enum VariableDependency { diff --git a/src/main/java/org/rumbledb/context/RuntimeStaticContext.java b/src/main/java/org/rumbledb/context/RuntimeStaticContext.java index e5cce1f65d..c8f0a73761 100644 --- a/src/main/java/org/rumbledb/context/RuntimeStaticContext.java +++ b/src/main/java/org/rumbledb/context/RuntimeStaticContext.java @@ -8,7 +8,12 @@ import org.rumbledb.expressions.ExecutionMode; import org.rumbledb.types.SequenceType; -public class RuntimeStaticContext implements Serializable { +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.KryoSerializable; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +public class RuntimeStaticContext implements Serializable, KryoSerializable { private static final long serialVersionUID = 1L; private RumbleRuntimeConfiguration configuration; @@ -16,6 +21,13 @@ public class RuntimeStaticContext implements Serializable { private ExecutionMode executionMode; private ExceptionMetadata metadata; + public RuntimeStaticContext() { + this.configuration = null; + this.staticType = null; + this.executionMode = null; + this.metadata = null; + } + public RuntimeStaticContext( RumbleRuntimeConfiguration configuration, SequenceType staticType, @@ -62,4 +74,20 @@ public ExceptionMetadata getMetadata() { return this.metadata; } + @Override + public void write(Kryo kryo, Output output) { + kryo.writeObject(output, this.configuration); + kryo.writeObject(output, this.staticType); + kryo.writeObject(output, this.executionMode); + kryo.writeObject(output, this.metadata); + } + + @Override + public void read(Kryo kryo, Input input) { + this.configuration = kryo.readObject(input, RumbleRuntimeConfiguration.class); + this.staticType = kryo.readObject(input, SequenceType.class); + this.executionMode = kryo.readObject(input, ExecutionMode.class); + this.metadata = kryo.readObject(input, ExceptionMetadata.class); + } + } diff --git a/src/main/java/org/rumbledb/exceptions/ExceptionMetadata.java b/src/main/java/org/rumbledb/exceptions/ExceptionMetadata.java index d4401e2b3d..e59acfb733 100644 --- a/src/main/java/org/rumbledb/exceptions/ExceptionMetadata.java +++ b/src/main/java/org/rumbledb/exceptions/ExceptionMetadata.java @@ -22,20 +22,36 @@ import java.io.Serializable; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.KryoSerializable; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + /** * Metadata for error reporting (line and column number) * * @author Stefan Irimescu, Ghislain Fourny */ -public class ExceptionMetadata implements Serializable { +public class ExceptionMetadata implements Serializable, KryoSerializable { + + private String location; + private int tokenLineNumber; + private int tokenColumnNumber; + private String code; private static final long serialVersionUID = 1L; - private final String location; - private final int tokenLineNumber; - private final int tokenColumnNumber; - private final String code; public static final ExceptionMetadata EMPTY_METADATA = new ExceptionMetadata("none", 1, 0, ""); + /** + * Builds a new empty metadata object (for serialization and deserialization only) + */ + public ExceptionMetadata() { + this.location = ""; + this.tokenLineNumber = -1; + this.tokenColumnNumber = -1; + this.code = ""; + } + /** * Builds a new metadata object * @@ -118,4 +134,20 @@ public String toString() { + getTokenColumnNumber() + ":"; } + + @Override + public void write(Kryo kryo, Output output) { + kryo.writeObject(output, this.location); + kryo.writeObject(output, this.tokenLineNumber); + kryo.writeObject(output, this.tokenColumnNumber); + kryo.writeObject(output, this.code); + } + + @Override + public void read(Kryo kryo, Input input) { + this.location = kryo.readObject(input, String.class); + this.tokenLineNumber= kryo.readObject(input, Integer.class); + this.tokenColumnNumber = kryo.readObject(input, Integer.class); + this.code = kryo.readObject(input, String.class); + } } diff --git a/src/main/java/org/rumbledb/items/FunctionItem.java b/src/main/java/org/rumbledb/items/FunctionItem.java index 4e0413abf0..7ce4a6f325 100644 --- a/src/main/java/org/rumbledb/items/FunctionItem.java +++ b/src/main/java/org/rumbledb/items/FunctionItem.java @@ -57,10 +57,9 @@ public class FunctionItem implements Item { private static final long serialVersionUID = 1L; + private FunctionIdentifier identifier; private List parameterNames; - - // signature contains type information for all parameters and the return value private FunctionSignature signature; private RuntimeIterator bodyIterator; private DynamicContext dynamicModuleContext; @@ -242,20 +241,8 @@ public void write(Kryo kryo, Output output) { "We do not support serializing DataFrames in function closures." ); } - // kryo.writeObject(output, this.dynamicModuleContext); - - // convert RuntimeIterator to byte[] data - /* - * try { - * byte[] data = SerializationUtils.serialize(this.bodyIterator); - * output.writeInt(data.length); - * output.writeBytes(data); - * } catch (Exception e) { - * throw new OurBadException( - * "Error converting functionItem-bodyRuntimeIterator to byte[]:" + e.getMessage() - * ); - * } - */ + kryo.writeObject(output, this.dynamicModuleContext); + kryo.writeClassAndObject(output, this.bodyIterator); } @SuppressWarnings("unchecked") @@ -267,20 +254,8 @@ public void read(Kryo kryo, Input input) { this.localVariablesInClosure = kryo.readObject(input, HashMap.class); this.RDDVariablesInClosure = new HashMap<>(); this.dataFrameVariablesInClosure = new HashMap<>(); - // this.dynamicModuleContext = kryo.readObject(input, DynamicContext.class); - // this.bodyIterator = kryo.readObject(input, RuntimeIterator.class); - - /* - * try { - * int dataLength = input.readInt(); - * byte[] data = input.readBytes(dataLength); - * this.bodyIterator = SerializationUtils.deserialize(data); - * } catch (Exception e) { - * throw new OurBadException( - * "Error converting functionItem-bodyRuntimeIterator to functionItem:" + e.getMessage() - * ); - * } - */ + this.dynamicModuleContext = kryo.readObject(input, DynamicContext.class); + this.bodyIterator = (RuntimeIterator) kryo.readClassAndObject(input); } @Override diff --git a/src/main/java/org/rumbledb/runtime/AtMostOneItemLocalRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/AtMostOneItemLocalRuntimeIterator.java index 325561efaa..d5462c7bb1 100644 --- a/src/main/java/org/rumbledb/runtime/AtMostOneItemLocalRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/AtMostOneItemLocalRuntimeIterator.java @@ -33,6 +33,10 @@ import org.rumbledb.expressions.comparison.ComparisonExpression.ComparisonOperator; import org.rumbledb.types.BuiltinTypesCatalogue; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + import sparksoniq.spark.SparkSessionManager; import org.rumbledb.runtime.misc.ComparisonIterator; @@ -47,6 +51,11 @@ public abstract class AtMostOneItemLocalRuntimeIterator extends RuntimeIterator private static final long serialVersionUID = 1L; private Item result; + protected AtMostOneItemLocalRuntimeIterator() { + super(); + this.result = null; + } + protected AtMostOneItemLocalRuntimeIterator( List children, RuntimeStaticContext staticContext @@ -186,4 +195,14 @@ public boolean getEffectiveBooleanValueOrCheckPosition(DynamicContext dynamicCon getMetadata() ); } + + @Override + public void write(Kryo kryo, Output output) { + super.write(kryo, output); + } + + @Override + public void read(Kryo kryo, Input input) { + super.read(kryo, input); + } } diff --git a/src/main/java/org/rumbledb/runtime/HybridRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/HybridRuntimeIterator.java index 131742fe57..13217126ce 100644 --- a/src/main/java/org/rumbledb/runtime/HybridRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/HybridRuntimeIterator.java @@ -43,6 +43,12 @@ public abstract class HybridRuntimeIterator extends RuntimeIterator { protected List result = null; private int currentResultIndex = 0; + protected HybridRuntimeIterator() { + super(); + this.result = null; + this.currentResultIndex = 0; + } + protected HybridRuntimeIterator( List children, RuntimeStaticContext staticContext diff --git a/src/main/java/org/rumbledb/runtime/RuntimeIterator.java b/src/main/java/org/rumbledb/runtime/RuntimeIterator.java index 429b88cc6e..436ccaf911 100644 --- a/src/main/java/org/rumbledb/runtime/RuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/RuntimeIterator.java @@ -72,6 +72,15 @@ public abstract class RuntimeIterator implements RuntimeIteratorInterface, KryoS protected URI staticURI; // private StaticContext staticContext; + public RuntimeIterator() { + this.hasNext = false; + this.isOpen = false; + this.children = null; + this.currentDynamicContextForLocalExecution = null; + this.staticContext = null; + this.staticURI = null; + } + protected RuntimeIterator(List children, RuntimeStaticContext staticContext) { this.staticContext = staticContext; if (this.staticContext.getStaticType() == null) { @@ -220,6 +229,7 @@ public void reset(DynamicContext context) { @Override public void write(Kryo kryo, Output output) { + kryo.writeObject(output, this.staticContext); kryo.writeObject(output, this.children); // TODO serializer other fields } @@ -230,6 +240,7 @@ public void read(Kryo kryo, Input input) { this.hasNext = false; this.isOpen = false; this.currentDynamicContextForLocalExecution = null; + this.staticContext = kryo.readObject(input, RuntimeStaticContext.class); this.children = kryo.readObject(input, ArrayList.class); // TODO serializer other fields } diff --git a/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java b/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java index 3fc8cb51bf..deaa5242db 100644 --- a/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java +++ b/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java @@ -23,12 +23,14 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; +import java.util.Set; import org.joda.time.DateTime; import org.joda.time.Period; import org.joda.time.PeriodType; import org.rumbledb.api.Item; import org.rumbledb.context.DynamicContext; +import org.rumbledb.context.Name; import org.rumbledb.context.RuntimeStaticContext; import org.rumbledb.exceptions.MoreThanOneItemException; import org.rumbledb.exceptions.NonAtomicKeyException; @@ -40,470 +42,388 @@ import org.rumbledb.types.SequenceType; import org.rumbledb.types.SequenceType.Arity; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; public class AdditiveOperationIterator extends AtMostOneItemLocalRuntimeIterator { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - private Item left; - private Item right; - private boolean isMinus; - private RuntimeIterator leftIterator; - private RuntimeIterator rightIterator; + private Item left; + private Item right; + private boolean isMinus; + private RuntimeIterator leftIterator; + private RuntimeIterator rightIterator; - public AdditiveOperationIterator( - RuntimeIterator leftIterator, - RuntimeIterator rightIterator, - boolean isMinus, - RuntimeStaticContext staticContext - ) { - super(Arrays.asList(leftIterator, rightIterator), staticContext); - this.leftIterator = leftIterator; - this.rightIterator = rightIterator; - this.isMinus = isMinus; - } + public AdditiveOperationIterator() { + super(); + this.leftIterator = null; + this.rightIterator = null; + this.isMinus = false; + } - public Item materializeFirstItemOrNull( - DynamicContext dynamicContext - ) { - try { - this.left = this.leftIterator.materializeAtMostOneItemOrNull(dynamicContext); - } catch (MoreThanOneItemException e) { - throw new UnexpectedTypeException( - "Addition expression requires at most one item in its left input sequence.", - getMetadata() - ); - } - try { - this.right = this.rightIterator.materializeAtMostOneItemOrNull(dynamicContext); - } catch (MoreThanOneItemException e) { - throw new UnexpectedTypeException( - "Addition expression requires at most one item in its right input sequence.", - getMetadata() - ); - } + public AdditiveOperationIterator(RuntimeIterator leftIterator, RuntimeIterator rightIterator, boolean isMinus, + RuntimeStaticContext staticContext) { + super(Arrays.asList(leftIterator, rightIterator), staticContext); + this.leftIterator = leftIterator; + this.rightIterator = rightIterator; + this.isMinus = isMinus; + } - // if left or right equals empty sequence, return empty sequence - if (this.left == null || this.right == null) { - return null; - } - if (!this.left.isAtomic()) { - String message = String.format( - "Can not atomize an %1$s item: an %1$s has probably been passed where " - + - "an atomic value is expected (e.g., as a key, or to a function expecting an atomic item)", - this.left.getDynamicType().toString() - ); - throw new NonAtomicKeyException(message, getMetadata()); - } - if (!this.right.isAtomic()) { - String message = String.format( - "Can not atomize an %1$s item: an %1$s has probably been passed where " - + - "an atomic value is expected (e.g., as a key, or to a function expecting an atomic item)", - this.right.getDynamicType().toString() - ); - throw new NonAtomicKeyException(message, getMetadata()); - } - Item result = processItem(this.left, this.right, this.isMinus); - if (result == null) { - throw new UnexpectedTypeException( - " \"+\": operation not possible with parameters of type \"" - + this.left.getDynamicType().toString() - + "\" and \"" - + this.right.getDynamicType().toString() - + "\"", - getMetadata() - ); - } - return result; - } + public Item materializeFirstItemOrNull(DynamicContext dynamicContext) { + try { + this.left = this.leftIterator.materializeAtMostOneItemOrNull(dynamicContext); + } catch (MoreThanOneItemException e) { + throw new UnexpectedTypeException( + "Addition expression requires at most one item in its left input sequence.", getMetadata()); + } + try { + this.right = this.rightIterator.materializeAtMostOneItemOrNull(dynamicContext); + } catch (MoreThanOneItemException e) { + throw new UnexpectedTypeException( + "Addition expression requires at most one item in its right input sequence.", getMetadata()); + } - public static Item processItem( - Item left, - Item right, - boolean isMinus - ) { - // The integer 0 is considered the default neutral element for addition in sum(), even though - // it is technically incompatible with durations. In the future, we should - // make sure an error is thrown if an actual 0 appears in the sum with durations. - if (!isMinus && left.isInteger() && left.getIntegerValue().equals(BigInteger.ZERO)) { - return right; - } - if (!isMinus && right.isInteger() && right.getIntegerValue().equals(BigInteger.ZERO)) { - return left; - } - if ( - left.isInt() - && right.isInt() - ) { - if ( - right.isInt() - && (left.getIntValue() < Integer.MAX_VALUE / 2 - && left.getIntValue() > -Integer.MAX_VALUE / 2 - && right.getIntValue() > -Integer.MAX_VALUE / 2 - && right.getIntValue() < Integer.MAX_VALUE / 2) - ) { - return processInt(left.getIntValue(), right.getIntValue(), isMinus); - } - } + // if left or right equals empty sequence, return empty sequence + if (this.left == null || this.right == null) { + return null; + } + if (!this.left.isAtomic()) { + String message = String.format( + "Can not atomize an %1$s item: an %1$s has probably been passed where " + + "an atomic value is expected (e.g., as a key, or to a function expecting an atomic item)", + this.left.getDynamicType().toString()); + throw new NonAtomicKeyException(message, getMetadata()); + } + if (!this.right.isAtomic()) { + String message = String.format( + "Can not atomize an %1$s item: an %1$s has probably been passed where " + + "an atomic value is expected (e.g., as a key, or to a function expecting an atomic item)", + this.right.getDynamicType().toString()); + throw new NonAtomicKeyException(message, getMetadata()); + } + Item result = processItem(this.left, this.right, this.isMinus); + if (result == null) { + throw new UnexpectedTypeException( + " \"+\": operation not possible with parameters of type \"" + this.left.getDynamicType().toString() + + "\" and \"" + this.right.getDynamicType().toString() + "\"", + getMetadata()); + } + return result; + } - // General cases - if (left.isDouble() && right.isNumeric()) { - double l = left.getDoubleValue(); - double r = 0; - if (right.isDouble()) { - r = right.getDoubleValue(); - } else { - r = right.castToDoubleValue(); - } - return processDouble(l, r, isMinus); - } - if (right.isDouble() && left.isNumeric()) { - double l = left.castToDoubleValue(); - double r = right.getDoubleValue(); - return processDouble(l, r, isMinus); - } - if (left.isFloat() && right.isNumeric()) { - float l = left.getFloatValue(); - float r = 0; - if (right.isFloat()) { - r = right.getFloatValue(); - } else { - r = right.castToFloatValue(); - } - return processFloat(l, r, isMinus); - } - if (right.isFloat() && left.isNumeric()) { - float l = left.castToFloatValue(); - float r = right.getFloatValue(); - return processFloat(l, r, isMinus); - } - if (left.isInteger() && right.isInteger()) { - BigInteger l = left.getIntegerValue(); - BigInteger r = right.getIntegerValue(); - return processInteger(l, r, isMinus); - } - if (left.isDecimal() && right.isDecimal()) { - BigDecimal l = left.getDecimalValue(); - BigDecimal r = right.getDecimalValue(); - return processDecimal(l, r, isMinus); - } - if (left.isYearMonthDuration() && right.isYearMonthDuration()) { - Period l = left.getDurationValue(); - Period r = right.getDurationValue(); - return processYearMonthDuration(l, r, isMinus); - } - if (left.isDayTimeDuration() && right.isDayTimeDuration()) { - Period l = left.getDurationValue(); - Period r = right.getDurationValue(); - return processDayTimeDuration(l, r, isMinus); - } - if (left.isDate() && right.isYearMonthDuration()) { - DateTime l = left.getDateTimeValue(); - Period r = right.getDurationValue(); - return processDateTimeDurationDate(l, r, isMinus, left.hasTimeZone()); - } - if (left.isDate() && right.isDayTimeDuration()) { - DateTime l = left.getDateTimeValue(); - Period r = right.getDurationValue(); - return processDateTimeDurationDate(l, r, isMinus, left.hasTimeZone()); - } - if (left.isYearMonthDuration() && right.isDate()) { - if (!isMinus) { - Period l = left.getDurationValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDurationDate(r, l, isMinus, right.hasTimeZone()); - } - } - if (left.isDayTimeDuration() && right.isDate()) { - if (!isMinus) { - Period l = left.getDurationValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDurationDate(r, l, isMinus, right.hasTimeZone()); - } - } - if (left.isTime() && right.isDayTimeDuration()) { - DateTime l = left.getDateTimeValue(); - Period r = right.getDurationValue(); - return processDateTimeDurationTime(l, r, isMinus, left.hasTimeZone()); - } - if (left.isDayTimeDuration() && right.isTime()) { - if (!isMinus) { - Period l = left.getDurationValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDurationTime(r, l, isMinus, right.hasTimeZone()); - } - } - if (left.isDateTime() && right.isYearMonthDuration()) { - DateTime l = left.getDateTimeValue(); - Period r = right.getDurationValue(); - return processDateTimeDurationDateTime(l, r, isMinus, left.hasTimeZone()); - } - if (left.isDateTime() && right.isDayTimeDuration()) { - DateTime l = left.getDateTimeValue(); - Period r = right.getDurationValue(); - return processDateTimeDurationDateTime(l, r, isMinus, left.hasTimeZone()); - } - if (left.isYearMonthDuration() && right.isDateTime()) { - if (!isMinus) { - Period l = left.getDurationValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDurationDateTime(r, l, isMinus, right.hasTimeZone()); - } - } - if (left.isDayTimeDuration() && right.isDateTime()) { - if (!isMinus) { - Period l = left.getDurationValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDurationDateTime(r, l, isMinus, right.hasTimeZone()); - } - } - if (left.isDate() && right.isDate()) { - if (isMinus) { - DateTime l = left.getDateTimeValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDayTime(l, r); - } - } - if (left.isTime() && right.isTime()) { - if (isMinus) { - DateTime l = left.getDateTimeValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDayTime(l, r); - } - } - if (left.isDateTime() && right.isDateTime()) { - if (isMinus) { - DateTime l = left.getDateTimeValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDayTime(l, r); - } - } - return null; - } + public static Item processItem(Item left, Item right, boolean isMinus) { + // The integer 0 is considered the default neutral element for addition in + // sum(), even though + // it is technically incompatible with durations. In the future, we should + // make sure an error is thrown if an actual 0 appears in the sum with + // durations. + if (!isMinus && left.isInteger() && left.getIntegerValue().equals(BigInteger.ZERO)) { + return right; + } + if (!isMinus && right.isInteger() && right.getIntegerValue().equals(BigInteger.ZERO)) { + return left; + } + if (left.isInt() && right.isInt()) { + if (right.isInt() && (left.getIntValue() < Integer.MAX_VALUE / 2 + && left.getIntValue() > -Integer.MAX_VALUE / 2 && right.getIntValue() > -Integer.MAX_VALUE / 2 + && right.getIntValue() < Integer.MAX_VALUE / 2)) { + return processInt(left.getIntValue(), right.getIntValue(), isMinus); + } + } - private static Item processDouble( - double l, - double r, - boolean isMinus - ) { - if (isMinus) { - return ItemFactory.getInstance().createDoubleItem(l - r); - } else { - return ItemFactory.getInstance().createDoubleItem(l + r); - } - } + // General cases + if (left.isDouble() && right.isNumeric()) { + double l = left.getDoubleValue(); + double r = 0; + if (right.isDouble()) { + r = right.getDoubleValue(); + } else { + r = right.castToDoubleValue(); + } + return processDouble(l, r, isMinus); + } + if (right.isDouble() && left.isNumeric()) { + double l = left.castToDoubleValue(); + double r = right.getDoubleValue(); + return processDouble(l, r, isMinus); + } + if (left.isFloat() && right.isNumeric()) { + float l = left.getFloatValue(); + float r = 0; + if (right.isFloat()) { + r = right.getFloatValue(); + } else { + r = right.castToFloatValue(); + } + return processFloat(l, r, isMinus); + } + if (right.isFloat() && left.isNumeric()) { + float l = left.castToFloatValue(); + float r = right.getFloatValue(); + return processFloat(l, r, isMinus); + } + if (left.isInteger() && right.isInteger()) { + BigInteger l = left.getIntegerValue(); + BigInteger r = right.getIntegerValue(); + return processInteger(l, r, isMinus); + } + if (left.isDecimal() && right.isDecimal()) { + BigDecimal l = left.getDecimalValue(); + BigDecimal r = right.getDecimalValue(); + return processDecimal(l, r, isMinus); + } + if (left.isYearMonthDuration() && right.isYearMonthDuration()) { + Period l = left.getDurationValue(); + Period r = right.getDurationValue(); + return processYearMonthDuration(l, r, isMinus); + } + if (left.isDayTimeDuration() && right.isDayTimeDuration()) { + Period l = left.getDurationValue(); + Period r = right.getDurationValue(); + return processDayTimeDuration(l, r, isMinus); + } + if (left.isDate() && right.isYearMonthDuration()) { + DateTime l = left.getDateTimeValue(); + Period r = right.getDurationValue(); + return processDateTimeDurationDate(l, r, isMinus, left.hasTimeZone()); + } + if (left.isDate() && right.isDayTimeDuration()) { + DateTime l = left.getDateTimeValue(); + Period r = right.getDurationValue(); + return processDateTimeDurationDate(l, r, isMinus, left.hasTimeZone()); + } + if (left.isYearMonthDuration() && right.isDate()) { + if (!isMinus) { + Period l = left.getDurationValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDurationDate(r, l, isMinus, right.hasTimeZone()); + } + } + if (left.isDayTimeDuration() && right.isDate()) { + if (!isMinus) { + Period l = left.getDurationValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDurationDate(r, l, isMinus, right.hasTimeZone()); + } + } + if (left.isTime() && right.isDayTimeDuration()) { + DateTime l = left.getDateTimeValue(); + Period r = right.getDurationValue(); + return processDateTimeDurationTime(l, r, isMinus, left.hasTimeZone()); + } + if (left.isDayTimeDuration() && right.isTime()) { + if (!isMinus) { + Period l = left.getDurationValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDurationTime(r, l, isMinus, right.hasTimeZone()); + } + } + if (left.isDateTime() && right.isYearMonthDuration()) { + DateTime l = left.getDateTimeValue(); + Period r = right.getDurationValue(); + return processDateTimeDurationDateTime(l, r, isMinus, left.hasTimeZone()); + } + if (left.isDateTime() && right.isDayTimeDuration()) { + DateTime l = left.getDateTimeValue(); + Period r = right.getDurationValue(); + return processDateTimeDurationDateTime(l, r, isMinus, left.hasTimeZone()); + } + if (left.isYearMonthDuration() && right.isDateTime()) { + if (!isMinus) { + Period l = left.getDurationValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDurationDateTime(r, l, isMinus, right.hasTimeZone()); + } + } + if (left.isDayTimeDuration() && right.isDateTime()) { + if (!isMinus) { + Period l = left.getDurationValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDurationDateTime(r, l, isMinus, right.hasTimeZone()); + } + } + if (left.isDate() && right.isDate()) { + if (isMinus) { + DateTime l = left.getDateTimeValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDayTime(l, r); + } + } + if (left.isTime() && right.isTime()) { + if (isMinus) { + DateTime l = left.getDateTimeValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDayTime(l, r); + } + } + if (left.isDateTime() && right.isDateTime()) { + if (isMinus) { + DateTime l = left.getDateTimeValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDayTime(l, r); + } + } + return null; + } - private static Item processFloat( - float l, - float r, - boolean isMinus - ) { - if (isMinus) { - return ItemFactory.getInstance().createFloatItem(l - r); - } else { - return ItemFactory.getInstance().createFloatItem(l + r); - } - } + private static Item processDouble(double l, double r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createDoubleItem(l - r); + } else { + return ItemFactory.getInstance().createDoubleItem(l + r); + } + } - private static Item processDecimal( - BigDecimal l, - BigDecimal r, - boolean isMinus - ) { - if (isMinus) { - return ItemFactory.getInstance().createDecimalItem(l.subtract(r)); - } else { - return ItemFactory.getInstance().createDecimalItem(l.add(r)); - } - } + private static Item processFloat(float l, float r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createFloatItem(l - r); + } else { + return ItemFactory.getInstance().createFloatItem(l + r); + } + } - private static Item processInteger( - BigInteger l, - BigInteger r, - boolean isMinus - ) { - if (isMinus) { - return ItemFactory.getInstance().createIntegerItem(l.subtract(r)); - } else { - return ItemFactory.getInstance().createIntegerItem(l.add(r)); - } - } + private static Item processDecimal(BigDecimal l, BigDecimal r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createDecimalItem(l.subtract(r)); + } else { + return ItemFactory.getInstance().createDecimalItem(l.add(r)); + } + } - private static Item processInt( - int l, - int r, - boolean isMinus - ) { - if (isMinus) { - return ItemFactory.getInstance().createIntItem(l - r); - } else { - return ItemFactory.getInstance().createIntItem(l + r); - } - } + private static Item processInteger(BigInteger l, BigInteger r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createIntegerItem(l.subtract(r)); + } else { + return ItemFactory.getInstance().createIntegerItem(l.add(r)); + } + } - private static Item processYearMonthDuration( - Period l, - Period r, - boolean isMinus - ) { - if (isMinus) { - return ItemFactory.getInstance().createYearMonthDurationItem(l.minus(r)); - } else { - return ItemFactory.getInstance().createYearMonthDurationItem(l.plus(r)); - } - } + private static Item processInt(int l, int r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createIntItem(l - r); + } else { + return ItemFactory.getInstance().createIntItem(l + r); + } + } - private static Item processDayTimeDuration( - Period l, - Period r, - boolean isMinus - ) { - if (isMinus) { - return ItemFactory.getInstance().createDayTimeDurationItem(l.minus(r)); - } else { - return ItemFactory.getInstance().createDayTimeDurationItem(l.plus(r)); - } - } + private static Item processYearMonthDuration(Period l, Period r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createYearMonthDurationItem(l.minus(r)); + } else { + return ItemFactory.getInstance().createYearMonthDurationItem(l.plus(r)); + } + } - private static Item processDateTimeDayTime( - DateTime l, - DateTime r - ) { - return ItemFactory.getInstance() - .createDayTimeDurationItem(new Period(r, l, PeriodType.dayTime())); - } + private static Item processDayTimeDuration(Period l, Period r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createDayTimeDurationItem(l.minus(r)); + } else { + return ItemFactory.getInstance().createDayTimeDurationItem(l.plus(r)); + } + } - private static Item processDateTimeDurationDate( - DateTime l, - Period r, - boolean isMinus, - boolean timeZone - ) { - if (isMinus) { - return ItemFactory.getInstance() - .createDateItem(l.minus(r), timeZone); - } else { - return ItemFactory.getInstance() - .createDateItem(l.plus(r), timeZone); - } - } + private static Item processDateTimeDayTime(DateTime l, DateTime r) { + return ItemFactory.getInstance().createDayTimeDurationItem(new Period(r, l, PeriodType.dayTime())); + } - private static Item processDateTimeDurationTime( - DateTime l, - Period r, - boolean isMinus, - boolean timeZone - ) { - if (isMinus) { - return ItemFactory.getInstance() - .createTimeItem(l.minus(r), timeZone); - } else { - return ItemFactory.getInstance() - .createTimeItem(l.plus(r), timeZone); - } - } + private static Item processDateTimeDurationDate(DateTime l, Period r, boolean isMinus, boolean timeZone) { + if (isMinus) { + return ItemFactory.getInstance().createDateItem(l.minus(r), timeZone); + } else { + return ItemFactory.getInstance().createDateItem(l.plus(r), timeZone); + } + } - private static Item processDateTimeDurationDateTime( - DateTime l, - Period r, - boolean isMinus, - boolean timeZone - ) { - if (isMinus) { - return ItemFactory.getInstance() - .createDateTimeItem(l.minus(r), timeZone); - } else { - return ItemFactory.getInstance() - .createDateTimeItem(l.plus(r), timeZone); - } - } + private static Item processDateTimeDurationTime(DateTime l, Period r, boolean isMinus, boolean timeZone) { + if (isMinus) { + return ItemFactory.getInstance().createTimeItem(l.minus(r), timeZone); + } else { + return ItemFactory.getInstance().createTimeItem(l.plus(r), timeZone); + } + } - @Override - public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { - NativeClauseContext leftResult = this.leftIterator.generateNativeQuery(nativeClauseContext); - if (leftResult == NativeClauseContext.NoNativeQuery) { - return NativeClauseContext.NoNativeQuery; - } - if (!this.leftIterator.getStaticType().getArity().equals(Arity.One)) { - return NativeClauseContext.NoNativeQuery; - } - NativeClauseContext rightResult = this.rightIterator.generateNativeQuery(nativeClauseContext); - if (rightResult == NativeClauseContext.NoNativeQuery) { - return NativeClauseContext.NoNativeQuery; - } - if (!this.rightIterator.getStaticType().getArity().equals(Arity.One)) { - return NativeClauseContext.NoNativeQuery; - } - String leftQuery = leftResult.getResultingQuery(); - String rightQuery = rightResult.getResultingQuery(); - if ( - this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) - && - this.rightIterator.getStaticType().getItemType().isNumeric() - ) { - if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { - rightQuery = "(CAST (" + rightQuery + " AS DOUBLE))"; - } - } else if ( - this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) - && - this.leftIterator.getStaticType().getItemType().isNumeric() - ) { - if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { - leftQuery = "(CAST (" + leftQuery + " AS DOUBLE))"; - } - } else if ( - this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) - && - this.rightIterator.getStaticType().getItemType().isNumeric() - ) { - if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { - rightQuery = "(CAST (" + rightQuery + " AS FLOAT))"; - } - } else if ( - this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) - && - this.leftIterator.getStaticType().getItemType().isNumeric() - ) { - if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { - leftQuery = "(CAST (" + leftQuery + " AS FLOAT))"; - } - } else if ( - this.leftIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) - && - this.rightIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) - ) { - } else if ( - this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) - && - this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) - ) { - } else { - return NativeClauseContext.NoNativeQuery; - } + private static Item processDateTimeDurationDateTime(DateTime l, Period r, boolean isMinus, boolean timeZone) { + if (isMinus) { + return ItemFactory.getInstance().createDateTimeItem(l.minus(r), timeZone); + } else { + return ItemFactory.getInstance().createDateTimeItem(l.plus(r), timeZone); + } + } - if (this.isMinus) { - String resultingQuery = "( " - + leftQuery - + " - " - + rightQuery - + " )"; - return new NativeClauseContext( - nativeClauseContext, - resultingQuery - ); - } else { - String resultingQuery = "( " - + leftQuery - + " + " - + rightQuery - + " )"; - return new NativeClauseContext( - nativeClauseContext, - resultingQuery - ); - } - } + @Override + public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { + NativeClauseContext leftResult = this.leftIterator.generateNativeQuery(nativeClauseContext); + if (leftResult == NativeClauseContext.NoNativeQuery) { + return NativeClauseContext.NoNativeQuery; + } + if (!this.leftIterator.getStaticType().getArity().equals(Arity.One)) { + return NativeClauseContext.NoNativeQuery; + } + NativeClauseContext rightResult = this.rightIterator.generateNativeQuery(nativeClauseContext); + if (rightResult == NativeClauseContext.NoNativeQuery) { + return NativeClauseContext.NoNativeQuery; + } + if (!this.rightIterator.getStaticType().getArity().equals(Arity.One)) { + return NativeClauseContext.NoNativeQuery; + } + String leftQuery = leftResult.getResultingQuery(); + String rightQuery = rightResult.getResultingQuery(); + if (this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) + && this.rightIterator.getStaticType().getItemType().isNumeric()) { + if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { + rightQuery = "(CAST (" + rightQuery + " AS DOUBLE))"; + } + } else if (this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) + && this.leftIterator.getStaticType().getItemType().isNumeric()) { + if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { + leftQuery = "(CAST (" + leftQuery + " AS DOUBLE))"; + } + } else if (this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) + && this.rightIterator.getStaticType().getItemType().isNumeric()) { + if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { + rightQuery = "(CAST (" + rightQuery + " AS FLOAT))"; + } + } else if (this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) + && this.leftIterator.getStaticType().getItemType().isNumeric()) { + if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { + leftQuery = "(CAST (" + leftQuery + " AS FLOAT))"; + } + } else if (this.leftIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) + && this.rightIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM)) { + } else if (this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) + && this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM)) { + } else { + return NativeClauseContext.NoNativeQuery; + } + + if (this.isMinus) { + String resultingQuery = "( " + leftQuery + " - " + rightQuery + " )"; + return new NativeClauseContext(nativeClauseContext, resultingQuery); + } else { + String resultingQuery = "( " + leftQuery + " + " + rightQuery + " )"; + return new NativeClauseContext(nativeClauseContext, resultingQuery); + } + } + + @Override + public void write(Kryo kryo, Output output) { + super.write(kryo, output); + kryo.writeClassAndObject(output, this.left); + kryo.writeClassAndObject(output, this.right); + kryo.writeObject(output, this.isMinus); + kryo.writeClassAndObject(output, this.leftIterator); + kryo.writeClassAndObject(output, this.rightIterator); + } + + @Override + public void read(Kryo kryo, Input input) { + super.read(kryo, input); + this.left = (Item) kryo.readClassAndObject(input); + this.right = (Item) kryo.readClassAndObject(input); + this.isMinus = kryo.readObject(input, Boolean.class); + this.leftIterator = (RuntimeIterator) kryo.readClassAndObject(input); + this.rightIterator = (RuntimeIterator) kryo.readClassAndObject(input); + } } diff --git a/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java index 1fc7cb348d..7910e9dc44 100644 --- a/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java @@ -24,12 +24,17 @@ import org.rumbledb.api.Item; import org.rumbledb.context.DynamicContext; +import org.rumbledb.context.Name; import org.rumbledb.context.RuntimeStaticContext; import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.RuntimeIterator; import org.rumbledb.runtime.flwor.NativeClauseContext; import org.rumbledb.types.SequenceType; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + public class AtMostOneItemIfRuntimeIterator extends AtMostOneItemLocalRuntimeIterator { @@ -89,4 +94,14 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC + " ) )"; return new NativeClauseContext(nativeClauseContext, resultingQuery); } + + @Override + public void write(Kryo kryo, Output output) { + super.write(kryo, output); + } + + @Override + public void read(Kryo kryo, Input input) { + super.read(kryo, input); + } } diff --git a/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java b/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java index 776f010b8a..f16505cb49 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java +++ b/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java @@ -78,9 +78,12 @@ import org.rumbledb.items.TimeItem; import org.rumbledb.items.YearMonthDurationItem; import org.rumbledb.items.structured.JSoundDataFrame; +import org.rumbledb.runtime.arithmetics.AdditiveOperationIterator; import org.rumbledb.runtime.flwor.FlworDataFrameColumn.ColumnFormat; -import org.rumbledb.types.ItemType; +import org.rumbledb.runtime.primary.VariableReferenceIterator; +import org.rumbledb.types.AtomicItemType; import org.rumbledb.types.ItemItemType; +import org.rumbledb.types.ItemType; import org.rumbledb.types.SequenceType; import com.esotericsoftware.kryo.Kryo; @@ -128,14 +131,19 @@ public static void registerKryoClassesKryo(Kryo kryo) { kryo.register(FunctionItem.class); kryo.register(FunctionIdentifier.class); kryo.register(Name.class); + kryo.register(SequenceType.class); kryo.register(SequenceType.Arity.class); kryo.register(ItemType.class); kryo.register(ItemItemType.class); + kryo.register(AtomicItemType.class); kryo.register(ArrayList.class); kryo.register(RumbleRuntimeConfiguration.class); + + kryo.register(AdditiveOperationIterator.class); + kryo.register(VariableReferenceIterator.class); } public static byte[] serializeItem(Item toSerialize, Kryo kryo, Output output) { diff --git a/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java b/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java index 1d4e8cc96a..10294ef667 100644 --- a/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java @@ -36,6 +36,10 @@ import org.rumbledb.runtime.flwor.FlworDataFrameUtils; import org.rumbledb.runtime.flwor.NativeClauseContext; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -48,6 +52,13 @@ public class VariableReferenceIterator extends HybridRuntimeIterator { private List items = null; private int currentIndex = 0; + public VariableReferenceIterator() { + super(); + this.variableName = null; + this.items= null; + this.currentIndex = 0; + } + public VariableReferenceIterator( Name variableName, RuntimeStaticContext staticContext @@ -157,4 +168,16 @@ public Map getVariableDependencies() { result.put(this.variableName, DynamicContext.VariableDependency.FULL); return result; } + + @Override + public void write(Kryo kryo, Output output) { + super.write(kryo, output); + kryo.writeObject(output, this.variableName); + } + + @Override + public void read(Kryo kryo, Input input) { + super.read(kryo, input); + this.variableName = kryo.readObject(input, Name.class); + } } diff --git a/src/main/java/org/rumbledb/types/AtomicItemType.java b/src/main/java/org/rumbledb/types/AtomicItemType.java index 02b3b48e82..97639ca9c1 100644 --- a/src/main/java/org/rumbledb/types/AtomicItemType.java +++ b/src/main/java/org/rumbledb/types/AtomicItemType.java @@ -4,6 +4,10 @@ import org.rumbledb.config.RumbleRuntimeConfiguration; import org.rumbledb.context.Name; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + import java.util.*; /** @@ -547,4 +551,26 @@ public boolean isCompatibleWithDataFrames(RumbleRuntimeConfiguration configurati } return true; } + + @Override + public void write(Kryo kryo, Output output) { + kryo.writeObject(output, this.name); + kryo.writeObject(output, this.allowedFacets.size()); + for(FacetTypes f : this.allowedFacets) + { + kryo.writeObject(output, f); + } + } + + @Override + public void read(Kryo kryo, Input input) { + this.name = kryo.readObject(input, Name.class); + int n = kryo.readObject(input, Integer.class); + this.allowedFacets = new HashSet<>(n); + for(int i = 0; i < n; ++i) + { + FacetTypes t = kryo.readObject(input, FacetTypes.class); + this.allowedFacets.add(t); + } + } } diff --git a/src/main/java/org/rumbledb/types/SequenceType.java b/src/main/java/org/rumbledb/types/SequenceType.java index 47db9f8d9e..3ab1a31dc2 100644 --- a/src/main/java/org/rumbledb/types/SequenceType.java +++ b/src/main/java/org/rumbledb/types/SequenceType.java @@ -749,7 +749,6 @@ public void write(Kryo kryo, Output output) { kryo.writeObject(output, this.arity); } - @SuppressWarnings("unchecked") @Override public void read(Kryo kryo, Input input) { this.itemType = (ItemType) kryo.readClassAndObject(input); From 84837644294c6907ab9e992a971dc2196c3906a0 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Tue, 5 Mar 2024 14:24:26 +0100 Subject: [PATCH 28/42] Spotless. --- .../org/rumbledb/context/DynamicContext.java | 2 +- .../context/RuntimeStaticContext.java | 30 +- .../exceptions/ExceptionMetadata.java | 20 +- .../AtMostOneItemLocalRuntimeIterator.java | 6 +- .../org/rumbledb/runtime/RuntimeIterator.java | 8 +- .../AdditiveOperationIterator.java | 744 +++++++++--------- .../AtMostOneItemIfRuntimeIterator.java | 7 +- .../primary/VariableReferenceIterator.java | 8 +- .../org/rumbledb/types/AtomicItemType.java | 14 +- 9 files changed, 432 insertions(+), 407 deletions(-) diff --git a/src/main/java/org/rumbledb/context/DynamicContext.java b/src/main/java/org/rumbledb/context/DynamicContext.java index a1075bf94a..5725564e1c 100644 --- a/src/main/java/org/rumbledb/context/DynamicContext.java +++ b/src/main/java/org/rumbledb/context/DynamicContext.java @@ -134,7 +134,7 @@ public void read(Kryo kryo, Input input) { this.conf = kryo.readObject(input, RumbleRuntimeConfiguration.class); this.variableValues = kryo.readObject(input, VariableValues.class); this.namedFunctions = new NamedFunctions(this.conf); - this.inScopeSchemaTypes= kryo.readObject(input, InScopeSchemaTypes.class); + this.inScopeSchemaTypes = kryo.readObject(input, InScopeSchemaTypes.class); this.currentDateTime = new DateTime(kryo.readObject(input, Long.class)); } diff --git a/src/main/java/org/rumbledb/context/RuntimeStaticContext.java b/src/main/java/org/rumbledb/context/RuntimeStaticContext.java index c8f0a73761..414cb50579 100644 --- a/src/main/java/org/rumbledb/context/RuntimeStaticContext.java +++ b/src/main/java/org/rumbledb/context/RuntimeStaticContext.java @@ -74,20 +74,20 @@ public ExceptionMetadata getMetadata() { return this.metadata; } - @Override - public void write(Kryo kryo, Output output) { - kryo.writeObject(output, this.configuration); - kryo.writeObject(output, this.staticType); - kryo.writeObject(output, this.executionMode); - kryo.writeObject(output, this.metadata); - } - - @Override - public void read(Kryo kryo, Input input) { - this.configuration = kryo.readObject(input, RumbleRuntimeConfiguration.class); - this.staticType = kryo.readObject(input, SequenceType.class); - this.executionMode = kryo.readObject(input, ExecutionMode.class); - this.metadata = kryo.readObject(input, ExceptionMetadata.class); - } + @Override + public void write(Kryo kryo, Output output) { + kryo.writeObject(output, this.configuration); + kryo.writeObject(output, this.staticType); + kryo.writeObject(output, this.executionMode); + kryo.writeObject(output, this.metadata); + } + + @Override + public void read(Kryo kryo, Input input) { + this.configuration = kryo.readObject(input, RumbleRuntimeConfiguration.class); + this.staticType = kryo.readObject(input, SequenceType.class); + this.executionMode = kryo.readObject(input, ExecutionMode.class); + this.metadata = kryo.readObject(input, ExceptionMetadata.class); + } } diff --git a/src/main/java/org/rumbledb/exceptions/ExceptionMetadata.java b/src/main/java/org/rumbledb/exceptions/ExceptionMetadata.java index e59acfb733..283fe8dbb6 100644 --- a/src/main/java/org/rumbledb/exceptions/ExceptionMetadata.java +++ b/src/main/java/org/rumbledb/exceptions/ExceptionMetadata.java @@ -136,18 +136,18 @@ public String toString() { } @Override - public void write(Kryo kryo, Output output) { - kryo.writeObject(output, this.location); - kryo.writeObject(output, this.tokenLineNumber); - kryo.writeObject(output, this.tokenColumnNumber); - kryo.writeObject(output, this.code); + public void write(Kryo kryo, Output output) { + kryo.writeObject(output, this.location); + kryo.writeObject(output, this.tokenLineNumber); + kryo.writeObject(output, this.tokenColumnNumber); + kryo.writeObject(output, this.code); } @Override public void read(Kryo kryo, Input input) { - this.location = kryo.readObject(input, String.class); - this.tokenLineNumber= kryo.readObject(input, Integer.class); - this.tokenColumnNumber = kryo.readObject(input, Integer.class); - this.code = kryo.readObject(input, String.class); - } + this.location = kryo.readObject(input, String.class); + this.tokenLineNumber = kryo.readObject(input, Integer.class); + this.tokenColumnNumber = kryo.readObject(input, Integer.class); + this.code = kryo.readObject(input, String.class); + } } diff --git a/src/main/java/org/rumbledb/runtime/AtMostOneItemLocalRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/AtMostOneItemLocalRuntimeIterator.java index d5462c7bb1..afa0f9ffc9 100644 --- a/src/main/java/org/rumbledb/runtime/AtMostOneItemLocalRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/AtMostOneItemLocalRuntimeIterator.java @@ -197,12 +197,12 @@ public boolean getEffectiveBooleanValueOrCheckPosition(DynamicContext dynamicCon } @Override - public void write(Kryo kryo, Output output) { - super.write(kryo, output); + public void write(Kryo kryo, Output output) { + super.write(kryo, output); } @Override public void read(Kryo kryo, Input input) { - super.read(kryo, input); + super.read(kryo, input); } } diff --git a/src/main/java/org/rumbledb/runtime/RuntimeIterator.java b/src/main/java/org/rumbledb/runtime/RuntimeIterator.java index 436ccaf911..aa76baefe1 100644 --- a/src/main/java/org/rumbledb/runtime/RuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/RuntimeIterator.java @@ -73,10 +73,10 @@ public abstract class RuntimeIterator implements RuntimeIteratorInterface, KryoS // private StaticContext staticContext; public RuntimeIterator() { - this.hasNext = false; - this.isOpen = false; - this.children = null; - this.currentDynamicContextForLocalExecution = null; + this.hasNext = false; + this.isOpen = false; + this.children = null; + this.currentDynamicContextForLocalExecution = null; this.staticContext = null; this.staticURI = null; } diff --git a/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java b/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java index deaa5242db..f0ff354710 100644 --- a/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java +++ b/src/main/java/org/rumbledb/runtime/arithmetics/AdditiveOperationIterator.java @@ -23,14 +23,12 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; -import java.util.Set; import org.joda.time.DateTime; import org.joda.time.Period; import org.joda.time.PeriodType; import org.rumbledb.api.Item; import org.rumbledb.context.DynamicContext; -import org.rumbledb.context.Name; import org.rumbledb.context.RuntimeStaticContext; import org.rumbledb.exceptions.MoreThanOneItemException; import org.rumbledb.exceptions.NonAtomicKeyException; @@ -48,382 +46,412 @@ public class AdditiveOperationIterator extends AtMostOneItemLocalRuntimeIterator { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - private Item left; - private Item right; - private boolean isMinus; - private RuntimeIterator leftIterator; - private RuntimeIterator rightIterator; + private Item left; + private Item right; + private boolean isMinus; + private RuntimeIterator leftIterator; + private RuntimeIterator rightIterator; - public AdditiveOperationIterator() { - super(); - this.leftIterator = null; - this.rightIterator = null; - this.isMinus = false; - } + public AdditiveOperationIterator() { + super(); + this.leftIterator = null; + this.rightIterator = null; + this.isMinus = false; + } - public AdditiveOperationIterator(RuntimeIterator leftIterator, RuntimeIterator rightIterator, boolean isMinus, - RuntimeStaticContext staticContext) { - super(Arrays.asList(leftIterator, rightIterator), staticContext); - this.leftIterator = leftIterator; - this.rightIterator = rightIterator; - this.isMinus = isMinus; - } + public AdditiveOperationIterator( + RuntimeIterator leftIterator, + RuntimeIterator rightIterator, + boolean isMinus, + RuntimeStaticContext staticContext + ) { + super(Arrays.asList(leftIterator, rightIterator), staticContext); + this.leftIterator = leftIterator; + this.rightIterator = rightIterator; + this.isMinus = isMinus; + } - public Item materializeFirstItemOrNull(DynamicContext dynamicContext) { - try { - this.left = this.leftIterator.materializeAtMostOneItemOrNull(dynamicContext); - } catch (MoreThanOneItemException e) { - throw new UnexpectedTypeException( - "Addition expression requires at most one item in its left input sequence.", getMetadata()); - } - try { - this.right = this.rightIterator.materializeAtMostOneItemOrNull(dynamicContext); - } catch (MoreThanOneItemException e) { - throw new UnexpectedTypeException( - "Addition expression requires at most one item in its right input sequence.", getMetadata()); - } + public Item materializeFirstItemOrNull(DynamicContext dynamicContext) { + try { + this.left = this.leftIterator.materializeAtMostOneItemOrNull(dynamicContext); + } catch (MoreThanOneItemException e) { + throw new UnexpectedTypeException( + "Addition expression requires at most one item in its left input sequence.", + getMetadata() + ); + } + try { + this.right = this.rightIterator.materializeAtMostOneItemOrNull(dynamicContext); + } catch (MoreThanOneItemException e) { + throw new UnexpectedTypeException( + "Addition expression requires at most one item in its right input sequence.", + getMetadata() + ); + } - // if left or right equals empty sequence, return empty sequence - if (this.left == null || this.right == null) { - return null; - } - if (!this.left.isAtomic()) { - String message = String.format( - "Can not atomize an %1$s item: an %1$s has probably been passed where " - + "an atomic value is expected (e.g., as a key, or to a function expecting an atomic item)", - this.left.getDynamicType().toString()); - throw new NonAtomicKeyException(message, getMetadata()); - } - if (!this.right.isAtomic()) { - String message = String.format( - "Can not atomize an %1$s item: an %1$s has probably been passed where " - + "an atomic value is expected (e.g., as a key, or to a function expecting an atomic item)", - this.right.getDynamicType().toString()); - throw new NonAtomicKeyException(message, getMetadata()); - } - Item result = processItem(this.left, this.right, this.isMinus); - if (result == null) { - throw new UnexpectedTypeException( - " \"+\": operation not possible with parameters of type \"" + this.left.getDynamicType().toString() - + "\" and \"" + this.right.getDynamicType().toString() + "\"", - getMetadata()); - } - return result; - } + // if left or right equals empty sequence, return empty sequence + if (this.left == null || this.right == null) { + return null; + } + if (!this.left.isAtomic()) { + String message = String.format( + "Can not atomize an %1$s item: an %1$s has probably been passed where " + + "an atomic value is expected (e.g., as a key, or to a function expecting an atomic item)", + this.left.getDynamicType().toString() + ); + throw new NonAtomicKeyException(message, getMetadata()); + } + if (!this.right.isAtomic()) { + String message = String.format( + "Can not atomize an %1$s item: an %1$s has probably been passed where " + + "an atomic value is expected (e.g., as a key, or to a function expecting an atomic item)", + this.right.getDynamicType().toString() + ); + throw new NonAtomicKeyException(message, getMetadata()); + } + Item result = processItem(this.left, this.right, this.isMinus); + if (result == null) { + throw new UnexpectedTypeException( + " \"+\": operation not possible with parameters of type \"" + + this.left.getDynamicType().toString() + + "\" and \"" + + this.right.getDynamicType().toString() + + "\"", + getMetadata() + ); + } + return result; + } - public static Item processItem(Item left, Item right, boolean isMinus) { - // The integer 0 is considered the default neutral element for addition in - // sum(), even though - // it is technically incompatible with durations. In the future, we should - // make sure an error is thrown if an actual 0 appears in the sum with - // durations. - if (!isMinus && left.isInteger() && left.getIntegerValue().equals(BigInteger.ZERO)) { - return right; - } - if (!isMinus && right.isInteger() && right.getIntegerValue().equals(BigInteger.ZERO)) { - return left; - } - if (left.isInt() && right.isInt()) { - if (right.isInt() && (left.getIntValue() < Integer.MAX_VALUE / 2 - && left.getIntValue() > -Integer.MAX_VALUE / 2 && right.getIntValue() > -Integer.MAX_VALUE / 2 - && right.getIntValue() < Integer.MAX_VALUE / 2)) { - return processInt(left.getIntValue(), right.getIntValue(), isMinus); - } - } + public static Item processItem(Item left, Item right, boolean isMinus) { + // The integer 0 is considered the default neutral element for addition in + // sum(), even though + // it is technically incompatible with durations. In the future, we should + // make sure an error is thrown if an actual 0 appears in the sum with + // durations. + if (!isMinus && left.isInteger() && left.getIntegerValue().equals(BigInteger.ZERO)) { + return right; + } + if (!isMinus && right.isInteger() && right.getIntegerValue().equals(BigInteger.ZERO)) { + return left; + } + if (left.isInt() && right.isInt()) { + if ( + right.isInt() + && (left.getIntValue() < Integer.MAX_VALUE / 2 + && left.getIntValue() > -Integer.MAX_VALUE / 2 + && right.getIntValue() > -Integer.MAX_VALUE / 2 + && right.getIntValue() < Integer.MAX_VALUE / 2) + ) { + return processInt(left.getIntValue(), right.getIntValue(), isMinus); + } + } - // General cases - if (left.isDouble() && right.isNumeric()) { - double l = left.getDoubleValue(); - double r = 0; - if (right.isDouble()) { - r = right.getDoubleValue(); - } else { - r = right.castToDoubleValue(); - } - return processDouble(l, r, isMinus); - } - if (right.isDouble() && left.isNumeric()) { - double l = left.castToDoubleValue(); - double r = right.getDoubleValue(); - return processDouble(l, r, isMinus); - } - if (left.isFloat() && right.isNumeric()) { - float l = left.getFloatValue(); - float r = 0; - if (right.isFloat()) { - r = right.getFloatValue(); - } else { - r = right.castToFloatValue(); - } - return processFloat(l, r, isMinus); - } - if (right.isFloat() && left.isNumeric()) { - float l = left.castToFloatValue(); - float r = right.getFloatValue(); - return processFloat(l, r, isMinus); - } - if (left.isInteger() && right.isInteger()) { - BigInteger l = left.getIntegerValue(); - BigInteger r = right.getIntegerValue(); - return processInteger(l, r, isMinus); - } - if (left.isDecimal() && right.isDecimal()) { - BigDecimal l = left.getDecimalValue(); - BigDecimal r = right.getDecimalValue(); - return processDecimal(l, r, isMinus); - } - if (left.isYearMonthDuration() && right.isYearMonthDuration()) { - Period l = left.getDurationValue(); - Period r = right.getDurationValue(); - return processYearMonthDuration(l, r, isMinus); - } - if (left.isDayTimeDuration() && right.isDayTimeDuration()) { - Period l = left.getDurationValue(); - Period r = right.getDurationValue(); - return processDayTimeDuration(l, r, isMinus); - } - if (left.isDate() && right.isYearMonthDuration()) { - DateTime l = left.getDateTimeValue(); - Period r = right.getDurationValue(); - return processDateTimeDurationDate(l, r, isMinus, left.hasTimeZone()); - } - if (left.isDate() && right.isDayTimeDuration()) { - DateTime l = left.getDateTimeValue(); - Period r = right.getDurationValue(); - return processDateTimeDurationDate(l, r, isMinus, left.hasTimeZone()); - } - if (left.isYearMonthDuration() && right.isDate()) { - if (!isMinus) { - Period l = left.getDurationValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDurationDate(r, l, isMinus, right.hasTimeZone()); - } - } - if (left.isDayTimeDuration() && right.isDate()) { - if (!isMinus) { - Period l = left.getDurationValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDurationDate(r, l, isMinus, right.hasTimeZone()); - } - } - if (left.isTime() && right.isDayTimeDuration()) { - DateTime l = left.getDateTimeValue(); - Period r = right.getDurationValue(); - return processDateTimeDurationTime(l, r, isMinus, left.hasTimeZone()); - } - if (left.isDayTimeDuration() && right.isTime()) { - if (!isMinus) { - Period l = left.getDurationValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDurationTime(r, l, isMinus, right.hasTimeZone()); - } - } - if (left.isDateTime() && right.isYearMonthDuration()) { - DateTime l = left.getDateTimeValue(); - Period r = right.getDurationValue(); - return processDateTimeDurationDateTime(l, r, isMinus, left.hasTimeZone()); - } - if (left.isDateTime() && right.isDayTimeDuration()) { - DateTime l = left.getDateTimeValue(); - Period r = right.getDurationValue(); - return processDateTimeDurationDateTime(l, r, isMinus, left.hasTimeZone()); - } - if (left.isYearMonthDuration() && right.isDateTime()) { - if (!isMinus) { - Period l = left.getDurationValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDurationDateTime(r, l, isMinus, right.hasTimeZone()); - } - } - if (left.isDayTimeDuration() && right.isDateTime()) { - if (!isMinus) { - Period l = left.getDurationValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDurationDateTime(r, l, isMinus, right.hasTimeZone()); - } - } - if (left.isDate() && right.isDate()) { - if (isMinus) { - DateTime l = left.getDateTimeValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDayTime(l, r); - } - } - if (left.isTime() && right.isTime()) { - if (isMinus) { - DateTime l = left.getDateTimeValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDayTime(l, r); - } - } - if (left.isDateTime() && right.isDateTime()) { - if (isMinus) { - DateTime l = left.getDateTimeValue(); - DateTime r = right.getDateTimeValue(); - return processDateTimeDayTime(l, r); - } - } - return null; - } + // General cases + if (left.isDouble() && right.isNumeric()) { + double l = left.getDoubleValue(); + double r = 0; + if (right.isDouble()) { + r = right.getDoubleValue(); + } else { + r = right.castToDoubleValue(); + } + return processDouble(l, r, isMinus); + } + if (right.isDouble() && left.isNumeric()) { + double l = left.castToDoubleValue(); + double r = right.getDoubleValue(); + return processDouble(l, r, isMinus); + } + if (left.isFloat() && right.isNumeric()) { + float l = left.getFloatValue(); + float r = 0; + if (right.isFloat()) { + r = right.getFloatValue(); + } else { + r = right.castToFloatValue(); + } + return processFloat(l, r, isMinus); + } + if (right.isFloat() && left.isNumeric()) { + float l = left.castToFloatValue(); + float r = right.getFloatValue(); + return processFloat(l, r, isMinus); + } + if (left.isInteger() && right.isInteger()) { + BigInteger l = left.getIntegerValue(); + BigInteger r = right.getIntegerValue(); + return processInteger(l, r, isMinus); + } + if (left.isDecimal() && right.isDecimal()) { + BigDecimal l = left.getDecimalValue(); + BigDecimal r = right.getDecimalValue(); + return processDecimal(l, r, isMinus); + } + if (left.isYearMonthDuration() && right.isYearMonthDuration()) { + Period l = left.getDurationValue(); + Period r = right.getDurationValue(); + return processYearMonthDuration(l, r, isMinus); + } + if (left.isDayTimeDuration() && right.isDayTimeDuration()) { + Period l = left.getDurationValue(); + Period r = right.getDurationValue(); + return processDayTimeDuration(l, r, isMinus); + } + if (left.isDate() && right.isYearMonthDuration()) { + DateTime l = left.getDateTimeValue(); + Period r = right.getDurationValue(); + return processDateTimeDurationDate(l, r, isMinus, left.hasTimeZone()); + } + if (left.isDate() && right.isDayTimeDuration()) { + DateTime l = left.getDateTimeValue(); + Period r = right.getDurationValue(); + return processDateTimeDurationDate(l, r, isMinus, left.hasTimeZone()); + } + if (left.isYearMonthDuration() && right.isDate()) { + if (!isMinus) { + Period l = left.getDurationValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDurationDate(r, l, isMinus, right.hasTimeZone()); + } + } + if (left.isDayTimeDuration() && right.isDate()) { + if (!isMinus) { + Period l = left.getDurationValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDurationDate(r, l, isMinus, right.hasTimeZone()); + } + } + if (left.isTime() && right.isDayTimeDuration()) { + DateTime l = left.getDateTimeValue(); + Period r = right.getDurationValue(); + return processDateTimeDurationTime(l, r, isMinus, left.hasTimeZone()); + } + if (left.isDayTimeDuration() && right.isTime()) { + if (!isMinus) { + Period l = left.getDurationValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDurationTime(r, l, isMinus, right.hasTimeZone()); + } + } + if (left.isDateTime() && right.isYearMonthDuration()) { + DateTime l = left.getDateTimeValue(); + Period r = right.getDurationValue(); + return processDateTimeDurationDateTime(l, r, isMinus, left.hasTimeZone()); + } + if (left.isDateTime() && right.isDayTimeDuration()) { + DateTime l = left.getDateTimeValue(); + Period r = right.getDurationValue(); + return processDateTimeDurationDateTime(l, r, isMinus, left.hasTimeZone()); + } + if (left.isYearMonthDuration() && right.isDateTime()) { + if (!isMinus) { + Period l = left.getDurationValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDurationDateTime(r, l, isMinus, right.hasTimeZone()); + } + } + if (left.isDayTimeDuration() && right.isDateTime()) { + if (!isMinus) { + Period l = left.getDurationValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDurationDateTime(r, l, isMinus, right.hasTimeZone()); + } + } + if (left.isDate() && right.isDate()) { + if (isMinus) { + DateTime l = left.getDateTimeValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDayTime(l, r); + } + } + if (left.isTime() && right.isTime()) { + if (isMinus) { + DateTime l = left.getDateTimeValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDayTime(l, r); + } + } + if (left.isDateTime() && right.isDateTime()) { + if (isMinus) { + DateTime l = left.getDateTimeValue(); + DateTime r = right.getDateTimeValue(); + return processDateTimeDayTime(l, r); + } + } + return null; + } - private static Item processDouble(double l, double r, boolean isMinus) { - if (isMinus) { - return ItemFactory.getInstance().createDoubleItem(l - r); - } else { - return ItemFactory.getInstance().createDoubleItem(l + r); - } - } + private static Item processDouble(double l, double r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createDoubleItem(l - r); + } else { + return ItemFactory.getInstance().createDoubleItem(l + r); + } + } - private static Item processFloat(float l, float r, boolean isMinus) { - if (isMinus) { - return ItemFactory.getInstance().createFloatItem(l - r); - } else { - return ItemFactory.getInstance().createFloatItem(l + r); - } - } + private static Item processFloat(float l, float r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createFloatItem(l - r); + } else { + return ItemFactory.getInstance().createFloatItem(l + r); + } + } - private static Item processDecimal(BigDecimal l, BigDecimal r, boolean isMinus) { - if (isMinus) { - return ItemFactory.getInstance().createDecimalItem(l.subtract(r)); - } else { - return ItemFactory.getInstance().createDecimalItem(l.add(r)); - } - } + private static Item processDecimal(BigDecimal l, BigDecimal r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createDecimalItem(l.subtract(r)); + } else { + return ItemFactory.getInstance().createDecimalItem(l.add(r)); + } + } - private static Item processInteger(BigInteger l, BigInteger r, boolean isMinus) { - if (isMinus) { - return ItemFactory.getInstance().createIntegerItem(l.subtract(r)); - } else { - return ItemFactory.getInstance().createIntegerItem(l.add(r)); - } - } + private static Item processInteger(BigInteger l, BigInteger r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createIntegerItem(l.subtract(r)); + } else { + return ItemFactory.getInstance().createIntegerItem(l.add(r)); + } + } - private static Item processInt(int l, int r, boolean isMinus) { - if (isMinus) { - return ItemFactory.getInstance().createIntItem(l - r); - } else { - return ItemFactory.getInstance().createIntItem(l + r); - } - } + private static Item processInt(int l, int r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createIntItem(l - r); + } else { + return ItemFactory.getInstance().createIntItem(l + r); + } + } - private static Item processYearMonthDuration(Period l, Period r, boolean isMinus) { - if (isMinus) { - return ItemFactory.getInstance().createYearMonthDurationItem(l.minus(r)); - } else { - return ItemFactory.getInstance().createYearMonthDurationItem(l.plus(r)); - } - } + private static Item processYearMonthDuration(Period l, Period r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createYearMonthDurationItem(l.minus(r)); + } else { + return ItemFactory.getInstance().createYearMonthDurationItem(l.plus(r)); + } + } - private static Item processDayTimeDuration(Period l, Period r, boolean isMinus) { - if (isMinus) { - return ItemFactory.getInstance().createDayTimeDurationItem(l.minus(r)); - } else { - return ItemFactory.getInstance().createDayTimeDurationItem(l.plus(r)); - } - } + private static Item processDayTimeDuration(Period l, Period r, boolean isMinus) { + if (isMinus) { + return ItemFactory.getInstance().createDayTimeDurationItem(l.minus(r)); + } else { + return ItemFactory.getInstance().createDayTimeDurationItem(l.plus(r)); + } + } - private static Item processDateTimeDayTime(DateTime l, DateTime r) { - return ItemFactory.getInstance().createDayTimeDurationItem(new Period(r, l, PeriodType.dayTime())); - } + private static Item processDateTimeDayTime(DateTime l, DateTime r) { + return ItemFactory.getInstance().createDayTimeDurationItem(new Period(r, l, PeriodType.dayTime())); + } - private static Item processDateTimeDurationDate(DateTime l, Period r, boolean isMinus, boolean timeZone) { - if (isMinus) { - return ItemFactory.getInstance().createDateItem(l.minus(r), timeZone); - } else { - return ItemFactory.getInstance().createDateItem(l.plus(r), timeZone); - } - } + private static Item processDateTimeDurationDate(DateTime l, Period r, boolean isMinus, boolean timeZone) { + if (isMinus) { + return ItemFactory.getInstance().createDateItem(l.minus(r), timeZone); + } else { + return ItemFactory.getInstance().createDateItem(l.plus(r), timeZone); + } + } - private static Item processDateTimeDurationTime(DateTime l, Period r, boolean isMinus, boolean timeZone) { - if (isMinus) { - return ItemFactory.getInstance().createTimeItem(l.minus(r), timeZone); - } else { - return ItemFactory.getInstance().createTimeItem(l.plus(r), timeZone); - } - } + private static Item processDateTimeDurationTime(DateTime l, Period r, boolean isMinus, boolean timeZone) { + if (isMinus) { + return ItemFactory.getInstance().createTimeItem(l.minus(r), timeZone); + } else { + return ItemFactory.getInstance().createTimeItem(l.plus(r), timeZone); + } + } - private static Item processDateTimeDurationDateTime(DateTime l, Period r, boolean isMinus, boolean timeZone) { - if (isMinus) { - return ItemFactory.getInstance().createDateTimeItem(l.minus(r), timeZone); - } else { - return ItemFactory.getInstance().createDateTimeItem(l.plus(r), timeZone); - } - } + private static Item processDateTimeDurationDateTime(DateTime l, Period r, boolean isMinus, boolean timeZone) { + if (isMinus) { + return ItemFactory.getInstance().createDateTimeItem(l.minus(r), timeZone); + } else { + return ItemFactory.getInstance().createDateTimeItem(l.plus(r), timeZone); + } + } - @Override - public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { - NativeClauseContext leftResult = this.leftIterator.generateNativeQuery(nativeClauseContext); - if (leftResult == NativeClauseContext.NoNativeQuery) { - return NativeClauseContext.NoNativeQuery; - } - if (!this.leftIterator.getStaticType().getArity().equals(Arity.One)) { - return NativeClauseContext.NoNativeQuery; - } - NativeClauseContext rightResult = this.rightIterator.generateNativeQuery(nativeClauseContext); - if (rightResult == NativeClauseContext.NoNativeQuery) { - return NativeClauseContext.NoNativeQuery; - } - if (!this.rightIterator.getStaticType().getArity().equals(Arity.One)) { - return NativeClauseContext.NoNativeQuery; - } - String leftQuery = leftResult.getResultingQuery(); - String rightQuery = rightResult.getResultingQuery(); - if (this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) - && this.rightIterator.getStaticType().getItemType().isNumeric()) { - if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { - rightQuery = "(CAST (" + rightQuery + " AS DOUBLE))"; - } - } else if (this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) - && this.leftIterator.getStaticType().getItemType().isNumeric()) { - if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { - leftQuery = "(CAST (" + leftQuery + " AS DOUBLE))"; - } - } else if (this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) - && this.rightIterator.getStaticType().getItemType().isNumeric()) { - if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { - rightQuery = "(CAST (" + rightQuery + " AS FLOAT))"; - } - } else if (this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) - && this.leftIterator.getStaticType().getItemType().isNumeric()) { - if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { - leftQuery = "(CAST (" + leftQuery + " AS FLOAT))"; - } - } else if (this.leftIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) - && this.rightIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM)) { - } else if (this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) - && this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM)) { - } else { - return NativeClauseContext.NoNativeQuery; - } + @Override + public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseContext) { + NativeClauseContext leftResult = this.leftIterator.generateNativeQuery(nativeClauseContext); + if (leftResult == NativeClauseContext.NoNativeQuery) { + return NativeClauseContext.NoNativeQuery; + } + if (!this.leftIterator.getStaticType().getArity().equals(Arity.One)) { + return NativeClauseContext.NoNativeQuery; + } + NativeClauseContext rightResult = this.rightIterator.generateNativeQuery(nativeClauseContext); + if (rightResult == NativeClauseContext.NoNativeQuery) { + return NativeClauseContext.NoNativeQuery; + } + if (!this.rightIterator.getStaticType().getArity().equals(Arity.One)) { + return NativeClauseContext.NoNativeQuery; + } + String leftQuery = leftResult.getResultingQuery(); + String rightQuery = rightResult.getResultingQuery(); + if ( + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) + && this.rightIterator.getStaticType().getItemType().isNumeric() + ) { + if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { + rightQuery = "(CAST (" + rightQuery + " AS DOUBLE))"; + } + } else if ( + this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM) + && this.leftIterator.getStaticType().getItemType().isNumeric() + ) { + if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DOUBLE_QM)) { + leftQuery = "(CAST (" + leftQuery + " AS DOUBLE))"; + } + } else if ( + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) + && this.rightIterator.getStaticType().getItemType().isNumeric() + ) { + if (!this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { + rightQuery = "(CAST (" + rightQuery + " AS FLOAT))"; + } + } else if ( + this.rightIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM) + && this.leftIterator.getStaticType().getItemType().isNumeric() + ) { + if (!this.leftIterator.getStaticType().isSubtypeOf(SequenceType.FLOAT_QM)) { + leftQuery = "(CAST (" + leftQuery + " AS FLOAT))"; + } + } else if ( + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) + && this.rightIterator.getStaticType().isSubtypeOf(SequenceType.INTEGER_QM) + ) { + } else if ( + this.leftIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) + && this.rightIterator.getStaticType().isSubtypeOf(SequenceType.DECIMAL_QM) + ) { + } else { + return NativeClauseContext.NoNativeQuery; + } - if (this.isMinus) { - String resultingQuery = "( " + leftQuery + " - " + rightQuery + " )"; - return new NativeClauseContext(nativeClauseContext, resultingQuery); - } else { - String resultingQuery = "( " + leftQuery + " + " + rightQuery + " )"; - return new NativeClauseContext(nativeClauseContext, resultingQuery); - } - } + if (this.isMinus) { + String resultingQuery = "( " + leftQuery + " - " + rightQuery + " )"; + return new NativeClauseContext(nativeClauseContext, resultingQuery); + } else { + String resultingQuery = "( " + leftQuery + " + " + rightQuery + " )"; + return new NativeClauseContext(nativeClauseContext, resultingQuery); + } + } - @Override - public void write(Kryo kryo, Output output) { - super.write(kryo, output); - kryo.writeClassAndObject(output, this.left); - kryo.writeClassAndObject(output, this.right); - kryo.writeObject(output, this.isMinus); - kryo.writeClassAndObject(output, this.leftIterator); - kryo.writeClassAndObject(output, this.rightIterator); - } + @Override + public void write(Kryo kryo, Output output) { + super.write(kryo, output); + kryo.writeClassAndObject(output, this.left); + kryo.writeClassAndObject(output, this.right); + kryo.writeObject(output, this.isMinus); + kryo.writeClassAndObject(output, this.leftIterator); + kryo.writeClassAndObject(output, this.rightIterator); + } - @Override - public void read(Kryo kryo, Input input) { - super.read(kryo, input); - this.left = (Item) kryo.readClassAndObject(input); - this.right = (Item) kryo.readClassAndObject(input); - this.isMinus = kryo.readObject(input, Boolean.class); - this.leftIterator = (RuntimeIterator) kryo.readClassAndObject(input); - this.rightIterator = (RuntimeIterator) kryo.readClassAndObject(input); - } + @Override + public void read(Kryo kryo, Input input) { + super.read(kryo, input); + this.left = (Item) kryo.readClassAndObject(input); + this.right = (Item) kryo.readClassAndObject(input); + this.isMinus = kryo.readObject(input, Boolean.class); + this.leftIterator = (RuntimeIterator) kryo.readClassAndObject(input); + this.rightIterator = (RuntimeIterator) kryo.readClassAndObject(input); + } } diff --git a/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java index 7910e9dc44..f95e3d84c1 100644 --- a/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/control/AtMostOneItemIfRuntimeIterator.java @@ -24,7 +24,6 @@ import org.rumbledb.api.Item; import org.rumbledb.context.DynamicContext; -import org.rumbledb.context.Name; import org.rumbledb.context.RuntimeStaticContext; import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; import org.rumbledb.runtime.RuntimeIterator; @@ -96,12 +95,12 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC } @Override - public void write(Kryo kryo, Output output) { - super.write(kryo, output); + public void write(Kryo kryo, Output output) { + super.write(kryo, output); } @Override public void read(Kryo kryo, Input input) { - super.read(kryo, input); + super.read(kryo, input); } } diff --git a/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java b/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java index 10294ef667..5c0cb9a5da 100644 --- a/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/VariableReferenceIterator.java @@ -55,7 +55,7 @@ public class VariableReferenceIterator extends HybridRuntimeIterator { public VariableReferenceIterator() { super(); this.variableName = null; - this.items= null; + this.items = null; this.currentIndex = 0; } @@ -170,14 +170,14 @@ public Map getVariableDependencies() { } @Override - public void write(Kryo kryo, Output output) { - super.write(kryo, output); + public void write(Kryo kryo, Output output) { + super.write(kryo, output); kryo.writeObject(output, this.variableName); } @Override public void read(Kryo kryo, Input input) { - super.read(kryo, input); + super.read(kryo, input); this.variableName = kryo.readObject(input, Name.class); } } diff --git a/src/main/java/org/rumbledb/types/AtomicItemType.java b/src/main/java/org/rumbledb/types/AtomicItemType.java index 97639ca9c1..49ce7b9b44 100644 --- a/src/main/java/org/rumbledb/types/AtomicItemType.java +++ b/src/main/java/org/rumbledb/types/AtomicItemType.java @@ -553,12 +553,11 @@ public boolean isCompatibleWithDataFrames(RumbleRuntimeConfiguration configurati } @Override - public void write(Kryo kryo, Output output) { + public void write(Kryo kryo, Output output) { kryo.writeObject(output, this.name); kryo.writeObject(output, this.allowedFacets.size()); - for(FacetTypes f : this.allowedFacets) - { - kryo.writeObject(output, f); + for (FacetTypes f : this.allowedFacets) { + kryo.writeObject(output, f); } } @@ -567,10 +566,9 @@ public void read(Kryo kryo, Input input) { this.name = kryo.readObject(input, Name.class); int n = kryo.readObject(input, Integer.class); this.allowedFacets = new HashSet<>(n); - for(int i = 0; i < n; ++i) - { - FacetTypes t = kryo.readObject(input, FacetTypes.class); - this.allowedFacets.add(t); + for (int i = 0; i < n; ++i) { + FacetTypes t = kryo.readObject(input, FacetTypes.class); + this.allowedFacets.add(t); } } } From 4357cb3245c083d84e832deae920d0a375720f65 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Tue, 5 Mar 2024 17:19:16 +0100 Subject: [PATCH 29/42] Serialize more. --- .../runtime/flwor/FlworDataFrameUtils.java | 2 + .../rumbledb/types/DerivedAtomicItemType.java | 56 ++++++++++++++++++- src/main/java/org/rumbledb/types/Facets.java | 2 + 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java b/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java index f16505cb49..d7c21918d0 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java +++ b/src/main/java/org/rumbledb/runtime/flwor/FlworDataFrameUtils.java @@ -81,6 +81,7 @@ import org.rumbledb.runtime.arithmetics.AdditiveOperationIterator; import org.rumbledb.runtime.flwor.FlworDataFrameColumn.ColumnFormat; import org.rumbledb.runtime.primary.VariableReferenceIterator; +import org.rumbledb.types.DerivedAtomicItemType; import org.rumbledb.types.AtomicItemType; import org.rumbledb.types.ItemItemType; import org.rumbledb.types.ItemType; @@ -137,6 +138,7 @@ public static void registerKryoClassesKryo(Kryo kryo) { kryo.register(ItemType.class); kryo.register(ItemItemType.class); kryo.register(AtomicItemType.class); + kryo.register(DerivedAtomicItemType.class); kryo.register(ArrayList.class); diff --git a/src/main/java/org/rumbledb/types/DerivedAtomicItemType.java b/src/main/java/org/rumbledb/types/DerivedAtomicItemType.java index 1f7ba3822a..d772f10223 100644 --- a/src/main/java/org/rumbledb/types/DerivedAtomicItemType.java +++ b/src/main/java/org/rumbledb/types/DerivedAtomicItemType.java @@ -11,6 +11,10 @@ import org.rumbledb.expressions.comparison.ComparisonExpression; import org.rumbledb.runtime.misc.ComparisonIterator; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + import java.util.List; import java.util.Set; @@ -18,17 +22,20 @@ public class DerivedAtomicItemType implements ItemType { private static final long serialVersionUID = 1L; - private final ItemType baseType; + private ItemType baseType; private ItemType primitiveType; private int typeTreeDepth; - private final boolean isUserDefined; - private final Name name; + private boolean isUserDefined; + private Name name; private Item minInclusive, maxInclusive, minExclusive, maxExclusive; private Integer minLength, length, maxLength, totalDigits, fractionDigits; private List constraints; private List enumeration; private TimezoneFacet explicitTimezone; + DerivedAtomicItemType() { + } + DerivedAtomicItemType(Name name, ItemType baseType, ItemType primitiveType, Facets facets) { this(name, baseType, primitiveType, facets, true); } @@ -613,4 +620,47 @@ public void processBaseType() { ExceptionMetadata.EMPTY_METADATA ); } + + @Override + public void write(Kryo kryo, Output output) { + kryo.writeObject(output, this.name); + kryo.writeClassAndObject(output, this.baseType); + kryo.writeClassAndObject(output, this.primitiveType); + kryo.writeObject(output, this.typeTreeDepth); + kryo.writeObject(output, this.isUserDefined); + kryo.writeClassAndObject(output, this.minInclusive); + kryo.writeClassAndObject(output, this.maxInclusive); + kryo.writeClassAndObject(output, this.minExclusive); + kryo.writeClassAndObject(output, this.maxExclusive); + kryo.writeObjectOrNull(output, this.minLength, Integer.class); + kryo.writeObjectOrNull(output, this.length, Integer.class); + kryo.writeObjectOrNull(output, this.maxLength, Integer.class); + kryo.writeObjectOrNull(output, this.totalDigits, Integer.class); + kryo.writeObjectOrNull(output, this.fractionDigits, Integer.class); + // kryo.writeObject(output, this.constraints); + // kryo.writeObjectOrNull(output, this.enumeration, ArrayList.class); + // kryo.writeObject(output, this.explicitTimezone); + } + + @SuppressWarnings("unchecked") + @Override + public void read(Kryo kryo, Input input) { + this.name = kryo.readObject(input, Name.class); + this.baseType = (ItemType) kryo.readClassAndObject(input); + this.primitiveType = (ItemType) kryo.readClassAndObject(input); + this.typeTreeDepth = kryo.readObject(input, Integer.class); + this.isUserDefined = kryo.readObject(input, Boolean.class); + this.minInclusive = (Item) kryo.readClassAndObject(input); + this.maxInclusive = (Item) kryo.readClassAndObject(input); + this.minExclusive = (Item) kryo.readClassAndObject(input); + this.maxExclusive = (Item) kryo.readClassAndObject(input); + this.minLength = kryo.readObjectOrNull(input, Integer.class); + this.length = kryo.readObjectOrNull(input, Integer.class); + this.maxLength = kryo.readObjectOrNull(input, Integer.class); + this.totalDigits = kryo.readObjectOrNull(input, Integer.class); + this.fractionDigits = kryo.readObjectOrNull(input, Integer.class); + // this.constraints = kryo.readObject(input, ArrayList.class); + // this.enumeration = kryo.readObjectOrNull(input, ArrayList.class); + // this.explicitTimezone = kryo.readObject(input, TimezoneFacet.class); + } } diff --git a/src/main/java/org/rumbledb/types/Facets.java b/src/main/java/org/rumbledb/types/Facets.java index 37b67515c6..d260a993e7 100644 --- a/src/main/java/org/rumbledb/types/Facets.java +++ b/src/main/java/org/rumbledb/types/Facets.java @@ -3,6 +3,7 @@ import org.rumbledb.api.Item; import java.util.Collections; +import java.util.ArrayList; import java.util.List; /** @@ -188,6 +189,7 @@ public static Facets createAtomicTypeFacets( facets.maxExclusive = maxExclusive; facets.totalDigits = totalDigits; facets.fractionDigits = fractionDigits; + facets.constraints = new ArrayList<>(); return facets; } } From eb26f365933167b0a4a36b3aa28b4a5137af03b6 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Tue, 5 Mar 2024 17:20:56 +0100 Subject: [PATCH 30/42] Fix issue. --- src/main/java/org/rumbledb/types/Facets.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/rumbledb/types/Facets.java b/src/main/java/org/rumbledb/types/Facets.java index d260a993e7..6fe5d73add 100644 --- a/src/main/java/org/rumbledb/types/Facets.java +++ b/src/main/java/org/rumbledb/types/Facets.java @@ -62,7 +62,7 @@ public static Facets createTimezoneFacets(TimezoneFacet explicitTimezone) { private Item minInclusive, maxInclusive; private Item minExclusive, maxExclusive; private Integer minLength, length, maxLength, totalDigits, fractionDigits; - private List constraints = Collections.emptyList(); + private List constraints = new ArrayList<>(); private List enumeration; private TimezoneFacet explicitTimezone; @@ -189,7 +189,6 @@ public static Facets createAtomicTypeFacets( facets.maxExclusive = maxExclusive; facets.totalDigits = totalDigits; facets.fractionDigits = fractionDigits; - facets.constraints = new ArrayList<>(); return facets; } } From c7c5e653646f898a680008bcb8e975c41c078073 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 13 Mar 2024 11:35:40 +0100 Subject: [PATCH 31/42] Fix spotless. --- src/main/java/org/rumbledb/types/Facets.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/rumbledb/types/Facets.java b/src/main/java/org/rumbledb/types/Facets.java index 6fe5d73add..b58624d77e 100644 --- a/src/main/java/org/rumbledb/types/Facets.java +++ b/src/main/java/org/rumbledb/types/Facets.java @@ -2,7 +2,6 @@ import org.rumbledb.api.Item; -import java.util.Collections; import java.util.ArrayList; import java.util.List; From b81cf1cf59fb22c821f8d7202e83e929b6d94122 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 13 Mar 2024 13:49:45 +0100 Subject: [PATCH 32/42] Fix test. --- .../runtime/primary/IntegerRuntimeIterator.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java b/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java index f38373a950..f652bae245 100644 --- a/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java +++ b/src/main/java/org/rumbledb/runtime/primary/IntegerRuntimeIterator.java @@ -25,6 +25,11 @@ import org.rumbledb.context.RuntimeStaticContext; import org.rumbledb.items.ItemFactory; import org.rumbledb.runtime.flwor.NativeClauseContext; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator; public class IntegerRuntimeIterator extends AtMostOneItemLocalRuntimeIterator { @@ -53,4 +58,16 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC "CAST (" + this.item.getIntValue() + "BD AS DECIMAL(38, 0))" ); } + + @Override + public void write(Kryo kryo, Output output) { + super.write(kryo, output); + kryo.writeClassAndObject(output, this.item); + } + + @Override + public void read(Kryo kryo, Input input) { + super.read(kryo, input); + this.item = (Item) kryo.readClassAndObject(input); + } } From 553dcb8fccf658169540510b2c7cd88989268b3e Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 13 Mar 2024 14:04:29 +0100 Subject: [PATCH 33/42] Fix test. --- .../runtime/flwor/clauses/WhereClauseSparkIterator.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/rumbledb/runtime/flwor/clauses/WhereClauseSparkIterator.java b/src/main/java/org/rumbledb/runtime/flwor/clauses/WhereClauseSparkIterator.java index 23a9268574..b4f0d7088d 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/clauses/WhereClauseSparkIterator.java +++ b/src/main/java/org/rumbledb/runtime/flwor/clauses/WhereClauseSparkIterator.java @@ -452,7 +452,8 @@ public static FlworDataFrame tryNativeQuery( .sparkSession() .sql( String.format( - "select * from %s where %s", + // Spark SQL but confusing where (FALSE) with a table and column name. + "select * from %s where true and %s", input, nativeQuery.getResultingQuery() ) From 912646bdf3ccfe4ae833bd836b543f5452037731 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Wed, 13 Mar 2024 14:05:09 +0100 Subject: [PATCH 34/42] Fix test. --- .../runtime/flwor/clauses/WhereClauseSparkIterator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/rumbledb/runtime/flwor/clauses/WhereClauseSparkIterator.java b/src/main/java/org/rumbledb/runtime/flwor/clauses/WhereClauseSparkIterator.java index b4f0d7088d..65788e1a25 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/clauses/WhereClauseSparkIterator.java +++ b/src/main/java/org/rumbledb/runtime/flwor/clauses/WhereClauseSparkIterator.java @@ -442,7 +442,7 @@ public static FlworDataFrame tryNativeQuery( .info( "Rumble was able to optimize a where clause to a native SQL query: " + String.format( - "select * from %s where %s", + "select * from %s where true and %s", input, nativeQuery.getResultingQuery() ) From 00a6493a8f106a54dbfc08ca9e1ffeaed111614c Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Fri, 15 Mar 2024 17:26:29 +0100 Subject: [PATCH 35/42] Deactivate SQL casting from date to string. --- .../java/org/rumbledb/runtime/typing/CastIterator.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index 6fc89b37bd..503fc7a1a7 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -958,12 +958,15 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC System.err.println("Float"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.stringItem)) { if (this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem)) { - resultingQuery = " (date_format(" + value.getResultingQuery() + ", \"yyyy-MM-dd'T'HH:mm:ss.SZZZZZ\")) "; + return NativeClauseContext.NoNativeQuery; + // resultingQuery = " (date_format(" + value.getResultingQuery() + ", \"yyyy-MM-dd'T'HH:mm:ss.SZZZZZ\")) + // "; } else if (this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateItem)) { if (getConfiguration().dateWithTimezone()) { return NativeClauseContext.NoNativeQuery; } - resultingQuery = " (date_format(" + value.getResultingQuery() + ", 'yyyy-MM-dd')) "; + return NativeClauseContext.NoNativeQuery; + // resultingQuery = " (date_format(" + value.getResultingQuery() + ", 'yyyy-MM-dd')) "; } else if ( this.children.get(0).getStaticType().getItemType().isSubtypeOf(BuiltinTypesCatalogue.decimalItem) ) { From aa2d4ab9565794080951e584aa44a38fa206b3fa Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Fri, 15 Mar 2024 17:37:35 +0100 Subject: [PATCH 36/42] Separate tests. --- .../runtime-native-flwor/literals/types-dates.jq | 16 ++++++++++++++++ .../runtime-native-flwor/literals/types.jq | 14 +------------- 2 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq new file mode 100644 index 0000000000..ccf2254bce --- /dev/null +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq @@ -0,0 +1,16 @@ +(:JIQS: ShouldRun; Output="({ "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" })" :) +for $i in parallelize((1 to 2), 10) +let $string10 := string(date("2023-05-12")) +let $string11 := string(dateTimeStamp("2023-05-12T12:34:56-07:00")) +let $date := date("2023-05-12") +let $date2 := date(dateTimeStamp("2023-05-12T12:34:56Z")) +let $dateTimeStamp := dateTimeStamp("2023-05-12T12:34:56+02:00") +let $dateTimeStamp2 := dateTimeStamp($date) +return { + "string10": $string10, + "string11": $string11, + "date" : $date, + "date2" : $date2, + "dateTimeStamp" : $dateTimeStamp, + "dateTimeStamp2" : $dateTimeStamp2 +} diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq index 9bda6f9788..49c18d99c5 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.01230498761230948", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.01230498761230948", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z", "hexBinary" : "1ABF", "null" : null })" :) +(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.01230498761230948", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.01230498761230948", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12","string12" : "1ABF", "anyURI" : "123465789", "hexBinary" : "1ABF", "null" : null })" :) for $i in parallelize((1 to 2), 10) let $boolean := boolean("false") let $boolean := boolean(anyURI("false")) @@ -84,14 +84,8 @@ let $string6 := string($integer) let $string7 := string($decimal) let $string8 := string($double) let $string9 := string($float) -let $string10 := string(date("2023-05-12")) -let $string11 := string(dateTimeStamp("2023-05-12T12:34:56-07:00")) let $string12 := string(hexBinary("1ABF")) let $anyURI := anyURI($string) -let $date := date("2023-05-12") -let $date2 := date(dateTimeStamp("2023-05-12T12:34:56Z")) -let $dateTimeStamp := dateTimeStamp("2023-05-12T12:34:56+02:00") -let $dateTimeStamp2 := dateTimeStamp($date) let $hexBinary := hexBinary("1ABF") let $null := "null" cast as null return { @@ -177,14 +171,8 @@ return { "string7": $string7, "string8": $string8, "string9": $string9, - "string10": $string10, - "string11": $string11, "string12": $string12, "anyURI" : $anyURI, - "date" : $date, - "date2" : $date2, - "dateTimeStamp" : $dateTimeStamp, - "dateTimeStamp2" : $dateTimeStamp2, "hexBinary" : $hexBinary, "null" : $null } From 72205403d83c88a5c6f86d92960b55d2fec5a59f Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Fri, 15 Mar 2024 17:43:17 +0100 Subject: [PATCH 37/42] Fix tests. --- .../test_files/runtime-native-flwor/literals/types-dates.jq | 2 +- .../resources/test_files/runtime-native-flwor/literals/types.jq | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq index ccf2254bce..9d0ec38e75 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="({ "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" })" :) +(:JIQS: ShouldRun; Output="({ "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" }, { "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" })" :) for $i in parallelize((1 to 2), 10) let $string10 := string(date("2023-05-12")) let $string11 := string(dateTimeStamp("2023-05-12T12:34:56-07:00")) diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq index 49c18d99c5..48e81097a5 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.01230498761230948", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "string12" : "1ABF", "anyURI" : "123465789", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.01230498761230948", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string10" : "2023-05-12","string12" : "1ABF", "anyURI" : "123465789", "hexBinary" : "1ABF", "null" : null })" :) +(:JIQS: ShouldRun; Output="({ "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.01230498761230948", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string12" : "1ABF", "anyURI" : "123465789", "hexBinary" : "1ABF", "null" : null }, { "boolean" : true, "boolean2" : false, "boolean3" : false, "boolean4" : false, "boolean5" : false, "boolean6" : true, "boolean7" : true, "boolean8" : true, "boolean9" : true, "boolean10" : true, "byte" : 12, "byte2" : 1, "byte3" : 0, "byte4" : 1, "byte5" : 2, "byte6" : 3, "short" : 12345, "short2" : 1, "short3" : 0, "short4" : 1, "short5" : 2, "short6" : 3, "int" : 12345, "int2" : 122345, "int3" : 1, "int4" : 0, "int5" : 1, "int6" : 2, "int7" : 3, "long" : 12345, "long2" : 12345, "long3" : 123546789000000, "long4" : 1, "long5" : 0, "long6" : 1, "long7" : 2, "long8" : 3, "integer" : 1235467890000001230498761230948, "integer2" : 12345, "integer3" : 1, "integer4" : 0, "integer5" : 1, "integer6" : 2, "integer7" : 3, "decimal" : 12354678900000.01230498761230948, "decimal2" : 12, "decimal3" : 12345, "decimal4" : 12345, "decimal5" : 12354678900000, "decimal6" : 1, "decimal7" : 0, "decimal8" : 1, "decimal9" : 2, "double" : 1.2354678900000012E13, "double2" : 0, "double3" : 12, "double4" : 12345, "double5" : 12345, "double6" : 12345, "double7" : 1.2354678900000012E30, "double8" : 1.2354678900000012E13, "double9" : 1, "double10" : 0, "float" : 1.23546789E13, "float2" : 0, "float3" : 12, "float4" : 12345, "float5" : 12345, "float6" : 12345, "float7" : 1.23546786E30, "float8" : 1.23546789E13, "float9" : 1, "float10" : 0, "string" : "123465789", "string2" : "12", "string3" : "12345", "string4" : "12345", "string5" : "12345", "string6" : "1235467890000001230498761230948", "string7" : "12354678900000.01230498761230948", "string8" : "1.2354678900000012E13", "string9" : "1.23546789E13", "string12" : "1ABF", "anyURI" : "123465789", "hexBinary" : "1ABF", "null" : null })" :) for $i in parallelize((1 to 2), 10) let $boolean := boolean("false") let $boolean := boolean(anyURI("false")) From e5604943acd1e62db69a77eb4f008f6a89250721 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Thu, 18 Apr 2024 09:50:35 +0200 Subject: [PATCH 38/42] Comment out date with timezone. --- .../test_files/runtime-native-flwor/literals/types-dates.jq | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq index 9d0ec38e75..0b8d63de6f 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq @@ -10,7 +10,7 @@ return { "string10": $string10, "string11": $string11, "date" : $date, - "date2" : $date2, +(: "date2" : $date2, :) "dateTimeStamp" : $dateTimeStamp, "dateTimeStamp2" : $dateTimeStamp2 } From 29b370b739b6c635da61d5f28c5e7da3b53ff457 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Thu, 18 Apr 2024 10:09:11 +0200 Subject: [PATCH 39/42] Fix tests. --- .../java/org/rumbledb/api/SequenceOfItems.java | 9 +++++++++ src/main/java/org/rumbledb/items/DateTimeItem.java | 1 - .../flwor/clauses/LetClauseSparkIterator.java | 9 --------- .../org/rumbledb/runtime/typing/CastIterator.java | 14 -------------- .../org/rumbledb/types/DerivedAtomicItemType.java | 1 - 5 files changed, 9 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/rumbledb/api/SequenceOfItems.java b/src/main/java/org/rumbledb/api/SequenceOfItems.java index a2b8937cdf..4f04cbca22 100644 --- a/src/main/java/org/rumbledb/api/SequenceOfItems.java +++ b/src/main/java/org/rumbledb/api/SequenceOfItems.java @@ -2,6 +2,7 @@ import java.util.List; +import org.apache.spark.SparkRuntimeException; import org.apache.spark.api.java.JavaRDD; import org.apache.spark.sql.Dataset; import org.apache.spark.sql.Row; @@ -207,6 +208,14 @@ public long populateListWithWarningOnlyIfCapReached(List resultList) { RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); ex.initCause(e); throw ex; + } catch (SparkRuntimeException e) { + if (e.getMessage().contains("CAST_INVALID_INPUT")) { + RumbleException ex = new CastException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + ex.initCause(e); + throw ex; + } else { + throw e; + } } catch (UnsupportedOperationException e) { RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); ex.initCause(e); diff --git a/src/main/java/org/rumbledb/items/DateTimeItem.java b/src/main/java/org/rumbledb/items/DateTimeItem.java index 896f0978d3..30e4f1c823 100644 --- a/src/main/java/org/rumbledb/items/DateTimeItem.java +++ b/src/main/java/org/rumbledb/items/DateTimeItem.java @@ -79,7 +79,6 @@ public DateTimeItem() { DateTimeItem(String dateTimeString) { this.value = parseDateTime(dateTimeString, BuiltinTypesCatalogue.dateTimeItem); if (doesLexicalValueHaveNoTimeZone(dateTimeString)) { - System.err.println("No time zone."); this.hasTimeZone = false; this.value = this.value.withZoneRetainFields(DateTimeZone.UTC); } diff --git a/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java b/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java index 533e46ef1f..8f4e9c098b 100644 --- a/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java +++ b/src/main/java/org/rumbledb/runtime/flwor/clauses/LetClauseSparkIterator.java @@ -833,15 +833,6 @@ public static Dataset tryNativeQuery( ); String selectSQL = FlworDataFrameUtils.getSQLColumnProjection(allColumns, true); String input = FlworDataFrameUtils.createTempView(dataFrame); - System.err.println( - String.format( - "select %s %s as `%s` from %s", - selectSQL, - nativeQuery.getResultingQuery(), - newVariableName, - input - ) - ); return dataFrame.sparkSession() .sql( String.format( diff --git a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java index 503fc7a1a7..c907c60ab2 100644 --- a/src/main/java/org/rumbledb/runtime/typing/CastIterator.java +++ b/src/main/java/org/rumbledb/runtime/typing/CastIterator.java @@ -835,7 +835,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "BOOLEAN" + ")) "; - System.err.println("Boolean"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.byteItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) @@ -850,7 +849,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "BYTE" + ")) "; - System.err.println("Byte"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.shortItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) @@ -865,7 +863,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "SHORT" + ")) "; - System.err.println("Short"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.intItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) @@ -880,7 +877,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "INT" + ")) "; - System.err.println("Int"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.longItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) @@ -895,7 +891,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "LONG" + ")) "; - System.err.println("Long"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.integerItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) @@ -910,7 +905,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DECIMAL(38,0)" + ")) "; - System.err.println("Integer"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.decimalItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) @@ -925,7 +919,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DECIMAL(38,19)" + ")) "; - System.err.println("Decimal"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.doubleItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) @@ -940,7 +933,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DOUBLE" + ")) "; - System.err.println("Double"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.floatItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.booleanItem) @@ -955,7 +947,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "FLOAT" + ")) "; - System.err.println("Float"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.stringItem)) { if (this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem)) { return NativeClauseContext.NoNativeQuery; @@ -974,7 +965,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC } else { resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; } - System.err.println("String"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.anyURIItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) @@ -983,7 +973,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "STRING" + ")) "; - System.err.println("anyURI"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.dateItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) @@ -1000,7 +989,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "DATE" + ")) "; - System.err.println("Date"); } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.dateTimeStampItem)) { if ( !this.children.get(0).getStaticType().getItemType().equals(BuiltinTypesCatalogue.stringItem) @@ -1014,7 +1002,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC return NativeClauseContext.NoNativeQuery; } resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "TIMESTAMP" + ")) "; - System.err.println("Timestamp"); /* * } else if (this.sequenceType.getItemType().equals(BuiltinTypesCatalogue.hexBinaryItem)) { * resultingQuery = " (CAST (" + value.getResultingQuery() + " AS " + "BINARY" + ")) "; @@ -1023,7 +1010,6 @@ public NativeClauseContext generateNativeQuery(NativeClauseContext nativeClauseC } else { return NativeClauseContext.NoNativeQuery; } - System.err.println("Return"); return new NativeClauseContext( nativeClauseContext, resultingQuery diff --git a/src/main/java/org/rumbledb/types/DerivedAtomicItemType.java b/src/main/java/org/rumbledb/types/DerivedAtomicItemType.java index d772f10223..a1e79429e7 100644 --- a/src/main/java/org/rumbledb/types/DerivedAtomicItemType.java +++ b/src/main/java/org/rumbledb/types/DerivedAtomicItemType.java @@ -642,7 +642,6 @@ public void write(Kryo kryo, Output output) { // kryo.writeObject(output, this.explicitTimezone); } - @SuppressWarnings("unchecked") @Override public void read(Kryo kryo, Input input) { this.name = kryo.readObject(input, Name.class); From 557042ae15e8e0f9036034555d444d3a5d71d4a6 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Thu, 18 Apr 2024 10:12:35 +0200 Subject: [PATCH 40/42] Fix test. --- .../test_files/runtime-native-flwor/literals/types-dates.jq | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq index 0b8d63de6f..8f3f5c6243 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="({ "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" }, { "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" })" :) +(:JIQS: ShouldRun; Output="({ "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" }, { "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" })" :) for $i in parallelize((1 to 2), 10) let $string10 := string(date("2023-05-12")) let $string11 := string(dateTimeStamp("2023-05-12T12:34:56-07:00")) From 24d9170243d8eacb4d286f5933bc7a4bcbff638b Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Thu, 18 Apr 2024 10:16:46 +0200 Subject: [PATCH 41/42] Fix test --- .../test_files/runtime-native-flwor/literals/types-dates.jq | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq b/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq index 8f3f5c6243..158a40af53 100644 --- a/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq +++ b/src/test/resources/test_files/runtime-native-flwor/literals/types-dates.jq @@ -1,4 +1,4 @@ -(:JIQS: ShouldRun; Output="({ "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" }, { "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "date2" : "2023-05-12Z", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" })" :) +(:JIQS: ShouldRun; Output="({ "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" }, { "string10" : "2023-05-12", "string11" : "2023-05-12T12:34:56-07:00", "date" : "2023-05-12", "dateTimeStamp" : "2023-05-12T12:34:56+02:00", "dateTimeStamp2" : "2023-05-12T00:00:00Z" })" :) for $i in parallelize((1 to 2), 10) let $string10 := string(date("2023-05-12")) let $string11 := string(dateTimeStamp("2023-05-12T12:34:56-07:00")) From 6e86b10939cc3847f73dae7c566e61df79a3d912 Mon Sep 17 00:00:00 2001 From: Ghislain Fourny Date: Thu, 18 Apr 2024 10:25:06 +0200 Subject: [PATCH 42/42] Throw cast error even when in SQL --- .../org/rumbledb/api/SequenceOfItems.java | 16 ++++++++ src/main/java/org/rumbledb/cli/Main.java | 41 +++++++++++-------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/rumbledb/api/SequenceOfItems.java b/src/main/java/org/rumbledb/api/SequenceOfItems.java index 4f04cbca22..a48de49e89 100644 --- a/src/main/java/org/rumbledb/api/SequenceOfItems.java +++ b/src/main/java/org/rumbledb/api/SequenceOfItems.java @@ -89,6 +89,14 @@ public boolean hasNext() { RumbleException ex = new CastException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); ex.initCause(e); throw ex; + } catch (SparkRuntimeException e) { + if (e.getMessage().contains("CAST_INVALID_INPUT")) { + RumbleException ex = new CastException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + ex.initCause(e); + throw ex; + } else { + throw e; + } } catch (UnsupportedOperationException e) { RumbleException ex = new CastException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); ex.initCause(e); @@ -140,6 +148,14 @@ public JavaRDD getAsRDD() { RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); ex.initCause(e); throw ex; + } catch (SparkRuntimeException e) { + if (e.getMessage().contains("CAST_INVALID_INPUT")) { + RumbleException ex = new CastException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); + ex.initCause(e); + throw ex; + } else { + throw e; + } } catch (UnsupportedOperationException e) { RumbleException ex = new UnexpectedTypeException(e.getMessage(), ExceptionMetadata.EMPTY_METADATA); ex.initCause(e); diff --git a/src/main/java/org/rumbledb/cli/Main.java b/src/main/java/org/rumbledb/cli/Main.java index 29562892bb..2c111a1188 100644 --- a/src/main/java/org/rumbledb/cli/Main.java +++ b/src/main/java/org/rumbledb/cli/Main.java @@ -24,7 +24,9 @@ import org.apache.commons.io.IOUtils; import org.apache.spark.SparkException; +import org.apache.spark.SparkRuntimeException; import org.rumbledb.config.RumbleRuntimeConfiguration; +import org.rumbledb.exceptions.CastException; import org.rumbledb.exceptions.ExceptionMetadata; import org.rumbledb.exceptions.OurBadException; import org.rumbledb.exceptions.RumbleException; @@ -149,23 +151,22 @@ private static void handleException(Throwable ex, boolean showErrorInfo) { ex.printStackTrace(); } System.exit(-42); - } else { - System.err.println( - "We are very embarrassed, because an error has occured that we did not anticipate 🙈: " - + ex.getMessage() - ); - System.err.println( - "We would like to investigate this and make sure to fix it. We would be very grateful if you could contact us or file an issue on GitHub with your query." - ); - System.err.println("Link: https://github.com/RumbleDB/rumble/issues"); - System.err.println( - "For more debug info (e.g., so you can communicate it to us), please try again using --show-error-info yes in your command line." - ); - if (showErrorInfo) { - ex.printStackTrace(); - } - System.exit(-42); } + System.err.println( + "We are very embarrassed, because an error has occured that we did not anticipate 🙈: " + + ex.getMessage() + ); + System.err.println( + "We would like to investigate this and make sure to fix it. We would be very grateful if you could contact us or file an issue on GitHub with your query." + ); + System.err.println("Link: https://github.com/RumbleDB/rumble/issues"); + System.err.println( + "For more debug info (e.g., so you can communicate it to us), please try again using --show-error-info yes in your command line." + ); + if (showErrorInfo) { + ex.printStackTrace(); + } + System.exit(-42); } } @@ -187,6 +188,14 @@ public static Throwable unboxException(Throwable ex) { ExceptionMetadata.EMPTY_METADATA ); } + if (ex instanceof SparkRuntimeException) { + if (ex.getMessage().contains("CAST_INVALID_INPUT")) { + RumbleException nex = new CastException(ex.getMessage(), ExceptionMetadata.EMPTY_METADATA); + ex.initCause(ex); + return nex; + } + // general message. + } return ex; } return new OurBadException("A null exception was returned.");