use of io.trino.spi.TrinoException in project trino by trinodb.
the class StringFunctions method safeCountCodePoints.
private static int safeCountCodePoints(Slice slice) {
int codePoints = 0;
for (int position = 0; position < slice.length(); ) {
int codePoint = tryGetCodePointAt(slice, position);
if (codePoint < 0) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid UTF-8 encoding in characters: " + slice.toStringUtf8());
}
position += lengthOfCodePoint(codePoint);
codePoints++;
}
return codePoints;
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class VarcharToTimestampCast method castToShortTimestamp.
@VisibleForTesting
public static long castToShortTimestamp(int precision, String value) {
checkArgument(precision <= MAX_SHORT_PRECISION, "precision must be less than max short timestamp precision");
Matcher matcher = DateTimes.DATETIME_PATTERN.matcher(value);
if (!matcher.matches()) {
throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value);
}
String year = matcher.group("year");
String month = matcher.group("month");
String day = matcher.group("day");
String hour = matcher.group("hour");
String minute = matcher.group("minute");
String second = matcher.group("second");
String fraction = matcher.group("fraction");
long epochSecond;
try {
epochSecond = ZonedDateTime.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day), hour == null ? 0 : Integer.parseInt(hour), minute == null ? 0 : Integer.parseInt(minute), second == null ? 0 : Integer.parseInt(second), 0, UTC).toEpochSecond();
} catch (DateTimeException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value, e);
}
int actualPrecision = 0;
long fractionValue = 0;
if (fraction != null) {
actualPrecision = fraction.length();
fractionValue = Long.parseLong(fraction);
}
if (actualPrecision > precision) {
fractionValue = round(fractionValue, actualPrecision - precision);
}
// scale to micros
return epochSecond * MICROSECONDS_PER_SECOND + rescale(fractionValue, actualPrecision, 6);
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class VarcharToTimestampCast method castToLongTimestamp.
@VisibleForTesting
public static LongTimestamp castToLongTimestamp(int precision, String value) {
checkArgument(precision > MAX_SHORT_PRECISION && precision <= MAX_PRECISION, "precision out of range");
Matcher matcher = DateTimes.DATETIME_PATTERN.matcher(value);
if (!matcher.matches()) {
throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value);
}
String year = matcher.group("year");
String month = matcher.group("month");
String day = matcher.group("day");
String hour = matcher.group("hour");
String minute = matcher.group("minute");
String second = matcher.group("second");
String fraction = matcher.group("fraction");
long epochSecond;
try {
epochSecond = ZonedDateTime.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day), hour == null ? 0 : Integer.parseInt(hour), minute == null ? 0 : Integer.parseInt(minute), second == null ? 0 : Integer.parseInt(second), 0, UTC).toEpochSecond();
} catch (DateTimeException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value, e);
}
int actualPrecision = 0;
long fractionValue = 0;
if (fraction != null) {
actualPrecision = fraction.length();
fractionValue = Long.parseLong(fraction);
}
if (actualPrecision > precision) {
fractionValue = round(fractionValue, actualPrecision - precision);
}
return longTimestamp(epochSecond, rescale(fractionValue, actualPrecision, 12));
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class PageFunctionCompiler method compileProjectionInternal.
private Supplier<PageProjection> compileProjectionInternal(RowExpression projection, Optional<String> classNameSuffix) {
requireNonNull(projection, "projection is null");
if (projection instanceof InputReferenceExpression) {
InputReferenceExpression input = (InputReferenceExpression) projection;
InputPageProjection projectionFunction = new InputPageProjection(input.getField(), input.getType());
return () -> projectionFunction;
}
if (projection instanceof ConstantExpression) {
ConstantExpression constant = (ConstantExpression) projection;
ConstantPageProjection projectionFunction = new ConstantPageProjection(constant.getValue(), constant.getType());
return () -> projectionFunction;
}
PageFieldsToInputParametersRewriter.Result result = rewritePageFieldsToInputParameters(projection);
CallSiteBinder callSiteBinder = new CallSiteBinder();
// generate Work
ClassDefinition pageProjectionWorkDefinition = definePageProjectWorkClass(result.getRewrittenExpression(), callSiteBinder, classNameSuffix);
Class<?> pageProjectionWorkClass;
try {
pageProjectionWorkClass = defineClass(pageProjectionWorkDefinition, Work.class, callSiteBinder.getBindings(), getClass().getClassLoader());
} catch (Exception e) {
if (Throwables.getRootCause(e) instanceof MethodTooLargeException) {
throw new TrinoException(COMPILER_ERROR, "Query exceeded maximum columns. Please reduce the number of columns referenced and re-run the query.", e);
}
throw new TrinoException(COMPILER_ERROR, e);
}
return () -> new GeneratedPageProjection(result.getRewrittenExpression(), isDeterministic(result.getRewrittenExpression()), result.getInputChannels(), constructorMethodHandle(pageProjectionWorkClass, BlockBuilder.class, ConnectorSession.class, Page.class, SelectedPositions.class));
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class FaultTolerantStageScheduler method updateTaskStatus.
private void updateTaskStatus(TaskStatus taskStatus, Optional<ExchangeSinkInstanceHandle> exchangeSinkInstanceHandle) {
TaskState state = taskStatus.getState();
if (!state.isDone()) {
return;
}
try {
RuntimeException failure = null;
SettableFuture<Void> future;
synchronized (this) {
TaskId taskId = taskStatus.getTaskId();
runningTasks.remove(taskId);
future = taskFinishedFuture;
if (!runningTasks.isEmpty()) {
taskFinishedFuture = SettableFuture.create();
} else {
taskFinishedFuture = null;
}
NodeAllocator.NodeLease nodeLease = requireNonNull(runningNodes.remove(taskId), () -> "node not found for task id: " + taskId);
nodeLease.release();
int partitionId = taskId.getPartitionId();
if (!finishedPartitions.contains(partitionId) && !closed) {
switch(state) {
case FINISHED:
finishedPartitions.add(partitionId);
if (sinkExchange.isPresent()) {
checkArgument(exchangeSinkInstanceHandle.isPresent(), "exchangeSinkInstanceHandle is expected to be present");
sinkExchange.get().sinkFinished(exchangeSinkInstanceHandle.get());
}
partitionToRemoteTaskMap.get(partitionId).forEach(RemoteTask::abort);
break;
case CANCELED:
log.debug("Task cancelled: %s", taskId);
break;
case ABORTED:
log.debug("Task aborted: %s", taskId);
break;
case FAILED:
ExecutionFailureInfo failureInfo = taskStatus.getFailures().stream().findFirst().map(this::rewriteTransportFailure).orElse(toFailure(new TrinoException(GENERIC_INTERNAL_ERROR, "A task failed for an unknown reason")));
log.warn(failureInfo.toException(), "Task failed: %s", taskId);
ErrorCode errorCode = failureInfo.getErrorCode();
int taskRemainingAttempts = remainingAttemptsPerTask.getOrDefault(partitionId, maxRetryAttemptsPerTask);
if (remainingRetryAttemptsOverall > 0 && taskRemainingAttempts > 0 && (errorCode == null || errorCode.getType() != USER_ERROR)) {
remainingRetryAttemptsOverall--;
remainingAttemptsPerTask.put(partitionId, taskRemainingAttempts - 1);
// update memory limits for next attempt
MemoryRequirements memoryLimits = partitionMemoryRequirements.get(partitionId);
verify(memoryLimits != null);
MemoryRequirements newMemoryLimits = partitionMemoryEstimator.getNextRetryMemoryRequirements(session, memoryLimits, errorCode);
partitionMemoryRequirements.put(partitionId, newMemoryLimits);
// reschedule
queuedPartitions.add(partitionId);
log.debug("Retrying partition %s for stage %s", partitionId, stage.getStageId());
} else {
failure = failureInfo.toException();
}
break;
default:
throw new IllegalArgumentException("Unexpected task state: " + state);
}
}
}
if (failure != null) {
// must be called outside the lock
fail(failure);
}
if (future != null && !future.isDone()) {
future.set(null);
}
} catch (Throwable t) {
fail(t);
}
}
Aggregations