Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions client/app/components/queries/SchemaData.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ class SchemaData extends React.PureComponent {
dataIndex: 'type',
width: 400,
key: 'type',
}, {
title: 'Example',
dataIndex: 'example',
width: 400,
key: 'example',
}];

const hasExample =
this.props.tableMetadata.some(columnMetadata => columnMetadata.example);

if (hasExample) {
columns.push({
title: 'Example',
dataIndex: 'example',
width: 400,
key: 'example',
});
}

return (
<Drawer
title={this.props.tableName}
Expand Down
1 change: 0 additions & 1 deletion client/app/components/queries/schema-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
<div uib-collapse="table.collapsed">
<div ng-repeat="column in table.columns | filter:$ctrl.schemaFilterColumn track by column.key" class="table-open">
{{column.name}}
<span ng-if="column.type !== undefined">({{column.type}})</span>
<i class="fa fa-angle-double-right copy-to-editor" aria-hidden="true"
ng-click="$ctrl.itemSelected($event, [column.name])"></i>
</div>
Expand Down
26 changes: 18 additions & 8 deletions redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,22 @@ def delete(self):

def get_schema(self):
schema = []
columns_by_table_id = {}
tables = TableMetadata.query.filter(TableMetadata.data_source_id == self.id).all()
columns = ColumnMetadata.query.all()

for column in columns:
if not column.exists:
continue

columns_by_table_id.setdefault(column.table_id, []).append({
'key': column.id,
'name': column.name,
'type': column.type,
'exists': column.exists,
'example': column.example
})

for table in tables:
if not table.exists:
continue
Expand All @@ -216,14 +231,9 @@ def get_schema(self):
'exists': table.exists,
'hasColumnMetadata': table.column_metadata,
'columns': []}
columns = ColumnMetadata.query.filter(ColumnMetadata.table_id == table.id)
table_info['columns'] = sorted([{
'key': column.id,
'name': column.name,
'type': column.type,
'exists': column.exists,
'example': column.example
} for column in columns if column.exists == True], key=itemgetter('name'))

table_info['columns'] = sorted(
columns_by_table_id.get(table.id, []), key=itemgetter('name'))
schema.append(table_info)

return sorted(schema, key=itemgetter('name'))
Expand Down
11 changes: 10 additions & 1 deletion redash/query_runner/athena.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,18 @@ def __get_schema_from_glue(self):
table_name = '%s.%s' % (database['Name'], table['Name'])
if table_name not in schema:
column = [columns['Name'] for columns in table['StorageDescriptor']['Columns']]
schema[table_name] = {'name': table_name, 'columns': column}
metadata = [{
"name": column_data['Name'],
"type": column_data['Type']
} for column_data in table['StorageDescriptor']['Columns']]
schema[table_name] = {'name': table_name, 'columns': column, 'metadata': metadata}
for partition in table.get('PartitionKeys', []):
schema[table_name]['columns'].append(partition['Name'])
schema[table_name]['metadata'].append({
"name": partition['Name'],
"type": partition['Type']
})

return schema.values()

def get_schema(self, get_stats=False):
Expand Down
2 changes: 1 addition & 1 deletion redash/tasks/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def refresh_schema(data_source_id):
"column_metadata": "metadata" in table
}
new_column_names[table_name] = table['columns']
new_column_metadata[table_name] = table['metadata']
new_column_metadata[table_name] = table.get('metadata', None)

insert_or_update_table_metadata(ds, existing_tables_set, table_data)
models.db.session.flush()
Expand Down
24 changes: 20 additions & 4 deletions tests/query_runner/test_athena.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ def test_external_table(self):
{'DatabaseName': 'test1'},
)
with self.stubber:
assert query_runner.get_schema() == [{'columns': ['row_id'], 'name': 'test1.jdbc_table'}]
assert query_runner.get_schema() == [{
'columns': ['row_id'],
'name': 'test1.jdbc_table',
'metadata': [{'type': 'int', 'name': 'row_id'}]
}]

def test_partitioned_table(self):
"""
Expand Down Expand Up @@ -118,7 +122,11 @@ def test_partitioned_table(self):
{'DatabaseName': 'test1'},
)
with self.stubber:
assert query_runner.get_schema() == [{'columns': ['sk', 'category'], 'name': 'test1.partitioned_table'}]
assert query_runner.get_schema() == [{
'columns': ['sk', 'category'],
'name': 'test1.partitioned_table',
'metadata': [{'type': 'int', 'name': 'sk'}, {'type': 'int', 'name': 'category'}]
}]

def test_view(self):
query_runner = Athena({'glue': True, 'region': 'mars-east-1'})
Expand Down Expand Up @@ -150,7 +158,11 @@ def test_view(self):
{'DatabaseName': 'test1'},
)
with self.stubber:
assert query_runner.get_schema() == [{'columns': ['sk'], 'name': 'test1.view'}]
assert query_runner.get_schema() == [{
'columns': ['sk'],
'name': 'test1.view',
'metadata': [{'type': 'int', 'name': 'sk'}]
}]

def test_dodgy_table_does_not_break_schema_listing(self):
"""
Expand Down Expand Up @@ -187,4 +199,8 @@ def test_dodgy_table_does_not_break_schema_listing(self):
{'DatabaseName': 'test1'},
)
with self.stubber:
assert query_runner.get_schema() == [{'columns': ['region'], 'name': 'test1.csv'}]
assert query_runner.get_schema() == [{
'columns': ['region'],
'name': 'test1.csv',
'metadata': [{'type': 'string', 'name': 'region'}]
}]