Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-4240] SqlTypeUtil#getMaxPrecisionScaleDecimal returns a decimal that with same precision and scale (Jiatao Tao) #2161

Closed
wants to merge 1 commit into from

Conversation

Aaaaaaron
Copy link
Member

@Aaaaaaron Aaaaaaron commented Sep 21, 2020

Hi @danny0405 , can you take a look at this PR~

"maxScale" should not greater than "maxPrecision". If they are equal, e.g. Decimal(19,19) means we can only have decimal places.

@Aaaaaaron
Copy link
Member Author

Still have some ut failed, will fix than, with review comments.

@julianhyde
Copy link
Contributor

julianhyde commented Sep 21, 2020

Remove the word 'fix' from the commit message. Change the message so that it is clear what the bug is.

Add space before '(' in commit message.

Why would DECIMAL(10, 15) not be valid? Say if you are measuring a small quantify such as the Gravitational constant, 6.674 * 10-11.

@Aaaaaaron
Copy link
Member Author

Aaaaaaron commented Sep 22, 2020

Remove the word 'fix' from the commit message. Change the message so that it is clear what the bug is.

Add space before '(' in commit message.

Why would DECIMAL(10, 15) not be valid? Say if you are measuring a small quantify such as the Gravitational constant, 6.674 * 10-11.

Hi @julianhyde , Roger the message irregular, will refine latter.

From my understanding, precision represents the total number of digits that can be represented by the column, so it must >= than scale.
DECIMAL(15, 10) means that the total number of digits is 15, and the decimal place is 10(in other words, the number of integer place is 5).

If you execute create table test (a decimal(1,3)); in MySQL, will occur error: Schema Error: Error: ER_M_BIGGER_THAN_D: For float(M,D), double(M,D) or decimal(M,D), M must be >= D (column 'a').

Also in Spark/hive:

scala> spark.sql("CREATE TABLE foo (a DECIMAL(5, 7))").collect
org.apache.spark.sql.catalyst.parser.ParseException:
Decimal scale (7) cannot be greater than precision (5).

hive> CREATE TABLE foo (a DECIMAL(5, 7));
2020-09-22 14:38:55,062	FAILED: IllegalArgumentException Decimal scale must be less than or equal to precision

@Aaaaaaron Aaaaaaron changed the title [CALCITE-4240] Fix SqlTypeUtil#getMaxPrecisionScaleDecimal returns a decimal that with same precision and scale(Jiatao Tao) [CALCITE-4240] SqlTypeUtil#getMaxPrecisionScaleDecimal returns a decimal that with same precision and scale (Jiatao Tao) Sep 22, 2020
// means we can only have decimal places.
while (maxScale >= maxPrecision) {
maxScale = maxScale / 2;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid not all the sql engines have the rule that maxScale = maxScale / 2, we should add a check for the validity instead of modifying the value silently. And i'm inclined to fix the default max precision/scale for Calcite type system.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid not all the sql engines have the rule that maxScale = maxScale / 2, we should add a check for the validity instead of modifying the value silently. And i'm inclined to fix the default max precision/scale for Calcite type system.

This is also a proposal, I'll take a look at Hive/Spark decimal's max scale/repcision, but I'm afraid they may be the same, or we don't get max but get default?

Copy link
Member Author

@Aaaaaaron Aaaaaaron Sep 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spark

Default: (10, 0)
Max: The precision can be up to 38, scale can also be up to 38 (less or equal to precision).

https://spark.apache.org/docs/latest/api/java/org/apache/spark/sql/types/DecimalType.html

Hive

Default: (10, 0)
Max: Decimal precision out of allowed range [1,38], Decimal scale out of allowed range [0,38]

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types

The "getMaxPrecisionScaleDecimal" means suppose to get a max Decimal, but the Decimal(max, max) is not this meaning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the impression that many systems support max precision scale as decimal(38, 18).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the impression that many systems support max precision scale as decimal(38, 18).

Hi @danny0405, IMO the specific number here is not so important (like18 or 19), we just need return a big decimal here, so can we finalize a number just like Decimal(38, 18)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just make the valid max scale half of the max precision, e.g. the max scale = max precision/2.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just make the valid max scale half of the max precision, e.g. the max scale = max precision/2.

OK

@danny0405
Copy link
Contributor

Thanks, can you fix the conflicts.

@Aaaaaaron Aaaaaaron force-pushed the CALCITE-4240 branch 4 times, most recently from 857dcc6 to 0ef386c Compare October 10, 2020 02:55
@danny0405
Copy link
Contributor

The tests still fails.

@Aaaaaaron
Copy link
Member Author

The tests still fails.

I'm working on this 😂

expr("'12.3' * '5'")
.columnType("DECIMAL(19, 19) NOT NULL");
.columnType("DECIMAL(19, 18) NOT NULL");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here comes the 18
image

expr("12.3/'5.1'")
.columnType("DECIMAL(19, 0) NOT NULL");
.columnType("DECIMAL(19, 8) NOT NULL");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here comes the 8, by the way, the origin scale 0 is not reasonable
image

…mal that with same precision and scale (Jiatao Tao)
@Aaaaaaron
Copy link
Member Author

Hi @danny0405 the ut passed, would you take a look?

@danny0405 danny0405 added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Oct 13, 2020
Copy link
Contributor

@danny0405 danny0405 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@danny0405 danny0405 closed this in 16b22b1 Oct 14, 2020
MalteBellmann pushed a commit to caespes/calcite that referenced this pull request Feb 21, 2021
…mal that with same precision and scale (Jiatao Tao)

The SqlTypeUtil#getMaxPrecisionScaleDecimal now returns decimal type with max
precision and scale half of that.

Previously it returns DECIMAL(19, 19) which is invalid.

close apache#2161
XuQianJin-Stars pushed a commit to XuQianJin-Stars/calcite that referenced this pull request Jul 14, 2021
…mal that with same precision and scale (Jiatao Tao)

The SqlTypeUtil#getMaxPrecisionScaleDecimal now returns decimal type with max
precision and scale half of that.

Previously it returns DECIMAL(19, 19) which is invalid.

close apache#2161
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LGTM-will-merge-soon Overall PR looks OK. Only minor things left. slow-tests-needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants