Skip to content

Commit ff47481

Browse files
committed
Add Coral type system
1 parent 12ab83f commit ff47481

File tree

13 files changed

+1034
-0
lines changed

13 files changed

+1034
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright 2024-2025 LinkedIn Corporation. All rights reserved.
3+
* Licensed under the BSD-2 Clause license.
4+
* See LICENSE in the project root for license information.
5+
*/
6+
package com.linkedin.coral.common.types;
7+
8+
import java.util.Objects;
9+
10+
11+
/**
12+
* Represents an array data type in the Coral type system.
13+
*/
14+
public final class CoralArrayType implements CoralDataType {
15+
private final CoralDataType elementType;
16+
private final boolean nullable;
17+
18+
/**
19+
* Creates a new array type.
20+
* @param elementType the type of elements in the array
21+
* @param nullable whether this type allows null values
22+
*/
23+
public CoralArrayType(CoralDataType elementType, boolean nullable) {
24+
this.elementType = Objects.requireNonNull(elementType, "Element type cannot be null");
25+
this.nullable = nullable;
26+
}
27+
28+
/**
29+
* Returns the type of elements in this array.
30+
* @return the element type
31+
*/
32+
public CoralDataType getElementType() {
33+
return elementType;
34+
}
35+
36+
@Override
37+
public CoralTypeKind getKind() {
38+
return CoralTypeKind.ARRAY;
39+
}
40+
41+
@Override
42+
public boolean isNullable() {
43+
return nullable;
44+
}
45+
46+
@Override
47+
public boolean equals(Object o) {
48+
if (this == o)
49+
return true;
50+
if (o == null || getClass() != o.getClass())
51+
return false;
52+
CoralArrayType that = (CoralArrayType) o;
53+
return nullable == that.nullable && Objects.equals(elementType, that.elementType);
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return Objects.hash(elementType, nullable);
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return "ARRAY<" + elementType + ">" + (nullable ? " NULL" : " NOT NULL");
64+
}
65+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright 2024-2025 LinkedIn Corporation. All rights reserved.
3+
* Licensed under the BSD-2 Clause license.
4+
* See LICENSE in the project root for license information.
5+
*/
6+
package com.linkedin.coral.common.types;
7+
8+
import java.util.Objects;
9+
10+
11+
/**
12+
* Represents a fixed-length character data type in the Coral type system.
13+
*/
14+
public final class CoralCharType implements CoralDataType {
15+
private final int length;
16+
private final boolean nullable;
17+
18+
/**
19+
* Creates a new CHAR type.
20+
* @param length the fixed length of the character string
21+
* @param nullable whether this type allows null values
22+
*/
23+
public CoralCharType(int length, boolean nullable) {
24+
if (length <= 0) {
25+
throw new IllegalArgumentException("Length must be positive, got: " + length);
26+
}
27+
this.length = length;
28+
this.nullable = nullable;
29+
}
30+
31+
/**
32+
* Returns the fixed length of this CHAR type.
33+
* @return the length
34+
*/
35+
public int getLength() {
36+
return length;
37+
}
38+
39+
@Override
40+
public CoralTypeKind getKind() {
41+
return CoralTypeKind.CHAR;
42+
}
43+
44+
@Override
45+
public boolean isNullable() {
46+
return nullable;
47+
}
48+
49+
@Override
50+
public boolean equals(Object o) {
51+
if (this == o)
52+
return true;
53+
if (o == null || getClass() != o.getClass())
54+
return false;
55+
CoralCharType that = (CoralCharType) o;
56+
return length == that.length && nullable == that.nullable;
57+
}
58+
59+
@Override
60+
public int hashCode() {
61+
return Objects.hash(length, nullable);
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return "CHAR(" + length + ")" + (nullable ? " NULL" : " NOT NULL");
67+
}
68+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2024-2025 LinkedIn Corporation. All rights reserved.
3+
* Licensed under the BSD-2 Clause license.
4+
* See LICENSE in the project root for license information.
5+
*/
6+
package com.linkedin.coral.common.types;
7+
8+
/**
9+
* Represents a data type in the Coral type system.
10+
* This interface provides a planner-agnostic abstraction for data types
11+
* that can be converted to various execution engine specific types.
12+
*/
13+
public interface CoralDataType {
14+
/**
15+
* Returns the kind of this data type.
16+
* @return the type kind
17+
*/
18+
CoralTypeKind getKind();
19+
20+
/**
21+
* Returns whether this data type allows null values.
22+
* @return true if nullable, false otherwise
23+
*/
24+
boolean isNullable();
25+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright 2024-2025 LinkedIn Corporation. All rights reserved.
3+
* Licensed under the BSD-2 Clause license.
4+
* See LICENSE in the project root for license information.
5+
*/
6+
package com.linkedin.coral.common.types;
7+
8+
import java.util.Objects;
9+
10+
11+
/**
12+
* Represents a decimal data type with precision and scale in the Coral type system.
13+
*/
14+
public final class CoralDecimalType implements CoralDataType {
15+
private final int precision;
16+
private final int scale;
17+
private final boolean nullable;
18+
19+
/**
20+
* Creates a new decimal type.
21+
* @param precision the total number of digits
22+
* @param scale the number of digits after the decimal point
23+
* @param nullable whether this type allows null values
24+
*/
25+
public CoralDecimalType(int precision, int scale, boolean nullable) {
26+
if (precision <= 0) {
27+
throw new IllegalArgumentException("Precision must be positive, got: " + precision);
28+
}
29+
if (scale < 0 || scale > precision) {
30+
throw new IllegalArgumentException(
31+
"Scale must be non-negative and <= precision, got scale=" + scale + ", precision=" + precision);
32+
}
33+
this.precision = precision;
34+
this.scale = scale;
35+
this.nullable = nullable;
36+
}
37+
38+
/**
39+
* Returns the precision (total number of digits).
40+
* @return the precision
41+
*/
42+
public int getPrecision() {
43+
return precision;
44+
}
45+
46+
/**
47+
* Returns the scale (number of digits after decimal point).
48+
* @return the scale
49+
*/
50+
public int getScale() {
51+
return scale;
52+
}
53+
54+
@Override
55+
public CoralTypeKind getKind() {
56+
return CoralTypeKind.DECIMAL;
57+
}
58+
59+
@Override
60+
public boolean isNullable() {
61+
return nullable;
62+
}
63+
64+
@Override
65+
public boolean equals(Object o) {
66+
if (this == o)
67+
return true;
68+
if (o == null || getClass() != o.getClass())
69+
return false;
70+
CoralDecimalType that = (CoralDecimalType) o;
71+
return precision == that.precision && scale == that.scale && nullable == that.nullable;
72+
}
73+
74+
@Override
75+
public int hashCode() {
76+
return Objects.hash(precision, scale, nullable);
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return "DECIMAL(" + precision + "," + scale + ")" + (nullable ? " NULL" : " NOT NULL");
82+
}
83+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright 2024-2025 LinkedIn Corporation. All rights reserved.
3+
* Licensed under the BSD-2 Clause license.
4+
* See LICENSE in the project root for license information.
5+
*/
6+
package com.linkedin.coral.common.types;
7+
8+
import java.util.Objects;
9+
10+
11+
/**
12+
* Represents a map data type in the Coral type system.
13+
*/
14+
public final class CoralMapType implements CoralDataType {
15+
private final CoralDataType keyType;
16+
private final CoralDataType valueType;
17+
private final boolean nullable;
18+
19+
/**
20+
* Creates a new map type.
21+
* @param keyType the type of keys in the map
22+
* @param valueType the type of values in the map
23+
* @param nullable whether this type allows null values
24+
*/
25+
public CoralMapType(CoralDataType keyType, CoralDataType valueType, boolean nullable) {
26+
this.keyType = Objects.requireNonNull(keyType, "Key type cannot be null");
27+
this.valueType = Objects.requireNonNull(valueType, "Value type cannot be null");
28+
this.nullable = nullable;
29+
}
30+
31+
/**
32+
* Returns the type of keys in this map.
33+
* @return the key type
34+
*/
35+
public CoralDataType getKeyType() {
36+
return keyType;
37+
}
38+
39+
/**
40+
* Returns the type of values in this map.
41+
* @return the value type
42+
*/
43+
public CoralDataType getValueType() {
44+
return valueType;
45+
}
46+
47+
@Override
48+
public CoralTypeKind getKind() {
49+
return CoralTypeKind.MAP;
50+
}
51+
52+
@Override
53+
public boolean isNullable() {
54+
return nullable;
55+
}
56+
57+
@Override
58+
public boolean equals(Object o) {
59+
if (this == o)
60+
return true;
61+
if (o == null || getClass() != o.getClass())
62+
return false;
63+
CoralMapType that = (CoralMapType) o;
64+
return nullable == that.nullable && Objects.equals(keyType, that.keyType)
65+
&& Objects.equals(valueType, that.valueType);
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
return Objects.hash(keyType, valueType, nullable);
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return "MAP<" + keyType + "," + valueType + ">" + (nullable ? " NULL" : " NOT NULL");
76+
}
77+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2024-2025 LinkedIn Corporation. All rights reserved.
3+
* Licensed under the BSD-2 Clause license.
4+
* See LICENSE in the project root for license information.
5+
*/
6+
package com.linkedin.coral.common.types;
7+
8+
import java.util.Objects;
9+
10+
11+
/**
12+
* Represents a primitive data type in the Coral type system.
13+
* This includes basic types like BOOLEAN, INT, DOUBLE, STRING, etc.
14+
*/
15+
public final class CoralPrimitiveType implements CoralDataType {
16+
private final CoralTypeKind kind;
17+
private final boolean nullable;
18+
19+
/**
20+
* Creates a new primitive type.
21+
* @param kind the type kind (must be a primitive type)
22+
* @param nullable whether this type allows null values
23+
*/
24+
public CoralPrimitiveType(CoralTypeKind kind, boolean nullable) {
25+
this.kind = Objects.requireNonNull(kind, "Type kind cannot be null");
26+
this.nullable = nullable;
27+
}
28+
29+
@Override
30+
public CoralTypeKind getKind() {
31+
return kind;
32+
}
33+
34+
@Override
35+
public boolean isNullable() {
36+
return nullable;
37+
}
38+
39+
@Override
40+
public boolean equals(Object o) {
41+
if (this == o)
42+
return true;
43+
if (o == null || getClass() != o.getClass())
44+
return false;
45+
CoralPrimitiveType that = (CoralPrimitiveType) o;
46+
return nullable == that.nullable && kind == that.kind;
47+
}
48+
49+
@Override
50+
public int hashCode() {
51+
return Objects.hash(kind, nullable);
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return kind.name() + (nullable ? " NULL" : " NOT NULL");
57+
}
58+
}

0 commit comments

Comments
 (0)