Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.prestosql.server.protocol;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableList;
import io.prestosql.Session;
import io.prestosql.client.ClientCapabilities;
import io.prestosql.client.Column;
import io.prestosql.spi.Page;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.block.Block;
import io.prestosql.spi.block.BlockBuilder;
import io.prestosql.spi.connector.ConnectorSession;
import io.prestosql.spi.type.ArrayType;
import io.prestosql.spi.type.MapType;
import io.prestosql.spi.type.RowType;
import io.prestosql.spi.type.SqlTime;
import io.prestosql.spi.type.SqlTimestamp;
import io.prestosql.spi.type.SqlTimestampWithTimeZone;
import io.prestosql.spi.type.TimeType;
import io.prestosql.spi.type.TimestampType;
import io.prestosql.spi.type.TimestampWithTimeZoneType;
import io.prestosql.spi.type.Type;
import javax.annotation.Nullable;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Verify.verify;
import static com.google.common.base.Verify.verifyNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.prestosql.spi.StandardErrorCode.SERIALIZATION_ERROR;
import static io.prestosql.spi.type.BigintType.BIGINT;
import static io.prestosql.spi.type.BooleanType.BOOLEAN;
import static java.lang.String.format;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
public class QueryResultRows
extends AbstractIterator>
implements Iterable>
{
private final ConnectorSession session;
private final Optional> columns;
private final Deque pages;
private final Optional> exceptionConsumer;
private final long totalRows;
private final boolean supportsParametricDateTime;
private Page currentPage;
private int rowPosition = -1;
private int inPageIndex = -1;
private QueryResultRows(Session session, Optional> columns, List pages, Consumer exceptionConsumer)
{
this.session = requireNonNull(session, "session is null").toConnectorSession();
this.columns = requireNonNull(columns, "columns is null");
this.pages = new ArrayDeque<>(requireNonNull(pages, "pages is null"));
this.exceptionConsumer = Optional.ofNullable(exceptionConsumer);
this.totalRows = countRows(pages);
this.currentPage = this.pages.pollFirst();
this.supportsParametricDateTime = session.getClientCapabilities().contains(ClientCapabilities.PARAMETRIC_DATETIME.toString());
verify(totalRows == 0 || (totalRows > 0 && columns.isPresent()), "data present without columns and types");
}
public boolean isEmpty()
{
return totalRows == 0;
}
public Optional> getColumns()
{
return columns.map(columns -> columns.stream()
.map(ColumnAndType::getColumn)
.collect(toImmutableList()));
}
/**
* Returns expected row count (we don't know yet if every row is serializable).
*/
@VisibleForTesting
public long getTotalRowsCount()
{
return totalRows;
}
public Optional getUpdateCount()
{
// We should have exactly single bigint value as an update count.
if (totalRows != 1 || columns.isEmpty()) {
return Optional.empty();
}
List columns = this.columns.get();
if (columns.size() != 1 || !columns.get(0).getType().equals(BIGINT)) {
return Optional.empty();
}
verifyNotNull(currentPage, "currentPage is null");
Number value = (Number) columns.get(0).getType().getObjectValue(session, currentPage.getBlock(0), 0);
return Optional.ofNullable(value).map(Number::longValue);
}
@Override
protected List