diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/DecimalPrecision.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/DecimalPrecision.scala index 46fbf071f4378..19b6f2cf8dabc 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/DecimalPrecision.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/DecimalPrecision.scala @@ -82,7 +82,7 @@ object DecimalPrecision extends TypeCoercionRule { val resultType = widerDecimalType(p1, s1, p2, s2) val newE1 = if (e1.dataType == resultType) e1 else Cast(e1, resultType) val newE2 = if (e2.dataType == resultType) e2 else Cast(e2, resultType) - b.makeCopy(Array(newE1, newE2)) + b.withNewChildren(Seq(newE1, newE2)) } /** @@ -201,21 +201,21 @@ object DecimalPrecision extends TypeCoercionRule { case (l: Literal, r) if r.dataType.isInstanceOf[DecimalType] && l.dataType.isInstanceOf[IntegralType] && literalPickMinimumPrecision => - b.makeCopy(Array(Cast(l, DecimalType.fromLiteral(l)), r)) + b.withNewChildren(Seq(Cast(l, DecimalType.fromLiteral(l)), r)) case (l, r: Literal) if l.dataType.isInstanceOf[DecimalType] && r.dataType.isInstanceOf[IntegralType] && literalPickMinimumPrecision => - b.makeCopy(Array(l, Cast(r, DecimalType.fromLiteral(r)))) + b.withNewChildren(Seq(l, Cast(r, DecimalType.fromLiteral(r)))) // Promote integers inside a binary expression with fixed-precision decimals to decimals, // and fixed-precision decimals in an expression with floats / doubles to doubles case (l @ IntegralType(), r @ DecimalType.Expression(_, _)) => - b.makeCopy(Array(Cast(l, DecimalType.forType(l.dataType)), r)) + b.withNewChildren(Seq(Cast(l, DecimalType.forType(l.dataType)), r)) case (l @ DecimalType.Expression(_, _), r @ IntegralType()) => - b.makeCopy(Array(l, Cast(r, DecimalType.forType(r.dataType)))) + b.withNewChildren(Seq(l, Cast(r, DecimalType.forType(r.dataType)))) case (l, r @ DecimalType.Expression(_, _)) if isFloat(l.dataType) => - b.makeCopy(Array(l, Cast(r, DoubleType))) + b.withNewChildren(Seq(l, Cast(r, DoubleType))) case (l @ DecimalType.Expression(_, _), r) if isFloat(r.dataType) => - b.makeCopy(Array(Cast(l, DoubleType), r)) + b.withNewChildren(Seq(Cast(l, DoubleType), r)) case _ => b } } diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala index 97552c3e6fdfa..1b889546b3512 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala @@ -1108,22 +1108,22 @@ object TypeCoercion extends TypeCoercionBase { case a @ BinaryArithmetic(left @ StringType(), right) if right.dataType != CalendarIntervalType => - a.makeCopy(Array(Cast(left, DoubleType), right)) + a.withNewChildren(Seq(Cast(left, DoubleType), right)) case a @ BinaryArithmetic(left, right @ StringType()) if left.dataType != CalendarIntervalType => - a.makeCopy(Array(left, Cast(right, DoubleType))) + a.withNewChildren(Seq(left, Cast(right, DoubleType))) // For equality between string and timestamp we cast the string to a timestamp // so that things like rounding of subsecond precision does not affect the comparison. case p @ Equality(left @ StringType(), right @ TimestampType()) => - p.makeCopy(Array(Cast(left, TimestampType), right)) + p.withNewChildren(Seq(Cast(left, TimestampType), right)) case p @ Equality(left @ TimestampType(), right @ StringType()) => - p.makeCopy(Array(left, Cast(right, TimestampType))) + p.withNewChildren(Seq(left, Cast(right, TimestampType))) case p @ BinaryComparison(left, right) if findCommonTypeForBinaryComparison(left.dataType, right.dataType, conf).isDefined => val commonType = findCommonTypeForBinaryComparison(left.dataType, right.dataType, conf).get - p.makeCopy(Array(castExpr(left, commonType), castExpr(right, commonType))) + p.withNewChildren(Seq(castExpr(left, commonType), castExpr(right, commonType))) } } diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/ansi/try_arithmetic.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/ansi/try_arithmetic.sql.out new file mode 100644 index 0000000000000..30654d1d71e2b --- /dev/null +++ b/sql/core/src/test/resources/sql-tests/analyzer-results/ansi/try_arithmetic.sql.out @@ -0,0 +1,491 @@ +-- Automatically generated by SQLQueryTestSuite +-- !query +SELECT try_add(1, 1) +-- !query analysis +Project [try_add(1, 1) AS try_add(1, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(2147483647, 1) +-- !query analysis +Project [try_add(2147483647, 1) AS try_add(2147483647, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(2147483647, decimal(1)) +-- !query analysis +Project [try_add(2147483647, cast(1 as decimal(10,0))) AS try_add(2147483647, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(2147483647, "1") +-- !query analysis +Project [try_add(2147483647, 1) AS try_add(2147483647, 1)#xL] ++- OneRowRelation + + +-- !query +SELECT try_add(-2147483648, -1) +-- !query analysis +Project [try_add(-2147483648, -1) AS try_add(-2147483648, -1)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(9223372036854775807L, 1) +-- !query analysis +Project [try_add(9223372036854775807, 1) AS try_add(9223372036854775807, 1)#xL] ++- OneRowRelation + + +-- !query +SELECT try_add(-9223372036854775808L, -1) +-- !query analysis +Project [try_add(-9223372036854775808, -1) AS try_add(-9223372036854775808, -1)#xL] ++- OneRowRelation + + +-- !query +SELECT try_add(1, (2147483647 + 1)) +-- !query analysis +Project [try_add(1, (2147483647 + 1)) AS try_add(1, (2147483647 + 1))#x] ++- OneRowRelation + + +-- !query +SELECT try_add(1L, (9223372036854775807L + 1L)) +-- !query analysis +Project [try_add(1, (9223372036854775807 + 1)) AS try_add(1, (9223372036854775807 + 1))#xL] ++- OneRowRelation + + +-- !query +SELECT try_add(1, 1.0 / 0.0) +-- !query analysis +Project [try_add(1, (1.0 / 0.0)) AS try_add(1, (1.0 / 0.0))#x] ++- OneRowRelation + + +-- !query +SELECT try_add(date'2021-01-01', 1) +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(1, date'2021-01-01') +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(date'2021-01-01', interval 2 year) +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(date'2021-01-01', interval 2 second) +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(interval 2 year, date'2021-01-01') +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(interval 2 second, date'2021-01-01') +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(timestamp_ltz'2021-01-01 00:00:00', interval 2 year) +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(timestamp_ntz'2021-01-01 00:00:00', interval 2 second) +-- !query analysis +Project [try_add(2021-01-01 00:00:00, INTERVAL '02' SECOND) AS try_add(TIMESTAMP_NTZ '2021-01-01 00:00:00', INTERVAL '02' SECOND)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(interval 2 year, timestamp_ltz'2021-01-01 00:00:00') +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(interval 2 second, timestamp_ntz'2021-01-01 00:00:00') +-- !query analysis +Project [try_add(INTERVAL '02' SECOND, 2021-01-01 00:00:00) AS try_add(INTERVAL '02' SECOND, TIMESTAMP_NTZ '2021-01-01 00:00:00')#x] ++- OneRowRelation + + +-- !query +SELECT try_add(interval 2 year, interval 2 year) +-- !query analysis +Project [try_add(INTERVAL '2' YEAR, INTERVAL '2' YEAR) AS try_add(INTERVAL '2' YEAR, INTERVAL '2' YEAR)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(interval 2 second, interval 2 second) +-- !query analysis +Project [try_add(INTERVAL '02' SECOND, INTERVAL '02' SECOND) AS try_add(INTERVAL '02' SECOND, INTERVAL '02' SECOND)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(interval 2 year, interval 2 second) +-- !query analysis +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE", + "sqlState" : "42K09", + "messageParameters" : { + "inputSql" : "\"INTERVAL '2' YEAR\"", + "inputType" : "\"INTERVAL YEAR\"", + "paramIndex" : "first", + "requiredType" : "\"(TIMESTAMP OR TIMESTAMP WITHOUT TIME ZONE)\"", + "sqlExpr" : "\"INTERVAL '2' YEAR + INTERVAL '02' SECOND\"" + }, + "queryContext" : [ { + "objectType" : "", + "objectName" : "", + "startIndex" : 8, + "stopIndex" : 50, + "fragment" : "try_add(interval 2 year, interval 2 second)" + } ] +} + + +-- !query +SELECT try_add(interval 2147483647 month, interval 2 month) +-- !query analysis +Project [try_add(INTERVAL '2147483647' MONTH, INTERVAL '2' MONTH) AS try_add(INTERVAL '2147483647' MONTH, INTERVAL '2' MONTH)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(interval 106751991 day, interval 3 day) +-- !query analysis +Project [try_add(INTERVAL '106751991' DAY, INTERVAL '3' DAY) AS try_add(INTERVAL '106751991' DAY, INTERVAL '3' DAY)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, 0.5) +-- !query analysis +Project [try_divide(1, 0.5) AS try_divide(1, 0.5)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, 0) +-- !query analysis +Project [try_divide(1, 0) AS try_divide(1, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(0, 0) +-- !query analysis +Project [try_divide(0, 0) AS try_divide(0, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, (2147483647 + 1)) +-- !query analysis +Project [try_divide(1, (2147483647 + 1)) AS try_divide(1, (2147483647 + 1))#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1L, (9223372036854775807L + 1L)) +-- !query analysis +Project [try_divide(1, (9223372036854775807 + 1)) AS try_divide(1, (9223372036854775807 + 1))#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, 1.0 / 0.0) +-- !query analysis +Project [try_divide(1, (1.0 / 0.0)) AS try_divide(1, (1.0 / 0.0))#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, decimal(0)) +-- !query analysis +Project [try_divide(1, cast(0 as decimal(10,0))) AS try_divide(1, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, "0") +-- !query analysis +Project [try_divide(1, 0) AS try_divide(1, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 2 year, 2) +-- !query analysis +Project [try_divide(INTERVAL '2' YEAR, 2) AS try_divide(INTERVAL '2' YEAR, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 2 second, 2) +-- !query analysis +Project [try_divide(INTERVAL '02' SECOND, 2) AS try_divide(INTERVAL '02' SECOND, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 2 year, 0) +-- !query analysis +Project [try_divide(INTERVAL '2' YEAR, 0) AS try_divide(INTERVAL '2' YEAR, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 2 second, 0) +-- !query analysis +Project [try_divide(INTERVAL '02' SECOND, 0) AS try_divide(INTERVAL '02' SECOND, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 2147483647 month, 0.5) +-- !query analysis +Project [try_divide(INTERVAL '2147483647' MONTH, 0.5) AS try_divide(INTERVAL '2147483647' MONTH, 0.5)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 106751991 day, 0.5) +-- !query analysis +Project [try_divide(INTERVAL '106751991' DAY, 0.5) AS try_divide(INTERVAL '106751991' DAY, 0.5)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(1, 1) +-- !query analysis +Project [try_subtract(1, 1) AS try_subtract(1, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(2147483647, -1) +-- !query analysis +Project [try_subtract(2147483647, -1) AS try_subtract(2147483647, -1)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(2147483647, decimal(-1)) +-- !query analysis +Project [try_subtract(2147483647, cast(-1 as decimal(10,0))) AS try_subtract(2147483647, -1)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(2147483647, "-1") +-- !query analysis +Project [try_subtract(2147483647, -1) AS try_subtract(2147483647, -1)#xL] ++- OneRowRelation + + +-- !query +SELECT try_subtract(-2147483648, 1) +-- !query analysis +Project [try_subtract(-2147483648, 1) AS try_subtract(-2147483648, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(9223372036854775807L, -1) +-- !query analysis +Project [try_subtract(9223372036854775807, -1) AS try_subtract(9223372036854775807, -1)#xL] ++- OneRowRelation + + +-- !query +SELECT try_subtract(-9223372036854775808L, 1) +-- !query analysis +Project [try_subtract(-9223372036854775808, 1) AS try_subtract(-9223372036854775808, 1)#xL] ++- OneRowRelation + + +-- !query +SELECT try_subtract(1, (2147483647 + 1)) +-- !query analysis +Project [try_subtract(1, (2147483647 + 1)) AS try_subtract(1, (2147483647 + 1))#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(1L, (9223372036854775807L + 1L)) +-- !query analysis +Project [try_subtract(1, (9223372036854775807 + 1)) AS try_subtract(1, (9223372036854775807 + 1))#xL] ++- OneRowRelation + + +-- !query +SELECT try_subtract(1, 1.0 / 0.0) +-- !query analysis +Project [try_subtract(1, (1.0 / 0.0)) AS try_subtract(1, (1.0 / 0.0))#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(interval 2 year, interval 3 year) +-- !query analysis +Project [try_subtract(INTERVAL '2' YEAR, INTERVAL '3' YEAR) AS try_subtract(INTERVAL '2' YEAR, INTERVAL '3' YEAR)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(interval 3 second, interval 2 second) +-- !query analysis +Project [try_subtract(INTERVAL '03' SECOND, INTERVAL '02' SECOND) AS try_subtract(INTERVAL '03' SECOND, INTERVAL '02' SECOND)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(interval 2147483647 month, interval -2 month) +-- !query analysis +Project [try_subtract(INTERVAL '2147483647' MONTH, INTERVAL '-2' MONTH) AS try_subtract(INTERVAL '2147483647' MONTH, INTERVAL '-2' MONTH)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(interval 106751991 day, interval -3 day) +-- !query analysis +Project [try_subtract(INTERVAL '106751991' DAY, INTERVAL '-3' DAY) AS try_subtract(INTERVAL '106751991' DAY, INTERVAL '-3' DAY)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(2, 3) +-- !query analysis +Project [try_multiply(2, 3) AS try_multiply(2, 3)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(2147483647, -2) +-- !query analysis +Project [try_multiply(2147483647, -2) AS try_multiply(2147483647, -2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(2147483647, decimal(-2)) +-- !query analysis +Project [try_multiply(2147483647, cast(-2 as decimal(10,0))) AS try_multiply(2147483647, -2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(2147483647, "-2") +-- !query analysis +Project [try_multiply(2147483647, -2) AS try_multiply(2147483647, -2)#xL] ++- OneRowRelation + + +-- !query +SELECT try_multiply(-2147483648, 2) +-- !query analysis +Project [try_multiply(-2147483648, 2) AS try_multiply(-2147483648, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(9223372036854775807L, 2) +-- !query analysis +Project [try_multiply(9223372036854775807, 2) AS try_multiply(9223372036854775807, 2)#xL] ++- OneRowRelation + + +-- !query +SELECT try_multiply(-9223372036854775808L, -2) +-- !query analysis +Project [try_multiply(-9223372036854775808, -2) AS try_multiply(-9223372036854775808, -2)#xL] ++- OneRowRelation + + +-- !query +SELECT try_multiply(1, (2147483647 + 1)) +-- !query analysis +Project [try_multiply(1, (2147483647 + 1)) AS try_multiply(1, (2147483647 + 1))#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(1L, (9223372036854775807L + 1L)) +-- !query analysis +Project [try_multiply(1, (9223372036854775807 + 1)) AS try_multiply(1, (9223372036854775807 + 1))#xL] ++- OneRowRelation + + +-- !query +SELECT try_multiply(1, 1.0 / 0.0) +-- !query analysis +Project [try_multiply(1, (1.0 / 0.0)) AS try_multiply(1, (1.0 / 0.0))#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 2 year, 2) +-- !query analysis +Project [try_multiply(INTERVAL '2' YEAR, 2) AS try_multiply(INTERVAL '2' YEAR, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 2 second, 2) +-- !query analysis +Project [try_multiply(INTERVAL '02' SECOND, 2) AS try_multiply(INTERVAL '02' SECOND, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 2 year, 0) +-- !query analysis +Project [try_multiply(INTERVAL '2' YEAR, 0) AS try_multiply(INTERVAL '2' YEAR, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 2 second, 0) +-- !query analysis +Project [try_multiply(INTERVAL '02' SECOND, 0) AS try_multiply(INTERVAL '02' SECOND, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 2147483647 month, 2) +-- !query analysis +Project [try_multiply(INTERVAL '2147483647' MONTH, 2) AS try_multiply(INTERVAL '2147483647' MONTH, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 106751991 day, 2) +-- !query analysis +Project [try_multiply(INTERVAL '106751991' DAY, 2) AS try_multiply(INTERVAL '106751991' DAY, 2)#x] ++- OneRowRelation diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/try_arithmetic.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/try_arithmetic.sql.out new file mode 100644 index 0000000000000..caf997f6ccbb2 --- /dev/null +++ b/sql/core/src/test/resources/sql-tests/analyzer-results/try_arithmetic.sql.out @@ -0,0 +1,491 @@ +-- Automatically generated by SQLQueryTestSuite +-- !query +SELECT try_add(1, 1) +-- !query analysis +Project [try_add(1, 1) AS try_add(1, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(2147483647, 1) +-- !query analysis +Project [try_add(2147483647, 1) AS try_add(2147483647, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(2147483647, decimal(1)) +-- !query analysis +Project [try_add(2147483647, cast(1 as decimal(10,0))) AS try_add(2147483647, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(2147483647, "1") +-- !query analysis +Project [try_add(2147483647, 1) AS try_add(2147483647, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(-2147483648, -1) +-- !query analysis +Project [try_add(-2147483648, -1) AS try_add(-2147483648, -1)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(9223372036854775807L, 1) +-- !query analysis +Project [try_add(9223372036854775807, 1) AS try_add(9223372036854775807, 1)#xL] ++- OneRowRelation + + +-- !query +SELECT try_add(-9223372036854775808L, -1) +-- !query analysis +Project [try_add(-9223372036854775808, -1) AS try_add(-9223372036854775808, -1)#xL] ++- OneRowRelation + + +-- !query +SELECT try_add(1, (2147483647 + 1)) +-- !query analysis +Project [try_add(1, (2147483647 + 1)) AS try_add(1, (2147483647 + 1))#x] ++- OneRowRelation + + +-- !query +SELECT try_add(1L, (9223372036854775807L + 1L)) +-- !query analysis +Project [try_add(1, (9223372036854775807 + 1)) AS try_add(1, (9223372036854775807 + 1))#xL] ++- OneRowRelation + + +-- !query +SELECT try_add(1, 1.0 / 0.0) +-- !query analysis +Project [try_add(1, (1.0 / 0.0)) AS try_add(1, (1.0 / 0.0))#x] ++- OneRowRelation + + +-- !query +SELECT try_add(date'2021-01-01', 1) +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(1, date'2021-01-01') +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(date'2021-01-01', interval 2 year) +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(date'2021-01-01', interval 2 second) +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(interval 2 year, date'2021-01-01') +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(interval 2 second, date'2021-01-01') +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(timestamp_ltz'2021-01-01 00:00:00', interval 2 year) +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(timestamp_ntz'2021-01-01 00:00:00', interval 2 second) +-- !query analysis +Project [try_add(2021-01-01 00:00:00, INTERVAL '02' SECOND) AS try_add(TIMESTAMP_NTZ '2021-01-01 00:00:00', INTERVAL '02' SECOND)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(interval 2 year, timestamp_ltz'2021-01-01 00:00:00') +-- !query analysis +[Analyzer test output redacted due to nondeterminism] + + +-- !query +SELECT try_add(interval 2 second, timestamp_ntz'2021-01-01 00:00:00') +-- !query analysis +Project [try_add(INTERVAL '02' SECOND, 2021-01-01 00:00:00) AS try_add(INTERVAL '02' SECOND, TIMESTAMP_NTZ '2021-01-01 00:00:00')#x] ++- OneRowRelation + + +-- !query +SELECT try_add(interval 2 year, interval 2 year) +-- !query analysis +Project [try_add(INTERVAL '2' YEAR, INTERVAL '2' YEAR) AS try_add(INTERVAL '2' YEAR, INTERVAL '2' YEAR)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(interval 2 second, interval 2 second) +-- !query analysis +Project [try_add(INTERVAL '02' SECOND, INTERVAL '02' SECOND) AS try_add(INTERVAL '02' SECOND, INTERVAL '02' SECOND)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(interval 2 year, interval 2 second) +-- !query analysis +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE", + "sqlState" : "42K09", + "messageParameters" : { + "inputSql" : "\"INTERVAL '2' YEAR\"", + "inputType" : "\"INTERVAL YEAR\"", + "paramIndex" : "first", + "requiredType" : "\"(TIMESTAMP OR TIMESTAMP WITHOUT TIME ZONE)\"", + "sqlExpr" : "\"INTERVAL '2' YEAR + INTERVAL '02' SECOND\"" + }, + "queryContext" : [ { + "objectType" : "", + "objectName" : "", + "startIndex" : 8, + "stopIndex" : 50, + "fragment" : "try_add(interval 2 year, interval 2 second)" + } ] +} + + +-- !query +SELECT try_add(interval 2147483647 month, interval 2 month) +-- !query analysis +Project [try_add(INTERVAL '2147483647' MONTH, INTERVAL '2' MONTH) AS try_add(INTERVAL '2147483647' MONTH, INTERVAL '2' MONTH)#x] ++- OneRowRelation + + +-- !query +SELECT try_add(interval 106751991 day, interval 3 day) +-- !query analysis +Project [try_add(INTERVAL '106751991' DAY, INTERVAL '3' DAY) AS try_add(INTERVAL '106751991' DAY, INTERVAL '3' DAY)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, 0.5) +-- !query analysis +Project [try_divide(1, 0.5) AS try_divide(1, 0.5)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, 0) +-- !query analysis +Project [try_divide(1, 0) AS try_divide(1, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(0, 0) +-- !query analysis +Project [try_divide(0, 0) AS try_divide(0, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, (2147483647 + 1)) +-- !query analysis +Project [try_divide(1, (2147483647 + 1)) AS try_divide(1, (2147483647 + 1))#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1L, (9223372036854775807L + 1L)) +-- !query analysis +Project [try_divide(1, (9223372036854775807 + 1)) AS try_divide(1, (9223372036854775807 + 1))#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, 1.0 / 0.0) +-- !query analysis +Project [try_divide(1, (1.0 / 0.0)) AS try_divide(1, (1.0 / 0.0))#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, decimal(0)) +-- !query analysis +Project [try_divide(1, cast(0 as decimal(10,0))) AS try_divide(1, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(1, "0") +-- !query analysis +Project [try_divide(1, 0) AS try_divide(1, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 2 year, 2) +-- !query analysis +Project [try_divide(INTERVAL '2' YEAR, 2) AS try_divide(INTERVAL '2' YEAR, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 2 second, 2) +-- !query analysis +Project [try_divide(INTERVAL '02' SECOND, 2) AS try_divide(INTERVAL '02' SECOND, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 2 year, 0) +-- !query analysis +Project [try_divide(INTERVAL '2' YEAR, 0) AS try_divide(INTERVAL '2' YEAR, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 2 second, 0) +-- !query analysis +Project [try_divide(INTERVAL '02' SECOND, 0) AS try_divide(INTERVAL '02' SECOND, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 2147483647 month, 0.5) +-- !query analysis +Project [try_divide(INTERVAL '2147483647' MONTH, 0.5) AS try_divide(INTERVAL '2147483647' MONTH, 0.5)#x] ++- OneRowRelation + + +-- !query +SELECT try_divide(interval 106751991 day, 0.5) +-- !query analysis +Project [try_divide(INTERVAL '106751991' DAY, 0.5) AS try_divide(INTERVAL '106751991' DAY, 0.5)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(1, 1) +-- !query analysis +Project [try_subtract(1, 1) AS try_subtract(1, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(2147483647, -1) +-- !query analysis +Project [try_subtract(2147483647, -1) AS try_subtract(2147483647, -1)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(2147483647, decimal(-1)) +-- !query analysis +Project [try_subtract(2147483647, cast(-1 as decimal(10,0))) AS try_subtract(2147483647, -1)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(2147483647, "-1") +-- !query analysis +Project [try_subtract(2147483647, -1) AS try_subtract(2147483647, -1)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(-2147483648, 1) +-- !query analysis +Project [try_subtract(-2147483648, 1) AS try_subtract(-2147483648, 1)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(9223372036854775807L, -1) +-- !query analysis +Project [try_subtract(9223372036854775807, -1) AS try_subtract(9223372036854775807, -1)#xL] ++- OneRowRelation + + +-- !query +SELECT try_subtract(-9223372036854775808L, 1) +-- !query analysis +Project [try_subtract(-9223372036854775808, 1) AS try_subtract(-9223372036854775808, 1)#xL] ++- OneRowRelation + + +-- !query +SELECT try_subtract(1, (2147483647 + 1)) +-- !query analysis +Project [try_subtract(1, (2147483647 + 1)) AS try_subtract(1, (2147483647 + 1))#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(1L, (9223372036854775807L + 1L)) +-- !query analysis +Project [try_subtract(1, (9223372036854775807 + 1)) AS try_subtract(1, (9223372036854775807 + 1))#xL] ++- OneRowRelation + + +-- !query +SELECT try_subtract(1, 1.0 / 0.0) +-- !query analysis +Project [try_subtract(1, (1.0 / 0.0)) AS try_subtract(1, (1.0 / 0.0))#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(interval 2 year, interval 3 year) +-- !query analysis +Project [try_subtract(INTERVAL '2' YEAR, INTERVAL '3' YEAR) AS try_subtract(INTERVAL '2' YEAR, INTERVAL '3' YEAR)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(interval 3 second, interval 2 second) +-- !query analysis +Project [try_subtract(INTERVAL '03' SECOND, INTERVAL '02' SECOND) AS try_subtract(INTERVAL '03' SECOND, INTERVAL '02' SECOND)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(interval 2147483647 month, interval -2 month) +-- !query analysis +Project [try_subtract(INTERVAL '2147483647' MONTH, INTERVAL '-2' MONTH) AS try_subtract(INTERVAL '2147483647' MONTH, INTERVAL '-2' MONTH)#x] ++- OneRowRelation + + +-- !query +SELECT try_subtract(interval 106751991 day, interval -3 day) +-- !query analysis +Project [try_subtract(INTERVAL '106751991' DAY, INTERVAL '-3' DAY) AS try_subtract(INTERVAL '106751991' DAY, INTERVAL '-3' DAY)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(2, 3) +-- !query analysis +Project [try_multiply(2, 3) AS try_multiply(2, 3)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(2147483647, -2) +-- !query analysis +Project [try_multiply(2147483647, -2) AS try_multiply(2147483647, -2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(2147483647, decimal(-2)) +-- !query analysis +Project [try_multiply(2147483647, cast(-2 as decimal(10,0))) AS try_multiply(2147483647, -2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(2147483647, "-2") +-- !query analysis +Project [try_multiply(2147483647, -2) AS try_multiply(2147483647, -2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(-2147483648, 2) +-- !query analysis +Project [try_multiply(-2147483648, 2) AS try_multiply(-2147483648, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(9223372036854775807L, 2) +-- !query analysis +Project [try_multiply(9223372036854775807, 2) AS try_multiply(9223372036854775807, 2)#xL] ++- OneRowRelation + + +-- !query +SELECT try_multiply(-9223372036854775808L, -2) +-- !query analysis +Project [try_multiply(-9223372036854775808, -2) AS try_multiply(-9223372036854775808, -2)#xL] ++- OneRowRelation + + +-- !query +SELECT try_multiply(1, (2147483647 + 1)) +-- !query analysis +Project [try_multiply(1, (2147483647 + 1)) AS try_multiply(1, (2147483647 + 1))#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(1L, (9223372036854775807L + 1L)) +-- !query analysis +Project [try_multiply(1, (9223372036854775807 + 1)) AS try_multiply(1, (9223372036854775807 + 1))#xL] ++- OneRowRelation + + +-- !query +SELECT try_multiply(1, 1.0 / 0.0) +-- !query analysis +Project [try_multiply(1, (1.0 / 0.0)) AS try_multiply(1, (1.0 / 0.0))#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 2 year, 2) +-- !query analysis +Project [try_multiply(INTERVAL '2' YEAR, 2) AS try_multiply(INTERVAL '2' YEAR, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 2 second, 2) +-- !query analysis +Project [try_multiply(INTERVAL '02' SECOND, 2) AS try_multiply(INTERVAL '02' SECOND, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 2 year, 0) +-- !query analysis +Project [try_multiply(INTERVAL '2' YEAR, 0) AS try_multiply(INTERVAL '2' YEAR, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 2 second, 0) +-- !query analysis +Project [try_multiply(INTERVAL '02' SECOND, 0) AS try_multiply(INTERVAL '02' SECOND, 0)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 2147483647 month, 2) +-- !query analysis +Project [try_multiply(INTERVAL '2147483647' MONTH, 2) AS try_multiply(INTERVAL '2147483647' MONTH, 2)#x] ++- OneRowRelation + + +-- !query +SELECT try_multiply(interval 106751991 day, 2) +-- !query analysis +Project [try_multiply(INTERVAL '106751991' DAY, 2) AS try_multiply(INTERVAL '106751991' DAY, 2)#x] ++- OneRowRelation diff --git a/sql/core/src/test/resources/sql-tests/inputs/try_arithmetic.sql b/sql/core/src/test/resources/sql-tests/inputs/try_arithmetic.sql index 55907b6701e50..943865b68d39e 100644 --- a/sql/core/src/test/resources/sql-tests/inputs/try_arithmetic.sql +++ b/sql/core/src/test/resources/sql-tests/inputs/try_arithmetic.sql @@ -1,6 +1,8 @@ -- Numeric + Numeric SELECT try_add(1, 1); SELECT try_add(2147483647, 1); +SELECT try_add(2147483647, decimal(1)); +SELECT try_add(2147483647, "1"); SELECT try_add(-2147483648, -1); SELECT try_add(9223372036854775807L, 1); SELECT try_add(-9223372036854775808L, -1); @@ -38,6 +40,8 @@ SELECT try_divide(0, 0); SELECT try_divide(1, (2147483647 + 1)); SELECT try_divide(1L, (9223372036854775807L + 1L)); SELECT try_divide(1, 1.0 / 0.0); +SELECT try_divide(1, decimal(0)); +SELECT try_divide(1, "0"); -- Interval / Numeric SELECT try_divide(interval 2 year, 2); @@ -50,6 +54,8 @@ SELECT try_divide(interval 106751991 day, 0.5); -- Numeric - Numeric SELECT try_subtract(1, 1); SELECT try_subtract(2147483647, -1); +SELECT try_subtract(2147483647, decimal(-1)); +SELECT try_subtract(2147483647, "-1"); SELECT try_subtract(-2147483648, 1); SELECT try_subtract(9223372036854775807L, -1); SELECT try_subtract(-9223372036854775808L, 1); @@ -66,6 +72,8 @@ SELECT try_subtract(interval 106751991 day, interval -3 day); -- Numeric * Numeric SELECT try_multiply(2, 3); SELECT try_multiply(2147483647, -2); +SELECT try_multiply(2147483647, decimal(-2)); +SELECT try_multiply(2147483647, "-2"); SELECT try_multiply(-2147483648, 2); SELECT try_multiply(9223372036854775807L, 2); SELECT try_multiply(-9223372036854775808L, -2); diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/try_arithmetic.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/try_arithmetic.sql.out index cf29eff19fbb8..2ec5bebde59ce 100644 --- a/sql/core/src/test/resources/sql-tests/results/ansi/try_arithmetic.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/ansi/try_arithmetic.sql.out @@ -15,6 +15,22 @@ struct NULL +-- !query +SELECT try_add(2147483647, decimal(1)) +-- !query schema +struct +-- !query output +2147483648 + + +-- !query +SELECT try_add(2147483647, "1") +-- !query schema +struct +-- !query output +2147483648 + + -- !query SELECT try_add(-2147483648, -1) -- !query schema @@ -341,6 +357,22 @@ org.apache.spark.SparkArithmeticException } +-- !query +SELECT try_divide(1, decimal(0)) +-- !query schema +struct +-- !query output +NULL + + +-- !query +SELECT try_divide(1, "0") +-- !query schema +struct +-- !query output +NULL + + -- !query SELECT try_divide(interval 2 year, 2) -- !query schema @@ -405,6 +437,22 @@ struct NULL +-- !query +SELECT try_subtract(2147483647, decimal(-1)) +-- !query schema +struct +-- !query output +2147483648 + + +-- !query +SELECT try_subtract(2147483647, "-1") +-- !query schema +struct +-- !query output +2147483648 + + -- !query SELECT try_subtract(-2147483648, 1) -- !query schema @@ -547,6 +595,22 @@ struct NULL +-- !query +SELECT try_multiply(2147483647, decimal(-2)) +-- !query schema +struct +-- !query output +-4294967294 + + +-- !query +SELECT try_multiply(2147483647, "-2") +-- !query schema +struct +-- !query output +-4294967294 + + -- !query SELECT try_multiply(-2147483648, 2) -- !query schema diff --git a/sql/core/src/test/resources/sql-tests/results/try_arithmetic.sql.out b/sql/core/src/test/resources/sql-tests/results/try_arithmetic.sql.out index 60d6750237a4a..30c4fbf81b124 100644 --- a/sql/core/src/test/resources/sql-tests/results/try_arithmetic.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/try_arithmetic.sql.out @@ -15,6 +15,22 @@ struct NULL +-- !query +SELECT try_add(2147483647, decimal(1)) +-- !query schema +struct +-- !query output +2147483648 + + +-- !query +SELECT try_add(2147483647, "1") +-- !query schema +struct +-- !query output +2.147483648E9 + + -- !query SELECT try_add(-2147483648, -1) -- !query schema @@ -249,6 +265,22 @@ struct NULL +-- !query +SELECT try_divide(1, decimal(0)) +-- !query schema +struct +-- !query output +NULL + + +-- !query +SELECT try_divide(1, "0") +-- !query schema +struct +-- !query output +NULL + + -- !query SELECT try_divide(interval 2 year, 2) -- !query schema @@ -313,6 +345,22 @@ struct NULL +-- !query +SELECT try_subtract(2147483647, decimal(-1)) +-- !query schema +struct +-- !query output +2147483648 + + +-- !query +SELECT try_subtract(2147483647, "-1") +-- !query schema +struct +-- !query output +2.147483648E9 + + -- !query SELECT try_subtract(-2147483648, 1) -- !query schema @@ -409,6 +457,22 @@ struct NULL +-- !query +SELECT try_multiply(2147483647, decimal(-2)) +-- !query schema +struct +-- !query output +-4294967294 + + +-- !query +SELECT try_multiply(2147483647, "-2") +-- !query schema +struct +-- !query output +-4.294967294E9 + + -- !query SELECT try_multiply(-2147483648, 2) -- !query schema