public static boolean columnExists(PTable table, String columnName) { try { table.getColumnForColumnName(columnName); return true; } catch (ColumnNotFoundException e) { return false; } catch (AmbiguousColumnException e) { return true; } }
@Override public PColumn getColumnForColumnName(String name) throws ColumnNotFoundException, AmbiguousColumnException { return delegate.getColumnForColumnName(name); }
/** * Returns the column info for the given column for the given table. * * @param table * @param columnName User-specified column name. May be family-qualified or bare. * @return columnInfo associated with the column in the table * @throws SQLException if parameters are null or if column is not found or if column is ambiguous. */ public static ColumnInfo getColumnInfo(PTable table, String columnName) throws SQLException { if (table==null) { throw new SQLException("Table must not be null."); } if (columnName==null) { throw new SQLException("columnName must not be null."); } PColumn pColumn = null; if (columnName.contains(QueryConstants.NAME_SEPARATOR)) { String[] tokens = columnName.split(QueryConstants.NAME_SEPARATOR_REGEX); if (tokens.length!=2) { throw new SQLException(String.format("Unable to process column %s, expected family-qualified name.",columnName)); } String familyName = tokens[0]; String familyColumn = tokens[1]; PColumnFamily family = table.getColumnFamily(familyName); pColumn = family.getPColumnForColumnName(familyColumn); } else { pColumn = table.getColumnForColumnName(columnName); } return getColumnInfo(pColumn); }
@Deprecated private static PColumn getPColumn(PTable table, @Nullable String familyName, String columnName) throws SQLException { if (table==null) { throw new SQLException("Table must not be null."); } if (columnName==null) { throw new SQLException("columnName must not be null."); } // normalize and remove quotes from family and column names before looking up. familyName = SchemaUtil.normalizeIdentifier(familyName); columnName = SchemaUtil.normalizeIdentifier(columnName); PColumn pColumn = null; if (familyName != null) { PColumnFamily family = table.getColumnFamily(familyName); pColumn = family.getPColumnForColumnName(columnName); } else { pColumn = table.getColumnForColumnName(columnName); } return pColumn; }
private static PColumn getColumn(PTable table, @Nullable String familyName, String columnName) throws SQLException { if (table==null) { throw new SQLException("Table must not be null."); } if (columnName==null) { throw new SQLException("columnName must not be null."); } // normalize and remove quotes from family and column names before looking up. familyName = SchemaUtil.normalizeIdentifier(familyName); columnName = SchemaUtil.normalizeIdentifier(columnName); // Column names are always for the data table, so we must translate them if // we're dealing with an index table. if (table.getType() == PTableType.INDEX) { columnName = IndexUtil.getIndexColumnName(familyName, columnName); } PColumn pColumn = null; if (familyName != null) { PColumnFamily family = table.getColumnFamily(familyName); pColumn = family.getPColumnForColumnName(columnName); } else { pColumn = table.getColumnForColumnName(columnName); } return pColumn; }
@Override public ColumnRef resolveColumn(String schemaName, String tableName, String colName) throws SQLException { TableRef tableRef = tableRefs.get(0); boolean resolveCF = false; if (schemaName != null || tableName != null) { String resolvedTableName = tableRef.getTable().getTableName().getString(); String resolvedSchemaName = tableRef.getTable().getSchemaName().getString(); if (schemaName != null && tableName != null) { if ( ! ( schemaName.equals(resolvedSchemaName) && tableName.equals(resolvedTableName) )) { if (!(resolveCF = schemaName.equals(alias))) { throw new ColumnNotFoundException(schemaName, tableName, null, colName); } } } else { // schemaName == null && tableName != null if (tableName != null && !tableName.equals(alias) && (!tableName.equals(resolvedTableName) || !resolvedSchemaName.equals(""))) { resolveCF = true; } } } PColumn column = resolveCF ? tableRef.getTable().getColumnFamily(tableName).getPColumnForColumnName(colName) : tableRef.getTable().getColumnForColumnName(colName); return new ColumnRef(tableRef, column.getPosition()); }
@Override public ColumnRef resolveColumn(String schemaName, String tableName, String colName) throws SQLException { PColumn column = tableName != null ? tableRef.getTable().getColumnFamily(tableName).getPColumnForColumnName(colName) : tableRef.getTable().getColumnForColumnName(colName); return new ColumnRef(tableRef, column.getPosition()); }
@Test public void testSameColumnNameInPKAndNonPK() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); try { String query = "CREATE TABLE t1 (k integer not null primary key, a.k decimal, b.k decimal)"; conn.createStatement().execute(query); PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class); PColumn c = pconn.getTable(new PTableKey(pconn.getTenantId(), "T1")).getColumnForColumnName("K"); assertTrue(SchemaUtil.isPKColumn(c)); } finally { conn.close(); } }
@Test public void testNullAlterTableDefaultRemoved() throws Exception { String ddl = "CREATE TABLE table_with_default (" + "pk INTEGER PRIMARY KEY)"; String ddl2 = "ALTER TABLE table_with_default " + "ADD v CHAR(3) DEFAULT null"; Connection conn = DriverManager.getConnection(getUrl()); conn.createStatement().execute(ddl); conn.createStatement().execute(ddl2); PTable table = conn.unwrap(PhoenixConnection.class).getMetaDataCache() .getTableRef(new PTableKey(null,"TABLE_WITH_DEFAULT")).getTable(); assertNull(table.getColumnForColumnName("V").getExpressionStr()); }
@Test public void testNullDefaultRemoved() throws Exception { String ddl = "CREATE TABLE table_with_default (" + "pk INTEGER PRIMARY KEY, " + "v VARCHAR DEFAULT null)"; Connection conn = DriverManager.getConnection(getUrl()); conn.createStatement().execute(ddl); PTable table = conn.unwrap(PhoenixConnection.class).getMetaDataCache() .getTableRef(new PTableKey(null,"TABLE_WITH_DEFAULT")).getTable(); assertNull(table.getColumnForColumnName("V").getExpressionStr()); }
@Test public void testWhereClause() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); String baseTableName = generateUniqueName(); String childViewName = generateUniqueName(); String grandChildViewName = generateUniqueName(); String baseTableDdl = "CREATE TABLE " + baseTableName + " (" + "A0 CHAR(1) NOT NULL PRIMARY KEY," + "A1 CHAR(1), A2 CHAR (1))"; conn.createStatement().execute(baseTableDdl); conn.createStatement().execute( "CREATE VIEW " + childViewName + " AS SELECT * FROM " + baseTableName + " WHERE A1 = 'X'"); conn.createStatement().execute( "CREATE VIEW " + grandChildViewName + " AS SELECT * FROM " + childViewName + " WHERE A2 = 'Y'"); PTable childViewTable = PhoenixRuntime.getTableNoCache(conn, childViewName); PTable grandChildViewTable = PhoenixRuntime.getTableNoCache(conn, grandChildViewName); assertNotNull(childViewTable.getColumnForColumnName("A1").getViewConstant()); assertNotNull(grandChildViewTable.getColumnForColumnName("A1").getViewConstant()); assertNotNull(grandChildViewTable.getColumnForColumnName("A2").getViewConstant()); }
@Override protected ColumnRef resolveColumn(ColumnParseNode node) throws SQLException { ColumnRef ref = super.resolveColumn(node); PTable table = ref.getTable(); // Track if we need to compare KeyValue during filter evaluation // using column family. If the column qualifier is enough, we // just use that. try { if (!SchemaUtil.isPKColumn(ref.getColumn())) { table.getColumnForColumnName(ref.getColumn().getName().getString()); } } catch (AmbiguousColumnException e) { disambiguateWithFamily = true; } return ref; } }
PTable table = phxConn.getTable(new PTableKey(phxConn.getTenantId(), dataTableFullName)); assertTrue(table.getColumnForColumnName("PK1").isRowTimestamp()); assertFalse(table.getColumnForColumnName("PK2").isRowTimestamp()); assertIsRowTimestampSet(schemaName, dataTableName, "PK1"); table = phxConn.getTable(new PTableKey(phxConn.getTenantId(), dataTableFullName2)); assertFalse(table.getColumnForColumnName("PK1").isRowTimestamp()); assertTrue(table.getColumnForColumnName("PK2").isRowTimestamp()); assertIsRowTimestampSet(schemaName, dataTableName2, "PK2"); String indexColName = IndexUtil.getIndexColumnName(table.getColumnForColumnName("PK2")); assertTrue(indexTable.getColumnForColumnName(indexColName).isRowTimestamp()); assertIsRowTimestampSet(schemaName, indexTableName, indexColName); String viewTableName2 = dataTableName2 + "_VIEW";
@Test public void testOrAggFuncInHaving() throws SQLException { String query = "select count(1) from atable group by a_string having count(1) >= 1 or a_string = 'foo'"; List<Object> binds = Collections.emptyList(); Expressions expressions = compileStatement(query,binds); PColumn aCol = ATABLE.getColumnForColumnName("A_STRING"); Expression h = or( constantComparison(CompareOp.GREATER_OR_EQUAL, new CountAggregateFunction(),1L), constantComparison(CompareOp.EQUAL, new RowKeyColumnExpression(aCol, // a_string comes from group by key in this case new RowKeyValueAccessor(Arrays.<PColumn>asList(aCol), 0)),"foo")); assertNull(expressions.whereClause); assertEquals(h, expressions.havingClause); }
@Test public void testAndPKAndNotPK() throws SQLException { String query = "select * from bugTable where ID = 'i2' and company = 'c3'"; PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class); pconn.createStatement().execute("create table bugTable(ID varchar primary key,company varchar)"); PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query); QueryPlan plan = pstmt.optimizeQuery(); Scan scan = plan.getContext().getScan(); Filter filter = scan.getFilter(); PColumn column = plan.getTableRef().getTable().getColumnForColumnName("COMPANY"); assertEquals( singleKVFilter(constantComparison( CompareOp.EQUAL, new KeyValueColumnExpression(column), "c3")), filter); }
PTable table = conn.getTable(new PTableKey(null, ATABLE_NAME)); ATABLE = table; ORGANIZATION_ID = new ColumnRef(new TableRef(table), table.getColumnForColumnName("ORGANIZATION_ID").getPosition()).newColumnExpression(); ENTITY_ID = new ColumnRef(new TableRef(table), table.getColumnForColumnName("ENTITY_ID").getPosition()).newColumnExpression(); A_INTEGER = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_INTEGER").getPosition()).newColumnExpression(); A_STRING = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_STRING").getPosition()).newColumnExpression(); B_STRING = new ColumnRef(new TableRef(table), table.getColumnForColumnName("B_STRING").getPosition()).newColumnExpression(); A_DATE = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_DATE").getPosition()).newColumnExpression(); A_TIME = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_TIME").getPosition()).newColumnExpression(); A_TIMESTAMP = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_TIMESTAMP").getPosition()).newColumnExpression(); X_DECIMAL = new ColumnRef(new TableRef(table), table.getColumnForColumnName("X_DECIMAL").getPosition()).newColumnExpression(); } finally { conn.close();
String indexColumnFamily = null; try { indexColumn = index.getColumnForColumnName(indexColName); ref = new ColumnRef(tableRef, indexColumn.getPosition()); indexColumnFamily = indexColumn.getFamilyName() == null ? null : indexColumn.getFamilyName().getString();
@Test public void testOrPKWithAndPKAndNotPK() throws SQLException { String query = "select * from bugTable where ID = 'i1' or (ID = 'i2' and company = 'c3')"; PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class); pconn.createStatement().execute("create table bugTable(ID varchar primary key,company varchar)"); PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query); QueryPlan plan = pstmt.optimizeQuery(); Scan scan = plan.getContext().getScan(); Filter filter = scan.getFilter(); Expression idExpression = new ColumnRef(plan.getTableRef(), plan.getTableRef().getTable().getColumnForColumnName("ID").getPosition()).newColumnExpression(); Expression id = new RowKeyColumnExpression(idExpression,new RowKeyValueAccessor(plan.getTableRef().getTable().getPKColumns(),0)); Expression company = new KeyValueColumnExpression(plan.getTableRef().getTable().getColumnForColumnName("COMPANY")); // FilterList has no equals implementation assertTrue(filter instanceof FilterList); FilterList filterList = (FilterList)filter; assertEquals(FilterList.Operator.MUST_PASS_ALL, filterList.getOperator()); assertEquals( Arrays.asList( new SkipScanFilter( ImmutableList.of(Arrays.asList( pointRange("i1"), pointRange("i2"))), SchemaUtil.VAR_BINARY_SCHEMA), singleKVFilter( or(constantComparison(CompareOp.EQUAL,id,"i1"), and(constantComparison(CompareOp.EQUAL,id,"i2"), constantComparison(CompareOp.EQUAL,company,"c3"))))), filterList.getFilters()); }
Filter filter = scan.getFilter(); PTable table = plan.getTableRef().getTable(); Expression aInteger = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_INTEGER").getPosition()).newColumnExpression(); Expression aString = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_STRING").getPosition()).newColumnExpression(); assertEquals( multiEncodedKVFilter(and(
Filter filter = scan.getFilter(); PTable table = plan.getTableRef().getTable(); Expression aInteger = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_INTEGER").getPosition()).newColumnExpression(); Expression aString = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_STRING").getPosition()).newColumnExpression(); assertEquals( multiEncodedKVFilter(and(