def delete_column(self, identifier: str, column_id: int, datastore: Datastore) -> VizualApiResult: """Delete a column in a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified column is unknown. Parameters ---------- identifier: string Unique dataset identifier column_id: int Unique column identifier datastore : vizier.datastore.fs.base.FileSystemDatastore Datastore to retireve and update datasets Returns ------- vizier.engine.packages.vizual.api.VizualApiResult """ # Get dataset. Raise exception if dataset is unknown dataset = datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Confirm that the column actually exists and convert the column_identifier to # a position in the schema (usually ==, but not guaranteed) col_index = get_index_for_column(dataset, column_id) command = {"id": "deleteColumn", "column": col_index} response = mimir.vizualScript(dataset.identifier, command) return VizualApiResult.from_mimir(response, identifier)
def update_cell(self, identifier, column_id, row_id, value, datastore): """Update a cell in a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified cell is outside of the current dataset ranges. Parameters ---------- identifier : string Unique dataset identifier column_id: int Unique column identifier for updated cell row_id: int Unique row identifier value: string New cell value datastore : vizier.datastore.fs.base.FileSystemDatastore Datastore to retireve and update datasets Returns ------- vizier.engine.packages.vizual.api.VizualApiResult """ # Get dataset. Raise exception if dataset is unknown dataset = datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get the index of the specified cell column col_index = get_index_for_column(dataset, column_id) # Raise exception if row id is not valid # Create a view for the modified dataset col_list = [] for i in range(len(dataset.columns)): col = dataset.columns[i] if i == col_index: try: val_stmt = col.to_sql_value(value) col_sql = val_stmt + ' ELSE ' + col.name_in_rdb + ' END ' except ValueError: col_sql = '\'' + str( value ) + '\' ELSE CAST({{input}}.' + col.name_in_rdb + ' AS varchar) END ' rid_sql = MIMIR_ROWID_COL.to_sql_value(row_id) stmt = 'CASE WHEN ' + ROW_ID + ' = ' + rid_sql + ' THEN ' stmt += col_sql stmt += 'AS ' + col.name_in_rdb col_list.append(stmt) else: col_list.append(col.name_in_rdb) sql = 'SELECT ' + ','.join( col_list) + ' FROM ' + dataset.table_name + ';' view_name, dependencies = mimir.createView(dataset.table_name, sql) # Store updated dataset information with new identifier ds = datastore.register_dataset(table_name=view_name, columns=dataset.columns, row_counter=dataset.row_counter, annotations=dataset.annotations) return VizualApiResult(ds)
def filter_columns(self, identifier, columns, names): """Dataset projection operator. Returns a copy of the dataset with the given identifier that contains only those columns listed in columns. The list of names contains optional new names for the filtered columns. A value of None in names indicates that the name of the corresponding column is not changed. Returns the number of rows in the dataset and the identifier of the projected dataset. Raises ValueError if no dataset with given identifier exists or if any of the filter columns are unknown. Parameters ---------- identifier: string Unique dataset identifier columns: list(int) List of column identifier for columns in the result. names: list(string) Optional new names for filtered columns. Returns ------- int, string """ # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # The schema of the new dataset only contains the columns in the given # list. Keep track of their index positions to filter values. schema = list() val_filter = list() for i in range(len(columns)): col_idx = get_index_for_column(dataset, columns[i]) col = dataset.columns[col_idx] if not names[i] is None: schema.append( DatasetColumn(identifier=col.identifier, name=names[i])) else: schema.append(col) val_filter.append(col_idx) # Create a list of projected rows rows = list() for row in dataset.fetch_rows(): values = list() for v_idx in val_filter: values.append(row.values[v_idx]) rows.append(DatasetRow(identifier=row.identifier, values=values)) # Store updated dataset to get new identifier ds = self.datastore.create_dataset( columns=schema, rows=rows, column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations.filter_columns(columns)) return len(rows), ds.identifier
def filter_columns(self, identifier: str, columns: List[int], names: List[str], datastore: Datastore) -> VizualApiResult: """Dataset projection operator. Returns a copy of the dataset with the given identifier that contains only those columns listed in columns. The list of names contains optional new names for the filtered columns. A value of None in names indicates that the name of the corresponding column is not changed. Raises ValueError if no dataset with given identifier exists or if any of the filter columns are unknown. Parameters ---------- identifier: string Unique dataset identifier columns: list(int) List of column identifier for columns in the result. names: list(string) Optional new names for filtered columns. datastore : vizier.datastore.fs.base.FileSystemDatastore Datastore to retireve and update datasets Returns ------- vizier.engine.packages.vizual.api.VizualApiResult """ # Get dataset. Raise exception if dataset is unknown dataset = datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # The schema of the new dataset only contains the columns in the given # list. A column might need to be renamed. schema = list() column_mapping = list() col_list = [] for i in range(len(columns)): col_idx = get_index_for_column(dataset, columns[i]) col = dataset.columns[col_idx] if not names[i] is None: if not is_valid_name(names[i]): raise ValueError('invalid column name \'' + str(names[i]) + '\'') schema.append( MimirDatasetColumn(identifier=col.identifier, name_in_dataset=names[i], name_in_rdb=names[i])) else: schema.append(col) column_mapping.append({ "columns_column": col_idx, "columns_name": schema[-1].name }) col_list.append(col.name_in_rdb) command = {"id": "projection", "columns": column_mapping} response = mimir.vizualScript(dataset.identifier, command) return VizualApiResult.from_mimir(response)
def filter_columns(self, identifier, columns, names): """Dataset projection operator. Returns a copy of the dataset with the given identifier that contains only those columns listed in columns. The list of names contains optional new names for the filtered columns. A value of None in names indicates that the name of the corresponding column is not changed. Returns the number of rows in the dataset and the identifier of the projected dataset. Raises ValueError if no dataset with given identifier exists or if any of the filter columns are unknown. Parameters ---------- identifier: string Unique dataset identifier columns: list(int) List of column identifier for columns in the result. names: list(string) Optional new names for filtered columns. Returns ------- int, string """ # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # The schema of the new dataset only contains the columns in the given # list. A column might need to be renamed. schema = list() col_list = [ROW_ID] for i in range(len(columns)): col_idx = get_index_for_column(dataset, columns[i]) col = dataset.columns[col_idx] if not names[i] is None: schema.append( MimirDatasetColumn(identifier=col.identifier, name_in_dataset=names[i], name_in_rdb=col.name_in_rdb)) else: schema.append(col) col_list.append(col.name_in_rdb) sql = 'SELECT ' + ','.join(col_list) + ' FROM ' + dataset.table_name view_name = mimir._mimir.createView(dataset.table_name, sql) # Store updated dataset information with new identifier ds = self.datastore.register_dataset( table_name=view_name, columns=schema, row_ids=dataset.row_ids, column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations.filter_columns(columns)) return len(dataset.row_ids), ds.identifier
def sort_dataset(self, identifier, columns, reversed): """Sort the dataset with the given identifier according to the order by statement. The order by statement is a pair of lists. The first list contains the identifier of columns to sort on. The second list contains boolean flags, one for each entry in columns, indicating whether sort order is revered for the corresponding column or not. Returns the number of rows in the database and the identifier of the sorted dataset. Raises ValueError if no dataset with given identifier exists or if any of the columns in the order by clause are unknown. Parameters ---------- identifier: string Unique dataset identifier columns: list(int) List of column identifier for sort columns. reversed: list(bool) Flags indicating whether the sort order of the corresponding column is reveresed. Returns ------- int, string """ # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Fetch the full set of rows rows = dataset.fetch_rows() # Sort multiple times, ones for each of the sort columns (in reverse # order of appearance in the order by clause) for i in range(len(columns)): l_idx = len(columns) - (i + 1) col_id = columns[l_idx] col_idx = get_index_for_column(dataset, col_id) reverse = reversed[l_idx] rows.sort(key=lambda row: row.values[col_idx], reverse=reverse) # Store updated dataset to get new identifier ds = self.datastore.create_dataset( columns=dataset.columns, rows=rows, column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations) return len(rows), ds.identifier
def move_column(self, identifier, column, position): """Move a column within a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified column is unknown or the target position invalid. Parameters ---------- identifier: string Unique dataset identifier column: int Unique column identifier position: int Target position for the column Returns ------- int, string Number of moved columns (i.e., 1) and identifier of resulting dataset """ # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Make sure that position is a valid column index in the new dataset if position < 0 or position > len(dataset.columns): raise ValueError('invalid target position \'' + str(position) + '\'') # Get index position of column that is being moved source_idx = get_index_for_column(dataset, column) # No need to do anything if source position equals target position if source_idx != position: columns = list(dataset.columns) columns.insert(position, columns.pop(source_idx)) rows = dataset.fetch_rows() for row in rows: row.values.insert(position, row.values.pop(source_idx)) # Store updated dataset to get new identifier ds = self.datastore.create_dataset( columns=columns, rows=rows, column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations) return 1, ds.identifier else: return 0, identifier
def move_column(self, identifier, column, position): """Move a column within a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified column is unknown or the target position invalid. Parameters ---------- identifier: string Unique dataset identifier column: int Unique column identifier position: int Target position for the column Returns ------- int, string Number of moved columns (i.e., 1) and identifier of resulting dataset """ # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Make sure that position is a valid column index in the new dataset if position < 0 or position > len(dataset.columns): raise ValueError('invalid target position \'' + str(position) + '\'') # Get index position of column that is being moved source_idx = get_index_for_column(dataset, column) # No need to do anything if source position equals target position if source_idx != position: # There are no changes to the underlying database. We only need to # change the column information in the dataset schema. schema = list(dataset.columns) schema.insert(position, schema.pop(source_idx)) # Store updated dataset to get new identifier ds = self.datastore.register_dataset( table_name=dataset.table_name, columns=schema, row_ids=dataset.row_ids, column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations) return 1, ds.identifier else: return 0, identifier
def rename_column(self, identifier, column, name): """Rename column in a given dataset. Raises ValueError if no dataset with given identifier exists, if the specified column is unknown, or if the given column name is invalid. Parameters ---------- identifier : string Unique dataset identifier column : int Unique column identifier name : string New column name Returns ------- int, string Number of renamed columns (i.e., 1) and identifier of resulting dataset """ # Raise ValueError if given colum name is invalid if not is_valid_name(name): raise ValueError('invalid column name \'' + name + '\'') # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get the specified column that is to be renamed and set the column name # to the new name schema = list(dataset.columns) col = schema[get_index_for_column(dataset, column)] # No need to do anything if the name hasn't changed if col.name.lower() != name.lower(): # There are no changes to the underlying database. We only need to # change the column information in the dataset schema. col.name = name # Store updated dataset to get new identifier ds = self.datastore.register_dataset( table_name=dataset.table_name, columns=schema, row_ids=dataset.row_ids, column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations) return 1, ds.identifier else: return 0, identifier
def update_cell(self, identifier, column, row, value): """Update a cell in a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified cell is outside of the current dataset ranges. Parameters ---------- identifier : string Unique dataset identifier column : int Unique column identifier row : int Row index for updated cell (starting at 0) value : string New cell value Returns ------- int, string Number of updated rows (i.e., 1) and identifier of resulting dataset """ # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get column index forst in case it raises an exception col_idx = get_index_for_column(dataset, column) # Make sure that row refers a valid row in the dataset rows = dataset.fetch_rows() if row < 0 or row >= len(rows): raise ValueError('invalid cell [' + str(column) + ', ' + str(row) + ']') # Update the specified cell in the given data array r = rows[row] values = list(r.values) values[col_idx] = value rows[row] = DatasetRow(r.identifier, values) # Store updated dataset to get new identifier ds = self.datastore.create_dataset( columns=dataset.columns, rows=rows, column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations) return 1, ds.identifier
def rename_column(self, identifier, column, name): """Rename column in a given dataset. Raises ValueError if no dataset with given identifier exists, if the specified column is unknown, or if the given column name is invalid. Parameters ---------- identifier: string Unique dataset identifier column: int Unique column identifier name: string New column name Returns ------- int, string Number of renamed columns (i.e., 1) and identifier of resulting dataset """ # Raise ValueError if given colum name is invalid if not is_valid_name(name): raise ValueError('invalid column name \'' + name + '\'') # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get the specified column that is to be renamed and set the column name # to the new name col_idx = get_index_for_column(dataset, column) # Nothing needs to be changed if name does not differ from column name if dataset.columns[col_idx].name.lower() != name.lower(): columns = list(dataset.columns) columns[col_idx] = DatasetColumn(columns[col_idx].identifier, name) # Store updated dataset to get new identifier ds = self.datastore.create_dataset( columns=columns, rows=dataset.fetch_rows(), column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations) return 1, ds.identifier else: return 0, identifier
def move_column(self, identifier, column_id, position, datastore): """Move a column within a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified column is unknown or the target position invalid. Parameters ---------- identifier: string Unique dataset identifier column_id: int Unique column identifier position: int Target position for the column datastore : vizier.datastore.fs.base.FileSystemDatastore Datastore to retireve and update datasets Returns ------- vizier.engine.packages.vizual.api.VizualApiResult """ # Get dataset. Raise exception if dataset is unknown dataset = datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Make sure that position is a valid column index in the new dataset if position < 0 or position > len(dataset.columns): raise ValueError('invalid target position \'' + str(position) + '\'') # Get index position of column that is being moved source_idx = get_index_for_column(dataset, column_id) # No need to do anything if source position equals target position if source_idx != position: # There are no changes to the underlying database. We only need to # change the column information in the dataset schema. schema = list(dataset.columns) schema.insert(position, schema.pop(source_idx)) # Store updated dataset to get new identifier ds = datastore.register_dataset(table_name=dataset.table_name, columns=schema, row_counter=dataset.row_counter, annotations=dataset.annotations) return VizualApiResult(ds) else: return VizualApiResult(dataset)
def move_column(self, identifier: str, column_id: int, position: int, datastore: Datastore) -> VizualApiResult: """Move a column within a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified column is unknown or the target position invalid. Parameters ---------- identifier: string Unique dataset identifier column_id: int Unique column identifier position: int Target position for the column datastore : vizier.datastore.fs.base.FileSystemDatastore Datastore to retireve and update datasets Returns ------- vizier.engine.packages.vizual.api.VizualApiResult """ # Get dataset. Raise exception if dataset is unknown dataset = datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Make sure that position is a valid column index in the new dataset if position < 0 or position > len(dataset.columns): raise ValueError('invalid target position \'' + str(position) + '\'') # Get index position of column that is being moved source_idx = get_index_for_column(dataset, column_id) # No need to do anything if source position equals target position if source_idx != position: # Keep the mimir-side schema aligned with the vizier-side schema command = { "id": "moveColumn", "column": source_idx, "position": position } response = mimir.vizualScript(dataset.identifier, command) return VizualApiResult.from_mimir(response) else: return VizualApiResult(dataset)
def delete_column(self, identifier, column): """Delete a column in a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified column is unknown. Parameters ---------- identifier: string Unique dataset identifier column: int Unique column identifier Returns ------- int, string Number of deleted columns (i.e., 1) and identifier of resulting dataset """ # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get the index of the specified column that is to be deleted. col_index = get_index_for_column(dataset, column) # Delete column from schema schema = list(dataset.columns) del schema[col_index] # Create a view for the modified schema col_list = [ROW_ID] for col in schema: col_list.append(col.name_in_rdb) sql = 'SELECT ' + ','.join(col_list) + ' FROM ' + dataset.table_name view_name = mimir._mimir.createView(dataset.table_name, sql) # Store updated dataset information with new identifier ds = self.datastore.register_dataset( table_name=view_name, columns=schema, row_ids=dataset.row_ids, column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations) return 1, ds.identifier
def delete_column(self, identifier, column_id, datastore): """Delete a column in a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified column is unknown. Parameters ---------- identifier: string Unique dataset identifier column_id: int Unique column identifier datastore : vizier.datastore.fs.base.FileSystemDatastore Datastore to retireve and update datasets Returns ------- vizier.engine.packages.vizual.api.VizualApiResult """ # Get dataset. Raise exception if dataset is unknown dataset = datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get the index of the specified column that is to be deleted. col_index = get_index_for_column(dataset, column_id) # Delete column from schema schema = list(dataset.columns) del schema[col_index] # Create a view for the modified schema col_list = [] for col in schema: col_list.append(col.name_in_rdb) sql = 'SELECT ' + ','.join( col_list) + ' FROM ' + dataset.table_name + ';' view_name, dependencies = mimir.createView(dataset.table_name, sql) # Store updated dataset information with new identifier ds = datastore.register_dataset(table_name=view_name, columns=schema, row_counter=dataset.row_counter, annotations=dataset.annotations) return VizualApiResult(ds)
def delete_column(self, identifier, column): """Delete a column in a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified column is unknown. Parameters ---------- identifier: string Unique dataset identifier column: int Unique column identifier Returns ------- int, string Number of deleted columns (i.e., 1) and identifier of resulting dataset """ # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get the index of the specified column that is to be deleted. col_index = get_index_for_column(dataset, column) # Delete column from schema columns = list(dataset.columns) del columns[col_index] # Delete all value for the deleted column rows = dataset.fetch_rows() for row in rows: del row.values[col_index] # Store updated dataset to get new identifier ds = self.datastore.create_dataset( columns=columns, rows=rows, column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations) return 1, ds.identifier
def update_cell(self, identifier: str, column_id: int, row_id: str, value: str, datastore: Datastore) -> VizualApiResult: """Update a cell in a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified cell is outside of the current dataset ranges. Parameters ---------- identifier : string Unique dataset identifier column_id: int Unique column identifier for updated cell row_id: int Unique row identifier value: string New cell value datastore : vizier.datastore.fs.base.FileSystemDatastore Datastore to retireve and update datasets Returns ------- vizier.engine.packages.vizual.api.VizualApiResult """ # Get dataset. Raise exception if dataset is unknown dataset = datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get the index of the specified cell column col_index = get_index_for_column(dataset, column_id) # Raise exception if row id is not valid command = { "id": "updateCell", "column": col_index, "row": row_id, "value": str(value) if value is not None else None } response = mimir.vizualScript(dataset.identifier, command) return VizualApiResult.from_mimir(response)
def rename_column(self, identifier: str, column_id: int, name: str, datastore: Datastore) -> VizualApiResult: """Rename column in a given dataset. Raises ValueError if no dataset with given identifier exists, if the specified column is unknown, or if the given column name is invalid. Parameters ---------- identifier: string Unique dataset identifier column_id: int Unique column identifier name: string New column name datastore : vizier.datastore.fs.base.FileSystemDatastore Datastore to retireve and update datasets Returns ------- vizier.engine.packages.vizual.api.VizualApiResult """ # Raise ValueError if given colum name is invalid if not is_valid_name(name): raise ValueError('invalid column name \'' + str(name) + '\'') # Get dataset. Raise exception if dataset is unknown dataset = datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get the specified column that is to be renamed and set the column name # to the new name target_col_index = get_index_for_column(dataset, column_id) command = { "id": "renameColumn", "column": target_col_index, "name": name } response = mimir.vizualScript(dataset.identifier, command) return VizualApiResult.from_mimir(response)
def update_cell(self, identifier, column, row, value): """Update a cell in a given dataset. Raises ValueError if no dataset with given identifier exists or if the specified cell is outside of the current dataset ranges. Parameters ---------- identifier : string Unique dataset identifier column : int Unique column identifier for updated cell (starting at 0) row : int Row index for updated cell (starting at 0) value : string New cell value Returns ------- int, string Number of updated rows (i.e., 1) and identifier of resulting dataset """ # Get dataset. Raise exception if dataset is unknown dataset = self.datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get the index of the specified cell column col_index = get_index_for_column(dataset, column) # Get id of the cell row row_id = row # Create a view for the modified dataset col_list = [ROW_ID] for i in range(len(dataset.columns)): col = dataset.columns[i] if i == col_index: try: val_stmt = col.to_sql_value(value) col_sql = val_stmt + ' ELSE ' + col.name_in_rdb + ' END ' except ValueError: col_sql = '\'' + str( value ) + '\' ELSE CAST({{input}}.' + col.name_in_rdb + ' AS varchar) END ' rid_sql = dataset.rowid_column.to_sql_value(row_id) stmt = 'CASE WHEN ' + ROW_ID + ' = ' + rid_sql + ' THEN ' stmt += col_sql stmt += 'AS ' + col.name_in_rdb col_list.append(stmt) else: col_list.append(col.name_in_rdb) sql = 'SELECT ' + ','.join(col_list) + ' FROM ' + dataset.table_name view_name = mimir._mimir.createView(dataset.table_name, sql) # Store updated dataset information with new identifier ds = self.datastore.register_dataset( table_name=view_name, columns=dataset.columns, row_ids=dataset.row_ids, column_counter=dataset.column_counter, row_counter=dataset.row_counter, annotations=dataset.annotations) return 1, ds.identifier
def rename_column(self, identifier, column_id, name, datastore): """Rename column in a given dataset. Raises ValueError if no dataset with given identifier exists, if the specified column is unknown, or if the given column name is invalid. Parameters ---------- identifier: string Unique dataset identifier column_id: int Unique column identifier name: string New column name datastore : vizier.datastore.fs.base.FileSystemDatastore Datastore to retireve and update datasets Returns ------- vizier.engine.packages.vizual.api.VizualApiResult """ # Raise ValueError if given colum name is invalid if not is_valid_name(name): raise ValueError('invalid column name \'' + str(name) + '\'') # Get dataset. Raise exception if dataset is unknown dataset = datastore.get_dataset(identifier) if dataset is None: raise ValueError('unknown dataset \'' + identifier + '\'') # Get the specified column that is to be renamed and set the column name # to the new name columns = list() schema = list(dataset.columns) colIndex = get_index_for_column(dataset, column_id) col = schema[colIndex] # No need to do anything if the name hasn't changed if col.name.lower() != name.lower(): sql = 'SELECT * FROM ' + dataset.table_name mimirSchema = mimir.getSchema(sql) # Create list of dataset columns colSql = '' idx = 0 for col in mimirSchema: col_id = len(columns) name_in_dataset = sanitize_column_name(col['name'].upper()) name_in_rdb = sanitize_column_name(col['name'].upper()) col = MimirDatasetColumn(identifier=col_id, name_in_dataset=name_in_dataset, name_in_rdb=name_in_rdb) if idx == 0: colSql = name_in_dataset + ' AS ' + name_in_rdb elif idx == colIndex: colSql = colSql + ', ' + name_in_dataset + ' AS ' + name col.name = name col.name_in_rdb = name else: colSql = colSql + ', ' + name_in_dataset + ' AS ' + name_in_rdb columns.append(col) idx = idx + 1 # Create view for loaded dataset sql = 'SELECT ' + colSql + ' FROM {{input}};' view_name, dependencies = mimir.createView(dataset.table_name, sql) # There are no changes to the underlying database. We only need to # change the column information in the dataset schema. # Store updated dataset to get new identifier ds = datastore.register_dataset(table_name=view_name, columns=columns, row_counter=dataset.row_counter, annotations=dataset.annotations) return VizualApiResult(ds) else: return VizualApiResult(dataset)