Search in sources :

Example 31 with TrinoException

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;
}
Also used : TrinoException(io.trino.spi.TrinoException) Constraint(io.trino.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint)

Example 32 with TrinoException

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);
}
Also used : DateTimeException(java.time.DateTimeException) Matcher(java.util.regex.Matcher) TrinoException(io.trino.spi.TrinoException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 33 with TrinoException

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));
}
Also used : DateTimeException(java.time.DateTimeException) Matcher(java.util.regex.Matcher) TrinoException(io.trino.spi.TrinoException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 34 with TrinoException

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));
}
Also used : InputReferenceExpression(io.trino.sql.relational.InputReferenceExpression) ConstantPageProjection(io.trino.operator.project.ConstantPageProjection) ConstantExpression(io.trino.sql.relational.ConstantExpression) GeneratedPageProjection(io.trino.operator.project.GeneratedPageProjection) Page(io.trino.spi.Page) ClassDefinition(io.airlift.bytecode.ClassDefinition) TrinoException(io.trino.spi.TrinoException) MethodTooLargeException(org.objectweb.asm.MethodTooLargeException) InputPageProjection(io.trino.operator.project.InputPageProjection) PageFieldsToInputParametersRewriter(io.trino.operator.project.PageFieldsToInputParametersRewriter) MethodTooLargeException(org.objectweb.asm.MethodTooLargeException) SelectedPositions(io.trino.operator.project.SelectedPositions) Work(io.trino.operator.Work) TrinoException(io.trino.spi.TrinoException) ConnectorSession(io.trino.spi.connector.ConnectorSession) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 35 with TrinoException

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);
    }
}
Also used : TaskId(io.trino.execution.TaskId) MemoryRequirements(io.trino.execution.scheduler.PartitionMemoryEstimator.MemoryRequirements) RemoteTask(io.trino.execution.RemoteTask) ExecutionFailureInfo(io.trino.execution.ExecutionFailureInfo) TrinoException(io.trino.spi.TrinoException) ErrorCode(io.trino.spi.ErrorCode) MoreFutures.asVoid(io.airlift.concurrent.MoreFutures.asVoid) TaskState(io.trino.execution.TaskState)

Aggregations

TrinoException (io.trino.spi.TrinoException)623 IOException (java.io.IOException)151 ImmutableList (com.google.common.collect.ImmutableList)105 List (java.util.List)100 Type (io.trino.spi.type.Type)93 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)90 SchemaTableName (io.trino.spi.connector.SchemaTableName)83 Path (org.apache.hadoop.fs.Path)83 Optional (java.util.Optional)79 ArrayList (java.util.ArrayList)77 Map (java.util.Map)76 ImmutableMap (com.google.common.collect.ImmutableMap)70 Objects.requireNonNull (java.util.Objects.requireNonNull)69 ConnectorSession (io.trino.spi.connector.ConnectorSession)63 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)62 ImmutableSet (com.google.common.collect.ImmutableSet)56 VarcharType (io.trino.spi.type.VarcharType)54 Set (java.util.Set)54 Slice (io.airlift.slice.Slice)53 Table (io.trino.plugin.hive.metastore.Table)52