-
Notifications
You must be signed in to change notification settings - Fork 25.3k
SQL: Add text formatting support for multivalue #68606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQL: Add text formatting support for multivalue #68606
Conversation
This adds the text formatting for multivalue doc fields, that is: - CSV and TSV exports - TXT formatting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, left 2 comments, a minor one and a question regarding the data type.
@@ -1055,6 +1055,16 @@ private void executeQueryWithNextPage(String format, String expectedHeader, Stri | |||
assertEquals(0, getNumberOfSearchContexts(client(), "test")); | |||
} | |||
|
|||
public void testMultiValueQueryText() throws IOException { | |||
index("{\"text\":[" + toJson("one") + "," + toJson("two, three") + "," + toJson("\"four\"") + "], \"number\" : [1, [2, 3], 4] }"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you use toJson
only for the values but not also for the field names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C&p inertia. Thx, updated it.
Because of the spoteless formatting, which looks pretty bad. But I've applied it now anyways...
} | ||
|
||
String typeName = fromJava(o).typeName(); | ||
headers.add(new ColumnInfo("index", typeName + "_column", typeName)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember our take here, shouldn't the type name reveal that it's an array? something like byte_array
or _array
(postgres format) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean the 3rd argument should be typeName + "_array"
? I've updated it anyways, to be clearer, but it's not used in these tests (the formatters don't look at the type, only content).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx, yep, I get, the columnInfo is not actually asserted, but it's clearer with the _array
suffix. Thx!
- extend `toJson` use in one test; - add + `"_array"` in column def in one test.
- which is pretty ugly in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thx!
|
||
public void testCsvMultiValueIntegers() { | ||
String text = CSV.format(req(), withData(singletonList(asList( | ||
asList((byte)1, (byte)2), asList((short)3, (short)4), asList(5, 6), asList(7L, 8L) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitty picky: whitespace between the casting and the casted value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Pinging @elastic/es-ql (Team:QL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
sb.append('['); | ||
Collection<?> values = (Collection<?>) value; | ||
values.forEach(x -> { | ||
// quote strings and `\`-escape the `"` character inside them | ||
if (x instanceof String) { // TODO: IPs should ideally not be quoted. | ||
sb.append('"'); | ||
sb.append(((String) x).replace("\"", "\\\"")); | ||
sb.append('"'); | ||
} else if (x == null) { | ||
sb.append("NULL"); | ||
} else { | ||
sb.append(toString(x, sqlVersion)); | ||
} | ||
sb.append(','); | ||
}); | ||
if (sb.length() > 1) { // strip trailing comma | ||
sb.deleteCharAt(sb.length() - 1); | ||
} | ||
sb.append(']'); | ||
return sb.toString(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use StringJoiner
since it's part of the JDK and handles the exact case of prefix/suffix plus delimiter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks, applied.
- white-space style; - s/StringBuilder/StringJoiner/
@@ -169,27 +170,19 @@ public static String toString(Object value, SqlVersion sqlVersion) { | |||
|
|||
// multivalue | |||
if (value instanceof Collection) { | |||
final StringBuilder sb = new StringBuilder(); | |||
sb.append('['); | |||
final StringJoiner sj = new StringJoiner(",", "[", "]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
if (x instanceof String) { // TODO: IPs should ideally not be quoted. | ||
sj.add('"' + ((String) x).replace("\"", "\\\"") + '"'); | ||
} else if (x == null) { | ||
sj.add("NULL"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicky: we return uppercase NULL
, but here we return lowercase null
for simple null objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, GH only updated the page with your comment after I hit merge.
The link in the comment doesn't seem to resolve correctly, but I assume you refer to the first if
in the function?
I followed Posgresql's capitalised formatting for lists. Generally (and debatable) nulls are capitalised in textual formats, so I'd be in favour for updating the listing of simple null objects.
As extra context:
The NULL
would appear "alone" in CSVs and TSVs -- a simple null object wouldn't be output at all in these formats (they'd be represented by two consecutive commas or tabs).
In CLI, the NULL
would also appear to the letter in arrays output and the CLI chooses how to represent JSON nulls by itself (which is currently small lettered).
In txt
formatting output the difference would be visible and "uncontrollable".
* Add text formatting support for multivalue This adds the text formatting for multivalue doc fields, that is: - CSV and TSV exports - TXT formatting
* Add text formatting support for multivalue This adds the text formatting for multivalue doc fields, that is: - CSV and TSV exports - TXT formatting
* Add text formatting support for multivalue This adds the text formatting for multivalue doc fields, that is: - CSV and TSV exports - TXT formatting
* Add text formatting support for multivalue This adds the text formatting for multivalue doc fields, that is: - CSV and TSV exports - TXT formatting
This adds the text formatting for multivalue fields (available through the ARRAY function),
that is: