Skip to content

Commit

Permalink
add handling for larger Float and Decimal numbers (#2756)
Browse files Browse the repository at this point in the history
* add handling for larger Float and Decimal numbers
* add tracing details to the error response
  • Loading branch information
shtirlets authored Apr 9, 2024
1 parent 27c2a30 commit de4eca6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapSetter;
import java.nio.charset.StandardCharsets;
import java.sql.ParameterMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.SortedSet;
import org.finos.legend.engine.postgres.handler.PostgresResultSet;
import org.finos.legend.engine.postgres.handler.PostgresResultSetMetaData;
import org.finos.legend.engine.postgres.types.PGType;
import org.finos.legend.engine.postgres.types.PGTypes;
import org.finos.legend.engine.postgres.utils.OpenTelemetryUtil;
import org.slf4j.Logger;

/**
Expand Down Expand Up @@ -191,11 +197,23 @@ static void sendAuthenticationError(Channel channel, String message)
METHOD_NAME_CLIENT_AUTH, errorCode);
}

static ChannelFuture sendErrorResponse(Channel channel, /*AccessControl accessControl,*/
Throwable throwable)

private static String buildErrorMessage(Throwable throwable)
{
TextMapSetter<Map> TEXT_MAP_SETTER = (map, key, value) -> Objects.requireNonNull(map).put(key, value);
Map<String, String> keys = new HashMap<>();
OpenTelemetryUtil.getPropagators().inject(Context.current(), keys, TEXT_MAP_SETTER);

StringBuilder errorMessage = new StringBuilder(throwable.getMessage());
keys.entrySet().stream().forEach(e -> errorMessage.append("\n").append(e.getKey()).append(": ").append(e.getValue()));
return errorMessage.toString();
}

static ChannelFuture sendErrorResponse(Channel channel, Throwable throwable)
{
//final PGError error = PGError.fromThrowable(SQLExceptions.prepareForClientTransmission(accessControl, throwable));
final PGError error = PGError.fromThrowable(throwable);
String errorMessage = buildErrorMessage(throwable);
LOGGER.error(errorMessage, throwable);
final PGError error = new PGError(PGErrorStatus.INTERNAL_ERROR, errorMessage, throwable);

ByteBuf buffer = channel.alloc().buffer();
buffer.writeByte('E');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ public Object getObject(int i) throws Exception
return ((LocalDate) temporalAccessor).atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli();
}
case INTEGER:
return ((Number) value).intValue();
return ((Number) value).longValue();
case FLOAT:
return ((Number) value).floatValue();
case NUMBER:
return ((Number) value).doubleValue();
case BOOLEAN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ public int getColumnType(int i) throws Exception
case DATE_TIME:
return Types.TIMESTAMP;
case INTEGER:
return Types.INTEGER;
return Types.BIGINT;
case FLOAT:
return Types.FLOAT;
case NUMBER:
return Types.DOUBLE;
case BOOLEAN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ private synchronized boolean readNextDataRow() throws IOException
value = null;
break;
case VALUE_NUMBER_INT:
value = parser.getIntValue();
value = parser.getLongValue();
break;
case VALUE_NUMBER_FLOAT:
value = parser.getFloatValue();
value = parser.getDoubleValue();
break;
case VALUE_TRUE:
value = parser.getBooleanValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void testMetadata() throws SQLException
Assert.assertEquals("Employee Type", resultSetMetaData.getColumnName(3));
Assert.assertEquals("Full Name", resultSetMetaData.getColumnName(4));
Assert.assertEquals("Derived Name", resultSetMetaData.getColumnName(5));
Assert.assertEquals("int4", resultSetMetaData.getColumnTypeName(1));
Assert.assertEquals("int8", resultSetMetaData.getColumnTypeName(1));
Assert.assertEquals("varchar", resultSetMetaData.getColumnTypeName(2));
Assert.assertEquals("varchar", resultSetMetaData.getColumnTypeName(3));
Assert.assertEquals("varchar", resultSetMetaData.getColumnTypeName(4));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,17 @@ public void testStrictDate() throws Exception
@Test
public void testFloat() throws Exception
{
validate(FLOAT, "5.5", "float4", 5.5F);
validate(FLOAT, "null", "float4", null);
validate(FLOAT, "5.5", "float8", 5.5D);
validate(FLOAT, "null", "float8", null);
validate(FLOAT, "2645198855588.533433343434", "float8", 2645198855588.533433343434D);
}

@Test
public void testInteger() throws Exception
{
validate(INTEGER, "5", "int4", 5);
validate(INTEGER, "null", "int4", null);
validate(INTEGER, "5", "int8", 5L);
validate(INTEGER, "2645198855588", "int8", 2645198855588L);
validate(INTEGER, "null", "int8", null);
}

@Test
Expand Down

0 comments on commit de4eca6

Please sign in to comment.