Repository: modin-project/modin

Issue:
BUG: HDK runs out of stack and Java heap
### Modin version checks

- [X] I have checked that this issue has not already been reported.

- [X] I have confirmed this bug exists on the latest released version of Modin.

- [X] I have confirmed this bug exists on the main branch of Modin. (In order to do this you can follow [this guide](https://modin.readthedocs.io/en/stable/getting_started/installation.html#installing-from-the-github-master-branch).)


### Reproducible Example

Attached is the zip archive with generator and benchmark code. When it is run as `python launcher.py -np -tr 10000 -m taxi --hdk` or crashes seemingly because it runs out of stack memory. When it is run as `python launcher.py -np -tr 100000 -m taxi --hdk` it produces as exception that Java heap is exhausted.


### Issue Description

With #6895 resolved there is now a problem with running the code on large datasets. When running the code on dataframes of size 100 or 1000 it seems to work well but increasing it to some larger sizes HDK seems to struggle.

### Expected Behavior

Work like Pandas with no crashes and exceptions. Number of records in the dataset is quite modest and should not be a problem for HDK engine.

### Error Logs

<details>

This is the example of stack overflow. Notice numbers of stack frames (over 61k calls up the stack):

```c++
... it continues like this down to frame #1 where process segfaulted ...

#61548 0x00007ffb9ce3a4a3 in ScalarExprVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visitBinOper(hdk::ir::BinOper const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61549 0x00007ffb9ce41b5b in ScalarExprVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visit(hdk::ir::Expr const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61550 0x00007ffb9ce3a4a3 in ScalarExprVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visitBinOper(hdk::ir::BinOper const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61551 0x00007ffb9ce41b5b in ScalarExprVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visit(hdk::ir::Expr const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61552 0x00007ffb9ce3a4a3 in ScalarExprVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visitBinOper(hdk::ir::BinOper const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61553 0x00007ffb9ce41b5b in ScalarExprVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visit(hdk::ir::Expr const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61554 0x00007ffb9ce3a4a3 in ScalarExprVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visitBinOper(hdk::ir::BinOper const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61555 0x00007ffb9ce41b5b in ScalarExprVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visit(hdk::ir::Expr const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61556 0x00007ffb9ce3a4a3 in ScalarExprVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visitBinOper(hdk::ir::BinOper const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61557 0x00007ffb9ce41b5b in ScalarExprVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visit(hdk::ir::Expr const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61558 0x00007ffb9ce420fd in (anonymous namespace)::PhysicalInputsNodeVisitor<(anonymous namespace)::PhysicalInputsVisitor, std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visitFilter(hdk::ir::Filter const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61559 0x00007ffb9ce3f5ea in RelAlgVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visit(hdk::ir::Node const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61560 0x00007ffb9ce3f4d4 in RelAlgVisitor<std::unordered_set<InputColDescriptor, std::hash<InputColDescriptor>, std::equal_to<InputColDescriptor>, std::allocator<InputColDescriptor> > >::visit(hdk::ir::Node const*) const () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61561 0x00007ffb9ce3fa4c in get_physical_inputs(hdk::ir::Node const*) () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61562 0x00007ffb9cebba2b in RelAlgExecutor::executeRelAlgQueryNoRetry(CompilationOptions const&, ExecutionOptions const&, bool) () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61563 0x00007ffb9cebcba7 in RelAlgExecutor::executeRelAlgQuery(CompilationOptions const&, ExecutionOptions const&, bool) () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/../../../../opt/hdk/lib64/libQueryEngine.so
#61564 0x00007ffb9334db7d in __pyx_pf_5pyhdk_4_sql_14RelAlgExecutor_2execute(__pyx_obj_5pyhdk_4_sql_RelAlgExecutor*, _object*) () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/_sql.cpython-311-x86_64-linux-gnu.so
#61565 0x00007ffb9334e0cd in __pyx_pw_5pyhdk_4_sql_14RelAlgExecutor_3execute(_object*, _object* const*, long, _object*) () from /home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/pyhdk/_sql.cpython-311-x86_64-linux-gnu.so
#61566 0x000055555578432f in _PyObject_VectorcallTstate (tstate=0x555555ad0218 <_PyRuntime+166328>, callable=0x7ffb9f66df20, args=0x7ff419c71600, nargsf=<optimized out>, kwnames=<optimized out>) at /usr/local/src/conda/python-3.11.7/Include/internal/pycore_call.h:92
#61567 0x00005555557837ce in method_vectorcall (method=<optimized out>, args=0x7ff419c71608, nargsf=<optimized out>, kwnames=<optimized out>) at /usr/local/src/conda/python-3.11.7/Objects/classobject.c:59
#61568 0x000055555576ed30 in _PyVectorcall_Call (kwargs=<optimized out>, tuple=<optimized out>, callable=0x7ffba3ab3940, func=0x5555557834a0 <method_vectorcall>, tstate=0x555555ad0218 <_PyRuntime+166328>) at /usr/local/src/conda/python-3.11.7/Objects/call.c:257
#61569 _PyObject_Call (kwargs=<optimized out>, args=<optimized out>, callable=0x7ffba3ab3940, tstate=0x555555ad0218 <_PyRuntime+166328>) at /usr/local/src/conda/python-3.11.7/Objects/call.c:328
#61570 PyObject_Call (callable=0x7ffba3ab3940, args=<optimized out>, kwargs=<optimized out>) at /usr/local/src/conda/python-3.11.7/Objects/call.c:355
#61571 0x0000555555745b1c in do_call_core (use_tracing=<optimized out>, kwdict=0x7ff3f0c63ec0, callargs=0x555555ab5e78 <_PyRuntime+58904>, func=0x7ffba3ab3940, tstate=<optimized out>) at /usr/local/src/conda/python-3.11.7/Python/ceval.c:7352
#61572 _PyEval_EvalFrameDefault (tstate=<optimized out>, frame=<optimized out>, throwflag=<optimized out>) at /usr/local/src/conda/python-3.11.7/Python/ceval.c:5376

... up the stack are some more python stack frames ...
```

This is the example of running out of Java heap:

```python-traceback
Traceback (most recent call last):
  File "/home/jovyan/tests/stack-overflow/launcher.py", line 241, in <module>
    main()
  File "/home/jovyan/tests/stack-overflow/launcher.py", line 229, in main
    result, total_time = benchmark.run()
                         ^^^^^^^^^^^^^^^
  File "/home/jovyan/tests/stack-overflow/launcher.py", line 44, in run
    res1 = taxi_run(self._datafile)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/tests/stack-overflow/benchmarks/taxi.py", line 157, in run
    features_train = preprocessor.fit_transform(features_train)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/sklearn/utils/_set_output.py", line 273, in wrapped
    data_to_wrap = f(self, X, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/sklearn/base.py", line 1351, in wrapper
    return fit_method(estimator, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/sklearn/compose/_column_transformer.py", line 902, in fit_transform
    self._check_n_features(X, reset=True)
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/sklearn/base.py", line 420, in _check_n_features
    n_features = _num_features(X)
                 ^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/sklearn/utils/validation.py", line 322, in _num_features
    if hasattr(X, "shape"):
       ^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/pandas/base.py", line 4163, in __getattribute__
    attr = super().__getattribute__(item)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/pandas/dataframe.py", line 355, in shape
    return len(self.index), len(self.columns)
               ^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/pandas/base.py", line 4163, in __getattribute__
    attr = super().__getattribute__(item)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/pandas/base.py", line 623, in _get_index
    return self._query_compiler.index
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/storage_formats/hdk/query_compiler.py", line 489, in _get_index
    return self._modin_frame.index
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py", line 2234, in _get_index
    return self._index_cache.get()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/core/dataframe/pandas/metadata/index.py", line 202, in get
    index, self._lengths_cache = self._value()
                                 ^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/logging/logger_decorator.py", line 129, in run_and_log
    return obj(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py", line 2186, in _compute_axis_labels_and_lengths
    obj = self._execute()
          ^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/logging/logger_decorator.py", line 129, in run_and_log
    return obj(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py", line 2041, in _execute
    result = frame()
             ^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/logging/logger_decorator.py", line 129, in run_and_log
    return obj(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py", line 2081, in _materialize
    partitions = self._partition_mgr_cls.run_exec_plan(self._op)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/logging/logger_decorator.py", line 129, in run_and_log
    return obj(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/partitioning/partition_manager.py", line 265, in run_exec_plan
    table = worker.executeRA(calcite_json, exec_calcite, **exec_args)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/hdk_worker.py", line 112, in executeRA
    ra = hdk._calcite.process(query, db_name="hdk", legacy_syntax=True)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "_sql.pyx", line 46, in pyhdk._sql.Calcite.process
ValueError: Java heap space
```

Java stack trace from pyhdk_log/log4j.log:

```java
2024-02-07 00:47:33 ERROR CalciteServerHandler:process:177 - class java.lang.OutOfMemoryError
2024-02-07 00:47:33 ERROR CalciteServerHandler:process:179 - Java heap space
java.lang.OutOfMemoryError: Java heap space
        at java.util.LinkedHashMap.newNode(LinkedHashMap.java:281) ~[?:?]
        at java.util.HashMap.putVal(HashMap.java:637) ~[?:?]
        at java.util.HashMap.put(HashMap.java:618) ~[?:?]
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializerNR$Scope.putValue(UntypedObjectDeserializerNR.java:517) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializerNR._deserializeNR(UntypedObjectDeserializerNR.java:270) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializerNR.deserialize(UntypedObjectDeserializerNR.java:79) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:623) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:449) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:32) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4730) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3677) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3660) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at org.apache.calcite.rel.externalize.MapDRelJsonReader.read(MapDRelJsonReader.java:83) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at org.apache.calcite.prepare.MapDPlanner.optimizeRaQuery(MapDPlanner.java:229) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.mapd.calcite.parser.MapDParser.optimizeRAQuery(MapDParser.java:204) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.mapd.parser.server.CalciteServerHandler.process(CalciteServerHandler.java:154) [calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
2024-02-07 01:05:39 ERROR CalciteServerHandler:process:177 - class java.lang.OutOfMemoryError
2024-02-07 01:05:39 ERROR CalciteServerHandler:process:179 - Java heap space
java.lang.OutOfMemoryError: Java heap space
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializerNR$Scope.childObject(UntypedObjectDeserializerNR.java:473) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializerNR._deserializeNR(UntypedObjectDeserializerNR.java:238) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializerNR.deserialize(UntypedObjectDeserializerNR.java:79) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:623) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:449) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:32) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4730) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3677) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3660) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at org.apache.calcite.rel.externalize.MapDRelJsonReader.read(MapDRelJsonReader.java:83) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at org.apache.calcite.prepare.MapDPlanner.optimizeRaQuery(MapDPlanner.java:229) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.mapd.calcite.parser.MapDParser.optimizeRAQuery(MapDParser.java:204) ~[calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.mapd.parser.server.CalciteServerHandler.process(CalciteServerHandler.java:154) [calcite-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
```

</details>


### Installed Versions

<details>
INSTALLED VERSIONS
------------------
commit              : a671b5a8bf5dd13fb19f0e88edc679bc9e15c673
python              : 3.11.7.final.0
python-bits         : 64
OS                  : Linux
OS-release          : 5.15.0-91-generic
Version             : #101-Ubuntu SMP Tue Nov 14 13:30:08 UTC 2023
machine             : x86_64
processor           : x86_64
byteorder           : little
LC_ALL              : None
LANG                : None
LOCALE              : en_US.UTF-8

pandas              : 2.1.4
numpy               : 1.26.3
pytz                : 2023.3.post1
dateutil            : 2.8.2
setuptools          : 69.0.3
pip                 : 23.3.2
Cython              : None
pytest              : None
hypothesis          : None
sphinx              : None
blosc               : None
feather             : None
xlsxwriter          : None
lxml.etree          : None
html5lib            : None
pymysql             : None
psycopg2            : None
jinja2              : 3.1.3
IPython             : None
pandas_datareader   : None
bs4                 : None
bottleneck          : None
dataframe-api-compat: None
fastparquet         : None
fsspec              : 2023.12.2
gcsfs               : None
matplotlib          : None
numba               : None
numexpr             : None
odfpy               : None
openpyxl            : None
pandas_gbq          : None
pyarrow             : 12.0.1
pyreadstat          : None
pyxlsb              : None
s3fs                : 2023.12.2
scipy               : 1.12.0
sqlalchemy          : None
tables              : None
tabulate            : None
xarray              : None
xlrd                : None
zstandard           : None
tzdata              : 2023.4
qtpy                : None
pyqt5               : None
</details>



Discussion:
[stackoverflow.zip](https://github.com/modin-project/modin/files/14187877/stackoverflow.zip)
Code of generator and benchmark.
Try to disable Calcite with env variable MODIN_USE_CALCITE=False
With `MODIN_USE_CALCITE=False` the behavior seems to be different but still bad. When running with 10000 records I am getting an exception in python
<details>

```python-traceback
Traceback (most recent call last):
  File "/home/jovyan/tests/stack-overflow/launcher.py", line 241, in <module>
    main()
  File "/home/jovyan/tests/stack-overflow/launcher.py", line 229, in main
    result, total_time = benchmark.run()
                         ^^^^^^^^^^^^^^^
  File "/home/jovyan/tests/stack-overflow/launcher.py", line 44, in run
    res1 = taxi_run(self._datafile)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/tests/stack-overflow/benchmarks/taxi.py", line 157, in run
    features_train = preprocessor.fit_transform(features_train)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/sklearn/utils/_set_output.py", line 273, in wrapped
    data_to_wrap = f(self, X, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/sklearn/base.py", line 1351, in wrapper
    return fit_method(estimator, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/sklearn/compose/_column_transformer.py", line 902, in fit_transform
    self._check_n_features(X, reset=True)
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/sklearn/base.py", line 420, in _check_n_features
    n_features = _num_features(X)
                 ^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/sklearn/utils/validation.py", line 322, in _num_features
    if hasattr(X, "shape"):
       ^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/pandas/base.py", line 4163, in __getattribute__
    attr = super().__getattribute__(item)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/pandas/dataframe.py", line 355, in shape
    return len(self.index), len(self.columns)
               ^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/pandas/base.py", line 4163, in __getattribute__
    attr = super().__getattribute__(item)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/pandas/base.py", line 623, in _get_index
    return self._query_compiler.index
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/storage_formats/hdk/query_compiler.py", line 489, in _get_index
    return self._modin_frame.index
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py", line 2234, in _get_index
    return self._index_cache.get()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/core/dataframe/pandas/metadata/index.py", line 202, in get
    index, self._lengths_cache = self._value()
                                 ^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/logging/logger_decorator.py", line 129, in run_and_log
    return obj(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py", line 2186, in _compute_axis_labels_and_lengths
    obj = self._execute()
          ^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/logging/logger_decorator.py", line 129, in run_and_log
    return obj(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py", line 2041, in _execute
    result = frame()
             ^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/logging/logger_decorator.py", line 129, in run_and_log
    return obj(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py", line 2081, in _materialize
    partitions = self._partition_mgr_cls.run_exec_plan(self._op)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/logging/logger_decorator.py", line 129, in run_and_log
    return obj(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/partitioning/partition_manager.py", line 265, in run_exec_plan
    table = worker.executeRA(calcite_json, exec_calcite, **exec_args)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jovyan/.conda/envs/modin/lib/python3.11/site-packages/modin/experimental/core/execution/native/implementations/hdk_on_native/hdk_worker.py", line 116, in executeRA
    table = ra_executor.execute(device_type=cls._preferred_device, **exec_args)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "_sql.pyx", line 209, in pyhdk._sql.RelAlgExecutor.execute
RuntimeError: Too many literals in the query
```
</details>

and when running with 100000 records I am getting segfault because of the same infinite (or too deep) recursion in C++ code.
Seems the problem is in train_test_split(). It performs rows selection by random index. The resulting json query contains enumeration of all the rows that should be selected and it's very big.
If the problem is really in `train_test_split`, this issue might be related to https://github.com/modin-project/modin/issues/6876 to some extent. In `train_test_split` there is a call to df.take, where Modin on HDK not only is inefficient, but also doesn't work properly at all. There is an [Ecosystem](https://modin.readthedocs.io/en/latest/ecosystem.html) page, where we say "If you feel that a certain library can operate efficiently with a specific format of data, it is possible to convert a Modin DataFrame to the format preferred". @gshimansky, what if you call to_pandas(modin_df) and pass a pandas DataFrame in `train_test_split`?

Resolving patch:
diff --git a/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py b/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py
--- a/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py
+++ b/modin/experimental/core/execution/native/implementations/hdk_on_native/dataframe/dataframe.py
@@ -439,6 +439,36 @@ def take_2d_labels_or_positional(
             return base
 
         row_positions = maybe_range(row_positions)
+
+        # If row_positions is not a range, then MaskNode will generate a filter,
+        # containing enumeration of all the positions. Filtering rows in this
+        # way is not efficient and, in case of too many values in row_positions,
+        # may result in a huge JSON query. To workaround this issue, creating an
+        # empty frame with row_positions index and inner joining with this one.
+        # If row_positions has less than 10 values, MaskNode is used.
+        if (
+            not is_range_like(row_positions)
+            and is_list_like(row_positions)
+            and len(row_positions) > 10
+        ):
+            lhs = base._maybe_materialize_rowid()
+            if len(lhs._index_cols) == 1 and is_integer_dtype(lhs._dtypes[0]):
+                pdf = pd.DataFrame(index=row_positions)
+                rhs = self.from_pandas(pdf)
+                exprs = lhs._index_exprs()
+                for col in lhs.columns:
+                    exprs[col] = lhs.ref(col)
+                condition = lhs._build_equi_join_condition(
+                    rhs, lhs._index_cols, rhs._index_cols
+                )
+                op = JoinNode(
+                    lhs,
+                    rhs,
+                    exprs=exprs,
+                    condition=condition,
+                )
+                return lhs.copy(op=op, index=pdf.index, partitions=None)
+
         base = base._maybe_materialize_rowid()
         op = MaskNode(base, row_labels=row_labels, row_positions=row_positions)
         base = self.__constructor__(


Test patch: