PHOENIX-1394 getColumnDisplaySize() method returns incorrect value for varchar columns (Samarth Jain)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/efd5b217 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/efd5b217 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/efd5b217 Branch: refs/heads/3.0 Commit: efd5b217941fb6c6484f2cdc7b07ee3757139df2 Parents: 4f1df52 Author: James Taylor <jtay...@salesforce.com> Authored: Thu Nov 6 19:22:08 2014 -0800 Committer: James Taylor <jtay...@salesforce.com> Committed: Thu Nov 6 22:41:36 2014 -0800 ---------------------------------------------------------------------- .../phoenix/jdbc/PhoenixResultSetMetaData.java | 18 +++----- .../jdbc/PhoenixResultSetMetadataTest.java | 45 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/efd5b217/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixResultSetMetaData.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixResultSetMetaData.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixResultSetMetaData.java index b58d5ad..30e9862 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixResultSetMetaData.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixResultSetMetaData.java @@ -55,9 +55,7 @@ import org.apache.phoenix.schema.PDataType; * @since 0.1 */ public class PhoenixResultSetMetaData implements ResultSetMetaData { - private static final int MIN_DISPLAY_WIDTH = 3; - private static final int MAX_DISPLAY_WIDTH = 40; - private static final int DEFAULT_DISPLAY_WIDTH = 10; + static final int DEFAULT_DISPLAY_WIDTH = 40; private final RowProjector rowProjector; private final PhoenixConnection connection; @@ -85,21 +83,19 @@ public class PhoenixResultSetMetaData implements ResultSetMetaData { @Override public int getColumnDisplaySize(int column) throws SQLException { ColumnProjector projector = rowProjector.getColumnProjector(column-1); - int displaySize = Math.max(projector.getName().length(),MIN_DISPLAY_WIDTH); PDataType type = projector.getExpression().getDataType(); if (type == null) { - return Math.min(Math.max(displaySize, QueryConstants.NULL_DISPLAY_TEXT.length()), MAX_DISPLAY_WIDTH); + return QueryConstants.NULL_DISPLAY_TEXT.length(); } if (type.isCoercibleTo(PDataType.DATE)) { - return Math.min(Math.max(displaySize, connection.getDatePattern().length()), MAX_DISPLAY_WIDTH); + return connection.getDatePattern().length(); } - if (type.isFixedWidth() && projector.getExpression().getMaxLength() != null) { - return Math.min(Math.max(displaySize, projector.getExpression().getMaxLength()), MAX_DISPLAY_WIDTH); + if (projector.getExpression().getMaxLength() != null) { + return projector.getExpression().getMaxLength(); } - - return Math.min(Math.max(displaySize, DEFAULT_DISPLAY_WIDTH), MAX_DISPLAY_WIDTH); + return DEFAULT_DISPLAY_WIDTH; } - + @Override public String getColumnLabel(int column) throws SQLException { return rowProjector.getColumnProjector(column-1).getName(); http://git-wip-us.apache.org/repos/asf/phoenix/blob/efd5b217/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixResultSetMetadataTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixResultSetMetadataTest.java b/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixResultSetMetadataTest.java new file mode 100644 index 0000000..9153595 --- /dev/null +++ b/phoenix-core/src/test/java/org/apache/phoenix/jdbc/PhoenixResultSetMetadataTest.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.phoenix.jdbc; + +import static org.junit.Assert.assertEquals; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; + +import org.apache.phoenix.query.BaseConnectionlessQueryTest; +import org.apache.phoenix.query.QueryConstants; +import org.junit.Test; + +public class PhoenixResultSetMetadataTest extends BaseConnectionlessQueryTest { + + @Test + public void testColumnDisplaySize() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + conn.createStatement().execute( + "CREATE TABLE T (pk1 CHAR(15) not null, pk2 VARCHAR not null, v1 VARCHAR(15), v2 DATE, v3 VARCHAR " + + "CONSTRAINT pk PRIMARY KEY (pk1, pk2)) "); + ResultSet rs = conn.createStatement().executeQuery("SELECT pk1, pk2, v1, v2, NULL FROM T"); + assertEquals(15, rs.getMetaData().getColumnDisplaySize(1)); + assertEquals(PhoenixResultSetMetaData.DEFAULT_DISPLAY_WIDTH, rs.getMetaData().getColumnDisplaySize(2)); + assertEquals(15, rs.getMetaData().getColumnDisplaySize(3)); + assertEquals(conn.unwrap(PhoenixConnection.class).getDatePattern().length(), rs.getMetaData().getColumnDisplaySize(4)); + assertEquals(QueryConstants.NULL_DISPLAY_TEXT.length(), rs.getMetaData().getColumnDisplaySize(5)); + } +}