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-6690] Refactor the Arrow adapter type system #4052

Closed
wants to merge 4 commits into from

Conversation

caicancai
Copy link
Member

@caicancai caicancai commented Nov 17, 2024

@caicancai
Copy link
Member Author

caicancai commented Nov 17, 2024

After this pr, for some data types, we can easily identify their precision and scale such as decimal, time, timestamp

LONG(Primitive.LONG),
BYTE(Primitive.BYTE),
SHORT(Primitive.SHORT);
class ArrowFieldType {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think enums are preferable, since they are typechecked by the compiler.
A Class can be anything.

Copy link
Member Author

@caicancai caicancai Nov 18, 2024

Choose a reason for hiding this comment

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

Later, I may dynamically pass some parameters according to the data type of arrow, which is difficult to achieve through enumeration.
e.g.

new ArrowFieldType(BigDecimal.class,
          ((ArrowType.Decimal) arrowType).getPrecision(),
          ((ArrowType.Decimal) arrowType).getScale())

Copy link
Contributor

Choose a reason for hiding this comment

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

Why later if you are refactoring the code now?
Do it correctly the first time.
I am suggesting to have 3 arguments for the enum constructor: Class, precision, scale.

Copy link
Member Author

@caicancai caicancai Nov 20, 2024

Choose a reason for hiding this comment

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

@mihaibudiu Sorry for the late reply, you are right. I looked at the arrow types and it seems that Class, precision, and scale are sufficient.

I will finish this work on the weekend

Copy link
Member Author

Choose a reason for hiding this comment

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

@mihaibudiu Hi. I have a question, how to dynamically pass parameters in enumeration class? Precision and scale are not constants for decimal, timestamp and other types.

Copy link
Contributor

Choose a reason for hiding this comment

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

What is the CLI?

Copy link
Member Author

Choose a reason for hiding this comment

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

What is the CLI?

Sorry. it is CI

Copy link
Member Author

@caicancai caicancai Nov 28, 2024

Choose a reason for hiding this comment

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

@mihaibudiu I'm using an enum class to represent this, but it doesn't seem ideal, CI always fails.
https://github.com/apache/calcite/actions/runs/12014265742/job/33489690898
https://github.com/apache/calcite/actions/runs/12071684172/job/33663981102?pr=4052

What do you recommend?

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tried to apply the recommendations made in the output of the failed CI run?

/home/runner/work/calcite/calcite/core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java:2654: Note: [MethodCanBeStatic] A private method that does not reference the enclosing instance can be static
    private void onMatch2(
    ^
    (see https://errorprone.info/bugpattern/MethodCanBeStatic)
  Did you mean 'private static void onMatch2('?
/home/runner/work/calcite/calcite/core/src/main/java/org/apache/calcite/util/format/postgresql/format/compiled/NumberCompiledPattern.java:82: Note: [MethodCanBeStatic] A private method that does not reference the enclosing instance can be static
  private String getOrdinalSuffix(String stringValue) {
  ^
    (see https://errorprone.info/bugpattern/MethodCanBeStatic)
  Did you mean 'private static String getOrdinalSuffix(String stringValue) {'?
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :arrow:compileJava
/home/runner/work/calcite/calcite/arrow/src/main/java/org/apache/calcite/adapter/arrow/ArrowTranslator.java:233: Note: [MethodCanBeStatic] A private method that does not reference the enclosing instance can be static
  private String translateUnaryOp(String op, String name) {
  ^
    (see https://errorprone.info/bugpattern/MethodCanBeStatic)
  Did you mean 'private static String translateUnaryOp(String op, String name) {'?
/home/runner/work/calcite/calcite/arrow/src/main/java/org/apache/calcite/adapter/arrow/ArrowFieldType.java:47: warning: [ImmutableEnumChecker] enums should be immutable: 'ArrowFieldType' has non-final field 'precision'
  private int precision;
              ^
    (see https://errorprone.info/bugpattern/ImmutableEnumChecker)
  Did you mean 'private final int precision;'?
/home/runner/work/calcite/calcite/arrow/src/main/java/org/apache/calcite/adapter/arrow/ArrowFieldType.java:48: warning: [ImmutableEnumChecker] enums should be immutable: 'ArrowFieldType' has non-final field 'scale'
  private int scale;
              ^
    (see https://errorprone.info/bugpattern/ImmutableEnumChecker)
  Did you mean 'private final int scale;'?

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 optimized your CI suggestion above, but unfortunately it still didn't work, and the code looks more confusing. I guess it has something to do with the arrow-java build mode.
I'm not sure if we still have to stick to the enumeration class to implement this.

@caicancai caicancai requested a review from mihaibudiu November 18, 2024 14:38
switch (vector.getMinorType()) {
case TINYINT:
switch (field.getName()) {
case "tinyIntField":
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a necessary change? What is the difference between decimalField2 and decimalField?

Copy link
Member Author

Choose a reason for hiding this comment

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

Required, the scale of decimalfield and decimalfiled2 are different, one is 2, the other is 3, the main purpose is to capture the accuracy of whether it is effective

@caicancai
Copy link
Member Author

Sorry for confusing the commit message. I'm not sure if it was my changes that caused the cli to fail. I haven't reproduced the problem locally.

@@ -55,9 +45,20 @@ enum ArrowFieldType {
this.clazz = clazz;
}

ArrowFieldType(Class<?> clazz, int precision, int scale) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the other constructors should call this one.
There are some values in the type system for "unspecified", maybe these could be reused.
Using 0 may not be always safe.

@@ -732,8 +732,8 @@ static void initializeArrowState(@TempDir Path sharedTempDir)
@Test void testFilteredAgg() {
String sql = "select SUM(SAL) FILTER (WHERE COMM > 400) as SALESSUM from EMP";
String plan = "PLAN=EnumerableAggregate(group=[{}], SALESSUM=[SUM($0) FILTER $1])\n"
+ " EnumerableCalc(expr#0..7=[{inputs}], expr#8=[400:DECIMAL(19, 0)], expr#9=[>($t6, $t8)], "
+ "expr#10=[IS TRUE($t9)], SAL=[$t5], $f1=[$t10])\n"
+ " EnumerableCalc(expr#0..7=[{inputs}], expr#8=[CAST($t6):DECIMAL(10, 2)], expr#9=[400.00:DECIMAL(10, 2)], "
Copy link
Contributor

Choose a reason for hiding this comment

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

can you also add or modify a test that uses a decimal number with digits after the decimal point? This would exercise the scale.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a table that comes with calcite and is not filled in the arrow adapter. If you modify it, it will affect many tests.

@caicancai
Copy link
Member Author

I increasingly feel that the ArrowFieldType design is a bit complicated. In fact, it is unnecessary. We can directly convert Arrowtype to RelDataType. I don't understand why there is an extra layer of conversion.

I will refactor this class next weekend to make ArrowFieldType align directly with RelDataType.

cc @mihaibudiu

@caicancai caicancai closed this Dec 1, 2024
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