@@ -6,7 +6,7 @@ import type {Column, Settings} from '@gravity-ui/react-data-table';
66import type { ColumnType , KeyValueRow } from '../../types/api/query' ;
77import { cn } from '../../utils/cn' ;
88import { DEFAULT_TABLE_SETTINGS } from '../../utils/constants' ;
9- import { getColumnType , getColumnWidthByType , prepareQueryResponse } from '../../utils/query' ;
9+ import { getColumnType , prepareQueryResponse } from '../../utils/query' ;
1010import { isNumeric } from '../../utils/utils' ;
1111import type { ResizeableDataTableProps } from '../ResizeableDataTable/ResizeableDataTable' ;
1212import { ResizeableDataTable } from '../ResizeableDataTable/ResizeableDataTable' ;
@@ -25,17 +25,30 @@ const TABLE_SETTINGS: Settings = {
2525
2626export const b = cn ( 'ydb-query-result-table' ) ;
2727
28- const prepareTypedColumns = ( columns : ColumnType [ ] ) => {
28+ const WIDTH_PREDICTION_ROWS_COUNT = 100 ;
29+ const MAX_COLUMN_WIDTH = 600 ;
30+
31+ const prepareTypedColumns = ( columns : ColumnType [ ] , data ?: KeyValueRow [ ] ) => {
2932 if ( ! columns . length ) {
3033 return [ ] ;
3134 }
3235
36+ const dataSlice = data ?. slice ( 0 , WIDTH_PREDICTION_ROWS_COUNT ) ;
37+
3338 return columns . map ( ( { name, type} ) => {
3439 const columnType = getColumnType ( type ) ;
3540
41+ const maxColumnContentLength =
42+ dataSlice ?. reduce (
43+ ( max , row ) => Math . max ( max , row [ name ] ? String ( row [ name ] ) . length : max ) ,
44+ name . length ,
45+ ) || name . length ;
46+
47+ const headerPadding = columnType === 'number' ? 40 : 20 ;
48+
3649 const column : Column < KeyValueRow > = {
3750 name,
38- width : getColumnWidthByType ( type , name ) ,
51+ width : Math . min ( maxColumnContentLength * 10 + headerPadding , MAX_COLUMN_WIDTH ) ,
3952 align : columnType === 'number' ? DataTable . RIGHT : DataTable . LEFT ,
4053 sortAccessor : ( row ) => {
4154 const value = row [ name ] ;
@@ -83,7 +96,7 @@ export const QueryResultTable = (props: QueryResultTableProps) => {
8396
8497 const data = React . useMemo ( ( ) => prepareQueryResponse ( rawData ) , [ rawData ] ) ;
8598 const columns = React . useMemo ( ( ) => {
86- return rawColumns ? prepareTypedColumns ( rawColumns ) : prepareGenericColumns ( data ) ;
99+ return rawColumns ? prepareTypedColumns ( rawColumns , data ) : prepareGenericColumns ( data ) ;
87100 } , [ data , rawColumns ] ) ;
88101 const settings = React . useMemo (
89102 ( ) => ( {
0 commit comments