Skip to content

Commit 390f8e5

Browse files
authored
Merge pull request #710 from KxSystems/KXI-68287
KXI-68287 Python structuredText collapses q tables to a single row
2 parents a126d95 + c866ca8 commit 390f8e5

File tree

5 files changed

+825
-243
lines changed

5 files changed

+825
-243
lines changed

resources/evaluate.q

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{[ctx; code; returnFormat]
1+
{[ctx; code; sampleFn; returnFormat]
22
if [`histogram in key `.qp;
33
if [not `display2 in key `.qp;
44
.qp.display2: (')[{x[`output][`bytes]}; .qp.display]
@@ -108,22 +108,22 @@
108108
prefix: ";[::;";
109109
suffix: $[(not isLastLine) and not ";" ~ last expr; ";]"; "]"];
110110
expr: prefix , expr , suffix;
111-
result: .Q.trp[{[expr] `result`errored`error`backtrace!({$[x ~ (::); (::); x]} value expr; 0b; ""; ())};
111+
result: .Q.trp[{[expr] `data`error`errorMsg`stacktrace!({$[x ~ (::); (::); x]} value expr; 0b; ""; ())};
112112
expr;
113-
{[suffix; prefix; err; backtrace]
113+
{[suffix; prefix; err; stacktrace]
114114
if [err ~ enlist " ";
115115
err: "syntax error"];
116-
userCode: (-1 + last where (.Q.trp ~ first first @) each backtrace) # backtrace;
116+
userCode: (-1 + last where (.Q.trp ~ first first @) each stacktrace) # stacktrace;
117117
userCode[;3]: reverse 1 + til count userCode;
118118
userCode[-1 + count userCode; 1; 3]: (neg count suffix) _ (count prefix) _ userCode[-1 + count userCode; 1; 3];
119119
userCode[-1 + count userCode; 2]-: count prefix;
120120
(!) . flip (
121-
(`result; ::);
122-
(`errored; 1b);
123-
(`error; err);
124-
(`backtrace; .Q.sbt userCode))
121+
(`data; ::);
122+
(`error; 1b);
123+
(`errorMsg; err);
124+
(`stacktrace; .Q.sbt userCode))
125125
}[suffix; prefix]];
126-
if [isLastLine or result`errored;
126+
if [isLastLine or result`error;
127127
system "d ", cachedCtx;
128128
: result];
129129
index +: 1];
@@ -138,11 +138,27 @@
138138
`compoundChar`compoundSymbol`compoundTimestamp`compoundMonth`compoundDate`compoundDatetime`compoundTimespan`compoundMinute`compoundSecond,
139139
`compoundTime`compoundEnum`table`dictionary`lambda`unary`binary`ternary`projection`composition,
140140
`$("f'";"f/";"f\\";"f':";"f/:";"f\\:";"dynamicload");
141-
removeTrailingNewline: {[text]
141+
removeTrailingNewline: {[text]
142142
if ["\n" = last text;
143143
text: -1 _ text];
144144
text
145145
};
146+
typeOf: {$[0>type x; .axq.i_PRIMCODE neg type x; .axq.i_NONPRIMCODE type x]};
147+
isAtom: {not type[x] within 0 99h};
148+
sample: {[sampleFn; sampleSize; data]
149+
sampleSize: min (sampleSize; count data);
150+
fn: $[ sampleFn ~ "random";
151+
{[sampleSize; data]
152+
$[ type[data] ~ 99h;
153+
[ ii: neg[sampleSize]?count data;
154+
(key[data] ii)!value[data]ii];
155+
neg[sampleSize]?data]
156+
};
157+
sampleFn ~ "first"; #;
158+
sampleFn ~ "last"; {neg[x]#y};
159+
' "Unrecognized sample function"];
160+
fn[sampleSize; data]
161+
};
146162
generateColumns:{[removeTrailingNewline; toString; originalType; isAtomic; isKey; data; name]
147163
attributes: attr data;
148164
types: $[
@@ -167,10 +183,25 @@
167183
' "This view is not supported for splayed tables"];
168184
generateColumns[originalType; isAtom; isKey] ./: flip (value; key) @\: flip data
169185
}[generateColumns];
170-
toStructuredText:{[generateTableColumns; generateColumns; data; quantity; isAtom; originalType]
171-
if[(type data) ~ 10h; data: enlist data];
186+
toStructuredText:{[generateTableColumns; generateColumns; sample; data; sampleFn]
187+
DEFAULT_TABULAR_LIMIT: 600000;
188+
TABULAR_LIMIT: DEFAULT_TABULAR_LIMIT^"J"$getenv `TABULAR_LIMIT;
189+
isNumber: {abs[type[x]] within abs[5 9h]};
190+
itemLimit: TABULAR_LIMIT;
191+
if[not isNumber itemLimit; itemLimit: DEFAULT_TABULAR_LIMIT];
192+
isEmpty: {0 ~ count x};
193+
warnings: ();
172194
isTable: .Q.qt data;
173195
isDict: 99h ~ type data;
196+
truncateSize: $[isTable;ceiling itemLimit%count cols data;isDict;ceiling itemLimit%2;itemLimit];
197+
if[not isEmpty data;if[(sum count each data) > truncateSize;data: sample["first";truncateSize;data];warnings,: enlist "Results truncated to TABULAR_LIMIT. Console view is faster for large data."];];
198+
typeOf: {$[0>type x; .axq.i_PRIMCODE neg type x; .axq.i_NONPRIMCODE type x]};
199+
isAtom: {not type[x] within 0 99h};
200+
isAtom: isAtom data;
201+
originalType: typeOf data;
202+
quantity: count data;
203+
data: sampleFn data;
204+
if[(type data) ~ 10h; data: enlist data];
174205
columns: $[
175206
isTable and isDict;
176207
raze (generateTableColumns[::;0b;1b;key data]; generateTableColumns[::;0b;0b;value data]);
@@ -180,29 +211,13 @@
180211
generateTableColumns[originalType;isAtom;0b;data];
181212
enlist generateColumns[originalType;isAtom;0b;data;"values"]
182213
];
183-
: .j.j `count`columns!(quantity; columns)
184-
}[generateTableColumns; generateColumns];
185-
typeOf: {$[0>type x; .axq.i_PRIMCODE neg type x; .axq.i_NONPRIMCODE type x]};
186-
isAtom: {not type[x] within 0 99h};
187-
sample: {[sampleFn; sampleSize; data]
188-
sampleSize: min (sampleSize; count data);
189-
fn: $[ sampleFn ~ "random";
190-
{[sampleSize; data]
191-
$[ type[data] ~ 99h;
192-
[ ii: neg[sampleSize]?count data;
193-
(key[data] ii)!value[data]ii];
194-
neg[sampleSize]?data]
195-
};
196-
sampleFn ~ "first"; #;
197-
sampleFn ~ "last"; {neg[x]#y};
198-
' "Unrecognized sample function"];
199-
fn[sampleSize; data]
200-
}
214+
: .j.j `count`columns`warnings!(quantity; columns;warnings)
215+
}[generateTableColumns; generateColumns; sample];
201216
result: evalInContext[ctx; splitExpression stripTrailingSemi wrapLines removeMultilineComments code];
202-
if [result `errored; :result];
217+
if [result`error; :result];
203218
if [returnFormat ~ "text";
204-
result[`result]: toString result `result];
219+
result[`data]: toString result `data];
205220
if [returnFormat ~ "structuredText";
206-
result[`result]: toStructuredText[result `result;count result`result; isAtom result`result; typeOf result`result]];
221+
result[`data]: toStructuredText[result`data; sampleFn]];
207222
result
208-
}
223+
}

0 commit comments

Comments
 (0)