Implementation of decimal rules. This is handled in the planner in the normal execution paths.
| 744 | // Implementation of decimal rules. This is handled in the planner in the normal |
| 745 | // execution paths. |
| 746 | ColumnType GetResultType(const ColumnType& t1, const ColumnType& t2, Op op, bool v2) { |
| 747 | // TODO: Implement V2 result types |
| 748 | switch (op) { |
| 749 | case ADD: |
| 750 | case SUBTRACT: |
| 751 | return ColumnType::CreateDecimalType( |
| 752 | max(t1.scale, t2.scale) + |
| 753 | max(t1.precision - t1.scale, t2.precision - t2.scale) + 1, |
| 754 | max(t1.scale, t2.scale)); |
| 755 | case MULTIPLY: |
| 756 | return ColumnType::CreateDecimalType( |
| 757 | t1.precision + t2.precision, t1.scale + t2.scale); |
| 758 | case DIVIDE: |
| 759 | if (v2) { |
| 760 | int result_scale = |
| 761 | max(ColumnType::MIN_ADJUSTED_SCALE, t1.scale + t2.precision + 1); |
| 762 | int result_precision =t1.precision - t1.scale + t2.scale + result_scale; |
| 763 | return ColumnType::CreateAdjustedDecimalType(result_precision, result_scale); |
| 764 | } else { |
| 765 | return ColumnType::CreateDecimalType( |
| 766 | min(ColumnType::MAX_PRECISION, |
| 767 | t1.precision - t1.scale + t2.scale + max(4, t1.scale + t2.precision + 1)), |
| 768 | min(ColumnType::MAX_PRECISION, max(4, t1.scale + t2.precision + 1))); |
| 769 | } |
| 770 | case MOD: |
| 771 | return ColumnType::CreateDecimalType( |
| 772 | min(t1.precision - t1.scale, t2.precision - t2.scale) + max(t1.scale, t2.scale), |
| 773 | max(t1.scale, t2.scale)); |
| 774 | default: |
| 775 | EXPECT_TRUE(false); |
| 776 | return ColumnType(); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | TEST(DecimalTest, ResultTypes) { |
| 781 | ColumnType t1 = ColumnType::CreateDecimalType(38, 10); |
no test coverage detected