[
https://issues.apache.org/jira/browse/CALCITE-7953?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18148440#comment-18148440
]
Jialiang Liang commented on CALCITE-7953:
-----------------------------------------
Good call, thanks. I've opened a PR:
https://github.com/apache/calcite/pull/5434. It now looks the names up in the
row type (so in {{fieldNameMap}} for wide rows) whenever the input has the row
type's fields, which covers scan, filter, sort, project and aggregate.
After a join with repeated names, stripping digits doesn't quite work: (A)
joined with (A, A0) gives row type names [A, A0, A00], so you can't tell the
real A0 from the renamed A, and a {{NavigableMap}} has the same problem. In
that case it just falls back to {{field(String)}}.
Also I added test that looks up all fields of a 100,000-field input with a
2-second timeout: about 25 s before the fix, 20 ms after.
> RelBuilder.fields(Iterable<String>) searches the input once per name, so
> resolving many fields by name is quadratic
> -------------------------------------------------------------------------------------------------------------------
>
> Key: CALCITE-7953
> URL: https://issues.apache.org/jira/browse/CALCITE-7953
> Project: Calcite
> Issue Type: Improvement
> Components: core
> Reporter: Jialiang Liang
> Priority: Major
> Labels: pull-request-available
>
> {{RelBuilder.field(String)}} finds the field with a linear search of the
> input's fields:
> {code:java}
> final List<String> fieldNames = Pair.left(frame.fields());
> int i = fieldNames.indexOf(fieldName);
> {code}
> So resolving many fields by name is O(n²) in the number of fields.
> {{RelBuilder.fields(Iterable<String>)}} does this, as does any caller that
> projects all of an input's fields by name.
> It matters on wide inputs. In OpenSearch SQL (PPL on Calcite), indices with
> tens of thousands of fields are possible, and several commands that project
> every column by name spent most of their planning time in this search, for
> example about 0.9 s for one command on a 14,000-field index. We have been
> fixing each call site with a name-to-ordinal map and {{field(int)}}
> ([#5913|https://github.com/opensearch-project/sql/pull/5913],
> [#5956|https://github.com/opensearch-project/sql/pull/5956],
> [#5960|https://github.com/opensearch-project/sql/pull/5960],
> [#5961|https://github.com/opensearch-project/sql/pull/5961]), but any new
> call site can bring it back.
> Time for {{RelBuilder.fields(names)}} with every field name, over a scan of
> an n-field table (best of 3, JDK 21):
> ||n||before||after||
> |1,000|2.0 ms|0.3 ms|
> |10,000|185 ms|2.0 ms|
> |25,000|1,129 ms|2.5 ms|
> |50,000|4,977 ms|3.5 ms|
> {{RelRecordType}} already handles this for {{RelDataType.getField}}: above 20
> fields it keeps a map from field name to field, keeping the first field when
> a name repeats.
> Proposed fix: on the first lookup, and only above the same 20-field
> threshold, {{RelBuilder.Frame}} builds a map from field name to the ordinal
> of the first field with that name, and {{field(int, int, String)}} uses it.
> The result (first match, case-sensitive) and the error for an unknown name
> are unchanged, and frames with 20 or fewer fields keep the linear search.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)