-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MIN and MAX aggregate support for MONEY datatype
This commit addresses the issue where SELECT...INTO with MIN and MAX aggregations on MONEY columns lost type information. The fix implements proper type preservation for these aggregate functions when used with MONEY datatypes. Key changes: Added support for MIN(money) -> money Added support for MAX(money) -> money Ensures type consistency in SELECT...INTO operations Task: BABEL-5479 Signed-off-by: Roshan Kanwar <[email protected]>
- Loading branch information
1 parent
c73d268
commit 2a2f007
Showing
11 changed files
with
1,094 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
DROP TABLE IF EXISTS TestMoneyTable; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTable1; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTable2; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTable3; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTable4; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTable5; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTable6; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTable7; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTable8; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTable9; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTable10; | ||
GO | ||
|
||
DROP TABLE IF EXISTS EmptyMoneyTable; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTableEmpty; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ExtremeMoneyTable; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTableExtreme; | ||
GO | ||
|
||
DROP TABLE IF EXISTS MixedNullMoneyTable; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTableMixedNull; | ||
GO | ||
|
||
DROP TABLE IF EXISTS OverflowMoneyTable; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTableOverflow; | ||
GO | ||
|
||
DROP TABLE IF EXISTS NonMoneyTable; | ||
GO | ||
|
||
DROP TABLE IF EXISTS ResultTableNonMoney; | ||
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
CREATE TABLE TestMoneyTable ( | ||
ID INT IDENTITY(1,1) PRIMARY KEY, | ||
Amount MONEY, | ||
Description NVARCHAR(100) | ||
); | ||
GO | ||
|
||
-- Insert test data | ||
INSERT INTO TestMoneyTable (Amount, Description) VALUES | ||
(100.50, 'Item 1'), | ||
(200.75, 'Item 2'), | ||
(150.25, 'Item 3'), | ||
(300.00, 'Item 4'), | ||
(250.50, 'Item 5'), | ||
(NULL, 'Null Amount'); | ||
GO | ||
~~ROW COUNT: 6~~ | ||
|
||
|
||
-- Test Case 1: MAX aggregation | ||
SELECT MAX(Amount) AS MaxAmount | ||
INTO ResultTable1 | ||
FROM TestMoneyTable; | ||
GO | ||
|
||
-- Test Case 2: MIN aggregation | ||
SELECT MIN(Amount) AS MinAmount | ||
INTO ResultTable2 | ||
FROM TestMoneyTable; | ||
GO | ||
|
||
-- Test Case 3: AVG aggregation | ||
SELECT AVG(Amount) AS AvgAmount | ||
INTO ResultTable3 | ||
FROM TestMoneyTable; | ||
GO | ||
|
||
-- Test Case 4: SUM aggregation | ||
SELECT SUM(Amount) AS TotalAmount | ||
INTO ResultTable4 | ||
FROM TestMoneyTable; | ||
GO | ||
|
||
-- Test Case 5: COUNT aggregation (should remain as INT) | ||
SELECT COUNT(Amount) AS CountAmount | ||
INTO ResultTable5 | ||
FROM TestMoneyTable; | ||
GO | ||
|
||
-- Test Case 6: Multiple aggregations in one query | ||
SELECT | ||
MAX(Amount) AS MaxAmount, | ||
MIN(Amount) AS MinAmount, | ||
AVG(Amount) AS AvgAmount, | ||
SUM(Amount) AS TotalAmount, | ||
COUNT(Amount) AS CountAmount | ||
INTO ResultTable6 | ||
FROM TestMoneyTable; | ||
GO | ||
|
||
-- Test Case 7: Aggregation with GROUP BY | ||
SELECT | ||
Description, | ||
MAX(Amount) AS MaxAmount | ||
INTO ResultTable7 | ||
FROM TestMoneyTable | ||
GROUP BY Description; | ||
GO | ||
|
||
-- Test Case 8: Aggregation with subquery | ||
SELECT MaxAmount | ||
INTO ResultTable8 | ||
FROM ( | ||
SELECT MAX(Amount) AS MaxAmount | ||
FROM TestMoneyTable | ||
) AS Subquery; | ||
GO | ||
|
||
-- Test Case 9: Aggregation with HAVING clause | ||
SELECT | ||
Description, | ||
MAX(Amount) AS MaxAmount | ||
INTO ResultTable9 | ||
FROM TestMoneyTable | ||
GROUP BY Description | ||
HAVING MAX(Amount) > 200; | ||
GO | ||
|
||
-- Test Case 10: Aggregation with calculated MONEY column | ||
SELECT | ||
MAX(Amount * 2) AS DoubleMaxAmount | ||
INTO ResultTable10 | ||
FROM TestMoneyTable; | ||
GO | ||
|
||
-- Negative Test Case: Empty table | ||
CREATE TABLE EmptyMoneyTable (Amount MONEY); | ||
GO | ||
|
||
SELECT MAX(Amount) AS MaxAmount | ||
INTO ResultTableEmpty | ||
FROM EmptyMoneyTable; | ||
GO | ||
|
||
-- Edge Test Case: Extreme values | ||
CREATE TABLE ExtremeMoneyTable (Amount MONEY); | ||
GO | ||
|
||
INSERT INTO ExtremeMoneyTable VALUES | ||
(922337203685477.5807), -- Maximum positive value for MONEY | ||
(-922337203685477.5808); -- Minimum negative value for MONEY | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
SELECT MAX(Amount) AS MaxAmount, MIN(Amount) AS MinAmount | ||
INTO ResultTableExtreme | ||
FROM ExtremeMoneyTable; | ||
GO | ||
|
||
-- Arbitrary Test Case: Mixing NULL and non-NULL values | ||
CREATE TABLE MixedNullMoneyTable (Amount MONEY); | ||
GO | ||
|
||
INSERT INTO MixedNullMoneyTable VALUES | ||
(100.00), (NULL), (200.00), (NULL), (300.00); | ||
GO | ||
~~ROW COUNT: 5~~ | ||
|
||
|
||
SELECT | ||
AVG(Amount) AS AvgAmount, | ||
SUM(Amount) AS TotalAmount, | ||
COUNT(Amount) AS CountNonNull, | ||
COUNT(*) AS CountAll | ||
INTO ResultTableMixedNull | ||
FROM MixedNullMoneyTable; | ||
GO | ||
|
||
-- Edge Test Case: Aggregating calculated values that exceed MONEY range | ||
CREATE TABLE OverflowMoneyTable (Amount MONEY); | ||
GO | ||
|
||
INSERT INTO OverflowMoneyTable VALUES | ||
(922337203685477), (922337203685477); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
SELECT SUM(Amount) AS TotalAmount | ||
INTO ResultTableOverflow | ||
FROM OverflowMoneyTable; | ||
GO | ||
~~ERROR (Code: 33557097)~~ | ||
|
||
~~ERROR (Message: fixeddecimal out of range)~~ | ||
|
||
|
||
-- Negative Test Case: Trying to aggregate non-MONEY column as MONEY | ||
CREATE TABLE NonMoneyTable (Amount VARCHAR(20)); | ||
GO | ||
|
||
INSERT INTO NonMoneyTable VALUES ('100.00'), ('200.00'); | ||
GO | ||
~~ROW COUNT: 2~~ | ||
|
||
|
||
SELECT SUM(CAST(Amount AS MONEY)) AS TotalAmount | ||
INTO ResultTableNonMoney | ||
FROM NonMoneyTable; | ||
GO |
Oops, something went wrong.