Skip to content

Handle HugeInt type for DuckDB connector #531

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

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@

public final class DuckdbTypes
{
// other types LIST, ENUM, HUGEINT, UTINYINT, USMALLINT, STRUCT, UUID, JSON, UINTEGER, UBIGINT, INTERVAL, MAP
// other types LIST, ENUM, UTINYINT, USMALLINT, STRUCT, UUID, JSON, UINTEGER, UBIGINT, INTERVAL, MAP
public static final DuckdbType BOOLEAN = new DuckdbType(Types.BOOLEAN, "BOOLEAN");
public static final DuckdbType BIGINT = new DuckdbType(Types.BIGINT, "BIGINT");
public static final DuckdbType HUGEINT = new DuckdbType(Types.DECIMAL, "HUGEINT");
public static final DuckdbType BIT = new DuckdbType(Types.BIT, "BIT");
public static final DuckdbType BLOB = new DuckdbType(Types.BLOB, "BLOB");
public static final DuckdbType DATE = new DuckdbType(Types.DATE, "DATE");
Expand Down Expand Up @@ -86,6 +87,7 @@ public final class DuckdbTypes
.put(TIMESTAMP_WITH_TIMEZONE.getName(), TIMESTAMP_WITH_TIMEZONE)
.put(VARCHAR.getName(), VARCHAR)
.put(JSON.getName(), JSON)
.put(HUGEINT.getName(), HUGEINT)
.build();

private static final Map<Integer, PGType<?>> duckdbTypeToPgTypeMap = ImmutableMap.<Integer, PGType<?>>builder()
Expand Down
19 changes: 15 additions & 4 deletions wren-base/src/main/java/io/wren/base/type/NumericType.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
import javax.annotation.Nonnull;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.nio.charset.StandardCharsets;

public class NumericType
extends PGType<BigDecimal>
extends PGType<Number>
{
static final int OID = 1700;

Expand Down Expand Up @@ -61,7 +62,17 @@ public String type()
}

@Override
public int writeAsBinary(ByteBuf buffer, @Nonnull BigDecimal value)
public int writeAsBinary(ByteBuf buffer, @Nonnull Number value)
{
return switch (value) {
case BigDecimal bigDecimal -> writeAsBinary(buffer, bigDecimal);
// TODO: customize the handling of BigInteger for performance.
case BigInteger bigInteger -> writeAsBinary(buffer, new BigDecimal(bigInteger));
default -> throw new IllegalArgumentException("Unsupported numeric type: " + value.getClass().getName());
};
}

private int writeAsBinary(ByteBuf buffer, @Nonnull BigDecimal value)
{
// Taken from https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/pgwire/types.go#L336
// and https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/numeric.c#L6760.
Expand Down Expand Up @@ -174,13 +185,13 @@ public BigDecimal readBinaryValue(ByteBuf buffer, int valueLength)
}

@Override
public byte[] encodeAsUTF8Text(@Nonnull BigDecimal value)
public byte[] encodeAsUTF8Text(@Nonnull Number value)
{
return value.toString().getBytes(StandardCharsets.UTF_8);
}

@Override
public BigDecimal decodeUTF8Text(byte[] bytes)
public Number decodeUTF8Text(byte[] bytes)
{
return new BigDecimal(new String(bytes, StandardCharsets.UTF_8));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public Object[][] queryModel()
{"SELECT * FROM Orders"},
{"SELECT * FROM Orders WHERE orderkey > 100"},
{"SELECT * FROM Orders a JOIN Customer b ON a.custkey = b.custkey"},
{"SELECT * FROM Orders WHERE nation_name IS NOT NULL"}
{"SELECT * FROM Orders WHERE nation_name IS NOT NULL"},
{"SELECT sum(orderkey) FROM Orders"}, // DuckDB always returns HUGEINT when aggregating integers
};
}

Expand Down
Loading