Skip to content

Conversation

@mayankadhikary-lab
Copy link

@mayankadhikary-lab mayankadhikary-lab commented Nov 10, 2025

Description: The MOD and DIV operators in MariaDB ColumnStore were not behaving consistently with InnoDB (the standard MariaDB storage engine), leading to incorrect results, particularly with floating-point or DECIMAL types.

  1. MOD Function (func_mod.cpp)
    Analysis and Root Cause
    The MOD operator was failing because the implementation in Func_mod::getDecimalVal only considered the first parameter (parm[0]) for the precision calculation and failed to use the second parameter (parm[1]) at all. This resulted in wrong precision and consequently, an incorrect remainder calculation.
    Fix Summary
    The fix ensures that:
  2. The second parameter (parm[1]) is correctly retrieved and used.
  3. The precision (scale) is determined by taking the maximum precision of both input parameters (d.scale and d1.scale) to guarantee the most accurate result.
  4. The calculation is performed using high-precision integer types (int128_t) to avoid floating-point loss.

Verification result:

image
  1. DIV Function (func_div.cpp)
    Analysis and Root Cause
    The primary issue in the Func_div::getIntVal function was the reliance on the double data type. The function used getDoubleVal to retrieve operands, forcing any high-precision DECIMAL data into the lossy binary floating-point format. This resulted in an inexact quotient that, when cast to int64_t, produced an incorrect integer result due to the inherent loss of precision.
    Fix Summary
    To ensure mathematically exact results for integer division, the fix replaces all reliance on floating-point arithmetic with the native high-precision decimal type:
  2. Dual Decimal Retrieval: Operands are retrieved as IDB_Decimal objects (e.g., using a new getDecimalVal accessor), entirely bypassing the lossy double conversion.
  3. Scale Normalization: The scales of the dividend (dec1) and divisor (dec2) are normalized to a common value to ensure both are represented with the same precision before division.
  4. High-Precision Division: The division is performed using the IDB_Decimal arithmetic methods.
  5. Final Cast: The resulting high-precision decimal quotient is then cast directly to int64_t. Because the division was performed precisely, this final conversion correctly yields the intended integer result

Verification result:

image

@mariadb-LeonidFedorov
Copy link
Collaborator

Thank you for the contribution!
Could you please add the tests covering your fix columnstore/basic/t/mcs227_div_function.test
To run the test and to update the reference output columnstore/basic/r/mcs227_div_function.result, you can use
--record of following script

➜ sudo tests/scripts/run_mtr.sh --help
usage: tests/scripts/run_mtr.sh [OPTIONS]
OPTIONS:

-s --suite:                  whole suite to run
-t --test_full_name:         Testname with suite as like bugfixes.mcol-4899
-f --full:                   Run full MTR [default:false]
-r --record:                 Record the result [default:false]
-e --no-extern:              Run with --extern [default:false]
-c --color:                  run with unbufer to colorize output [default:false]
    -? --help  :  usage

@mayankadhikary-lab
Copy link
Author

Adding the detailed document, containing the description, analysis, code changes and verification result with test cases for both mod and div functions.
Issue - 4601.docx

@drrtuy
Copy link
Collaborator

drrtuy commented Nov 11, 2025

Adding the detailed document, containing the description, analysis, code changes and verification result with test cases for both mod and div functions. Issue - 4601.docx

Could you plz implement the test cases in a form of MTR tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants