/** * Submits query via JDBC * * @param query SQL query string * @param statement sql statement to execute the query with * @param outputFilename name of file result set is to be written to * @param timeout time allowed for query execution * @throws Exception */ public void submitQueryJDBC( String query, Statement statement, String outputFilename, long timeout) throws Exception { BufferedWriter writer = null; if (outputFilename != null) { writer = new BufferedWriter(new FileWriter(new File(outputFilename))); } ResultSet resultSet = null; try { RunThread runThread = new RunThread(statement, query); processThread(runThread, timeout); resultSet = runThread.getResultSet(); if (resultSet == null) { throw runThread.getException(); } if (outputFilename == null) { return; } int columnCount = resultSet.getMetaData().getColumnCount(); columnLabels = new ArrayList<String>(); for (int i = 1; i <= columnCount; i++) { columnLabels.add(resultSet.getMetaData().getColumnLabel(i)); } Object[] types = new Object[columnCount]; for (int i = 1; i <= columnCount; i++) { types[i - 1] = resultSet.getMetaData().getColumnType(i); } ColumnList.setTypes(types); LOG.debug("Result set data types:"); LOG.debug(Utils.getTypesInStrings(ColumnList.getTypes())); while (resultSet.next()) { Object[] values = new Object[columnCount]; for (int i = 1; i <= columnCount; i++) { try { if (resultSet.getObject(i) == null) { values[i - 1] = null; continue; } values[i - 1] = new String(resultSet.getBytes(i)); } catch (Exception e) { if (resultSet.getMetaData().getColumnType(i) == Types.DATE) { values[i - 1] = resultSet.getDate(i); } else { values[i - 1] = resultSet.getObject(i); } } } ColumnList columnList = new ColumnList(values); if (writer != null) { writer.write(columnList + "\n"); } } if (writer != null) { writer.close(); } } finally { if (resultSet != null) { resultSet.close(); } } }
/** Tests that a column list with one element has a size of 1. */ @Test public void testSizeOneElement() { List<String> element = new ArrayList<String>(1); element.add(ELEMENT_A); ColumnList columnList = new ColumnList(element); Assert.assertEquals(1, columnList.size()); }
/** * Tests that when a column list with one element being just the parent and another element being * a parent with a child has a size of 1 because the size is based on the number at that depth, * not the entire tree. */ @Test public void testSizeParentAndChild() { List<String> element = new ArrayList<String>(1); element.add(ELEMENT_A); element.add(ELEMENT_A_CHILD_A); ColumnList columnList = new ColumnList(element); Assert.assertEquals(1, columnList.size()); }
/** * Tests that creating a complex list and then returning it to a list results in, effectively, the * same list, although the elements may be reordered. */ @Test public void testToList() { List<String> element = new ArrayList<String>(1); element.add(ELEMENT_A_CHILD_A); element.add(ELEMENT_A_CHILD_B); element.add(ELEMENT_B); ColumnList columnList = new ColumnList(element); // First, get the result list. List<String> result = columnList.toList(); // Then, check to be sure the lists are the same length. Assert.assertEquals(element.size(), result.size()); // Finally, check that they contain the same elements. element.removeAll(result); Assert.assertEquals(0, element.size()); }
/** Creates a DrillScan. */ public DrillScanRel( final RelOptCluster cluster, final RelTraitSet traits, final RelOptTable table, final RelDataType rowType, final List<SchemaPath> columns) { super(DRILL_LOGICAL, cluster, traits, table); this.settings = PrelUtil.getPlannerSettings(cluster.getPlanner()); this.rowType = rowType; if (columns == null) { // planner asks to scan all of the columns this.columns = ColumnList.all(); } else if (columns.size() == 0) { // planner asks to skip all of the columns this.columns = ColumnList.none(); } else { // planner asks to scan some columns this.columns = ColumnList.some(columns); } try { this.groupScan = drillTable.getGroupScan().clone(this.columns); } catch (final IOException e) { throw new DrillRuntimeException("Failure creating scan.", e); } }
/** * Turns a TopcatModel into a StarTable, ready for serialization. * * @param tcModel model * @return table */ public StarTable encode(TopcatModel tcModel) { /* Prepare table data and metadata for use and adjustment. */ final StarTable dataModel = tcModel.getDataModel(); List<DescribedValue> paramList = new ArrayList<DescribedValue>(); long nrow = dataModel.getRowCount(); ColumnStarTable extraTable = ColumnStarTable.makeTableWithRows(nrow); /* Mark as serialized TopcatModel. */ paramList.add(new DescribedValue(IS_TCMODEL_INFO, Boolean.TRUE)); paramList.add(new DescribedValue(VERSION_INFO, TopcatUtils.getVersion())); /* Record label. */ paramList.add(new DescribedValue(LABEL_INFO, tcModel.getLabel())); /* Record column sequences. */ ColumnList colList = tcModel.getColumnList(); int nCol = colList.size(); int[] icols = new int[nCol]; boolean[] activs = new boolean[nCol]; for (int jc = 0; jc < nCol; jc++) { icols[jc] = colList.getColumn(jc).getModelIndex(); activs[jc] = colList.isActive(jc); } paramList.add(new DescribedValue(COLS_INDEX_INFO, icols)); paramList.add(new DescribedValue(COLS_VISIBLE_INFO, activs)); /* Record whether to broadcast rows. */ paramList.add(new DescribedValue(SEND_ROWS_INFO, tcModel.getRowSendModel().isSelected())); /* Record sort order. */ SortOrder sortOrder = tcModel.getSelectedSort(); TableColumn sortCol = sortOrder == null ? null : sortOrder.getColumn(); if (sortCol != null) { int icolSort = tcModel.getColumnList().indexOf(sortCol); if (icolSort >= 0) { boolean sense = tcModel.getSortSenseModel().isSelected(); paramList.add(new DescribedValue(SORT_COLUMN_INFO, new Integer(icolSort))); paramList.add(new DescribedValue(SORT_SENSE_INFO, Boolean.valueOf(sense))); } } /* Store row subset flags in a new column. */ List<RowSubset> subsetList = new ArrayList<RowSubset>(tcModel.getSubsets()); boolean hadAll = subsetList.remove(RowSubset.ALL); assert hadAll; RowSubset[] subsets = subsetList.toArray(new RowSubset[0]); if (subsets.length > 0) { String[] subsetNames = new String[subsets.length]; for (int is = 0; is < subsets.length; is++) { subsetNames[is] = subsets[is].getName(); } paramList.add(new DescribedValue(SUBSET_NAMES_INFO, subsetNames)); Object flagsArray = createFlagsArray(dataModel, subsets); if (flagsArray != null) { ColumnData flagsCol = ArrayColumn.makeColumn(SUBSET_FLAGS_INFO.getName(), flagsArray); ColumnInfo info = new ColumnInfo(SUBSET_FLAGS_INFO); info.setContentClass(flagsCol.getColumnInfo().getContentClass()); flagsCol.setColumnInfo(info); extraTable.addColumn(flagsCol); } } /* Record current subset. */ int iset = subsetList.indexOf(tcModel.getSelectedSubset()); if (iset >= 0) { paramList.add(new DescribedValue(CURRENT_SUBSET_INFO, new Integer(iset))); } /* Copy parameters from the input table. * Be paranoid about possible name clashes. */ for (Iterator it = dataModel.getParameters().iterator(); it.hasNext(); ) { Object item = it.next(); if (item instanceof DescribedValue) { DescribedValue dval = (DescribedValue) item; String name = dval.getInfo().getName(); String utype = dval.getInfo().getUtype(); if (!isCodecUtype(utype)) { paramList.add(dval); } } } /* Prepare the output table object. */ List<StarTable> joinList = new ArrayList<StarTable>(); joinList.add(dataModel); if (extraTable.getColumnCount() > 0) { joinList.add(extraTable); } StarTable[] joins = joinList.toArray(new StarTable[0]); StarTable outTable = new MetaCopyStarTable(new JoinStarTable(joinList.toArray(new StarTable[0]))); outTable.setName(dataModel.getName()); /* Set the parameters. */ outTable.getParameters().clear(); outTable.getParameters().addAll(paramList); /* Return the result. */ return outTable; }
/** * Does the work for the decoding. May throw an unchecked exception, for instance a * ClassCastException if certain metadata items are present but have the wrong type (not likely * excepting deliberate sabotage, but conceivable). * * @param table encoded table * @param location table location string * @param controlWindow control window * @return topcat model, or null */ private TopcatModel doDecode(StarTable inTable, String location, ControlWindow controlWindow) { CodecTable codec = new CodecTable(inTable); /* Determine if this is a TopcatModel encoded by this class. */ if (!Boolean.TRUE.equals(codec.getCodecValue(IS_TCMODEL_INFO))) { return null; } TopcatModel tcModel = TopcatModel.createRawTopcatModel(codec.getDataTable(), location, controlWindow); /* Get label. */ tcModel.setLabel((String) codec.getCodecValue(LABEL_INFO)); /* Get columns. This is a bit involved, since a TopcatModel has * both a TableColumnModel and a ColumnList which must be updated * in a consistent way, to reflect the column order and which * columns are currently visible. */ /* First get a record of the order of columns and their visibility. */ int[] icols = (int[]) codec.getCodecValue(COLS_INDEX_INFO); boolean[] activs = (boolean[]) codec.getCodecValue(COLS_VISIBLE_INFO); TableColumnModel colModel = tcModel.getColumnModel(); ColumnList colList = tcModel.getColumnList(); int ncol = colModel.getColumnCount(); assert ncol == colList.size(); TableColumn[] tcols = new TableColumn[ncol]; for (int ic = 0; ic < ncol; ic++) { tcols[ic] = colList.getColumn(icols[ic]); } /* Reorder the columns in the TableColumnModel to match the saved * order. This has the effect of updating the ColumnList as well, * since it is a listener. */ for (int ic = 0; ic < ncol; ic++) { TableColumn tcol = tcols[ic]; if (colModel.getColumn(ic) != tcol) { int kc = -1; for (int jc = ic; jc < ncol && kc < 0; jc++) { if (colModel.getColumn(jc) == tcol) { kc = jc; } } assert kc >= 0; colModel.moveColumn(kc, ic); } } for (int ic = 0; ic < ncol; ic++) { assert colModel.getColumn(ic) == tcols[ic]; assert colList.getColumn(ic) == tcols[ic]; } /* Finally flag each column as visible or not, according to the saved * state. */ for (int ic = 0; ic < ncol; ic++) { colList.setActive(ic, activs[ic]); } /* Get whether to broadcast rows. */ tcModel.getRowSendModel().setSelected(Boolean.TRUE.equals(codec.getCodecValue(SEND_ROWS_INFO))); /* Get current subset index. */ Integer indexCurrentSubset = (Integer) codec.getCodecValue(CURRENT_SUBSET_INFO); int iCurrentSubset = indexCurrentSubset != null ? indexCurrentSubset.intValue() : -1; /* Get subsets. */ String[] subsetNames = (String[]) codec.getCodecValue(SUBSET_NAMES_INFO); if (subsetNames != null && subsetNames.length > 0) { RowSubset currentSubset = null; int nset = subsetNames.length; int icolSubsets = codec.getCodecColumnIndex(SUBSET_FLAGS_INFO); List<RowSubset> setList = new ArrayList<RowSubset>(); for (int is = 0; is < nset; is++) { RowSubset rset = createRowSubset(subsetNames[is], inTable, icolSubsets, is); if (rset != null) { setList.add(rset); } if (is == iCurrentSubset) { currentSubset = rset; } } for (RowSubset rset : setList) { tcModel.addSubset(rset); } if (currentSubset != null) { tcModel.applySubset(currentSubset); } } /* Get sort order. */ Integer icolSort = (Integer) codec.getCodecValue(SORT_COLUMN_INFO); if (icolSort != null) { int icSort = icolSort.intValue(); boolean sortSense = Boolean.TRUE.equals(codec.getCodecValue(SORT_SENSE_INFO)); TableColumn tcolSort = colList.getColumn(icSort); tcModel.getSortSenseModel().setSelected(sortSense); tcModel.sortBy(new SortOrder(tcolSort), sortSense); } /* Return result. */ return tcModel; }
/** Tests that when a column list with a value of null is created, it has an appropriate size. */ @Test public void testSizeNull() { ColumnList columnList = new ColumnList(null); Assert.assertEquals(0, columnList.size()); }
/** Tests that when a column list with no children is created, it has an appropriate size. */ @Test public void testSizeEmpty() { ColumnList columnList = new ColumnList(Collections.<String>emptyList()); Assert.assertEquals(0, columnList.size()); }