[ 
https://issues.apache.org/jira/browse/CALCITE-7953?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18148429#comment-18148429
 ] 

Jialiang Liang commented on CALCITE-7953:
-----------------------------------------

Thanks, Julian. Agreed: the problem is {{{}fields{}}}, and I'll leave 
{{field(String)}} as it is. (I have also updated the issue title)

On the alternatives (JDK 21, 10,000 input fields, microseconds):
||names requested||loop (today)||hash on record's names||hash on requested 
names||sort-merge||
|1|1.7|114|3.3|106|
|1,000|4,461|98|51|200|
|10,000|44,759|182|355|1,632|
 * Hashing the requested names fits {{fields}} best: it keeps nothing on the 
frame and makes one pass over the input, stopping once every name is found.
 * Merge join loses to hashing because field names are not sorted.
 * The record's own name map ({{{}RelRecordType{}}}, above 20 fields) can't be 
reused, because a frame's names can differ from its row type's: after a join 
the frame has {{DEPTNO}} twice, while the row type has {{DEPTNO}} and 
{{{}DEPTNO0{}}}.

So my plan is to change only {{{}fields(Iterable<String>){}}}. With 2 or fewer 
names, or 20 or fewer input fields, it keeps today's loop; otherwise it hashes 
the requested names. Results and errors are the same as calling {{field}} once 
per name.

For the test: 1,000 of 10,000 fields takes only about 30 ms today, so the test 
looks up all fields of a 50,000-field record instead: about 7 seconds today, 
about 5 ms after. Like the test for 
https://issues.apache.org/jira/browse/CALCITE-1960, it has no timing assertion; 
the javadoc records the times.

If this sounds right, I will open a PR.

> 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
>
> {{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)

Reply via email to