You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by ro...@apache.org on 2022/05/17 21:56:35 UTC

[pinot] branch multi_stage_query_engine updated (08da4ee9e7 -> fa2b2fa7cf)

This is an automated email from the ASF dual-hosted git repository.

rongr pushed a change to branch multi_stage_query_engine
in repository https://gitbox.apache.org/repos/asf/pinot.git


    omit 08da4ee9e7 [hotfix] multi stage engine serde (#8689)
    omit 77b59bf9db row/columnar compatible block (#8583)
    omit c9bebeb504 [hotfix] fix server discovery (#8664)
    omit 6ae5f76d77 fix rebase issue after PQL deprecation (#8652)
    omit 58df2c2003 add support for project/filter pushdown (#8558)
    omit 1bbded3eac Use proto for query plan serialization (#8479)
    omit 0e38a5f4d5 add pinot-query-runtime (#8412)
    omit e819d11fa8 Add pinot-query-planner module (#8340)
    omit e8ddc1dbe8 make github action trigger on multi_stage_query_engine branch (#8342)
     add 6192229a60 Revert "Enable minion liveness probe (#8593)" (#8650)
     add ec42a3a6a4 fix column data type store for data table (#8648)
     add d0f9aaf5b3 fix GcsPinotFs listFiles by using bucket directly (#8656)
     add b4943458a5 [issue-7357] Add support for Avro logical types (#7358)
     add c0056dc278 Allow broker to automatically rewrite expensive function to its approximate counterpart (#8655)
     add 8fac50c038 [CI] Reduce quickstart testing to only LTS (#8665)
     add b9bfb8e752 Support ADLS authentication with AAD + proxy (#8654)
     add 497d0517ab Introduce Real Time Native Text Index (#8636)
     add d208f577ed Adding pinot file system command (#8659)
     add abf62f571d fix pruning in expressions by max/min/bloom (#8672)
     add 65cecd259e Randomly pick the first URI for RoundRobinURIProvider (#8666)
     add f4c85e9596 Fix the flaky UpsertTableSegmentUploadIntegrationTest (#8675)
     add 5135acb95d refine error msg related to segment name generation to be more actionable (#8676)
     add d616a097e5 Add BigDecimal OnHeap/OffHeap mutable dictionaries (#8678)
     add a12b760b74 Amend schema to add timestamp index during segment reloading (#8679)
     add d7f2054b6e Import dry run (#8673)
     add 23a81d07b5 Add TLS configuration to JDBC driver (#8578)
     add a7e2c12c99 Add InstanceTypeUtils to get instance type from the id (#8681)
     add 59bf3827db By default start 1 minion for all quickstart (#8682)
     add b58650658c Allow updating start/end offset for pushed segments (#8653)
     add 56282e8b2f track group by evaluation statistics (#8683)
     add 6d785e0eb3 Upgrade jersey to fix parallel segment upload (#8677)
     add e12bda1cf6 Fix the race condition of reading time boundary info (#8685)
     add 24a86acec0 Fixing GRPC ITCase timeout (#8686)
     add f9e55ce7a6 Handle the race condition for common dest segments in startReplaceSegments(). (#8667)
     add df29b81c27 Change minion's default port, enable minion health and readiness probe via helm chart (#8669)
     add 088da3f8c2 deduplicate predicates optimistically (#8687)
     add c3d17b9ed7 Fix PulsarConsumerTest (#8691)
     add cfb0074a2a Upgrade h3 to 3.7.2 for M1 Mac support (#8696)
     add f65b401dc7 Set max number of parallel segment downloads per table in pinot-server (#8694)
     add d06338acb8 avoid putting null into segment download uri (#8693)
     add 3983728e64 make exclusive bounds exclusive (#8703)
     add 078d8969bf Reduce wait time for pulsar tests (#8702)
     add d3c97d54d2 Add Pulsar plugin back to distribution (#8657)
     add f90137b1d5 rationalise Pair types (#8709)
     add e4a8e6ed60 throw when integer overflow occurs (#8710)
     add e788a0afc6 Fix when there're two index filter operator h3 inclusion index throw exception (#8707)
     add 159afb7820 Cleanup segment upload logic and allow validation on real-time table (#8695)
     add 7548b67592 CSV Realtime Decoder (#8658)
     add 70f505f028 Bugfixing, don't remove timestmap index during server restart. (#8705)
     add 09bae15030 Scalable Gapfill Implementation for Avg/Count/Sum (#8647)
     add d62438c36c [Minor] Make warning log more explicit for segment upload (#8715)
     add b8af790c2f Enable uploading segments to realtime tables (#8584)
     add b672af1b9f Disable Groovy function by default (#8711)
     add 3bbae6ef61 Remove big-decimal getters for lower/upper bound (#8712)
     add 2c3813bb97 Add pluggable client auth provider (#8670)
     new 90e722a6b0 make github action trigger on multi_stage_query_engine branch (#8342)
     new 8c8380005b Add pinot-query-planner module (#8340)
     new dacd28187a add pinot-query-runtime (#8412)
     new c06bbfbaf3 Use proto for query plan serialization (#8479)
     new 3b01b2dec8 add support for project/filter pushdown (#8558)
     new 30bc434343 fix rebase issue after PQL deprecation (#8652)
     new 4d77924e59 [hotfix] fix server discovery (#8664)
     new 2c07cb04e2 row/columnar compatible block (#8583)
     new fa2b2fa7cf [hotfix] multi stage engine serde (#8689)

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (08da4ee9e7)
            \
             N -- N -- N   refs/heads/multi_stage_query_engine (fa2b2fa7cf)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 9 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .github/workflows/pinot_tests.yml                  |   4 +-
 .github/workflows/scripts/.pinot_quickstart.sh     |  33 +-
 LICENSE-binary                                     |  42 +-
 .../config/BrokerConfig.properties                 |   1 +
 .../config/ControllerConfig.properties             |   1 +
 .../broker/broker/helix/BaseBrokerStarter.java     |   9 +-
 .../requesthandler/BaseBrokerRequestHandler.java   | 299 +++++---
 .../pinot/broker/routing/BrokerRoutingManager.java |   9 +-
 .../broker/broker/HelixBrokerStarterTest.java      |   2 +-
 .../requesthandler/DistinctCountRewriteTest.java   |  44 --
 .../requesthandler/QueryLimitOverrideTest.java     |  50 --
 .../broker/requesthandler/QueryOverrideTest.java   | 123 +++
 pinot-clients/pinot-jdbc-client/pom.xml            |   4 +
 .../java/org/apache/pinot/client/PinotDriver.java  |  35 +-
 .../controller/PinotControllerTransport.java       |  31 +-
 .../PinotControllerTransportFactory.java}          |  12 +-
 .../org/apache/pinot/client/utils/DriverUtils.java |  19 +-
 .../org/apache/pinot/common/auth/AuthConfig.java   |  28 +-
 .../pinot/common/auth/AuthProviderUtils.java       | 171 +++++
 .../NullAuthProvider.java}                         |  27 +-
 .../pinot/common/auth/StaticTokenAuthProvider.java |  68 ++
 .../apache/pinot/common/auth/UrlAuthProvider.java  |  87 +++
 .../common/metadata/segment/SegmentZKMetadata.java |   3 +-
 .../org/apache/pinot/common/utils/DataSchema.java  |   4 +
 .../common/utils/FileUploadDownloadClient.java     |  45 +-
 .../java/org/apache/pinot/common/utils/Pair.java   |  49 --
 .../pinot/common/utils/RoundRobinURIProvider.java  |   4 +-
 .../apache/pinot/common/utils/SegmentUtils.java    |  32 +-
 .../org/apache/pinot/common/utils/URIUtils.java    |  14 +-
 .../common/utils/fetcher/BaseSegmentFetcher.java   |   7 +-
 .../common/utils/fetcher/HttpSegmentFetcher.java   |   4 +-
 .../utils/fetcher/SegmentFetcherFactory.java       |  15 +-
 .../pinot/common/utils/helix/HelixHelper.java      |   4 +-
 .../apache/pinot/common/utils/http/HttpClient.java |  70 +-
 .../common/utils/RoundRobinURIProviderTest.java    | 151 ++--
 .../common/utils/config/TableConfigSerDeTest.java  |   6 +-
 .../pinot/controller/BaseControllerStarter.java    |   8 +-
 .../apache/pinot/controller/ControllerConf.java    |   6 +-
 .../resources/PinotIngestionRestletResource.java   |  32 +-
 .../PinotSegmentUploadDownloadRestletResource.java | 268 +++----
 .../api/upload/SegmentValidationUtils.java         |  94 +++
 .../controller/api/upload/SegmentValidator.java    | 122 ---
 .../pinot/controller/api/upload/ZKOperator.java    | 246 +++---
 .../helix/core/PinotHelixResourceManager.java      | 189 +++--
 .../segment/RealtimeSegmentAssignment.java         |  50 +-
 .../core/minion/MinionInstancesCleanupTask.java    |   4 +-
 .../helix/core/minion/TaskMetricsEmitter.java      |  20 +-
 .../realtime/PinotLLCRealtimeSegmentManager.java   |  16 +-
 .../core/realtime/PinotRealtimeSegmentManager.java |  10 +-
 .../helix/core/util/ZKMetadataUtils.java           | 105 ++-
 .../pinot/controller/util/FileIngestionHelper.java |  10 +-
 .../pinot/controller/api/TableViewsTest.java       |   9 +-
 .../controller/api/upload/ZKOperatorTest.java      |  40 +-
 .../pinot/controller/helix/ControllerTest.java     |   3 +-
 .../helix/core/PinotHelixResourceManagerTest.java  |  62 +-
 ...altimeNonReplicaGroupSegmentAssignmentTest.java | 114 ++-
 ...NonReplicaGroupTieredSegmentAssignmentTest.java |   5 +-
 .../RealtimeReplicaGroupSegmentAssignmentTest.java | 132 +++-
 .../helix/core/realtime/SegmentCompletionTest.java |  10 +-
 .../helix/core/retention/RetentionManagerTest.java |  38 +-
 .../validation/ValidationManagerTest.java          |   3 +-
 pinot-core/pom.xml                                 |  12 +
 .../core/data/manager/BaseTableDataManager.java    |  58 +-
 .../manager/offline/OfflineTableDataManager.java   |  12 -
 .../manager/offline/TableDataManagerProvider.java  |   7 +-
 .../manager/realtime/RealtimeTableDataManager.java |   5 +-
 .../manager/realtime/SegmentCommitterFactory.java  |   2 +-
 .../realtime/Server2ControllerSegmentUploader.java |  11 +-
 .../core/operator/filter/FilterOperatorUtils.java  |   3 +-
 .../filter/H3InclusionIndexFilterOperator.java     |  52 +-
 .../predicate/RangePredicateEvaluatorFactory.java  |  48 +-
 .../query/AggregationGroupByOrderByOperator.java   |   2 +
 .../plan/AggregationGroupByOrderByPlanNode.java    |   2 +-
 .../pinot/core/plan/AggregationPlanNode.java       |   2 +-
 .../org/apache/pinot/core/plan/FilterPlanNode.java |  29 +-
 .../statement/JsonStatementOptimizer.java          |  20 +-
 .../query/pruner/ColumnValueSegmentPruner.java     |  19 +-
 .../core/query/reduce/BaseGapfillProcessor.java    | 229 ++++++
 .../core/query/reduce/BrokerReduceService.java     |   7 +-
 .../core/query/reduce/CountGapfillProcessor.java   | 131 ++++
 .../pinot/core/query/reduce/GapfillProcessor.java  | 138 +---
 .../core/query/reduce/GapfillProcessorFactory.java |  65 ++
 .../core/query/reduce/SumAvgGapfillProcessor.java  | 212 ++++++
 .../apache/pinot/core/startree/StarTreeUtils.java  |  24 +-
 .../apache/pinot/core/util/ListenerConfigUtil.java |  19 +-
 .../server/realtime/ControllerLeaderLocator.java   |   6 +-
 .../ServerSegmentCompletionProtocolHandler.java    |  26 +-
 .../core/common/datatable/DataTableSerDeTest.java  |  75 +-
 .../BaseTableDataManagerAcquireSegmentTest.java    |   5 +-
 .../data/manager/BaseTableDataManagerTest.java     |   5 +-
 .../offline/DimensionTableDataManagerTest.java     |   2 +-
 .../query/pruner/ColumnValueSegmentPrunerTest.java |  32 +-
 .../pinot/core/startree/v2/BaseStarTreeV2Test.java |   2 +-
 .../queries/GapfillQueriesScalabilityTest.java     | 822 +++++++++++++++++++++
 .../apache/pinot/queries/GapfillQueriesTest.java   |   1 +
 .../apache/pinot/queries/H3IndexQueriesTest.java   |  28 +
 .../queries/JsonIngestionFromAvroQueriesTest.java  |  10 +-
 .../org/apache/pinot/queries/RangeQueriesTest.java |  72 +-
 .../realtime/ControllerLeaderLocatorTest.java      |  30 +-
 pinot-distribution/pinot-assembly.xml              |   2 -
 pinot-distribution/pom.xml                         |  12 -
 .../tests/BaseClusterIntegrationTest.java          |  15 +-
 .../pinot/integration/tests/ClusterTest.java       | 100 +--
 pinot-integration-tests/pom.xml                    |  13 +-
 .../tests/BaseClusterIntegrationTestSet.java       |   3 +-
 .../tests/BasicAuthBatchIntegrationTest.java       |   4 +-
 .../tests/LLCRealtimeClusterIntegrationTest.java   |  96 ++-
 .../tests/OfflineClusterIntegrationTest.java       |  48 +-
 .../tests/OfflineGRPCServerIntegrationTest.java    |   2 +-
 .../tests/RealtimeClusterIntegrationTest.java      |  16 +-
 ...TextIndicesRealtimeClusterIntegrationTest.java} |  50 +-
 .../integration/tests/TlsIntegrationTest.java      |  58 ++
 .../UpsertTableSegmentUploadIntegrationTest.java   | 218 +++---
 ...st.java => UrlAuthRealtimeIntegrationTest.java} |  55 +-
 .../ControllerLeaderLocatorIntegrationTest.java    |   8 +-
 .../src/test/resources/url-auth-token-prefixed.txt |   1 +
 .../src/test/resources/url-auth-token.txt          |   1 +
 .../org/apache/pinot/minion/BaseMinionStarter.java |   7 +-
 .../org/apache/pinot/perf/BenchmarkQueries.java    |   6 +-
 .../v0_deprecated/pinot-hadoop/pom.xml             |  12 -
 .../pinot/plugin/filesystem/ADLSGen2PinotFS.java   |  83 ++-
 .../org/apache/pinot/plugin/filesystem/GcsUri.java |  16 +-
 .../pinot/plugin/filesystem/TestGcsPinotFS.java    |  10 +-
 .../apache/pinot/plugin/filesystem/TestGcsUri.java |  20 +-
 .../inputformat/avro/AvroRecordExtractor.java      |  32 +
 .../avro/AvroRecordExtractorConfig.java}           |  20 +-
 .../plugin/inputformat/avro/AvroRecordReader.java  |   8 +-
 .../inputformat/avro/AvroRecordReaderConfig.java}  |  19 +-
 .../plugin/inputformat/avro/AvroSchemaUtil.java    |  58 ++
 .../inputformat/avro/AvroRecordExtractorTest.java  | 143 +++-
 .../inputformat/avro/AvroSchemaUtilTest.java       |  83 +++
 .../inputformat/avro/KafkaAvroMessageDecoder.java  |  10 +-
 .../inputformat/avro/SimpleAvroMessageDecoder.java |  10 +-
 ...aConfluentSchemaRegistryAvroMessageDecoder.java |   5 +-
 .../plugin/inputformat/csv/CSVMessageDecoder.java  | 146 ++++
 .../inputformat/csv/CSVMessageDecoderTest.java     | 157 ++++
 .../BaseMultipleSegmentsConversionExecutor.java    |  19 +-
 .../tasks/BaseSingleSegmentConversionExecutor.java |   7 +-
 .../minion/tasks/SegmentConversionUtils.java       |  19 +-
 .../segmentuploader/SegmentUploaderDefault.java    |  10 +-
 .../pinot-stream-ingestion/pinot-pulsar/pom.xml    |   2 +-
 .../plugin/stream/pulsar/PulsarConsumerTest.java   |  77 +-
 .../local/data/manager/TableDataManager.java       |   4 +-
 .../local/data/manager/TableDataManagerConfig.java |  14 +-
 .../immutable/ImmutableSegmentLoader.java          |   2 +
 .../indexsegment/mutable/MutableSegmentImpl.java   |  25 +-
 .../writer/impl/BaseChunkSVForwardIndexWriter.java |   1 +
 ...ava => BigDecimalOffHeapMutableDictionary.java} | 130 ++--
 ...java => BigDecimalOnHeapMutableDictionary.java} | 105 +--
 .../impl/dictionary/MutableDictionaryFactory.java  |   5 +
 .../impl/invertedindex/NativeMutableTextIndex.java | 123 +++
 .../invertedindex/RealtimeLuceneTextIndex.java     |   2 +-
 .../creator/impl/SegmentColumnarIndexCreator.java  |   9 +-
 .../creator/impl/inv/RangeIndexCreator.java        |  18 +-
 .../pinot/segment/local/utils/IngestionUtils.java  |  17 +-
 .../segment/local/utils/SegmentPushUtils.java      |  16 +-
 .../impl/dictionary/MutableDictionaryTest.java     |  15 +-
 .../NativeAndLuceneMutableTextIndexTest.java       | 107 +++
 .../NativeMutableTextIndexConcurrentTest.java      | 251 +++++++
 .../NativeMutableTextIndexReaderWriterTest.java}   |  44 +-
 .../forward/VarByteChunkSVForwardIndexTest.java    |  24 +
 .../mutablefst/MutableFSTConcurrentTest.java       |  74 +-
 .../apache/pinot/segment/spi/SegmentMetadata.java  |   4 +
 .../org/apache/pinot/segment/spi/V1Constants.java  |   5 +
 .../creator/name/FixedSegmentNameGenerator.java    |   6 +-
 .../name/InputFileSegmentNameGenerator.java        |  31 +-
 .../name/NormalizedDateSegmentNameGenerator.java   |  22 +-
 .../creator/name/SimpleSegmentNameGenerator.java   |  17 +-
 .../spi/index/metadata/SegmentMetadataImpl.java    |  29 +-
 .../name/FixedSegmentNameGeneratorTest.java}       |  22 +-
 .../name/InputFileSegmentNameGeneratorTest.java    |   3 +-
 .../NormalizedDateSegmentNameGeneratorTest.java    |  17 +-
 .../name/SimpleSegmentNameGeneratorTest.java       |   1 +
 .../server/starter/helix/BaseServerStarter.java    |   4 +
 .../starter/helix/HelixInstanceDataManager.java    |   7 +-
 .../helix/HelixInstanceDataManagerConfig.java      |  15 +-
 .../apache/pinot/server/api/BaseResourceTest.java  |   2 +-
 .../auth/{AuthContext.java => AuthProvider.java}   |  21 +-
 .../config/instance/InstanceDataManagerConfig.java |   2 +-
 .../apache/pinot/spi/config/table/QueryConfig.java |  23 +-
 .../spi/data/readers/RecordExtractorConfig.java    |   4 +
 .../spi/data/readers/RecordReaderFactory.java      |   6 +-
 .../batch/spec/SegmentGenerationJobSpec.java       |   6 +
 .../segment/uploader/SegmentUploader.java          |  10 +-
 .../org/apache/pinot/spi/plugin/PluginManager.java |   1 +
 .../pinot/spi/stream/StreamMessageDecoder.java     |   1 +
 .../pinot/spi/trace/InvocationRecording.java       |   9 +
 .../apache/pinot/spi/utils/CommonConstants.java    |  14 +-
 .../apache/pinot/spi/utils/InstanceTypeUtils.java  |  62 ++
 .../main/java/org/apache/pinot/spi/utils/Pair.java |  73 --
 .../data/readers/AbstractRecordExtractorTest.java  |  12 +-
 .../spi/data/readers/RecordReaderFactoryTest.java  |   4 +-
 pinot-tools/pom.xml                                |  20 +-
 .../org/apache/pinot/tools/AuthQuickstart.java     |   6 +-
 .../pinot/tools/BatchQuickstartWithMinion.java     |  49 --
 .../org/apache/pinot/tools/BootstrapTableTool.java |  13 +-
 .../org/apache/pinot/tools/EmptyQuickstart.java    |   7 +-
 .../org/apache/pinot/tools/GenericQuickstart.java  |   2 +-
 .../apache/pinot/tools/GitHubEventsQuickstart.java |   2 +-
 .../org/apache/pinot/tools/HybridQuickstart.java   |   2 +-
 .../org/apache/pinot/tools/JoinQuickStart.java     |   2 +-
 .../apache/pinot/tools/JsonIndexQuickStart.java    |   2 +-
 .../OfflineComplexTypeHandlingQuickStart.java      |   2 +-
 .../pinot/tools/PartialUpsertQuickStart.java       |   2 +-
 .../java/org/apache/pinot/tools/Quickstart.java    |  12 +-
 .../RealtimeComplexTypeHandlingQuickStart.java     |   2 +-
 .../pinot/tools/RealtimeJsonIndexQuickStart.java   |   2 +-
 .../org/apache/pinot/tools/RealtimeQuickStart.java |   2 +-
 .../pinot/tools/TimestampIndexQuickstart.java      |   2 +-
 .../apache/pinot/tools/UpsertJsonQuickStart.java   |   2 +-
 .../org/apache/pinot/tools/UpsertQuickStart.java   |   2 +-
 .../pinot/tools/admin/PinotAdministrator.java      |   4 +
 .../admin/command/AbstractBaseAdminCommand.java    |  38 +-
 .../tools/admin/command/AddSchemaCommand.java      |  14 +-
 .../pinot/tools/admin/command/AddTableCommand.java |  20 +-
 .../tools/admin/command/AddTenantCommand.java      |  17 +-
 .../tools/admin/command/BootstrapTableCommand.java |  16 +-
 .../tools/admin/command/ChangeTableState.java      |  20 +-
 .../admin/command/DataImportDryRunCommand.java     |  96 +++
 .../tools/admin/command/FileSystemCommand.java     | 111 +++
 .../tools/admin/command/ImportDataCommand.java     |  17 +-
 .../command/LaunchDataIngestionJobCommand.java     |  11 +-
 .../admin/command/OperateClusterConfigCommand.java |  19 +-
 .../tools/admin/command/PostQueryCommand.java      |  17 +-
 .../tools/admin/command/QuickstartRunner.java      |  59 +-
 .../tools/admin/command/UploadSegmentCommand.java  |  20 +-
 .../command/filesystem/BaseFileOperation.java      |  58 ++
 .../tools/admin/command/filesystem/CopyFiles.java  | 113 +++
 .../admin/command/filesystem/DeleteFiles.java      |  81 ++
 .../tools/admin/command/filesystem/ListFiles.java  |  93 +++
 .../tools/admin/command/filesystem/MoveFiles.java  |  93 +++
 .../tools/admin/command/filesystem/Utils.java      |  26 +-
 .../src/main/resources/conf/pinot-minion.conf      |   5 +-
 .../tools/admin/command/TestQuickStartCommand.java | 159 ++--
 pom.xml                                            |  36 +-
 235 files changed, 7076 insertions(+), 2628 deletions(-)
 delete mode 100644 pinot-broker/src/test/java/org/apache/pinot/broker/requesthandler/DistinctCountRewriteTest.java
 delete mode 100644 pinot-broker/src/test/java/org/apache/pinot/broker/requesthandler/QueryLimitOverrideTest.java
 create mode 100644 pinot-broker/src/test/java/org/apache/pinot/broker/requesthandler/QueryOverrideTest.java
 copy pinot-clients/{pinot-java-client/src/main/java/org/apache/pinot/client/JsonAsyncHttpPinotClientTransportFactory.java => pinot-jdbc-client/src/main/java/org/apache/pinot/client/controller/PinotControllerTransportFactory.java} (81%)
 copy pinot-spi/src/main/java/org/apache/pinot/spi/services/ServiceStartable.java => pinot-common/src/main/java/org/apache/pinot/common/auth/AuthConfig.java (64%)
 create mode 100644 pinot-common/src/main/java/org/apache/pinot/common/auth/AuthProviderUtils.java
 copy pinot-common/src/main/java/org/apache/pinot/common/{request/context/predicate/BasePredicate.java => auth/NullAuthProvider.java} (64%)
 create mode 100644 pinot-common/src/main/java/org/apache/pinot/common/auth/StaticTokenAuthProvider.java
 create mode 100644 pinot-common/src/main/java/org/apache/pinot/common/auth/UrlAuthProvider.java
 delete mode 100644 pinot-common/src/main/java/org/apache/pinot/common/utils/Pair.java
 create mode 100644 pinot-controller/src/main/java/org/apache/pinot/controller/api/upload/SegmentValidationUtils.java
 delete mode 100644 pinot-controller/src/main/java/org/apache/pinot/controller/api/upload/SegmentValidator.java
 create mode 100644 pinot-core/src/main/java/org/apache/pinot/core/query/reduce/BaseGapfillProcessor.java
 create mode 100644 pinot-core/src/main/java/org/apache/pinot/core/query/reduce/CountGapfillProcessor.java
 create mode 100644 pinot-core/src/main/java/org/apache/pinot/core/query/reduce/GapfillProcessorFactory.java
 create mode 100644 pinot-core/src/main/java/org/apache/pinot/core/query/reduce/SumAvgGapfillProcessor.java
 create mode 100644 pinot-core/src/test/java/org/apache/pinot/queries/GapfillQueriesScalabilityTest.java
 rename pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/{LuceneRealtimeClusterIntegrationTest.java => TextIndicesRealtimeClusterIntegrationTest.java} (75%)
 rename pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/{BasicAuthRealtimeIntegrationTest.java => UrlAuthRealtimeIntegrationTest.java} (70%)
 create mode 100644 pinot-integration-tests/src/test/resources/url-auth-token-prefixed.txt
 create mode 100644 pinot-integration-tests/src/test/resources/url-auth-token.txt
 copy pinot-plugins/pinot-input-format/{pinot-thrift/src/main/java/org/apache/pinot/plugin/inputformat/thrift/ThriftRecordExtractorConfig.java => pinot-avro-base/src/main/java/org/apache/pinot/plugin/inputformat/avro/AvroRecordExtractorConfig.java} (62%)
 copy pinot-plugins/pinot-input-format/{pinot-protobuf/src/main/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufRecordReaderConfig.java => pinot-avro-base/src/main/java/org/apache/pinot/plugin/inputformat/avro/AvroRecordReaderConfig.java} (69%)
 create mode 100644 pinot-plugins/pinot-input-format/pinot-avro-base/src/test/java/org/apache/pinot/plugin/inputformat/avro/AvroSchemaUtilTest.java
 create mode 100644 pinot-plugins/pinot-input-format/pinot-csv/src/main/java/org/apache/pinot/plugin/inputformat/csv/CSVMessageDecoder.java
 create mode 100644 pinot-plugins/pinot-input-format/pinot-csv/src/test/java/org/apache/pinot/plugin/inputformat/csv/CSVMessageDecoderTest.java
 copy pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/dictionary/{IntOffHeapMutableDictionary.java => BigDecimalOffHeapMutableDictionary.java} (54%)
 copy pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/dictionary/{LongOnHeapMutableDictionary.java => BigDecimalOnHeapMutableDictionary.java} (58%)
 create mode 100644 pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/NativeMutableTextIndex.java
 create mode 100644 pinot-segment-local/src/test/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/NativeAndLuceneMutableTextIndexTest.java
 create mode 100644 pinot-segment-local/src/test/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/NativeMutableTextIndexConcurrentTest.java
 copy pinot-segment-local/src/test/java/org/apache/pinot/segment/local/{segment/index/creator/NativeTextIndexCreatorTest.java => realtime/impl/invertedindex/NativeMutableTextIndexReaderWriterTest.java} (52%)
 copy pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/{reader/H3IndexResolutionTest.java => creator/name/FixedSegmentNameGeneratorTest.java} (60%)
 rename pinot-spi/src/main/java/org/apache/pinot/spi/auth/{AuthContext.java => AuthProvider.java} (62%)
 create mode 100644 pinot-spi/src/main/java/org/apache/pinot/spi/utils/InstanceTypeUtils.java
 delete mode 100644 pinot-spi/src/main/java/org/apache/pinot/spi/utils/Pair.java
 delete mode 100644 pinot-tools/src/main/java/org/apache/pinot/tools/BatchQuickstartWithMinion.java
 create mode 100644 pinot-tools/src/main/java/org/apache/pinot/tools/admin/command/DataImportDryRunCommand.java
 create mode 100644 pinot-tools/src/main/java/org/apache/pinot/tools/admin/command/FileSystemCommand.java
 create mode 100644 pinot-tools/src/main/java/org/apache/pinot/tools/admin/command/filesystem/BaseFileOperation.java
 create mode 100644 pinot-tools/src/main/java/org/apache/pinot/tools/admin/command/filesystem/CopyFiles.java
 create mode 100644 pinot-tools/src/main/java/org/apache/pinot/tools/admin/command/filesystem/DeleteFiles.java
 create mode 100644 pinot-tools/src/main/java/org/apache/pinot/tools/admin/command/filesystem/ListFiles.java
 create mode 100644 pinot-tools/src/main/java/org/apache/pinot/tools/admin/command/filesystem/MoveFiles.java
 copy pinot-common/src/main/java/org/apache/pinot/common/utils/fetcher/PinotFSSegmentFetcher.java => pinot-tools/src/main/java/org/apache/pinot/tools/admin/command/filesystem/Utils.java (57%)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[pinot] 08/09: row/columnar compatible block (#8583)

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rongr pushed a commit to branch multi_stage_query_engine
in repository https://gitbox.apache.org/repos/asf/pinot.git

commit 2c07cb04e25c9fc506b144b157756bca9ee612ac
Author: Rong Rong <wa...@gmail.com>
AuthorDate: Tue May 10 14:30:46 2022 -0700

    row/columnar compatible block (#8583)
    
    adding tests and verify both row/columnar format
    
    adding test and validating all types
    
    fix and add query dispatcher test
    
    Co-authored-by: Rong Rong <ro...@startree.ai>
---
 .../apache/pinot/query/runtime/QueryRunner.java    |  15 +-
 .../pinot/query/runtime/blocks/BaseDataBlock.java  | 697 +++++++++++++++++++++
 .../query/runtime/blocks/ColumnarDataBlock.java    |  96 +++
 .../query/runtime/blocks/DataBlockBuilder.java     | 406 ++++++++++++
 .../pinot/query/runtime/blocks/DataBlockUtils.java | 174 +++++
 .../query/runtime/blocks/DataTableBlockUtils.java  |  71 ---
 .../pinot/query/runtime/blocks/MetadataBlock.java  |  66 ++
 .../pinot/query/runtime/blocks/RowDataBlock.java   |  90 +++
 ...{DataTableBlock.java => TransferableBlock.java} |  37 +-
 .../runtime/executor/WorkerQueryExecutor.java      |  16 +-
 .../query/runtime/operator/HashJoinOperator.java   |  51 +-
 .../runtime/operator/MailboxReceiveOperator.java   |  19 +-
 .../runtime/operator/MailboxSendOperator.java      |  48 +-
 .../pinot/query/service/QueryDispatcher.java       |  54 +-
 .../query/mailbox/GrpcMailboxServiceTest.java      |   7 +-
 .../pinot/query/runtime/QueryRunnerTest.java       |  52 +-
 .../pinot/query/runtime/blocks/DataBlockTest.java  |  80 +++
 .../query/runtime/blocks/DataBlockTestUtils.java   | 181 ++++++
 .../pinot/query/service/QueryDispatcherTest.java   |  92 +++
 19 files changed, 2034 insertions(+), 218 deletions(-)

diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java
index f33cde43c2..e25c6e03b8 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java
@@ -18,6 +18,8 @@
  */
 package org.apache.pinot.query.runtime;
 
+import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
@@ -32,6 +34,8 @@ import org.apache.pinot.query.mailbox.GrpcMailboxService;
 import org.apache.pinot.query.mailbox.MailboxService;
 import org.apache.pinot.query.planner.StageMetadata;
 import org.apache.pinot.query.planner.stage.MailboxSendNode;
+import org.apache.pinot.query.runtime.blocks.BaseDataBlock;
+import org.apache.pinot.query.runtime.blocks.DataBlockUtils;
 import org.apache.pinot.query.runtime.executor.WorkerQueryExecutor;
 import org.apache.pinot.query.runtime.operator.MailboxSendOperator;
 import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
@@ -94,12 +98,19 @@ public class QueryRunner {
           ServerRequestUtils.constructServerQueryRequest(distributedStagePlan, requestMetadataMap);
 
       // send the data table via mailbox in one-off fashion (e.g. no block-level split, one data table/partition key)
-      DataTable dataTable = _serverExecutor.processQuery(serverQueryRequest, executorService, null);
+      BaseDataBlock dataBlock;
+      try {
+        DataTable dataTable = _serverExecutor.processQuery(serverQueryRequest, executorService, null);
+        // this works because default DataTableImplV3 will have ordinal 0, which maps to ROW(0)
+        dataBlock = DataBlockUtils.getDataBlock(ByteBuffer.wrap(dataTable.toBytes()));
+      } catch (IOException e) {
+        throw new RuntimeException("Unable to convert byte buffer", e);
+      }
 
       MailboxSendNode sendNode = (MailboxSendNode) distributedStagePlan.getStageRoot();
       StageMetadata receivingStageMetadata = distributedStagePlan.getMetadataMap().get(sendNode.getReceiverStageId());
       MailboxSendOperator mailboxSendOperator =
-          new MailboxSendOperator(_mailboxService, dataTable, receivingStageMetadata.getServerInstances(),
+          new MailboxSendOperator(_mailboxService, dataBlock, receivingStageMetadata.getServerInstances(),
               sendNode.getExchangeType(), sendNode.getPartitionKeySelector(), _hostname, _port,
               serverQueryRequest.getRequestId(), sendNode.getStageId());
       mailboxSendOperator.nextBlock();
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/BaseDataBlock.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/BaseDataBlock.java
new file mode 100644
index 0000000000..97d007b58b
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/BaseDataBlock.java
@@ -0,0 +1,697 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import com.google.common.primitives.Ints;
+import com.google.common.primitives.Longs;
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.pinot.common.response.ProcessingException;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.core.common.ObjectSerDeUtils;
+import org.apache.pinot.core.common.datatable.DataTableUtils;
+import org.apache.pinot.core.query.request.context.ThreadTimer;
+import org.apache.pinot.spi.utils.BigDecimalUtils;
+import org.apache.pinot.spi.utils.ByteArray;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+
+/**
+ * Base data block mostly replicating implementation of {@link org.apache.pinot.core.common.datatable.DataTableImplV3}.
+ *
+ * +-----------------------------------------------+
+ * | 13 integers of header:                        |
+ * | VERSION                                       |
+ * | NUM_ROWS                                      |
+ * | NUM_COLUMNS                                   |
+ * | EXCEPTIONS SECTION START OFFSET               |
+ * | EXCEPTIONS SECTION LENGTH                     |
+ * | DICTIONARY_MAP SECTION START OFFSET           |
+ * | DICTIONARY_MAP SECTION LENGTH                 |
+ * | DATA_SCHEMA SECTION START OFFSET              |
+ * | DATA_SCHEMA SECTION LENGTH                    |
+ * | FIXED_SIZE_DATA SECTION START OFFSET          |
+ * | FIXED_SIZE_DATA SECTION LENGTH                |
+ * | VARIABLE_SIZE_DATA SECTION START OFFSET       |
+ * | VARIABLE_SIZE_DATA SECTION LENGTH             |
+ * +-----------------------------------------------+
+ * | EXCEPTIONS SECTION                            |
+ * +-----------------------------------------------+
+ * | DICTIONARY_MAP SECTION                        |
+ * +-----------------------------------------------+
+ * | DATA_SCHEMA SECTION                           |
+ * +-----------------------------------------------+
+ * | FIXED_SIZE_DATA SECTION                       |
+ * +-----------------------------------------------+
+ * | VARIABLE_SIZE_DATA SECTION                    |
+ * +-----------------------------------------------+
+ * | METADATA LENGTH                               |
+ * | METADATA SECTION                              |
+ * +-----------------------------------------------+
+ *
+ * To support both row and columnar data format. the size of the data payload will be exactly the same. the only
+ * difference is the data layout in FIXED_SIZE_DATA and VARIABLE_SIZE_DATA section, see each impl for details.
+ */
+@SuppressWarnings("DuplicatedCode")
+public abstract class BaseDataBlock implements DataTable {
+  protected static final int HEADER_SIZE = Integer.BYTES * 13;
+  // _errCodeToExceptionMap stores exceptions as a map of errorCode->errorMessage
+  protected Map<Integer, String> _errCodeToExceptionMap;
+
+  protected int _numRows;
+  protected int _numColumns;
+  protected DataSchema _dataSchema;
+  protected Map<String, Map<Integer, String>> _dictionaryMap;
+  protected byte[] _fixedSizeDataBytes;
+  protected ByteBuffer _fixedSizeData;
+  protected byte[] _variableSizeDataBytes;
+  protected ByteBuffer _variableSizeData;
+  protected Map<String, String> _metadata;
+
+  /**
+   * construct a base data block.
+   * @param numRows num of rows in the block
+   * @param dataSchema schema of the data in the block
+   * @param dictionaryMap dictionary encoding map
+   * @param fixedSizeDataBytes byte[] for fix-sized columns.
+   * @param variableSizeDataBytes byte[] for variable length columns (arrays).
+   */
+  public BaseDataBlock(int numRows, DataSchema dataSchema, Map<String, Map<Integer, String>> dictionaryMap,
+      byte[] fixedSizeDataBytes, byte[] variableSizeDataBytes) {
+    _numRows = numRows;
+    _numColumns = dataSchema.size();
+    _dataSchema = dataSchema;
+    _dictionaryMap = dictionaryMap;
+    _fixedSizeDataBytes = fixedSizeDataBytes;
+    _fixedSizeData = ByteBuffer.wrap(fixedSizeDataBytes);
+    _variableSizeDataBytes = variableSizeDataBytes;
+    _variableSizeData = ByteBuffer.wrap(variableSizeDataBytes);
+    _metadata = new HashMap<>();
+    _errCodeToExceptionMap = new HashMap<>();
+  }
+
+  /**
+   * Construct empty data table.
+   */
+  public BaseDataBlock() {
+    _numRows = 0;
+    _numColumns = 0;
+    _dataSchema = null;
+    _dictionaryMap = null;
+    _fixedSizeDataBytes = null;
+    _fixedSizeData = null;
+    _variableSizeDataBytes = null;
+    _variableSizeData = null;
+    _metadata = new HashMap<>();
+    _errCodeToExceptionMap = new HashMap<>();
+  }
+
+  public BaseDataBlock(ByteBuffer byteBuffer)
+      throws IOException {
+    // Read header.
+    _numRows = byteBuffer.getInt();
+    _numColumns = byteBuffer.getInt();
+    int exceptionsStart = byteBuffer.getInt();
+    int exceptionsLength = byteBuffer.getInt();
+    int dictionaryMapStart = byteBuffer.getInt();
+    int dictionaryMapLength = byteBuffer.getInt();
+    int dataSchemaStart = byteBuffer.getInt();
+    int dataSchemaLength = byteBuffer.getInt();
+    int fixedSizeDataStart = byteBuffer.getInt();
+    int fixedSizeDataLength = byteBuffer.getInt();
+    int variableSizeDataStart = byteBuffer.getInt();
+    int variableSizeDataLength = byteBuffer.getInt();
+
+
+    // Read exceptions.
+    if (exceptionsLength != 0) {
+      byteBuffer.position(exceptionsStart);
+      _errCodeToExceptionMap = deserializeExceptions(byteBuffer);
+    } else {
+      _errCodeToExceptionMap = new HashMap<>();
+    }
+
+    // Read dictionary.
+    if (dictionaryMapLength != 0) {
+      byteBuffer.position(dictionaryMapStart);
+      _dictionaryMap = deserializeDictionaryMap(byteBuffer);
+    } else {
+      _dictionaryMap = null;
+    }
+
+    // Read data schema.
+    if (dataSchemaLength != 0) {
+      byteBuffer.position(dataSchemaStart);
+      _dataSchema = DataSchema.fromBytes(byteBuffer);
+    } else {
+      _dataSchema = null;
+    }
+
+    // Read fixed size data.
+    if (fixedSizeDataLength != 0) {
+      _fixedSizeDataBytes = new byte[fixedSizeDataLength];
+      byteBuffer.position(fixedSizeDataStart);
+      byteBuffer.get(_fixedSizeDataBytes);
+      _fixedSizeData = ByteBuffer.wrap(_fixedSizeDataBytes);
+    } else {
+      _fixedSizeDataBytes = null;
+      _fixedSizeData = null;
+    }
+
+    // Read variable size data.
+    if (variableSizeDataLength != 0) {
+      _variableSizeDataBytes = new byte[variableSizeDataLength];
+      byteBuffer.position(variableSizeDataStart);
+      byteBuffer.get(_variableSizeDataBytes);
+      _variableSizeData = ByteBuffer.wrap(_variableSizeDataBytes);
+    } else {
+      _variableSizeDataBytes = null;
+      _variableSizeData = null;
+    }
+
+    // Read metadata.
+    int metadataLength = byteBuffer.getInt();
+    if (metadataLength != 0) {
+      _metadata = deserializeMetadata(byteBuffer);
+    }
+  }
+
+  /**
+   * Return the int serialized form of the data block version and type.
+   * @return
+   */
+  protected abstract int getDataBlockVersionType();
+
+  /**
+   * position the {@code _fixedSizeDataBytes} member variable to the corresponding row/column ID.
+   * @param rowId row ID
+   * @param colId column ID
+   */
+  protected abstract void positionCursorInFixSizedBuffer(int rowId, int colId);
+
+  /**
+   * position the {@code _variableSizeDataBytes} member variable to the corresponding row/column ID. and return the
+   * length of bytes to extract from the variable size buffer.
+   *
+   * @param rowId row ID
+   * @param colId column ID
+   * @return the length to extract from variable size buffer.
+   */
+  protected abstract int positionCursorInVariableBuffer(int rowId, int colId);
+
+  @Override
+  public Map<String, String> getMetadata() {
+    return _metadata;
+  }
+
+  @Override
+  public DataSchema getDataSchema() {
+    return _dataSchema;
+  }
+
+  @Override
+  public int getNumberOfRows() {
+    return _numRows;
+  }
+
+  // --------------------------------------------------------------------------
+  // Fixed sized element access.
+  // --------------------------------------------------------------------------
+
+  @Override
+  public int getInt(int rowId, int colId) {
+    positionCursorInFixSizedBuffer(rowId, colId);
+    return _fixedSizeData.getInt();
+  }
+
+  @Override
+  public long getLong(int rowId, int colId) {
+    positionCursorInFixSizedBuffer(rowId, colId);
+    return _fixedSizeData.getLong();
+  }
+
+  @Override
+  public float getFloat(int rowId, int colId) {
+    positionCursorInFixSizedBuffer(rowId, colId);
+    return _fixedSizeData.getFloat();
+  }
+
+  @Override
+  public double getDouble(int rowId, int colId) {
+    positionCursorInFixSizedBuffer(rowId, colId);
+    return _fixedSizeData.getDouble();
+  }
+
+  @Override
+  public BigDecimal getBigDecimal(int rowId, int colId) {
+    int size = positionCursorInVariableBuffer(rowId, colId);
+    ByteBuffer byteBuffer = _variableSizeData.slice();
+    byteBuffer.limit(size);
+    return BigDecimalUtils.deserialize(byteBuffer);
+  }
+
+  @Override
+  public String getString(int rowId, int colId) {
+    positionCursorInFixSizedBuffer(rowId, colId);
+    int dictId = _fixedSizeData.getInt();
+    return _dictionaryMap.get(_dataSchema.getColumnName(colId)).get(dictId);
+  }
+
+  @Override
+  public ByteArray getBytes(int rowId, int colId) {
+    int size = positionCursorInVariableBuffer(rowId, colId);
+    byte[] buffer = new byte[size];
+    _variableSizeData.get(buffer);
+    return new ByteArray(buffer);
+  }
+
+  // --------------------------------------------------------------------------
+  // Variable sized element access.
+  // --------------------------------------------------------------------------
+
+  @Override
+  public <T> T getObject(int rowId, int colId) {
+    int size = positionCursorInVariableBuffer(rowId, colId);
+    int objectTypeValue = _variableSizeData.getInt();
+    ByteBuffer byteBuffer = _variableSizeData.slice();
+    byteBuffer.limit(size);
+    return ObjectSerDeUtils.deserialize(byteBuffer, objectTypeValue);
+  }
+
+  @Override
+  public int[] getIntArray(int rowId, int colId) {
+    int length = positionCursorInVariableBuffer(rowId, colId);
+    int[] ints = new int[length];
+    for (int i = 0; i < length; i++) {
+      ints[i] = _variableSizeData.getInt();
+    }
+    return ints;
+  }
+
+  @Override
+  public long[] getLongArray(int rowId, int colId) {
+    int length = positionCursorInVariableBuffer(rowId, colId);
+    long[] longs = new long[length];
+    for (int i = 0; i < length; i++) {
+      longs[i] = _variableSizeData.getLong();
+    }
+    return longs;
+  }
+
+  @Override
+  public float[] getFloatArray(int rowId, int colId) {
+    int length = positionCursorInVariableBuffer(rowId, colId);
+    float[] floats = new float[length];
+    for (int i = 0; i < length; i++) {
+      floats[i] = _variableSizeData.getFloat();
+    }
+    return floats;
+  }
+
+  @Override
+  public double[] getDoubleArray(int rowId, int colId) {
+    int length = positionCursorInVariableBuffer(rowId, colId);
+    double[] doubles = new double[length];
+    for (int i = 0; i < length; i++) {
+      doubles[i] = _variableSizeData.getDouble();
+    }
+    return doubles;
+  }
+
+  @Override
+  public String[] getStringArray(int rowId, int colId) {
+    int length = positionCursorInVariableBuffer(rowId, colId);
+    String[] strings = new String[length];
+    Map<Integer, String> dictionary = _dictionaryMap.get(_dataSchema.getColumnName(colId));
+    for (int i = 0; i < length; i++) {
+      strings[i] = dictionary.get(_variableSizeData.getInt());
+    }
+    return strings;
+  }
+
+  // --------------------------------------------------------------------------
+  // Ser/De and exception handling
+  // --------------------------------------------------------------------------
+
+  /**
+   * Helper method to serialize dictionary map.
+   */
+  protected byte[] serializeDictionaryMap()
+      throws IOException {
+    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+    DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
+
+    dataOutputStream.writeInt(_dictionaryMap.size());
+    for (Map.Entry<String, Map<Integer, String>> dictionaryMapEntry : _dictionaryMap.entrySet()) {
+      String columnName = dictionaryMapEntry.getKey();
+      Map<Integer, String> dictionary = dictionaryMapEntry.getValue();
+      byte[] bytes = columnName.getBytes(UTF_8);
+      dataOutputStream.writeInt(bytes.length);
+      dataOutputStream.write(bytes);
+      dataOutputStream.writeInt(dictionary.size());
+
+      for (Map.Entry<Integer, String> dictionaryEntry : dictionary.entrySet()) {
+        dataOutputStream.writeInt(dictionaryEntry.getKey());
+        byte[] valueBytes = dictionaryEntry.getValue().getBytes(UTF_8);
+        dataOutputStream.writeInt(valueBytes.length);
+        dataOutputStream.write(valueBytes);
+      }
+    }
+
+    return byteArrayOutputStream.toByteArray();
+  }
+
+  /**
+   * Helper method to deserialize dictionary map.
+   */
+  protected Map<String, Map<Integer, String>> deserializeDictionaryMap(ByteBuffer buffer)
+      throws IOException {
+    int numDictionaries = buffer.getInt();
+    Map<String, Map<Integer, String>> dictionaryMap = new HashMap<>(numDictionaries);
+
+    for (int i = 0; i < numDictionaries; i++) {
+      String column = DataTableUtils.decodeString(buffer);
+      int dictionarySize = buffer.getInt();
+      Map<Integer, String> dictionary = new HashMap<>(dictionarySize);
+      for (int j = 0; j < dictionarySize; j++) {
+        int key = buffer.getInt();
+        String value = DataTableUtils.decodeString(buffer);
+        dictionary.put(key, value);
+      }
+      dictionaryMap.put(column, dictionary);
+    }
+
+    return dictionaryMap;
+  }
+
+  @Override
+  public void addException(ProcessingException processingException) {
+    _errCodeToExceptionMap.put(processingException.getErrorCode(), processingException.getMessage());
+  }
+
+  @Override
+  public void addException(int errCode, String errMsg) {
+    _errCodeToExceptionMap.put(errCode, errMsg);
+  }
+
+  @Override
+  public Map<Integer, String> getExceptions() {
+    return _errCodeToExceptionMap;
+  }
+
+  @Override
+  public byte[] toBytes()
+      throws IOException {
+    ThreadTimer threadTimer = new ThreadTimer();
+
+    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+    DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
+    writeLeadingSections(dataOutputStream);
+
+    // Add table serialization time metadata if thread timer is enabled.
+    if (ThreadTimer.isThreadCpuTimeMeasurementEnabled()) {
+      long responseSerializationCpuTimeNs = threadTimer.getThreadTimeNs();
+      getMetadata().put(MetadataKey.RESPONSE_SER_CPU_TIME_NS.getName(), String.valueOf(responseSerializationCpuTimeNs));
+    }
+
+    // Write metadata: length followed by actual metadata bytes.
+    // NOTE: We ignore metadata serialization time in "responseSerializationCpuTimeNs" as it's negligible while
+    // considering it will bring a lot code complexity.
+    byte[] metadataBytes = serializeMetadata();
+    dataOutputStream.writeInt(metadataBytes.length);
+    dataOutputStream.write(metadataBytes);
+
+    return byteArrayOutputStream.toByteArray();
+  }
+
+  private void writeLeadingSections(DataOutputStream dataOutputStream)
+      throws IOException {
+    dataOutputStream.writeInt(getDataBlockVersionType());
+    dataOutputStream.writeInt(_numRows);
+    dataOutputStream.writeInt(_numColumns);
+    int dataOffset = HEADER_SIZE;
+
+    // Write exceptions section offset(START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    byte[] exceptionsBytes;
+    exceptionsBytes = serializeExceptions();
+    dataOutputStream.writeInt(exceptionsBytes.length);
+    dataOffset += exceptionsBytes.length;
+
+    // Write dictionary map section offset(START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    byte[] dictionaryMapBytes = null;
+    if (_dictionaryMap != null) {
+      dictionaryMapBytes = serializeDictionaryMap();
+      dataOutputStream.writeInt(dictionaryMapBytes.length);
+      dataOffset += dictionaryMapBytes.length;
+    } else {
+      dataOutputStream.writeInt(0);
+    }
+
+    // Write data schema section offset(START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    byte[] dataSchemaBytes = null;
+    if (_dataSchema != null) {
+      dataSchemaBytes = _dataSchema.toBytes();
+      dataOutputStream.writeInt(dataSchemaBytes.length);
+      dataOffset += dataSchemaBytes.length;
+    } else {
+      dataOutputStream.writeInt(0);
+    }
+
+    // Write fixed size data section offset(START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    if (_fixedSizeDataBytes != null) {
+      dataOutputStream.writeInt(_fixedSizeDataBytes.length);
+      dataOffset += _fixedSizeDataBytes.length;
+    } else {
+      dataOutputStream.writeInt(0);
+    }
+
+    // Write variable size data section offset(START|SIZE).
+    dataOutputStream.writeInt(dataOffset);
+    if (_variableSizeDataBytes != null) {
+      dataOutputStream.writeInt(_variableSizeDataBytes.length);
+    } else {
+      dataOutputStream.writeInt(0);
+    }
+
+    // Write actual data.
+    // Write exceptions bytes.
+    dataOutputStream.write(exceptionsBytes);
+    // Write dictionary map bytes.
+    if (dictionaryMapBytes != null) {
+      dataOutputStream.write(dictionaryMapBytes);
+    }
+    // Write data schema bytes.
+    if (dataSchemaBytes != null) {
+      dataOutputStream.write(dataSchemaBytes);
+    }
+    // Write fixed size data bytes.
+    if (_fixedSizeDataBytes != null) {
+      dataOutputStream.write(_fixedSizeDataBytes);
+    }
+    // Write variable size data bytes.
+    if (_variableSizeDataBytes != null) {
+      dataOutputStream.write(_variableSizeDataBytes);
+    }
+  }
+
+  /**
+   * Serialize metadata section to bytes.
+   * Format of the bytes looks like:
+   * [numEntries, bytesOfKV2, bytesOfKV2, bytesOfKV3]
+   * For each KV pair:
+   * - if the value type is String, encode it as: [enumKeyOrdinal, valueLength, Utf8EncodedValue].
+   * - if the value type is int, encode it as: [enumKeyOrdinal, bigEndianRepresentationOfIntValue]
+   * - if the value type is long, encode it as: [enumKeyOrdinal, bigEndianRepresentationOfLongValue]
+   *
+   * Unlike V2, where numeric metadata values (int and long) in V3 are encoded in UTF-8 in the wire format,
+   * in V3 big endian representation is used.
+   */
+  private byte[] serializeMetadata()
+      throws IOException {
+    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+    DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
+
+    dataOutputStream.writeInt(_metadata.size());
+
+    for (Map.Entry<String, String> entry : _metadata.entrySet()) {
+      MetadataKey key = MetadataKey.getByName(entry.getKey());
+      // Ignore unknown keys.
+      if (key == null) {
+        continue;
+      }
+      String value = entry.getValue();
+      dataOutputStream.writeInt(key.ordinal());
+      if (key.getValueType() == MetadataValueType.INT) {
+        dataOutputStream.write(Ints.toByteArray(Integer.parseInt(value)));
+      } else if (key.getValueType() == MetadataValueType.LONG) {
+        dataOutputStream.write(Longs.toByteArray(Long.parseLong(value)));
+      } else {
+        byte[] valueBytes = value.getBytes(UTF_8);
+        dataOutputStream.writeInt(valueBytes.length);
+        dataOutputStream.write(valueBytes);
+      }
+    }
+
+    return byteArrayOutputStream.toByteArray();
+  }
+
+  /**
+   * Even though the wire format of V3 uses UTF-8 for string/bytes and big-endian for numeric values,
+   * the in-memory representation is STRING based for processing the metadata before serialization
+   * (by the server as it adds the statistics in metadata) and after deserialization (by the broker as it receives
+   * DataTable from each server and aggregates the values).
+   * This is to make V3 implementation keep the consumers of Map<String, String> getMetadata() API in the code happy
+   * by internally converting it.
+   *
+   * This method use relative operations on the ByteBuffer and expects the buffer's position to be set correctly.
+   */
+  private Map<String, String> deserializeMetadata(ByteBuffer buffer)
+      throws IOException {
+    int numEntries = buffer.getInt();
+    Map<String, String> metadata = new HashMap<>();
+    for (int i = 0; i < numEntries; i++) {
+      int keyId = buffer.getInt();
+      MetadataKey key = MetadataKey.getByOrdinal(keyId);
+      // Ignore unknown keys.
+      if (key == null) {
+        continue;
+      }
+      if (key.getValueType() == MetadataValueType.INT) {
+        String value = "" + buffer.getInt();
+        metadata.put(key.getName(), value);
+      } else if (key.getValueType() == MetadataValueType.LONG) {
+        String value = "" + buffer.getLong();
+        metadata.put(key.getName(), value);
+      } else {
+        String value = DataTableUtils.decodeString(buffer);
+        metadata.put(key.getName(), value);
+      }
+    }
+    return metadata;
+  }
+
+  private byte[] serializeExceptions()
+      throws IOException {
+    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+    DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
+
+    dataOutputStream.writeInt(_errCodeToExceptionMap.size());
+
+    for (Map.Entry<Integer, String> entry : _errCodeToExceptionMap.entrySet()) {
+      int key = entry.getKey();
+      String value = entry.getValue();
+      byte[] valueBytes = value.getBytes(UTF_8);
+      dataOutputStream.writeInt(key);
+      dataOutputStream.writeInt(valueBytes.length);
+      dataOutputStream.write(valueBytes);
+    }
+
+    return byteArrayOutputStream.toByteArray();
+  }
+
+  private Map<Integer, String> deserializeExceptions(ByteBuffer buffer)
+      throws IOException {
+    int numExceptions = buffer.getInt();
+    Map<Integer, String> exceptions = new HashMap<>(numExceptions);
+    for (int i = 0; i < numExceptions; i++) {
+      int errCode = buffer.getInt();
+      String errMessage = DataTableUtils.decodeString(buffer);
+      exceptions.put(errCode, errMessage);
+    }
+    return exceptions;
+  }
+
+  @Override
+  public String toString() {
+    if (_dataSchema == null) {
+      return _metadata.toString();
+    }
+
+    StringBuilder stringBuilder = new StringBuilder();
+    stringBuilder.append(_dataSchema.toString()).append('\n');
+    stringBuilder.append("numRows: ").append(_numRows).append('\n');
+
+    DataSchema.ColumnDataType[] storedColumnDataTypes = _dataSchema.getStoredColumnDataTypes();
+    _fixedSizeData.position(0);
+    for (int rowId = 0; rowId < _numRows; rowId++) {
+      for (int colId = 0; colId < _numColumns; colId++) {
+        switch (storedColumnDataTypes[colId]) {
+          case INT:
+            stringBuilder.append(_fixedSizeData.getInt());
+            break;
+          case LONG:
+            stringBuilder.append(_fixedSizeData.getLong());
+            break;
+          case FLOAT:
+            stringBuilder.append(_fixedSizeData.getFloat());
+            break;
+          case DOUBLE:
+            stringBuilder.append(_fixedSizeData.getDouble());
+            break;
+          case STRING:
+            stringBuilder.append(_fixedSizeData.getInt());
+            break;
+          // Object and array.
+          default:
+            stringBuilder.append(String.format("(%s:%s)", _fixedSizeData.getInt(), _fixedSizeData.getInt()));
+            break;
+        }
+        stringBuilder.append("\t");
+      }
+      stringBuilder.append("\n");
+    }
+    return stringBuilder.toString();
+  }
+
+  public enum Type {
+    ROW(0),
+    COLUMNAR(1),
+    METADATA(2);
+
+    private final int _ordinal;
+
+    Type(int ordinal) {
+      _ordinal = ordinal;
+    }
+
+    public static Type fromOrdinal(int ordinal) {
+      switch (ordinal) {
+        case 0:
+          return ROW;
+        case 1:
+          return COLUMNAR;
+        case 2:
+          return METADATA;
+        default:
+          throw new IllegalArgumentException("Invalid ordinal: " + ordinal);
+      }
+    }
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/ColumnarDataBlock.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/ColumnarDataBlock.java
new file mode 100644
index 0000000000..8510043251
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/ColumnarDataBlock.java
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Map;
+import org.apache.pinot.common.utils.DataSchema;
+
+
+/**
+ * Column-wise data table. It stores data in columnar-major format.
+ */
+public class ColumnarDataBlock extends BaseDataBlock {
+  private static final int VERSION = 1;
+  protected int[] _cumulativeColumnOffsetSizeInBytes;
+  protected int[] _columnSizeInBytes;
+
+  public ColumnarDataBlock() {
+    super();
+  }
+
+  public ColumnarDataBlock(int numRows, DataSchema dataSchema, Map<String, Map<Integer, String>> dictionaryMap,
+      byte[] fixedSizeDataBytes, byte[] variableSizeDataBytes) {
+    super(numRows, dataSchema, dictionaryMap, fixedSizeDataBytes, variableSizeDataBytes);
+    computeBlockObjectConstants();
+  }
+
+  public ColumnarDataBlock(ByteBuffer byteBuffer)
+      throws IOException {
+    super(byteBuffer);
+    computeBlockObjectConstants();
+  }
+
+  protected void computeBlockObjectConstants() {
+    if (_dataSchema != null) {
+      _cumulativeColumnOffsetSizeInBytes = new int[_numColumns];
+      _columnSizeInBytes = new int[_numColumns];
+      DataBlockUtils.computeColumnSizeInBytes(_dataSchema, _columnSizeInBytes);
+      int cumulativeColumnOffset = 0;
+      for (int i = 0; i < _numColumns; i++) {
+        _cumulativeColumnOffsetSizeInBytes[i] = cumulativeColumnOffset;
+        cumulativeColumnOffset += _columnSizeInBytes[i] * _numRows;
+      }
+    }
+  }
+
+  @Override
+  protected int getDataBlockVersionType() {
+    return VERSION + (Type.COLUMNAR.ordinal() << DataBlockUtils.VERSION_TYPE_SHIFT);
+  }
+
+  @Override
+  protected void positionCursorInFixSizedBuffer(int rowId, int colId) {
+    int position = _cumulativeColumnOffsetSizeInBytes[colId] + _columnSizeInBytes[colId] * rowId;
+    _fixedSizeData.position(position);
+  }
+
+  @Override
+  protected int positionCursorInVariableBuffer(int rowId, int colId) {
+    positionCursorInFixSizedBuffer(rowId, colId);
+    _variableSizeData.position(_fixedSizeData.getInt());
+    return _fixedSizeData.getInt();
+  }
+
+  @Override
+  public ColumnarDataBlock toMetadataOnlyDataTable() {
+    ColumnarDataBlock metadataOnlyDataTable = new ColumnarDataBlock();
+    metadataOnlyDataTable._metadata.putAll(_metadata);
+    metadataOnlyDataTable._errCodeToExceptionMap.putAll(_errCodeToExceptionMap);
+    return metadataOnlyDataTable;
+  }
+
+  @Override
+  public ColumnarDataBlock toDataOnlyDataTable() {
+    return new ColumnarDataBlock(_numRows, _dataSchema, _dictionaryMap, _fixedSizeDataBytes, _variableSizeDataBytes);
+  }
+
+  // TODO: add whole-column access methods.
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataBlockBuilder.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataBlockBuilder.java
new file mode 100644
index 0000000000..0c456c1d85
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataBlockBuilder.java
@@ -0,0 +1,406 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.ObjectSerDeUtils;
+import org.apache.pinot.core.common.datatable.DataTableUtils;
+import org.apache.pinot.spi.utils.ArrayCopyUtils;
+import org.apache.pinot.spi.utils.BigDecimalUtils;
+import org.apache.pinot.spi.utils.ByteArray;
+
+
+public class DataBlockBuilder {
+  private final DataSchema _dataSchema;
+  private final BaseDataBlock.Type _blockType;
+  private final DataSchema.ColumnDataType[] _columnDataType;
+
+  private int[] _columnOffsets;
+  private int _rowSizeInBytes;
+  private int[] _cumulativeColumnOffsetSizeInBytes;
+  private int[] _columnSizeInBytes;
+
+  private int _numRows;
+  private int _numColumns;
+
+  private final Map<String, Map<String, Integer>> _dictionaryMap = new HashMap<>();
+  private final Map<String, Map<Integer, String>> _reverseDictionaryMap = new HashMap<>();
+  private final ByteArrayOutputStream _fixedSizeDataByteArrayOutputStream = new ByteArrayOutputStream();
+  private final ByteArrayOutputStream _variableSizeDataByteArrayOutputStream = new ByteArrayOutputStream();
+  private final DataOutputStream _variableSizeDataOutputStream =
+      new DataOutputStream(_variableSizeDataByteArrayOutputStream);
+
+
+  private ByteBuffer _currentRowDataByteBuffer;
+
+  private DataBlockBuilder(DataSchema dataSchema, BaseDataBlock.Type blockType) {
+    _dataSchema = dataSchema;
+    _columnDataType = dataSchema.getStoredColumnDataTypes();
+    _blockType = blockType;
+    _numColumns = dataSchema.size();
+    if (_blockType == BaseDataBlock.Type.COLUMNAR) {
+      _cumulativeColumnOffsetSizeInBytes = new int[_numColumns];
+      _columnSizeInBytes = new int[_numColumns];
+      DataBlockUtils.computeColumnSizeInBytes(_dataSchema, _columnSizeInBytes);
+      int cumulativeColumnOffset = 0;
+      for (int i = 0; i < _numColumns; i++) {
+        _cumulativeColumnOffsetSizeInBytes[i] = cumulativeColumnOffset;
+        cumulativeColumnOffset += _columnSizeInBytes[i] * _numRows;
+      }
+    } else if (_blockType == BaseDataBlock.Type.ROW) {
+      _columnOffsets = new int[_numColumns];
+      _rowSizeInBytes = DataTableUtils.computeColumnOffsets(dataSchema, _columnOffsets);
+    }
+  }
+
+  public static RowDataBlock buildFromRows(List<Object[]> rows, DataSchema dataSchema)
+      throws IOException {
+    DataBlockBuilder rowBuilder = new DataBlockBuilder(dataSchema, BaseDataBlock.Type.ROW);
+    rowBuilder._numRows = rows.size();
+    for (Object[] row : rows) {
+      ByteBuffer byteBuffer = ByteBuffer.allocate(rowBuilder._rowSizeInBytes);
+      for (int i = 0; i < rowBuilder._numColumns; i++) {
+        Object value = row[i];
+        switch (rowBuilder._columnDataType[i]) {
+          // Single-value column
+          case INT:
+            byteBuffer.putInt(((Number) value).intValue());
+            break;
+          case LONG:
+            byteBuffer.putLong(((Number) value).longValue());
+            break;
+          case FLOAT:
+            byteBuffer.putFloat(((Number) value).floatValue());
+            break;
+          case DOUBLE:
+            byteBuffer.putDouble(((Number) value).doubleValue());
+            break;
+          case BIG_DECIMAL:
+            setColumn(rowBuilder, byteBuffer, (BigDecimal) value);
+            break;
+          case STRING:
+            setColumn(rowBuilder, byteBuffer, i, (String) value);
+            break;
+          case BYTES:
+            setColumn(rowBuilder, byteBuffer, (ByteArray) value);
+            break;
+          case OBJECT:
+            setColumn(rowBuilder, byteBuffer, value);
+            break;
+          // Multi-value column
+          case BOOLEAN_ARRAY:
+          case INT_ARRAY:
+            setColumn(rowBuilder, byteBuffer, (int[]) value);
+            break;
+          case TIMESTAMP_ARRAY:
+          case LONG_ARRAY:
+            // LONG_ARRAY type covers INT_ARRAY and LONG_ARRAY
+            if (value instanceof int[]) {
+              int[] ints = (int[]) value;
+              int length = ints.length;
+              long[] longs = new long[length];
+              ArrayCopyUtils.copy(ints, longs, length);
+              setColumn(rowBuilder, byteBuffer, longs);
+            } else {
+              setColumn(rowBuilder, byteBuffer, (long[]) value);
+            }
+            break;
+          case FLOAT_ARRAY:
+            setColumn(rowBuilder, byteBuffer, (float[]) value);
+            break;
+          case DOUBLE_ARRAY:
+            // DOUBLE_ARRAY type covers INT_ARRAY, LONG_ARRAY, FLOAT_ARRAY and DOUBLE_ARRAY
+            if (value instanceof int[]) {
+              int[] ints = (int[]) value;
+              int length = ints.length;
+              double[] doubles = new double[length];
+              ArrayCopyUtils.copy(ints, doubles, length);
+              setColumn(rowBuilder, byteBuffer, doubles);
+            } else if (value instanceof long[]) {
+              long[] longs = (long[]) value;
+              int length = longs.length;
+              double[] doubles = new double[length];
+              ArrayCopyUtils.copy(longs, doubles, length);
+              setColumn(rowBuilder, byteBuffer, doubles);
+            } else if (value instanceof float[]) {
+              float[] floats = (float[]) value;
+              int length = floats.length;
+              double[] doubles = new double[length];
+              ArrayCopyUtils.copy(floats, doubles, length);
+              setColumn(rowBuilder, byteBuffer, doubles);
+            } else {
+              setColumn(rowBuilder, byteBuffer, (double[]) value);
+            }
+            break;
+          case BYTES_ARRAY:
+          case STRING_ARRAY:
+            setColumn(rowBuilder, byteBuffer, i, (String[]) value);
+            break;
+          default:
+            throw new IllegalStateException(String.format(
+                "Unsupported data type: %s for column: %s", rowBuilder._columnDataType[i],
+                rowBuilder._dataSchema.getColumnName(i)));
+        }
+      }
+      rowBuilder._fixedSizeDataByteArrayOutputStream.write(byteBuffer.array(), 0, byteBuffer.position());
+    }
+    return buildRowBlock(rowBuilder);
+  }
+
+  public static ColumnarDataBlock buildFromColumns(List<Object[]> columns, DataSchema dataSchema)
+      throws IOException {
+    DataBlockBuilder columnarBuilder = new DataBlockBuilder(dataSchema, BaseDataBlock.Type.COLUMNAR);
+    for (int i = 0; i < columns.size(); i++) {
+      Object[] column = columns.get(i);
+      columnarBuilder._numRows = column.length;
+      ByteBuffer byteBuffer = ByteBuffer.allocate(columnarBuilder._numRows * columnarBuilder._columnSizeInBytes[i]);
+      switch (columnarBuilder._columnDataType[i]) {
+        // Single-value column
+        case INT:
+          for (Object value : column) {
+            byteBuffer.putInt(((Number) value).intValue());
+          }
+          break;
+        case LONG:
+          for (Object value : column) {
+            byteBuffer.putLong(((Number) value).longValue());
+          }
+          break;
+        case FLOAT:
+          for (Object value : column) {
+            byteBuffer.putFloat(((Number) value).floatValue());
+          }
+          break;
+        case DOUBLE:
+          for (Object value : column) {
+            byteBuffer.putDouble(((Number) value).doubleValue());
+          }
+          break;
+        case BIG_DECIMAL:
+          for (Object value : column) {
+            setColumn(columnarBuilder, byteBuffer, (BigDecimal) value);
+          }
+          break;
+        case STRING:
+          for (Object value : column) {
+            setColumn(columnarBuilder, byteBuffer, i, (String) value);
+          }
+          break;
+        case BYTES:
+          for (Object value : column) {
+            setColumn(columnarBuilder, byteBuffer, (ByteArray) value);
+          }
+          break;
+        case OBJECT:
+          for (Object value : column) {
+            setColumn(columnarBuilder, byteBuffer, value);
+          }
+          break;
+        // Multi-value column
+        case BOOLEAN_ARRAY:
+        case INT_ARRAY:
+          for (Object value : column) {
+            setColumn(columnarBuilder, byteBuffer, (int[]) value);
+          }
+          break;
+        case TIMESTAMP_ARRAY:
+        case LONG_ARRAY:
+          for (Object value : column) {
+            if (value instanceof int[]) {
+              // LONG_ARRAY type covers INT_ARRAY and LONG_ARRAY
+              int[] ints = (int[]) value;
+              int length = ints.length;
+              long[] longs = new long[length];
+              ArrayCopyUtils.copy(ints, longs, length);
+              setColumn(columnarBuilder, byteBuffer, longs);
+            } else {
+              setColumn(columnarBuilder, byteBuffer, (long[]) value);
+            }
+          }
+          break;
+        case FLOAT_ARRAY:
+          for (Object value : column) {
+            setColumn(columnarBuilder, byteBuffer, (float[]) value);
+          }
+          break;
+        case DOUBLE_ARRAY:
+          for (Object value : column) {
+            // DOUBLE_ARRAY type covers INT_ARRAY, LONG_ARRAY, FLOAT_ARRAY and DOUBLE_ARRAY
+            if (value instanceof int[]) {
+              int[] ints = (int[]) value;
+              int length = ints.length;
+              double[] doubles = new double[length];
+              ArrayCopyUtils.copy(ints, doubles, length);
+              setColumn(columnarBuilder, byteBuffer, doubles);
+            } else if (value instanceof long[]) {
+              long[] longs = (long[]) value;
+              int length = longs.length;
+              double[] doubles = new double[length];
+              ArrayCopyUtils.copy(longs, doubles, length);
+              setColumn(columnarBuilder, byteBuffer, doubles);
+            } else if (value instanceof float[]) {
+              float[] floats = (float[]) value;
+              int length = floats.length;
+              double[] doubles = new double[length];
+              ArrayCopyUtils.copy(floats, doubles, length);
+              setColumn(columnarBuilder, byteBuffer, doubles);
+            } else {
+              setColumn(columnarBuilder, byteBuffer, (double[]) value);
+            }
+          }
+          break;
+        case BYTES_ARRAY:
+        case STRING_ARRAY:
+          for (Object value : column) {
+            setColumn(columnarBuilder, byteBuffer, i, (String[]) value);
+          }
+          break;
+        default:
+          throw new IllegalStateException(String.format(
+              "Unsupported data type: %s for column: %s", columnarBuilder._columnDataType[i],
+              columnarBuilder._dataSchema.getColumnName(i)));
+      }
+      columnarBuilder._fixedSizeDataByteArrayOutputStream.write(byteBuffer.array(), 0, byteBuffer.position());
+    }
+    return buildColumnarBlock(columnarBuilder);
+  }
+
+  private static RowDataBlock buildRowBlock(DataBlockBuilder builder) {
+    return new RowDataBlock(builder._numRows, builder._dataSchema, builder._reverseDictionaryMap,
+        builder._fixedSizeDataByteArrayOutputStream.toByteArray(),
+        builder._variableSizeDataByteArrayOutputStream.toByteArray());
+  }
+
+  private static ColumnarDataBlock buildColumnarBlock(DataBlockBuilder builder) {
+    return new ColumnarDataBlock(builder._numRows, builder._dataSchema, builder._reverseDictionaryMap,
+        builder._fixedSizeDataByteArrayOutputStream.toByteArray(),
+        builder._variableSizeDataByteArrayOutputStream.toByteArray());
+  }
+
+  private static void setColumn(DataBlockBuilder builder, ByteBuffer byteBuffer, BigDecimal value)
+      throws IOException {
+    byteBuffer.putInt(builder._variableSizeDataByteArrayOutputStream.size());
+    byte[] bytes = BigDecimalUtils.serialize(value);
+    byteBuffer.putInt(bytes.length);
+    builder._variableSizeDataByteArrayOutputStream.write(bytes);
+  }
+
+  private static void setColumn(DataBlockBuilder builder, ByteBuffer byteBuffer, int colId, String value) {
+    String columnName = builder._dataSchema.getColumnName(colId);
+    Map<String, Integer> dictionary = builder._dictionaryMap.get(columnName);
+    if (dictionary == null) {
+      dictionary = new HashMap<>();
+      builder._dictionaryMap.put(columnName, dictionary);
+      builder._reverseDictionaryMap.put(columnName, new HashMap<>());
+    }
+    Integer dictId = dictionary.get(value);
+    if (dictId == null) {
+      dictId = dictionary.size();
+      dictionary.put(value, dictId);
+      builder._reverseDictionaryMap.get(columnName).put(dictId, value);
+    }
+    byteBuffer.putInt(dictId);
+  }
+
+  private static void setColumn(DataBlockBuilder builder, ByteBuffer byteBuffer, ByteArray value)
+      throws IOException {
+    byteBuffer.putInt(builder._variableSizeDataByteArrayOutputStream.size());
+    byte[] bytes = value.getBytes();
+    byteBuffer.putInt(bytes.length);
+    builder._variableSizeDataByteArrayOutputStream.write(bytes);
+  }
+
+  private static void setColumn(DataBlockBuilder builder, ByteBuffer byteBuffer, Object value)
+      throws IOException {
+    byteBuffer.putInt(builder._variableSizeDataByteArrayOutputStream.size());
+    int objectTypeValue = ObjectSerDeUtils.ObjectType.getObjectType(value).getValue();
+    byte[] bytes = ObjectSerDeUtils.serialize(value, objectTypeValue);
+    byteBuffer.putInt(bytes.length);
+    builder._variableSizeDataOutputStream.writeInt(objectTypeValue);
+    builder._variableSizeDataByteArrayOutputStream.write(bytes);
+  }
+
+  private static void setColumn(DataBlockBuilder builder, ByteBuffer byteBuffer, int[] values)
+      throws IOException {
+    byteBuffer.putInt(builder._variableSizeDataByteArrayOutputStream.size());
+    byteBuffer.putInt(values.length);
+    for (int value : values) {
+      builder._variableSizeDataOutputStream.writeInt(value);
+    }
+  }
+
+  private static void setColumn(DataBlockBuilder builder, ByteBuffer byteBuffer, long[] values)
+      throws IOException {
+    byteBuffer.putInt(builder._variableSizeDataByteArrayOutputStream.size());
+    byteBuffer.putInt(values.length);
+    for (long value : values) {
+      builder._variableSizeDataOutputStream.writeLong(value);
+    }
+  }
+
+  private static void setColumn(DataBlockBuilder builder, ByteBuffer byteBuffer, float[] values)
+      throws IOException {
+    byteBuffer.putInt(builder._variableSizeDataByteArrayOutputStream.size());
+    byteBuffer.putInt(values.length);
+    for (float value : values) {
+      builder._variableSizeDataOutputStream.writeFloat(value);
+    }
+  }
+
+  private static void setColumn(DataBlockBuilder builder, ByteBuffer byteBuffer, double[] values)
+      throws IOException {
+    byteBuffer.putInt(builder._variableSizeDataByteArrayOutputStream.size());
+    byteBuffer.putInt(values.length);
+    for (double value : values) {
+      builder._variableSizeDataOutputStream.writeDouble(value);
+    }
+  }
+
+  private static void setColumn(DataBlockBuilder builder, ByteBuffer byteBuffer, int colId, String[] values)
+      throws IOException {
+    byteBuffer.putInt(builder._variableSizeDataByteArrayOutputStream.size());
+    byteBuffer.putInt(values.length);
+
+    String columnName = builder._dataSchema.getColumnName(colId);
+    Map<String, Integer> dictionary = builder._dictionaryMap.get(columnName);
+    if (dictionary == null) {
+      dictionary = new HashMap<>();
+      builder._dictionaryMap.put(columnName, dictionary);
+      builder._reverseDictionaryMap.put(columnName, new HashMap<>());
+    }
+
+    for (String value : values) {
+      Integer dictId = dictionary.get(value);
+      if (dictId == null) {
+        dictId = dictionary.size();
+        dictionary.put(value, dictId);
+        builder._reverseDictionaryMap.get(columnName).put(dictId, value);
+      }
+      builder._variableSizeDataOutputStream.writeInt(dictId);
+    }
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataBlockUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataBlockUtils.java
new file mode 100644
index 0000000000..690be1353a
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataBlockUtils.java
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.pinot.common.exception.QueryException;
+import org.apache.pinot.common.response.ProcessingException;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.common.utils.DataTable;
+
+
+public final class DataBlockUtils {
+  protected static final int VERSION_TYPE_SHIFT = 5;
+  private DataBlockUtils() {
+    // do not instantiate.
+  }
+
+  private static final DataSchema EMPTY_SCHEMA = new DataSchema(new String[0], new DataSchema.ColumnDataType[0]);
+  private static final MetadataBlock EOS_DATA_BLOCK = new MetadataBlock(EMPTY_SCHEMA);
+  static {
+    EOS_DATA_BLOCK._metadata.put(DataTable.MetadataKey.TABLE.getName(), "END_OF_STREAM");
+  }
+  private static final TransferableBlock EOS_TRANSFERABLE_BLOCK = new TransferableBlock(EOS_DATA_BLOCK);
+
+  public static TransferableBlock getEndOfStreamTransferableBlock() {
+    return EOS_TRANSFERABLE_BLOCK;
+  }
+
+  public static MetadataBlock getEndOfStreamDataBlock() {
+    return EOS_DATA_BLOCK;
+  }
+
+  public static MetadataBlock getErrorDataBlock(Exception e) {
+    MetadataBlock errorBlock = new MetadataBlock(EMPTY_SCHEMA);
+    errorBlock._metadata.put(DataTable.MetadataKey.TABLE.getName(), "ERROR");
+    if (e instanceof ProcessingException) {
+      errorBlock.addException(((ProcessingException) e).getErrorCode(), e.getMessage());
+    } else {
+      errorBlock.addException(QueryException.UNKNOWN_ERROR_CODE, e.getMessage());
+    }
+    return errorBlock;
+  }
+
+  public static TransferableBlock getErrorTransferableBlock(Exception e) {
+    return new TransferableBlock(getErrorDataBlock(e));
+  }
+
+  public static MetadataBlock getEmptyDataBlock(DataSchema dataSchema) {
+    return dataSchema == null ? EOS_DATA_BLOCK : new MetadataBlock(dataSchema);
+  }
+
+  public static TransferableBlock getEmptyTransferableBlock(DataSchema dataSchema) {
+    return new TransferableBlock(getEmptyDataBlock(dataSchema));
+  }
+
+  public static boolean isEndOfStream(TransferableBlock transferableBlock) {
+    return transferableBlock.getType().equals(BaseDataBlock.Type.METADATA)
+        && "END_OF_STREAM".equals(transferableBlock.getDataBlock().getMetadata()
+            .get(DataTable.MetadataKey.TABLE.getName()));
+  }
+
+  public static BaseDataBlock getDataBlock(ByteBuffer byteBuffer)
+      throws IOException {
+    int versionType = byteBuffer.getInt();
+    int version = versionType & ((1 << VERSION_TYPE_SHIFT) - 1);
+    BaseDataBlock.Type type = BaseDataBlock.Type.fromOrdinal(versionType >> VERSION_TYPE_SHIFT);
+    switch (type) {
+      case COLUMNAR:
+        return new ColumnarDataBlock(byteBuffer);
+      case ROW:
+        return new RowDataBlock(byteBuffer);
+      case METADATA:
+        return new MetadataBlock(byteBuffer);
+      default:
+        throw new UnsupportedOperationException("Unsupported data table version: " + version + " with type: " + type);
+    }
+  }
+
+  /**
+   * Given a {@link DataSchema}, compute each column's offset and fill them into the passed in array, then return the
+   * row size in bytes.
+   *
+   * @param dataSchema data schema.
+   * @param columnOffsets array of column offsets.
+   * @return row size in bytes.
+   */
+  public static int computeColumnOffsets(DataSchema dataSchema, int[] columnOffsets) {
+    int numColumns = columnOffsets.length;
+    assert numColumns == dataSchema.size();
+
+    DataSchema.ColumnDataType[] storedColumnDataTypes = dataSchema.getStoredColumnDataTypes();
+    int rowSizeInBytes = 0;
+    for (int i = 0; i < numColumns; i++) {
+      columnOffsets[i] = rowSizeInBytes;
+      switch (storedColumnDataTypes[i]) {
+        case INT:
+          rowSizeInBytes += 4;
+          break;
+        case LONG:
+          rowSizeInBytes += 8;
+          break;
+        case FLOAT:
+          rowSizeInBytes += 4;
+          break;
+        case DOUBLE:
+          rowSizeInBytes += 8;
+          break;
+        case STRING:
+          rowSizeInBytes += 4;
+          break;
+        // Object and array. (POSITION|LENGTH)
+        default:
+          rowSizeInBytes += 8;
+          break;
+      }
+    }
+
+    return rowSizeInBytes;
+  }
+
+  /**
+   * Given a {@link DataSchema}, compute each column's size and fill them into the passed in array.
+   *
+   * @param dataSchema data schema.
+   * @param columnSizes array of column size.
+   * @return row size in bytes.
+   */
+  public static void computeColumnSizeInBytes(DataSchema dataSchema, int[] columnSizes) {
+    int numColumns = columnSizes.length;
+    assert numColumns == dataSchema.size();
+
+    DataSchema.ColumnDataType[] storedColumnDataTypes = dataSchema.getStoredColumnDataTypes();
+    for (int i = 0; i < numColumns; i++) {
+      switch (storedColumnDataTypes[i]) {
+        case INT:
+          columnSizes[i] = 4;
+          break;
+        case LONG:
+          columnSizes[i] = 8;
+          break;
+        case FLOAT:
+          columnSizes[i] = 4;
+          break;
+        case DOUBLE:
+          columnSizes[i] = 8;
+          break;
+        case STRING:
+          columnSizes[i] = 4;
+          break;
+        // Object and array. (POSITION|LENGTH)
+        default:
+          columnSizes[i] = 8;
+          break;
+      }
+    }
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlockUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlockUtils.java
deleted file mode 100644
index c429e2e014..0000000000
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlockUtils.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.pinot.query.runtime.blocks;
-
-import org.apache.pinot.common.exception.QueryException;
-import org.apache.pinot.common.utils.DataSchema;
-import org.apache.pinot.common.utils.DataTable;
-import org.apache.pinot.core.common.datatable.DataTableBuilder;
-
-
-public final class DataTableBlockUtils {
-  private DataTableBlockUtils() {
-    // do not instantiate.
-  }
-
-  // used to indicate a datatable block status
-  private static final DataSchema EMPTY_SCHEMA = new DataSchema(new String[0], new DataSchema.ColumnDataType[0]);
-  private static final DataTable EMPTY_DATATABLE = new DataTableBuilder(EMPTY_SCHEMA).build();
-  private static final DataTableBlock END_OF_STREAM_DATATABLE_BLOCK = new DataTableBlock(EMPTY_DATATABLE);
-
-  public static DataTableBlock getEndOfStreamDataTableBlock() {
-    return END_OF_STREAM_DATATABLE_BLOCK;
-  }
-
-  public static DataTable getEndOfStreamDataTable() {
-    return EMPTY_DATATABLE;
-  }
-
-  public static DataTable getErrorDataTable(Exception e) {
-    DataTable errorDataTable = new DataTableBuilder(EMPTY_SCHEMA).build();
-    errorDataTable.addException(QueryException.UNKNOWN_ERROR_CODE, e.getMessage());
-    return errorDataTable;
-  }
-
-  public static DataTableBlock getErrorDatatableBlock(Exception e) {
-    return new DataTableBlock(getErrorDataTable(e));
-  }
-
-  public static DataTable getEmptyDataTable(DataSchema dataSchema) {
-    if (dataSchema != null) {
-      return new DataTableBuilder(dataSchema).build();
-    } else {
-      return EMPTY_DATATABLE;
-    }
-  }
-
-  public static DataTableBlock getEmptyDataTableBlock(DataSchema dataSchema) {
-    return new DataTableBlock(getEmptyDataTable(dataSchema));
-  }
-
-  public static boolean isEndOfStream(DataTableBlock dataTableBlock) {
-    DataSchema dataSchema = dataTableBlock.getDataTable().getDataSchema();
-    return dataSchema.getColumnNames().length == 0 && dataSchema.getColumnDataTypes().length == 0;
-  }
-}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/MetadataBlock.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/MetadataBlock.java
new file mode 100644
index 0000000000..a3ffc7a126
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/MetadataBlock.java
@@ -0,0 +1,66 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import org.apache.pinot.common.utils.DataSchema;
+
+
+/**
+ * Wrapper for row-wise data table. It stores data in row-major format.
+ */
+public class MetadataBlock extends BaseDataBlock {
+  private static final int VERSION = 1;
+
+  public MetadataBlock(DataSchema dataSchema) {
+    super(0, dataSchema, Collections.emptyMap(), new byte[]{0}, new byte[]{0});
+  }
+
+  public MetadataBlock(ByteBuffer byteBuffer)
+      throws IOException {
+    super(byteBuffer);
+  }
+
+  @Override
+  protected int getDataBlockVersionType() {
+    return VERSION + (Type.METADATA.ordinal() << DataBlockUtils.VERSION_TYPE_SHIFT);
+  }
+
+  @Override
+  protected void positionCursorInFixSizedBuffer(int rowId, int colId) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  protected int positionCursorInVariableBuffer(int rowId, int colId) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public MetadataBlock toMetadataOnlyDataTable() {
+    return this;
+  }
+
+  @Override
+  public MetadataBlock toDataOnlyDataTable() {
+    return new MetadataBlock(_dataSchema);
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/RowDataBlock.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/RowDataBlock.java
new file mode 100644
index 0000000000..f8520c54f6
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/RowDataBlock.java
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Map;
+import org.apache.pinot.common.utils.DataSchema;
+
+
+/**
+ * Wrapper for row-wise data table. It stores data in row-major format.
+ */
+public class RowDataBlock extends BaseDataBlock {
+  private static final int VERSION = 1;
+  protected int[] _columnOffsets;
+  protected int _rowSizeInBytes;
+
+  public RowDataBlock() {
+    super();
+  }
+
+  public RowDataBlock(int numRows, DataSchema dataSchema, Map<String, Map<Integer, String>> dictionaryMap,
+      byte[] fixedSizeDataBytes, byte[] variableSizeDataBytes) {
+    super(numRows, dataSchema, dictionaryMap, fixedSizeDataBytes, variableSizeDataBytes);
+    computeBlockObjectConstants();
+  }
+
+  public RowDataBlock(ByteBuffer byteBuffer)
+      throws IOException {
+    super(byteBuffer);
+    computeBlockObjectConstants();
+  }
+
+  protected void computeBlockObjectConstants() {
+    if (_dataSchema != null) {
+      _columnOffsets = new int[_numColumns];
+      _rowSizeInBytes = DataBlockUtils.computeColumnOffsets(_dataSchema, _columnOffsets);
+    }
+  }
+
+  @Override
+  protected int getDataBlockVersionType() {
+    return VERSION + (Type.ROW.ordinal() << DataBlockUtils.VERSION_TYPE_SHIFT);
+  }
+
+  @Override
+  protected void positionCursorInFixSizedBuffer(int rowId, int colId) {
+    int position = rowId * _rowSizeInBytes + _columnOffsets[colId];
+    _fixedSizeData.position(position);
+  }
+
+  @Override
+  protected int positionCursorInVariableBuffer(int rowId, int colId) {
+    positionCursorInFixSizedBuffer(rowId, colId);
+    _variableSizeData.position(_fixedSizeData.getInt());
+    return _fixedSizeData.getInt();
+  }
+
+  @Override
+  public RowDataBlock toMetadataOnlyDataTable() {
+    RowDataBlock metadataOnlyDataTable = new RowDataBlock();
+    metadataOnlyDataTable._metadata.putAll(_metadata);
+    metadataOnlyDataTable._errCodeToExceptionMap.putAll(_errCodeToExceptionMap);
+    return metadataOnlyDataTable;
+  }
+
+  @Override
+  public RowDataBlock toDataOnlyDataTable() {
+    return new RowDataBlock(_numRows, _dataSchema, _dictionaryMap, _fixedSizeDataBytes, _variableSizeDataBytes);
+  }
+
+  // TODO: add whole-row access methods.
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlock.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/TransferableBlock.java
similarity index 70%
rename from pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlock.java
rename to pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/TransferableBlock.java
index a8cc63c624..9284011f06 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlock.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/TransferableBlock.java
@@ -19,31 +19,39 @@
 package org.apache.pinot.query.runtime.blocks;
 
 import java.io.IOException;
-import org.apache.pinot.common.utils.DataTable;
 import org.apache.pinot.core.common.Block;
 import org.apache.pinot.core.common.BlockDocIdSet;
 import org.apache.pinot.core.common.BlockDocIdValueSet;
 import org.apache.pinot.core.common.BlockMetadata;
 import org.apache.pinot.core.common.BlockValSet;
-import org.apache.pinot.core.operator.blocks.InstanceResponseBlock;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 
 /**
- * A {@code DataTableBlock} is a row-based data block backed by a {@link DataTable}.
+ * A {@code TransferableBlock} is a wrapper around {@link BaseDataBlock} for transferring data using
+ * {@link org.apache.pinot.common.proto.Mailbox}.
  */
-public class DataTableBlock implements Block {
-  private static final Logger LOGGER = LoggerFactory.getLogger(InstanceResponseBlock.class);
+public class TransferableBlock implements Block {
 
-  private DataTable _dataTable;
+  private BaseDataBlock _dataBlock;
+  private BaseDataBlock.Type _type;
 
-  public DataTableBlock(DataTable dataTable) {
-    _dataTable = dataTable;
+  public TransferableBlock(BaseDataBlock dataBlock) {
+    _dataBlock = dataBlock;
+    _type = dataBlock instanceof ColumnarDataBlock ? BaseDataBlock.Type.COLUMNAR
+        : dataBlock instanceof RowDataBlock ? BaseDataBlock.Type.ROW : BaseDataBlock.Type.METADATA;
   }
 
-  public DataTable getDataTable() {
-    return _dataTable;
+  public BaseDataBlock getDataBlock() {
+    return _dataBlock;
+  }
+
+  public BaseDataBlock.Type getType() {
+    return _type;
+  }
+
+  public byte[] toBytes()
+      throws IOException {
+    return _dataBlock.toBytes();
   }
 
   @Override
@@ -65,9 +73,4 @@ public class DataTableBlock implements Block {
   public BlockMetadata getMetadata() {
     throw new UnsupportedOperationException();
   }
-
-  public byte[] toBytes()
-      throws IOException {
-    return _dataTable.toBytes();
-  }
 }
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java
index 9a66bf86c1..d8ec43e040 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java
@@ -36,8 +36,8 @@ import org.apache.pinot.query.planner.stage.MailboxReceiveNode;
 import org.apache.pinot.query.planner.stage.MailboxSendNode;
 import org.apache.pinot.query.planner.stage.ProjectNode;
 import org.apache.pinot.query.planner.stage.StageNode;
-import org.apache.pinot.query.runtime.blocks.DataTableBlock;
-import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+import org.apache.pinot.query.runtime.blocks.DataBlockUtils;
+import org.apache.pinot.query.runtime.blocks.TransferableBlock;
 import org.apache.pinot.query.runtime.operator.HashJoinOperator;
 import org.apache.pinot.query.runtime.operator.MailboxReceiveOperator;
 import org.apache.pinot.query.runtime.operator.MailboxSendOperator;
@@ -83,12 +83,12 @@ public class WorkerQueryExecutor {
       ExecutorService executorService) {
     long requestId = Long.parseLong(requestMetadataMap.get("REQUEST_ID"));
     StageNode stageRoot = queryRequest.getStageRoot();
-    BaseOperator<DataTableBlock> rootOperator = getOperator(requestId, stageRoot, queryRequest.getMetadataMap());
+    BaseOperator<TransferableBlock> rootOperator = getOperator(requestId, stageRoot, queryRequest.getMetadataMap());
     executorService.submit(new TraceRunnable() {
       @Override
       public void runJob() {
         ThreadTimer executionThreadTimer = new ThreadTimer();
-        while (!DataTableBlockUtils.isEndOfStream(rootOperator.nextBlock())) {
+        while (!DataBlockUtils.isEndOfStream(rootOperator.nextBlock())) {
           LOGGER.debug("Result Block acquired");
         }
         LOGGER.info("Execution time:" + executionThreadTimer.getThreadTimeNs());
@@ -97,7 +97,7 @@ public class WorkerQueryExecutor {
   }
 
   // TODO: split this PhysicalPlanner into a separate module
-  private BaseOperator<DataTableBlock> getOperator(long requestId, StageNode stageNode,
+  private BaseOperator<TransferableBlock> getOperator(long requestId, StageNode stageNode,
       Map<Integer, StageMetadata> metadataMap) {
     // TODO: optimize this into a framework. (physical planner)
     if (stageNode instanceof MailboxReceiveNode) {
@@ -107,15 +107,15 @@ public class WorkerQueryExecutor {
           requestId, receiveNode.getSenderStageId());
     } else if (stageNode instanceof MailboxSendNode) {
       MailboxSendNode sendNode = (MailboxSendNode) stageNode;
-      BaseOperator<DataTableBlock> nextOperator = getOperator(requestId, sendNode.getInputs().get(0), metadataMap);
+      BaseOperator<TransferableBlock> nextOperator = getOperator(requestId, sendNode.getInputs().get(0), metadataMap);
       StageMetadata receivingStageMetadata = metadataMap.get(sendNode.getReceiverStageId());
       return new MailboxSendOperator(_mailboxService, nextOperator, receivingStageMetadata.getServerInstances(),
           sendNode.getExchangeType(), sendNode.getPartitionKeySelector(), _hostName, _port, requestId,
           sendNode.getStageId());
     } else if (stageNode instanceof JoinNode) {
       JoinNode joinNode = (JoinNode) stageNode;
-      BaseOperator<DataTableBlock> leftOperator = getOperator(requestId, joinNode.getInputs().get(0), metadataMap);
-      BaseOperator<DataTableBlock> rightOperator = getOperator(requestId, joinNode.getInputs().get(1), metadataMap);
+      BaseOperator<TransferableBlock> leftOperator = getOperator(requestId, joinNode.getInputs().get(0), metadataMap);
+      BaseOperator<TransferableBlock> rightOperator = getOperator(requestId, joinNode.getInputs().get(1), metadataMap);
       return new HashJoinOperator(leftOperator, rightOperator, joinNode.getCriteria());
     } else if (stageNode instanceof FilterNode) {
       throw new UnsupportedOperationException("Unsupported!");
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java
index 238c1d807b..ff99dae5ed 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java
@@ -24,14 +24,15 @@ import java.util.HashMap;
 import java.util.List;
 import javax.annotation.Nullable;
 import org.apache.pinot.common.utils.DataSchema;
-import org.apache.pinot.common.utils.DataTable;
 import org.apache.pinot.core.common.Operator;
 import org.apache.pinot.core.operator.BaseOperator;
 import org.apache.pinot.core.query.selection.SelectionOperatorUtils;
 import org.apache.pinot.query.planner.partitioning.KeySelector;
 import org.apache.pinot.query.planner.stage.JoinNode;
-import org.apache.pinot.query.runtime.blocks.DataTableBlock;
-import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+import org.apache.pinot.query.runtime.blocks.BaseDataBlock;
+import org.apache.pinot.query.runtime.blocks.DataBlockBuilder;
+import org.apache.pinot.query.runtime.blocks.DataBlockUtils;
+import org.apache.pinot.query.runtime.blocks.TransferableBlock;
 
 
 /**
@@ -42,12 +43,12 @@ import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
  *
  * <p>For each of the data block received from the left table, it will generate a joint data block.
  */
-public class HashJoinOperator extends BaseOperator<DataTableBlock> {
+public class HashJoinOperator extends BaseOperator<TransferableBlock> {
   private static final String EXPLAIN_NAME = "BROADCAST_JOIN";
 
   private final HashMap<Object, List<Object[]>> _broadcastHashTable;
-  private final BaseOperator<DataTableBlock> _leftTableOperator;
-  private final BaseOperator<DataTableBlock> _rightTableOperator;
+  private final BaseOperator<TransferableBlock> _leftTableOperator;
+  private final BaseOperator<TransferableBlock> _rightTableOperator;
 
   private DataSchema _leftTableSchema;
   private DataSchema _rightTableSchema;
@@ -56,8 +57,8 @@ public class HashJoinOperator extends BaseOperator<DataTableBlock> {
   private KeySelector<Object[], Object> _leftKeySelector;
   private KeySelector<Object[], Object> _rightKeySelector;
 
-  public HashJoinOperator(BaseOperator<DataTableBlock> leftTableOperator,
-      BaseOperator<DataTableBlock> rightTableOperator, List<JoinNode.JoinClause> criteria) {
+  public HashJoinOperator(BaseOperator<TransferableBlock> leftTableOperator,
+      BaseOperator<TransferableBlock> rightTableOperator, List<JoinNode.JoinClause> criteria) {
     // TODO: this assumes right table is broadcast.
     _leftKeySelector = criteria.get(0).getLeftJoinKeySelector();
     _rightKeySelector = criteria.get(0).getRightJoinKeySelector();
@@ -80,25 +81,25 @@ public class HashJoinOperator extends BaseOperator<DataTableBlock> {
   }
 
   @Override
-  protected DataTableBlock getNextBlock() {
+  protected TransferableBlock getNextBlock() {
     buildBroadcastHashTable();
     try {
-      return new DataTableBlock(buildJoinedDataTable(_leftTableOperator.nextBlock()));
+      return new TransferableBlock(buildJoinedDataBlock(_leftTableOperator.nextBlock()));
     } catch (Exception e) {
-      return DataTableBlockUtils.getErrorDatatableBlock(e);
+      return DataBlockUtils.getErrorTransferableBlock(e);
     }
   }
 
   private void buildBroadcastHashTable() {
     if (!_isHashTableBuilt) {
-      DataTableBlock rightBlock = _rightTableOperator.nextBlock();
-      while (!DataTableBlockUtils.isEndOfStream(rightBlock)) {
-        DataTable dataTable = rightBlock.getDataTable();
-        _rightTableSchema = dataTable.getDataSchema();
-        int numRows = dataTable.getNumberOfRows();
+      TransferableBlock rightBlock = _rightTableOperator.nextBlock();
+      while (!DataBlockUtils.isEndOfStream(rightBlock)) {
+        BaseDataBlock dataBlock = rightBlock.getDataBlock();
+        _rightTableSchema = dataBlock.getDataSchema();
+        int numRows = dataBlock.getNumberOfRows();
         // put all the rows into corresponding hash collections keyed by the key selector function.
         for (int rowId = 0; rowId < numRows; rowId++) {
-          Object[] objects = SelectionOperatorUtils.extractRowFromDataTable(dataTable, rowId);
+          Object[] objects = SelectionOperatorUtils.extractRowFromDataTable(dataBlock, rowId);
           List<Object[]> hashCollection =
               _broadcastHashTable.computeIfAbsent(_rightKeySelector.getKey(objects), k -> new ArrayList<>());
           hashCollection.add(objects);
@@ -109,25 +110,25 @@ public class HashJoinOperator extends BaseOperator<DataTableBlock> {
     }
   }
 
-  private DataTable buildJoinedDataTable(DataTableBlock block)
+  private BaseDataBlock buildJoinedDataBlock(TransferableBlock block)
       throws Exception {
-    if (DataTableBlockUtils.isEndOfStream(block)) {
-      return DataTableBlockUtils.getEndOfStreamDataTable();
+    if (DataBlockUtils.isEndOfStream(block)) {
+      return DataBlockUtils.getEndOfStreamDataBlock();
     }
     List<Object[]> rows = new ArrayList<>();
-    DataTable dataTable = block.getDataTable();
-    _leftTableSchema = dataTable.getDataSchema();
+    BaseDataBlock dataBlock = block.getDataBlock();
+    _leftTableSchema = dataBlock.getDataSchema();
     _resultRowSize = _leftTableSchema.size() + _rightTableSchema.size();
-    int numRows = dataTable.getNumberOfRows();
+    int numRows = dataBlock.getNumberOfRows();
     for (int rowId = 0; rowId < numRows; rowId++) {
-      Object[] leftRow = SelectionOperatorUtils.extractRowFromDataTable(dataTable, rowId);
+      Object[] leftRow = SelectionOperatorUtils.extractRowFromDataTable(dataBlock, rowId);
       List<Object[]> hashCollection =
           _broadcastHashTable.getOrDefault(_leftKeySelector.getKey(leftRow), Collections.emptyList());
       for (Object[] rightRow : hashCollection) {
         rows.add(joinRow(leftRow, rightRow));
       }
     }
-    return SelectionOperatorUtils.getDataTableFromRows(rows, computeSchema());
+    return DataBlockBuilder.buildFromRows(rows, computeSchema());
   }
 
   private Object[] joinRow(Object[] leftRow, Object[] rightRow) {
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java
index 7b396b13cc..b84d8f40f0 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java
@@ -24,16 +24,15 @@ import javax.annotation.Nullable;
 import org.apache.calcite.rel.RelDistribution;
 import org.apache.pinot.common.proto.Mailbox;
 import org.apache.pinot.common.utils.DataSchema;
-import org.apache.pinot.common.utils.DataTable;
 import org.apache.pinot.core.common.Operator;
-import org.apache.pinot.core.common.datatable.DataTableFactory;
 import org.apache.pinot.core.operator.BaseOperator;
 import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.query.mailbox.MailboxService;
 import org.apache.pinot.query.mailbox.ReceivingMailbox;
 import org.apache.pinot.query.mailbox.StringMailboxIdentifier;
-import org.apache.pinot.query.runtime.blocks.DataTableBlock;
-import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+import org.apache.pinot.query.runtime.blocks.BaseDataBlock;
+import org.apache.pinot.query.runtime.blocks.DataBlockUtils;
+import org.apache.pinot.query.runtime.blocks.TransferableBlock;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,7 +41,7 @@ import org.slf4j.LoggerFactory;
  * This {@code MailboxReceiveOperator} receives data from a {@link ReceivingMailbox} and serve it out from the
  * {@link BaseOperator#getNextBlock()} API.
  */
-public class MailboxReceiveOperator extends BaseOperator<DataTableBlock> {
+public class MailboxReceiveOperator extends BaseOperator<TransferableBlock> {
   private static final Logger LOGGER = LoggerFactory.getLogger(MailboxReceiveOperator.class);
   private static final String EXPLAIN_NAME = "MAILBOX_RECEIVE";
   private static final long DEFAULT_TIMEOUT_NANO = 10_000_000_000L;
@@ -80,7 +79,7 @@ public class MailboxReceiveOperator extends BaseOperator<DataTableBlock> {
   }
 
   @Override
-  protected DataTableBlock getNextBlock() {
+  protected TransferableBlock getNextBlock() {
     // TODO: do a round robin check against all MailboxContentStreamObservers and find which one that has data.
     boolean hasOpenedMailbox = true;
     DataSchema dataSchema = null;
@@ -99,10 +98,10 @@ public class MailboxReceiveOperator extends BaseOperator<DataTableBlock> {
             if (mailboxContent != null) {
               ByteBuffer byteBuffer = mailboxContent.getPayload().asReadOnlyByteBuffer();
               if (byteBuffer.hasRemaining()) {
-                DataTable dataTable = DataTableFactory.getDataTable(byteBuffer);
-                if (dataTable.getNumberOfRows() > 0) {
+                BaseDataBlock dataBlock = DataBlockUtils.getDataBlock(byteBuffer);
+                if (dataBlock.getNumberOfRows() > 0) {
                   // here we only return data table block when it is not empty.
-                  return new DataTableBlock(dataTable);
+                  return new TransferableBlock(dataBlock);
                 }
               }
             }
@@ -117,7 +116,7 @@ public class MailboxReceiveOperator extends BaseOperator<DataTableBlock> {
     }
     // TODO: we need to at least return one data table with schema if there's no error.
     // we need to condition this on whether there's already things being returned or not.
-    return DataTableBlockUtils.getEndOfStreamDataTableBlock();
+    return DataBlockUtils.getEndOfStreamTransferableBlock();
   }
 
   public RelDistribution.Type getExchangeType() {
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
index 51f24c97d4..8629663b60 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
@@ -37,16 +37,18 @@ import org.apache.pinot.query.mailbox.MailboxService;
 import org.apache.pinot.query.mailbox.SendingMailbox;
 import org.apache.pinot.query.mailbox.StringMailboxIdentifier;
 import org.apache.pinot.query.planner.partitioning.KeySelector;
-import org.apache.pinot.query.runtime.blocks.DataTableBlock;
-import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+import org.apache.pinot.query.runtime.blocks.BaseDataBlock;
+import org.apache.pinot.query.runtime.blocks.DataBlockBuilder;
+import org.apache.pinot.query.runtime.blocks.DataBlockUtils;
+import org.apache.pinot.query.runtime.blocks.TransferableBlock;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 
 /**
- * This {@code MailboxSendOperator} is created to send {@link DataTableBlock}s to the receiving end.
+ * This {@code MailboxSendOperator} is created to send {@link TransferableBlock}s to the receiving end.
  */
-public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
+public class MailboxSendOperator extends BaseOperator<TransferableBlock> {
   private static final Logger LOGGER = LoggerFactory.getLogger(MailboxSendOperator.class);
   private static final String EXPLAIN_NAME = "MAILBOX_SEND";
   private static final Set<RelDistribution.Type> SUPPORTED_EXCHANGE_TYPE =
@@ -61,11 +63,11 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
   private final long _jobId;
   private final int _stageId;
   private final MailboxService<Mailbox.MailboxContent> _mailboxService;
-  private BaseOperator<DataTableBlock> _dataTableBlockBaseOperator;
-  private DataTable _dataTable;
+  private BaseOperator<TransferableBlock> _dataTableBlockBaseOperator;
+  private BaseDataBlock _dataTable;
 
   public MailboxSendOperator(MailboxService<Mailbox.MailboxContent> mailboxService,
-      BaseOperator<DataTableBlock> dataTableBlockBaseOperator, List<ServerInstance> receivingStageInstances,
+      BaseOperator<TransferableBlock> dataTableBlockBaseOperator, List<ServerInstance> receivingStageInstances,
       RelDistribution.Type exchangeType, KeySelector<Object[], Object> keySelector, String hostName, int port,
       long jobId, int stageId) {
     _mailboxService = mailboxService;
@@ -86,7 +88,7 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
    * we create a {@link org.apache.pinot.core.query.executor.ServerQueryExecutorV1Impl} that can handle the
    * creation of MailboxSendOperator we should not use this API.
    */
-  public MailboxSendOperator(MailboxService<Mailbox.MailboxContent> mailboxService, DataTable dataTable,
+  public MailboxSendOperator(MailboxService<Mailbox.MailboxContent> mailboxService, BaseDataBlock dataTable,
       List<ServerInstance> receivingStageInstances, RelDistribution.Type exchangeType,
       KeySelector<Object[], Object> keySelector, String hostName, int port, long jobId, int stageId) {
     _mailboxService = mailboxService;
@@ -113,14 +115,14 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
   }
 
   @Override
-  protected DataTableBlock getNextBlock() {
-    DataTable dataTable;
-    DataTableBlock dataTableBlock = null;
+  protected TransferableBlock getNextBlock() {
+    BaseDataBlock dataTable;
+    TransferableBlock transferableBlock = null;
     boolean isEndOfStream;
     if (_dataTableBlockBaseOperator != null) {
-      dataTableBlock = _dataTableBlockBaseOperator.nextBlock();
-      dataTable = dataTableBlock.getDataTable();
-      isEndOfStream = DataTableBlockUtils.isEndOfStream(dataTableBlock);
+      transferableBlock = _dataTableBlockBaseOperator.nextBlock();
+      dataTable = transferableBlock.getDataBlock();
+      isEndOfStream = DataBlockUtils.isEndOfStream(transferableBlock);
     } else {
       dataTable = _dataTable;
       isEndOfStream = true;
@@ -137,7 +139,7 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
             // we no longer need to send data to the rest of the receiving instances, but we still need to transfer
             // the dataTable over indicating that we are a potential sender. thus next time a random server is selected
             // it might still be useful.
-            dataTable = DataTableBlockUtils.getEmptyDataTable(dataTable.getDataSchema());
+            dataTable = DataBlockUtils.getEmptyDataBlock(dataTable.getDataSchema());
           }
           break;
         case BROADCAST_DISTRIBUTED:
@@ -147,7 +149,7 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
           break;
         case HASH_DISTRIBUTED:
           // TODO: ensure that server instance list is sorted using same function in sender.
-          List<DataTable> dataTableList = constructPartitionedDataBlock(dataTable, _keySelector,
+          List<BaseDataBlock> dataTableList = constructPartitionedDataBlock(dataTable, _keySelector,
               _receivingStageInstances.size());
           for (int i = 0; i < _receivingStageInstances.size(); i++) {
             sendDataTableBlock(_receivingStageInstances.get(i), dataTableList.get(i), isEndOfStream);
@@ -162,10 +164,10 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
     } catch (Exception e) {
       LOGGER.error("Exception occur while sending data via mailbox", e);
     }
-    return dataTableBlock;
+    return transferableBlock;
   }
 
-  private static List<DataTable> constructPartitionedDataBlock(DataTable dataTable,
+  private static List<BaseDataBlock> constructPartitionedDataBlock(DataTable dataTable,
       KeySelector<Object[], Object> keySelector, int partitionSize)
       throws Exception {
     List<List<Object[]>> temporaryRows = new ArrayList<>(partitionSize);
@@ -178,10 +180,10 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
       // TODO: support other partitioning algorithm
       temporaryRows.get(hashToIndex(key, partitionSize)).add(row);
     }
-    List<DataTable> dataTableList = new ArrayList<>(partitionSize);
+    List<BaseDataBlock> dataTableList = new ArrayList<>(partitionSize);
     for (int i = 0; i < partitionSize; i++) {
       List<Object[]> objects = temporaryRows.get(i);
-      dataTableList.add(SelectionOperatorUtils.getDataTableFromRows(objects, dataTable.getDataSchema()));
+      dataTableList.add(DataBlockBuilder.buildFromRows(objects, dataTable.getDataSchema()));
     }
     return dataTableList;
   }
@@ -190,7 +192,7 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
     return (key.hashCode()) % partitionSize;
   }
 
-  private void sendDataTableBlock(ServerInstance serverInstance, DataTable dataTable, boolean isEndOfStream)
+  private void sendDataTableBlock(ServerInstance serverInstance, BaseDataBlock dataTable, boolean isEndOfStream)
       throws IOException {
     String mailboxId = toMailboxId(serverInstance);
     SendingMailbox<Mailbox.MailboxContent> sendingMailbox = _mailboxService.getSendingMailbox(mailboxId);
@@ -201,10 +203,10 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
     }
   }
 
-  private Mailbox.MailboxContent toMailboxContent(String mailboxId, DataTable dataTable, boolean isEndOfStream)
+  private Mailbox.MailboxContent toMailboxContent(String mailboxId, BaseDataBlock dataTable, boolean isEndOfStream)
       throws IOException {
     Mailbox.MailboxContent.Builder builder = Mailbox.MailboxContent.newBuilder().setMailboxId(mailboxId)
-        .setPayload(ByteString.copyFrom(new DataTableBlock(dataTable).toBytes()));
+        .setPayload(ByteString.copyFrom(new TransferableBlock(dataTable).toBytes()));
     if (isEndOfStream) {
       builder.putMetadata("finished", "true");
     }
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java
index 7ea69b8d21..fa9f04808c 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java
@@ -33,8 +33,9 @@ import org.apache.pinot.query.mailbox.MailboxService;
 import org.apache.pinot.query.planner.QueryPlan;
 import org.apache.pinot.query.planner.StageMetadata;
 import org.apache.pinot.query.planner.stage.MailboxReceiveNode;
-import org.apache.pinot.query.runtime.blocks.DataTableBlock;
-import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+import org.apache.pinot.query.runtime.blocks.BaseDataBlock;
+import org.apache.pinot.query.runtime.blocks.DataBlockUtils;
+import org.apache.pinot.query.runtime.blocks.TransferableBlock;
 import org.apache.pinot.query.runtime.operator.MailboxReceiveOperator;
 import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
 import org.apache.pinot.query.runtime.plan.serde.QueryPlanSerDeUtils;
@@ -58,24 +59,13 @@ public class QueryDispatcher {
       throws Exception {
     // submit all the distributed stages.
     int reduceStageId = submit(requestId, queryPlan);
-
-    // run reduce stage.
+    // run reduce stage and return result.
     MailboxReceiveNode reduceNode = (MailboxReceiveNode) queryPlan.getQueryStageMap().get(reduceStageId);
     MailboxReceiveOperator mailboxReceiveOperator = createReduceStageOperator(mailboxService,
         queryPlan.getStageMetadataMap().get(reduceNode.getSenderStageId()).getServerInstances(),
         requestId, reduceNode.getSenderStageId(), mailboxService.getHostname(),
         mailboxService.getMailboxPort());
-
-    List<DataTable> queryResults = new ArrayList<>();
-    long timeoutWatermark = System.nanoTime() + timeoutNano;
-    while (System.nanoTime() < timeoutWatermark) {
-      DataTableBlock dataTableBlock = mailboxReceiveOperator.nextBlock();
-      queryResults.add(dataTableBlock.getDataTable());
-      if (DataTableBlockUtils.isEndOfStream(dataTableBlock)) {
-        break;
-      }
-    }
-    return queryResults;
+    return reduceMailboxReceive(mailboxReceiveOperator);
   }
 
   public int submit(long requestId, QueryPlan queryPlan)
@@ -109,12 +99,9 @@ public class QueryDispatcher {
     return reduceStageId;
   }
 
-  protected MailboxReceiveOperator createReduceStageOperator(MailboxService<Mailbox.MailboxContent> mailboxService,
-      List<ServerInstance> sendingInstances, long jobId, int stageId, String hostname, int port) {
-    MailboxReceiveOperator mailboxReceiveOperator =
-        new MailboxReceiveOperator(mailboxService, RelDistribution.Type.ANY, sendingInstances, hostname, port, jobId,
-            stageId);
-    return mailboxReceiveOperator;
+  private DispatchClient getOrCreateDispatchClient(String host, int port) {
+    String key = String.format("%s_%d", host, port);
+    return _dispatchClientMap.computeIfAbsent(key, k -> new DispatchClient(host, port));
   }
 
   public static DistributedStagePlan constructDistributedStagePlan(QueryPlan queryPlan, int stageId,
@@ -123,9 +110,28 @@ public class QueryDispatcher {
         queryPlan.getStageMetadataMap());
   }
 
-  private DispatchClient getOrCreateDispatchClient(String host, int port) {
-    String key = String.format("%s_%d", host, port);
-    return _dispatchClientMap.computeIfAbsent(key, k -> new DispatchClient(host, port));
+  public static List<DataTable> reduceMailboxReceive(MailboxReceiveOperator mailboxReceiveOperator) {
+    List<DataTable> resultDataBlocks = new ArrayList<>();
+    TransferableBlock transferableBlock;
+    while (true) {
+      transferableBlock = mailboxReceiveOperator.nextBlock();
+      if (DataBlockUtils.isEndOfStream(transferableBlock)) {
+        break;
+      }
+      if (transferableBlock.getDataBlock() != null) {
+        BaseDataBlock dataTable = transferableBlock.getDataBlock();
+        resultDataBlocks.add(dataTable);
+      }
+    }
+    return resultDataBlocks;
+  }
+
+  public static MailboxReceiveOperator createReduceStageOperator(MailboxService<Mailbox.MailboxContent> mailboxService,
+      List<ServerInstance> sendingInstances, long jobId, int stageId, String hostname, int port) {
+    MailboxReceiveOperator mailboxReceiveOperator =
+        new MailboxReceiveOperator(mailboxService, RelDistribution.Type.ANY, sendingInstances, hostname, port, jobId,
+            stageId);
+    return mailboxReceiveOperator;
   }
 
   public static class DispatchClient {
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcMailboxServiceTest.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcMailboxServiceTest.java
index f1863bc339..e254778db1 100644
--- a/pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcMailboxServiceTest.java
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcMailboxServiceTest.java
@@ -24,8 +24,8 @@ import com.google.protobuf.ByteString;
 import java.io.IOException;
 import java.util.Map;
 import org.apache.pinot.common.proto.Mailbox;
-import org.apache.pinot.core.common.datatable.DataTableBuilder;
-import org.apache.pinot.query.runtime.blocks.DataTableBlock;
+import org.apache.pinot.query.runtime.blocks.DataBlockUtils;
+import org.apache.pinot.query.runtime.blocks.TransferableBlock;
 import org.apache.pinot.util.TestUtils;
 import org.testng.Assert;
 import org.testng.annotations.Test;
@@ -67,6 +67,7 @@ public class GrpcMailboxServiceTest extends GrpcMailboxServiceTestBase {
       throws IOException {
     return Mailbox.MailboxContent.newBuilder().setMailboxId(mailboxId)
         .putAllMetadata(ImmutableMap.of("key", "value", "finished", "true"))
-        .setPayload(ByteString.copyFrom(new DataTableBlock(DataTableBuilder.getEmptyDataTable()).toBytes())).build();
+        .setPayload(ByteString.copyFrom(new TransferableBlock(DataBlockUtils.getEndOfStreamDataBlock()).toBytes()))
+        .build();
   }
 }
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java
index 2316195bce..d133584797 100644
--- a/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java
@@ -27,7 +27,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
-import org.apache.calcite.rel.RelDistribution;
 import org.apache.commons.io.FileUtils;
 import org.apache.pinot.common.utils.DataTable;
 import org.apache.pinot.core.transport.ServerInstance;
@@ -38,8 +37,6 @@ import org.apache.pinot.query.mailbox.GrpcMailboxService;
 import org.apache.pinot.query.planner.QueryPlan;
 import org.apache.pinot.query.planner.stage.MailboxReceiveNode;
 import org.apache.pinot.query.routing.WorkerInstance;
-import org.apache.pinot.query.runtime.blocks.DataTableBlock;
-import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
 import org.apache.pinot.query.runtime.operator.MailboxReceiveOperator;
 import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
 import org.apache.pinot.query.service.QueryConfig;
@@ -84,8 +81,8 @@ public class QueryRunnerTest {
     _mailboxService = new GrpcMailboxService(_reducerHostname, _reducerGrpcPort);
     _mailboxService.start();
 
-    _queryEnvironment =
-        QueryEnvironmentTestUtils.getQueryEnvironment(_reducerGrpcPort, server1.getPort(), server2.getPort());
+    _queryEnvironment = QueryEnvironmentTestUtils.getQueryEnvironment(_reducerGrpcPort, server1.getPort(),
+        server2.getPort());
     server1.start();
     server2.start();
     // this doesn't test the QueryServer functionality so the server port can be the same as the mailbox port.
@@ -111,9 +108,10 @@ public class QueryRunnerTest {
     for (int stageId : queryPlan.getStageMetadataMap().keySet()) {
       if (queryPlan.getQueryStageMap().get(stageId) instanceof MailboxReceiveNode) {
         MailboxReceiveNode reduceNode = (MailboxReceiveNode) queryPlan.getQueryStageMap().get(stageId);
-        mailboxReceiveOperator = createReduceStageOperator(
+        mailboxReceiveOperator = QueryDispatcher.createReduceStageOperator(_mailboxService,
             queryPlan.getStageMetadataMap().get(reduceNode.getSenderStageId()).getServerInstances(),
-            Long.parseLong(requestMetadataMap.get("REQUEST_ID")), reduceNode.getSenderStageId(), _reducerGrpcPort);
+            Long.parseLong(requestMetadataMap.get("REQUEST_ID")), reduceNode.getSenderStageId(), "localhost",
+            _reducerGrpcPort);
       } else {
         for (ServerInstance serverInstance : queryPlan.getStageMetadataMap().get(stageId).getServerInstances()) {
           DistributedStagePlan distributedStagePlan =
@@ -124,10 +122,21 @@ public class QueryRunnerTest {
     }
     Preconditions.checkNotNull(mailboxReceiveOperator);
 
-    List<Object[]> resultRows = reduceMailboxReceive(mailboxReceiveOperator);
+    List<Object[]> resultRows = toRows(QueryDispatcher.reduceMailboxReceive(mailboxReceiveOperator));
     Assert.assertEquals(resultRows.size(), expectedRowCount);
   }
 
+  private static List<Object[]> toRows(List<DataTable> dataTables) {
+    List<Object[]> resultRows = new ArrayList<>();
+    for (DataTable dataTable : dataTables) {
+      int numRows = dataTable.getNumberOfRows();
+      for (int rowId = 0; rowId < numRows; rowId++) {
+        resultRows.add(extractRowFromDataTable(dataTable, rowId));
+      }
+    }
+    return resultRows;
+  }
+
   @DataProvider(name = "testDataWithSqlToFinalRowCount")
   private Object[][] provideTestSqlAndRowCount() {
     return new Object[][] {
@@ -152,31 +161,4 @@ public class QueryRunnerTest {
             + " WHERE a.col3 >= 0 AND a.col2 = 'foo' AND b.col3 >= 0", 3},
     };
   }
-
-  protected static List<Object[]> reduceMailboxReceive(MailboxReceiveOperator mailboxReceiveOperator) {
-    List<Object[]> resultRows = new ArrayList<>();
-    DataTableBlock dataTableBlock;
-    while (true) {
-      dataTableBlock = mailboxReceiveOperator.nextBlock();
-      if (DataTableBlockUtils.isEndOfStream(dataTableBlock)) {
-        break;
-      }
-      if (dataTableBlock.getDataTable() != null) {
-        DataTable dataTable = dataTableBlock.getDataTable();
-        int numRows = dataTable.getNumberOfRows();
-        for (int rowId = 0; rowId < numRows; rowId++) {
-          resultRows.add(extractRowFromDataTable(dataTable, rowId));
-        }
-      }
-    }
-    return resultRows;
-  }
-
-  protected MailboxReceiveOperator createReduceStageOperator(List<ServerInstance> sendingInstances, long jobId,
-      int stageId, int port) {
-    MailboxReceiveOperator mailboxReceiveOperator =
-        new MailboxReceiveOperator(_mailboxService, RelDistribution.Type.ANY, sendingInstances, "localhost", port,
-            jobId, stageId);
-    return mailboxReceiveOperator;
-  }
 }
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/blocks/DataBlockTest.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/blocks/DataBlockTest.java
new file mode 100644
index 0000000000..bf33160b92
--- /dev/null
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/blocks/DataBlockTest.java
@@ -0,0 +1,80 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import java.io.IOException;
+import java.util.List;
+import org.apache.pinot.common.exception.QueryException;
+import org.apache.pinot.common.response.ProcessingException;
+import org.apache.pinot.common.utils.DataSchema;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class DataBlockTest {
+  private static final int TEST_ROW_COUNT = 2;
+
+  @Test
+  public void testException()
+      throws IOException {
+    Exception originalException = new UnsupportedOperationException("Expected test exception.");
+    ProcessingException processingException =
+        QueryException.getException(QueryException.QUERY_EXECUTION_ERROR, originalException);
+    String expected = processingException.getMessage();
+
+    BaseDataBlock dataBlock = DataBlockUtils.getErrorDataBlock(originalException);
+    dataBlock.addException(processingException);
+    Assert.assertEquals(dataBlock.getDataSchema().getColumnNames().length, 0);
+    Assert.assertEquals(dataBlock.getDataSchema().getColumnDataTypes().length, 0);
+    Assert.assertEquals(dataBlock.getNumberOfRows(), 0);
+
+    // Assert processing exception and original exception both matches.
+    String actual = dataBlock.getExceptions().get(QueryException.QUERY_EXECUTION_ERROR.getErrorCode());
+    Assert.assertEquals(actual, expected);
+    Assert.assertEquals(dataBlock.getExceptions().get(QueryException.UNKNOWN_ERROR_CODE),
+        originalException.getMessage());
+  }
+
+  @Test
+  public void testAllDataTypes()
+      throws IOException {
+    DataSchema.ColumnDataType[] columnDataTypes = DataSchema.ColumnDataType.values();
+    int numColumns = columnDataTypes.length;
+    String[] columnNames = new String[numColumns];
+    for (int i = 0; i < numColumns; i++) {
+      columnNames[i] = columnDataTypes[i].name();
+    }
+
+    DataSchema dataSchema = new DataSchema(columnNames, columnDataTypes);
+    List<Object[]> rows = DataBlockTestUtils.getRandomRows(dataSchema, TEST_ROW_COUNT);
+    List<Object[]> columnars = DataBlockTestUtils.convertColumnar(dataSchema, rows);
+    RowDataBlock rowBlock = DataBlockBuilder.buildFromRows(rows, dataSchema);
+    ColumnarDataBlock columnarBlock = DataBlockBuilder.buildFromColumns(columnars, dataSchema);
+
+    for (int colId = 0; colId < dataSchema.getColumnNames().length; colId++) {
+      DataSchema.ColumnDataType columnDataType = dataSchema.getColumnDataType(colId);
+      for (int rowId = 0; rowId < TEST_ROW_COUNT; rowId++) {
+        Object rowVal = DataBlockTestUtils.getElement(rowBlock, rowId, colId, columnDataType);
+        Object colVal = DataBlockTestUtils.getElement(columnarBlock, rowId, colId, columnDataType);
+        Assert.assertEquals(rowVal, colVal, "Error comparing Row/Column Block at (" + rowId + "," + colId + ")"
+            + " of Type: " + columnDataType + "! rowValue: [" + rowVal + "], columnarValue: [" + colVal + "]");
+      }
+    }
+  }
+}
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/blocks/DataBlockTestUtils.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/blocks/DataBlockTestUtils.java
new file mode 100644
index 0000000000..caa9b55ccf
--- /dev/null
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/blocks/DataBlockTestUtils.java
@@ -0,0 +1,181 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import org.apache.commons.lang.RandomStringUtils;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.spi.utils.ByteArray;
+
+
+public class DataBlockTestUtils {
+  private static final Random RANDOM = new Random();
+  private static final int ARRAY_SIZE = 5;
+
+  private DataBlockTestUtils() {
+    // do not instantiate.
+  }
+
+  public static Object[] getRandomRow(DataSchema dataSchema) {
+    final int numColumns = dataSchema.getColumnNames().length;
+    DataSchema.ColumnDataType[] columnDataTypes = dataSchema.getColumnDataTypes();
+    Object[] row = new Object[numColumns];
+    for (int colId = 0; colId < numColumns; colId++) {
+      switch (columnDataTypes[colId].getStoredType()) {
+        case INT:
+          row[colId] = RANDOM.nextInt();
+          break;
+        case LONG:
+          row[colId] = RANDOM.nextLong();
+          break;
+        case FLOAT:
+          row[colId] = RANDOM.nextFloat();
+          break;
+        case DOUBLE:
+          row[colId] = RANDOM.nextDouble();
+          break;
+        case BIG_DECIMAL:
+          row[colId] = BigDecimal.valueOf(RANDOM.nextDouble());
+          break;
+        case STRING:
+          row[colId] = RandomStringUtils.random(RANDOM.nextInt(20));
+          break;
+        case BYTES:
+          row[colId] = new ByteArray(RandomStringUtils.random(RANDOM.nextInt(20)).getBytes());
+          break;
+        // Just test Double here, all object types will be covered in ObjectCustomSerDeTest.
+        case OBJECT:
+          row[colId] = RANDOM.nextDouble();
+          break;
+        case BOOLEAN_ARRAY:
+        case INT_ARRAY:
+          int length = RANDOM.nextInt(ARRAY_SIZE);
+          int[] intArray = new int[length];
+          for (int i = 0; i < length; i++) {
+            intArray[i] = RANDOM.nextInt();
+          }
+          row[colId] = intArray;
+          break;
+        case TIMESTAMP_ARRAY:
+        case LONG_ARRAY:
+          length = RANDOM.nextInt(ARRAY_SIZE);
+          long[] longArray = new long[length];
+          for (int i = 0; i < length; i++) {
+            longArray[i] = RANDOM.nextLong();
+          }
+          row[colId] = longArray;
+          break;
+        case FLOAT_ARRAY:
+          length = RANDOM.nextInt(ARRAY_SIZE);
+          float[] floatArray = new float[length];
+          for (int i = 0; i < length; i++) {
+            floatArray[i] = RANDOM.nextFloat();
+          }
+          row[colId] = floatArray;
+          break;
+        case DOUBLE_ARRAY:
+          length = RANDOM.nextInt(ARRAY_SIZE);
+          double[] doubleArray = new double[length];
+          for (int i = 0; i < length; i++) {
+            doubleArray[i] = RANDOM.nextDouble();
+          }
+          row[colId] = doubleArray;
+          break;
+        case BYTES_ARRAY:
+        case STRING_ARRAY:
+          length = RANDOM.nextInt(ARRAY_SIZE);
+          String[] stringArray = new String[length];
+          for (int i = 0; i < length; i++) {
+            stringArray[i] = RandomStringUtils.random(RANDOM.nextInt(20));
+          }
+          row[colId] = stringArray;
+          break;
+        default:
+          throw new UnsupportedOperationException("Can't fill random data for column type: " + columnDataTypes[colId]);
+      }
+    }
+    return row;
+  }
+
+  public static Object getElement(BaseDataBlock dataBlock, int rowId, int colId,
+      DataSchema.ColumnDataType columnDataType) {
+    switch (columnDataType.getStoredType()) {
+      case INT:
+        return dataBlock.getInt(rowId, colId);
+      case LONG:
+        return dataBlock.getLong(rowId, colId);
+      case FLOAT:
+        return dataBlock.getFloat(rowId, colId);
+      case DOUBLE:
+        return dataBlock.getDouble(rowId, colId);
+      case BIG_DECIMAL:
+        return dataBlock.getBigDecimal(rowId, colId);
+      case STRING:
+        return dataBlock.getString(rowId, colId);
+      case BYTES:
+        return dataBlock.getBytes(rowId, colId);
+      case OBJECT:
+        return dataBlock.getObject(rowId, colId);
+      case BOOLEAN_ARRAY:
+      case INT_ARRAY:
+        return dataBlock.getIntArray(rowId, colId);
+      case TIMESTAMP_ARRAY:
+      case LONG_ARRAY:
+        return dataBlock.getLongArray(rowId, colId);
+      case FLOAT_ARRAY:
+        return dataBlock.getFloatArray(rowId, colId);
+      case DOUBLE_ARRAY:
+        return dataBlock.getDoubleArray(rowId, colId);
+      case BYTES_ARRAY:
+      case STRING_ARRAY:
+        return dataBlock.getStringArray(rowId, colId);
+      default:
+        throw new UnsupportedOperationException("Can't retrieve data for column type: " + columnDataType);
+    }
+  }
+
+  public static List<Object[]> getRandomRows(DataSchema dataSchema, int numRows) {
+    List<Object[]> rows = new ArrayList<>(numRows);
+    for (int i = 0; i < numRows; i++) {
+      rows.add(getRandomRow(dataSchema));
+    }
+    return rows;
+  }
+
+  public static List<Object[]> getRandomColumnar(DataSchema dataSchema, int numRows) {
+    List<Object[]> rows = getRandomRows(dataSchema, numRows);
+    return convertColumnar(dataSchema, rows);
+  }
+
+  public static List<Object[]> convertColumnar(DataSchema dataSchema, List<Object[]> rows) {
+    final int numRows = rows.size();
+    final int numColumns = dataSchema.getColumnNames().length;
+    List<Object[]> columnars = new ArrayList<>(numColumns);
+    for (int colId = 0; colId < numColumns; colId++) {
+      columnars.add(new Object[numRows]);
+      for (int rowId = 0; rowId < numRows; rowId++) {
+        columnars.get(colId)[rowId] = rows.get(rowId)[colId];
+      }
+    }
+    return columnars;
+  }
+}
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryDispatcherTest.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryDispatcherTest.java
new file mode 100644
index 0000000000..9057d1a7ec
--- /dev/null
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryDispatcherTest.java
@@ -0,0 +1,92 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.service;
+
+import com.google.common.collect.Lists;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import org.apache.pinot.query.QueryEnvironment;
+import org.apache.pinot.query.QueryEnvironmentTestUtils;
+import org.apache.pinot.query.planner.PlannerUtils;
+import org.apache.pinot.query.planner.QueryPlan;
+import org.apache.pinot.query.runtime.QueryRunner;
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+
+public class QueryDispatcherTest {
+  private static final Random RANDOM_REQUEST_ID_GEN = new Random();
+  private static final int QUERY_SERVER_COUNT = 2;
+  private final Map<Integer, QueryServer> _queryServerMap = new HashMap<>();
+  private final Map<Integer, QueryRunner> _queryRunnerMap = new HashMap<>();
+
+  private QueryEnvironment _queryEnvironment;
+
+  @BeforeClass
+  public void setUp()
+      throws Exception {
+
+    for (int i = 0; i < QUERY_SERVER_COUNT; i++) {
+      int availablePort = QueryEnvironmentTestUtils.getAvailablePort();
+      QueryRunner queryRunner = Mockito.mock(QueryRunner.class);
+      QueryServer queryServer = new QueryServer(availablePort, queryRunner);
+      queryServer.start();
+      _queryServerMap.put(availablePort, queryServer);
+      _queryRunnerMap.put(availablePort, queryRunner);
+    }
+
+    List<Integer> portList = Lists.newArrayList(_queryServerMap.keySet());
+
+    // reducer port doesn't matter, we are testing the worker instance not GRPC.
+    _queryEnvironment = QueryEnvironmentTestUtils.getQueryEnvironment(1, portList.get(0), portList.get(1));
+  }
+
+  @AfterClass
+  public void tearDown() {
+    for (QueryServer worker : _queryServerMap.values()) {
+      worker.shutdown();
+    }
+  }
+
+  @Test(dataProvider = "testDataWithSqlToCompiledAsWorkerRequest")
+  public void testQueryDispatcherCanSendCorrectPayload(String sql)
+      throws Exception {
+    QueryPlan queryPlan = _queryEnvironment.planQuery(sql);
+    QueryDispatcher dispatcher = new QueryDispatcher();
+    int reducerStageId = dispatcher.submit(RANDOM_REQUEST_ID_GEN.nextLong(), queryPlan);
+    Assert.assertTrue(PlannerUtils.isRootStage(reducerStageId));
+  }
+
+  @DataProvider(name = "testDataWithSqlToCompiledAsWorkerRequest")
+  private Object[][] provideTestSqlToCompiledToWorkerRequest() {
+    return new Object[][] {
+        new Object[]{"SELECT * FROM b"},
+        new Object[]{"SELECT * FROM a"},
+        new Object[]{"SELECT * FROM a JOIN b ON a.col3 = b.col3"},
+        new Object[]{"SELECT a.col1, a.ts, c.col2, c.col3 FROM a JOIN c ON a.col1 = c.col2 "
+            + " WHERE (a.col3 >= 0 OR a.col2 = 'foo') AND c.col3 >= 0"},
+    };
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[pinot] 03/09: add pinot-query-runtime (#8412)

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rongr pushed a commit to branch multi_stage_query_engine
in repository https://gitbox.apache.org/repos/asf/pinot.git

commit dacd28187adf919f353f00fb0103c10276eaa69c
Author: Rong Rong <ro...@apache.org>
AuthorDate: Wed Apr 6 01:03:53 2022 -0700

    add pinot-query-runtime (#8412)
    
    * add pinot-query-runtime
    
    - inter-stage exchange interface
    - worker executor interface
    - dispatcher and dispatch-receiving server interface
    - row-based runtime operator (incomplete)
    
    several component that's need in order to access current pinot-server
    functionality
    - converter between stage plan into existing PinotQuery/QueryContext
    - after-execution exchange sender connected to InstanceResponseOperator
    
    * fix jdk8 compile
    
    * documentation for mailbox module
    
    * document the runtime
    
    * change stageId to Integer
    
    * change jobId to also long type to align with current pinot requestId
    
    * change the serializable format of dispatchable QueryPlan
    
    Work Items Done
    1. split worker.proto into the service proto and the message proto
    2. created StageRoot proto object that can encode the input StageNodes as serialized bytes.
    3. modify ser/deser modules in query-runtime
    4.remove unnecessary plan node operations in stage plan
    
    Work Items TODO
    1. make a proto for StageNode, should include all possible StageNode Fields.
    2. make a serializer and deserializer (real one to replace the current
       java.io.Serializable)
    
    * remove plan.proto
    
    will add it back once the serializable StageNode format is determined
    
    * fix serde
    
    * address diff comments
    
    Co-authored-by: Rong Rong <ro...@startree.ai>
---
 pinot-common/src/main/proto/mailbox.proto          |  55 ++++
 pinot-common/src/main/proto/worker.proto           |  74 ++++++
 pinot-distribution/pom.xml                         |   8 +
 .../{nodes/CalcNode.java => PlannerUtils.java}     |  26 +-
 .../org/apache/pinot/query/planner/QueryPlan.java  |  10 +-
 .../pinot/query/planner/RelToStageConverter.java   |  45 +++-
 .../apache/pinot/query/planner/StageMetadata.java  |   2 +-
 .../apache/pinot/query/planner/StagePlanner.java   |  24 +-
 .../query/planner/nodes/AbstractStageNode.java     |   6 +-
 .../apache/pinot/query/planner/nodes/CalcNode.java |  13 +-
 .../apache/pinot/query/planner/nodes/JoinNode.java |  77 ++----
 .../query/planner/nodes/MailboxReceiveNode.java    |  19 +-
 .../pinot/query/planner/nodes/MailboxSendNode.java |  22 +-
 .../pinot/query/planner/nodes/StageNode.java       |   2 +-
 .../pinot/query/planner/nodes/TableScanNode.java   |  26 +-
 .../partitioning/FieldSelectionKeySelector.java    |   4 +-
 .../query/planner/partitioning/KeySelector.java    |   5 +-
 .../apache/pinot/query/routing/WorkerManager.java  |   5 +-
 .../apache/pinot/query/QueryEnvironmentTest.java   |   5 +-
 .../pinot/query/QueryEnvironmentTestUtils.java     |   8 +-
 pinot-query-runtime/pom.xml                        |  83 ++++++
 .../pinot/query/mailbox/GrpcMailboxService.java    | 101 ++++++++
 .../pinot/query/mailbox/GrpcReceivingMailbox.java  |  88 +++++++
 .../pinot/query/mailbox/GrpcSendingMailbox.java    |  74 ++++++
 .../pinot/query/mailbox/MailboxIdentifier.java     |  42 +++-
 .../apache/pinot/query/mailbox/MailboxService.java |  70 ++++++
 .../pinot/query/mailbox/ReceivingMailbox.java      |  37 ++-
 .../apache/pinot/query/mailbox/SendingMailbox.java |  30 ++-
 .../query/mailbox/StringMailboxIdentifier.java     | 100 ++++++++
 .../java/org/apache/pinot/query/mailbox/Utils.java |  32 +--
 .../query/mailbox/channel/ChannelManager.java      |  62 +++++
 .../query/mailbox/channel/GrpcMailboxServer.java   |  72 ++++++
 .../channel/MailboxContentStreamObserver.java      | 114 +++++++++
 .../channel/MailboxStatusStreamObserver.java       |  88 +++++++
 .../apache/pinot/query/runtime/QueryRunner.java    | 118 +++++++++
 .../pinot/query/runtime/blocks/DataTableBlock.java |  73 ++++++
 .../query/runtime/blocks/DataTableBlockUtils.java  |  71 ++++++
 .../runtime/executor/WorkerQueryExecutor.java      | 122 +++++++++
 .../runtime/operator/BroadcastJoinOperator.java    | 165 ++++++++++++
 .../runtime/operator/MailboxReceiveOperator.java   | 137 ++++++++++
 .../runtime/operator/MailboxSendOperator.java      | 182 ++++++++++++++
 .../query/runtime/plan/DistributedStagePlan.java   |  76 ++++++
 .../runtime/plan/serde/QueryPlanSerDeUtils.java    | 108 ++++++++
 .../runtime/plan/serde/StageNodeSerDeUtils.java    |  56 +++++
 .../query/runtime/utils/ServerRequestUtils.java    | 109 ++++++++
 .../apache/pinot/query/service/QueryConfig.java    |  26 +-
 .../pinot/query/service/QueryDispatcher.java       | 143 +++++++++++
 .../apache/pinot/query/service/QueryServer.java    | 115 +++++++++
 .../apache/pinot/query/QueryServerEnclosure.java   | 192 ++++++++++++++
 .../query/mailbox/GrpcMailboxServiceTest.java      |  72 ++++++
 .../query/mailbox/GrpcMailboxServiceTestBase.java  |  48 ++++
 .../pinot/query/runtime/QueryRunnerTest.java       | 278 +++++++++++++++++++++
 .../pinot/query/service/QueryServerTest.java       | 107 ++++++++
 pom.xml                                            |  19 ++
 54 files changed, 3413 insertions(+), 233 deletions(-)

diff --git a/pinot-common/src/main/proto/mailbox.proto b/pinot-common/src/main/proto/mailbox.proto
new file mode 100644
index 0000000000..6e1cca9a92
--- /dev/null
+++ b/pinot-common/src/main/proto/mailbox.proto
@@ -0,0 +1,55 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+syntax = "proto3";
+
+package org.apache.pinot.common.proto;
+
+service PinotMailbox {
+  rpc open(stream MailboxContent) returns (stream MailboxStatus);
+}
+
+message MailboxStatus {
+  string mailboxId = 1;
+  map<string, string> metadata = 2;
+}
+
+message MailboxContent {
+  string mailboxId = 1;
+  bytes payload = 2;
+  map<string, string> metadata = 3;
+}
diff --git a/pinot-common/src/main/proto/worker.proto b/pinot-common/src/main/proto/worker.proto
new file mode 100644
index 0000000000..c64798fa63
--- /dev/null
+++ b/pinot-common/src/main/proto/worker.proto
@@ -0,0 +1,74 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+syntax = "proto3";
+
+package org.apache.pinot.common.proto;
+
+service PinotQueryWorker {
+  // Dispatch a QueryRequest to a PinotQueryWorker
+  rpc Submit(QueryRequest) returns (QueryResponse);
+}
+
+// QueryRequest is the dispatched content for a specific query stage on a specific worker.
+message QueryRequest {
+  map<string, string> metadata = 1;
+  StagePlan stagePlan = 2;
+}
+
+// QueryResponse is the dispatched response from worker, it doesn't contain actual data, only dispatch status.
+message QueryResponse {
+  map<string, string> metadata = 1;
+  bytes payload = 2;
+}
+
+message StagePlan {
+  int32 stageId = 1;
+  string instanceId = 2;
+  bytes serializedStageRoot = 3;
+  map<int32, StageMetadata> stageMetadata = 4;
+}
+
+message StageMetadata {
+  repeated string instances = 1;
+  repeated string dataSources = 2;
+  map<string, SegmentList> instanceToSegmentList = 3;
+}
+
+message SegmentList {
+  repeated string segments = 1;
+}
diff --git a/pinot-distribution/pom.xml b/pinot-distribution/pom.xml
index 353dee6d4c..ae464d2dca 100644
--- a/pinot-distribution/pom.xml
+++ b/pinot-distribution/pom.xml
@@ -64,6 +64,14 @@
       <groupId>org.apache.pinot</groupId>
       <artifactId>pinot-controller</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.pinot</groupId>
+      <artifactId>pinot-query-planner</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.pinot</groupId>
+      <artifactId>pinot-query-runtime</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.apache.pinot</groupId>
       <artifactId>pinot-tools</artifactId>
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/PlannerUtils.java
similarity index 63%
copy from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
copy to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/PlannerUtils.java
index 4b4ca9165e..43ab79d6bc 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/PlannerUtils.java
@@ -16,25 +16,17 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner;
 
-import org.apache.calcite.rel.logical.LogicalCalc;
-
-
-public class CalcNode extends AbstractStageNode {
-  private final String _expression;
-
-  public CalcNode(LogicalCalc node, String currentStageId) {
-    super(currentStageId);
-    _expression = toExpression(node);
-  }
-
-  public String getExpression() {
-    return _expression;
+/**
+ * Utilities used by planner.
+ */
+public class PlannerUtils {
+  private PlannerUtils() {
+    // do not instantiate.
   }
 
-  private String toExpression(LogicalCalc node) {
-    // TODO: make it real.
-    return node.getDigest();
+  public static boolean isRootStage(int stageId) {
+    return stageId == 0;
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java
index bdaba4ac70..cb075bb9f3 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java
@@ -34,10 +34,10 @@ import org.apache.pinot.query.planner.nodes.StageNode;
  * </ul>
  */
 public class QueryPlan {
-  private Map<String, StageNode> _queryStageMap;
-  private Map<String, StageMetadata> _stageMetadataMap;
+  private Map<Integer, StageNode> _queryStageMap;
+  private Map<Integer, StageMetadata> _stageMetadataMap;
 
-  public QueryPlan(Map<String, StageNode> queryStageMap, Map<String, StageMetadata> stageMetadataMap) {
+  public QueryPlan(Map<Integer, StageNode> queryStageMap, Map<Integer, StageMetadata> stageMetadataMap) {
     _queryStageMap = queryStageMap;
     _stageMetadataMap = stageMetadataMap;
   }
@@ -46,7 +46,7 @@ public class QueryPlan {
    * Get the map between stageID and the stage plan root node.
    * @return stage plan map.
    */
-  public Map<String, StageNode> getQueryStageMap() {
+  public Map<Integer, StageNode> getQueryStageMap() {
     return _queryStageMap;
   }
 
@@ -54,7 +54,7 @@ public class QueryPlan {
    * Get the stage metadata information.
    * @return stage metadata info.
    */
-  public Map<String, StageMetadata> getStageMetadataMap() {
+  public Map<Integer, StageMetadata> getStageMetadataMap() {
     return _stageMetadataMap;
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java
index d167521f20..e558694aab 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java
@@ -18,14 +18,26 @@
  */
 package org.apache.pinot.query.planner;
 
+import com.google.common.base.Preconditions;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
 import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.JoinRelType;
 import org.apache.calcite.rel.logical.LogicalCalc;
 import org.apache.calcite.rel.logical.LogicalJoin;
 import org.apache.calcite.rel.logical.LogicalTableScan;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.sql.SqlKind;
 import org.apache.pinot.query.planner.nodes.CalcNode;
 import org.apache.pinot.query.planner.nodes.JoinNode;
 import org.apache.pinot.query.planner.nodes.StageNode;
 import org.apache.pinot.query.planner.nodes.TableScanNode;
+import org.apache.pinot.query.planner.partitioning.FieldSelectionKeySelector;
+import org.apache.pinot.query.planner.partitioning.KeySelector;
 
 
 /**
@@ -45,7 +57,7 @@ public final class RelToStageConverter {
    * @param node relational node
    * @return stage node.
    */
-  public static StageNode toStageNode(RelNode node, String currentStageId) {
+  public static StageNode toStageNode(RelNode node, int currentStageId) {
     if (node instanceof LogicalCalc) {
       return convertLogicalCal((LogicalCalc) node, currentStageId);
     } else if (node instanceof LogicalTableScan) {
@@ -57,15 +69,34 @@ public final class RelToStageConverter {
     }
   }
 
-  private static StageNode convertLogicalTableScan(LogicalTableScan node, String currentStageId) {
-    return new TableScanNode(node, currentStageId);
+  private static StageNode convertLogicalTableScan(LogicalTableScan node, int currentStageId) {
+    String tableName = node.getTable().getQualifiedName().get(0);
+    List<String> columnNames = node.getRowType().getFieldList().stream()
+        .map(RelDataTypeField::getName).collect(Collectors.toList());
+    return new TableScanNode(currentStageId, tableName, columnNames);
   }
 
-  private static StageNode convertLogicalCal(LogicalCalc node, String currentStageId) {
-    return new CalcNode(node, currentStageId);
+  private static StageNode convertLogicalCal(LogicalCalc node, int currentStageId) {
+    // TODO: support actual calcNode
+    return new CalcNode(currentStageId, node.getDigest());
   }
 
-  private static StageNode convertLogicalJoin(LogicalJoin node, String currentStageId) {
-    return new JoinNode(node, currentStageId);
+  private static StageNode convertLogicalJoin(LogicalJoin node, int currentStageId) {
+    JoinRelType joinType = node.getJoinType();
+    RexCall joinCondition = (RexCall) node.getCondition();
+    Preconditions.checkState(
+        joinCondition.getOperator().getKind().equals(SqlKind.EQUALS) && joinCondition.getOperands().size() == 2,
+        "only equality JOIN is supported");
+    Preconditions.checkState(joinCondition.getOperands().get(0) instanceof RexInputRef, "only reference supported");
+    Preconditions.checkState(joinCondition.getOperands().get(1) instanceof RexInputRef, "only reference supported");
+    RelDataType leftRowType = node.getLeft().getRowType();
+    RelDataType rightRowType = node.getRight().getRowType();
+    int leftOperandIndex = ((RexInputRef) joinCondition.getOperands().get(0)).getIndex();
+    int rightOperandIndex = ((RexInputRef) joinCondition.getOperands().get(1)).getIndex();
+    KeySelector<Object[], Object> leftFieldSelectionKeySelector = new FieldSelectionKeySelector(leftOperandIndex);
+    KeySelector<Object[], Object> rightFieldSelectionKeySelector =
+          new FieldSelectionKeySelector(rightOperandIndex - leftRowType.getFieldNames().size());
+    return new JoinNode(currentStageId, joinType, Collections.singletonList(new JoinNode.JoinClause(
+        leftFieldSelectionKeySelector, rightFieldSelectionKeySelector)));
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java
index 91eb5b77ca..90dd22a8aa 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java
@@ -55,7 +55,7 @@ public class StageMetadata implements Serializable {
 
   public void attach(StageNode stageNode) {
     if (stageNode instanceof TableScanNode) {
-      _scannedTables.add(((TableScanNode) stageNode).getTableName().get(0));
+      _scannedTables.add(((TableScanNode) stageNode).getTableName());
     }
   }
 
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StagePlanner.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StagePlanner.java
index 175e77f6ef..f6ec38738c 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StagePlanner.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StagePlanner.java
@@ -41,8 +41,8 @@ public class StagePlanner {
   private final PlannerContext _plannerContext;
   private final WorkerManager _workerManager;
 
-  private Map<String, StageNode> _queryStageMap;
-  private Map<String, StageMetadata> _stageMetadataMap;
+  private Map<Integer, StageNode> _queryStageMap;
+  private Map<Integer, StageMetadata> _stageMetadataMap;
   private int _stageIdCounter;
 
   public StagePlanner(PlannerContext plannerContext, WorkerManager workerManager) {
@@ -60,7 +60,8 @@ public class StagePlanner {
     // clear the state
     _queryStageMap = new HashMap<>();
     _stageMetadataMap = new HashMap<>();
-    _stageIdCounter = 0;
+    // Stage ID starts with 1, 0 will be reserved for ROOT stage.
+    _stageIdCounter = 1;
 
     // walk the plan and create stages.
     StageNode globalStageRoot = walkRelPlan(relRoot, getNewStageId());
@@ -68,9 +69,10 @@ public class StagePlanner {
     // global root needs to send results back to the ROOT, a.k.a. the client response node.
     // the last stage is always a broadcast-gather.
     StageNode globalReceiverNode =
-        new MailboxReceiveNode("ROOT", globalStageRoot.getStageId(), RelDistribution.Type.BROADCAST_DISTRIBUTED);
-    StageNode globalSenderNode = new MailboxSendNode(globalStageRoot, globalReceiverNode.getStageId(),
+        new MailboxReceiveNode(0, globalStageRoot.getStageId(), RelDistribution.Type.BROADCAST_DISTRIBUTED);
+    StageNode globalSenderNode = new MailboxSendNode(globalStageRoot.getStageId(), globalReceiverNode.getStageId(),
         RelDistribution.Type.BROADCAST_DISTRIBUTED);
+    globalSenderNode.addInput(globalStageRoot);
     _queryStageMap.put(globalSenderNode.getStageId(), globalSenderNode);
     StageMetadata stageMetadata = _stageMetadataMap.get(globalSenderNode.getStageId());
     stageMetadata.attach(globalSenderNode);
@@ -81,7 +83,7 @@ public class StagePlanner {
     _stageMetadataMap.put(globalReceiverNode.getStageId(), globalReceivingStageMetadata);
 
     // assign workers to each stage.
-    for (Map.Entry<String, StageMetadata> e : _stageMetadataMap.entrySet()) {
+    for (Map.Entry<Integer, StageMetadata> e : _stageMetadataMap.entrySet()) {
       _workerManager.assignWorkerToStage(e.getKey(), e.getValue());
     }
 
@@ -89,7 +91,7 @@ public class StagePlanner {
   }
 
   // non-threadsafe
-  private StageNode walkRelPlan(RelNode node, String currentStageId) {
+  private StageNode walkRelPlan(RelNode node, int currentStageId) {
     if (isExchangeNode(node)) {
       // 1. exchangeNode always have only one input, get its input converted as a new stage root.
       StageNode nextStageRoot = walkRelPlan(node.getInput(0), getNewStageId());
@@ -97,7 +99,9 @@ public class StagePlanner {
 
       // 2. make an exchange sender and receiver node pair
       StageNode mailboxReceiver = new MailboxReceiveNode(currentStageId, nextStageRoot.getStageId(), exchangeType);
-      StageNode mailboxSender = new MailboxSendNode(nextStageRoot, mailboxReceiver.getStageId(), exchangeType);
+      StageNode mailboxSender = new MailboxSendNode(nextStageRoot.getStageId(), mailboxReceiver.getStageId(),
+          exchangeType);
+      mailboxSender.addInput(nextStageRoot);
 
       // 3. put the sender side as a completed stage.
       _queryStageMap.put(mailboxSender.getStageId(), mailboxSender);
@@ -120,7 +124,7 @@ public class StagePlanner {
     return (node instanceof LogicalExchange);
   }
 
-  private String getNewStageId() {
-    return String.valueOf(_stageIdCounter++);
+  private int getNewStageId() {
+    return _stageIdCounter++;
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
index 71701df762..b99075429a 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
@@ -24,10 +24,10 @@ import java.util.List;
 
 public abstract class AbstractStageNode implements StageNode {
 
-  protected final String _stageId;
+  protected final int _stageId;
   protected final List<StageNode> _inputs;
 
-  public AbstractStageNode(String stageId) {
+  public AbstractStageNode(int stageId) {
     _stageId = stageId;
     _inputs = new ArrayList<>();
   }
@@ -43,7 +43,7 @@ public abstract class AbstractStageNode implements StageNode {
   }
 
   @Override
-  public String getStageId() {
+  public int getStageId() {
     return _stageId;
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
index 4b4ca9165e..b188b8e2f7 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
@@ -18,23 +18,16 @@
  */
 package org.apache.pinot.query.planner.nodes;
 
-import org.apache.calcite.rel.logical.LogicalCalc;
-
 
 public class CalcNode extends AbstractStageNode {
   private final String _expression;
 
-  public CalcNode(LogicalCalc node, String currentStageId) {
-    super(currentStageId);
-    _expression = toExpression(node);
+  public CalcNode(int stageId, String expression) {
+    super(stageId);
+    _expression = expression;
   }
 
   public String getExpression() {
     return _expression;
   }
-
-  private String toExpression(LogicalCalc node) {
-    // TODO: make it real.
-    return node.getDigest();
-  }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java
index 520af693d6..94af122c74 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java
@@ -18,69 +18,46 @@
  */
 package org.apache.pinot.query.planner.nodes;
 
-import com.google.common.base.Preconditions;
+import java.io.Serializable;
+import java.util.List;
 import org.apache.calcite.rel.core.JoinRelType;
-import org.apache.calcite.rel.logical.LogicalJoin;
-import org.apache.calcite.rel.type.RelDataType;
-import org.apache.calcite.rex.RexCall;
-import org.apache.calcite.rex.RexInputRef;
-import org.apache.calcite.sql.SqlKind;
-import org.apache.pinot.query.planner.partitioning.FieldSelectionKeySelector;
+import org.apache.pinot.query.planner.partitioning.KeySelector;
 
 
 public class JoinNode extends AbstractStageNode {
-  private final JoinRelType _joinType;
-  private final int _leftOperandIndex;
-  private final int _rightOperandIndex;
-  private final FieldSelectionKeySelector _leftFieldSelectionKeySelector;
-  private final FieldSelectionKeySelector _rightFieldSelectionKeySelector;
+  private final JoinRelType _joinRelType;
+  private final List<JoinClause> _criteria;
 
-  private transient final RelDataType _leftRowType;
-  private transient final RelDataType _rightRowType;
-
-  public JoinNode(LogicalJoin node, String currentStageId) {
-    super(currentStageId);
-    _joinType = node.getJoinType();
-    RexCall joinCondition = (RexCall) node.getCondition();
-    Preconditions.checkState(
-        joinCondition.getOperator().getKind().equals(SqlKind.EQUALS) && joinCondition.getOperands().size() == 2,
-        "only equality JOIN is supported");
-    Preconditions.checkState(joinCondition.getOperands().get(0) instanceof RexInputRef, "only reference supported");
-    Preconditions.checkState(joinCondition.getOperands().get(1) instanceof RexInputRef, "only reference supported");
-    _leftRowType = node.getLeft().getRowType();
-    _rightRowType = node.getRight().getRowType();
-    _leftOperandIndex = ((RexInputRef) joinCondition.getOperands().get(0)).getIndex();
-    _rightOperandIndex = ((RexInputRef) joinCondition.getOperands().get(1)).getIndex();
-    _leftFieldSelectionKeySelector = new FieldSelectionKeySelector(_leftOperandIndex);
-    _rightFieldSelectionKeySelector =
-        new FieldSelectionKeySelector(_rightOperandIndex - _leftRowType.getFieldNames().size());
-  }
-
-  public JoinRelType getJoinType() {
-    return _joinType;
+  public JoinNode(int stageId, JoinRelType joinRelType, List<JoinClause> criteria
+  ) {
+    super(stageId);
+    _joinRelType = joinRelType;
+    _criteria = criteria;
   }
 
-  public RelDataType getLeftRowType() {
-    return _leftRowType;
+  public JoinRelType getJoinRelType() {
+    return _joinRelType;
   }
 
-  public RelDataType getRightRowType() {
-    return _rightRowType;
+  public List<JoinClause> getCriteria() {
+    return _criteria;
   }
 
-  public int getLeftOperandIndex() {
-    return _leftOperandIndex;
-  }
+  public static class JoinClause implements Serializable {
+    private final KeySelector<Object[], Object> _leftJoinKeySelector;
+    private final KeySelector<Object[], Object> _rightJoinKeySelector;
 
-  public int getRightOperandIndex() {
-    return _rightOperandIndex;
-  }
+    public JoinClause(KeySelector<Object[], Object> leftKeySelector, KeySelector<Object[], Object> rightKeySelector) {
+      _leftJoinKeySelector = leftKeySelector;
+      _rightJoinKeySelector = rightKeySelector;
+    }
 
-  public FieldSelectionKeySelector getLeftJoinKeySelector() {
-    return _leftFieldSelectionKeySelector;
-  }
+    public KeySelector<Object[], Object> getLeftJoinKeySelector() {
+      return _leftJoinKeySelector;
+    }
 
-  public FieldSelectionKeySelector getRightJoinKeySelector() {
-    return _rightFieldSelectionKeySelector;
+    public KeySelector<Object[], Object> getRightJoinKeySelector() {
+      return _rightJoinKeySelector;
+    }
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java
index 947d449383..d8269346f3 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java
@@ -18,33 +18,20 @@
  */
 package org.apache.pinot.query.planner.nodes;
 
-import java.util.Collections;
-import java.util.List;
 import org.apache.calcite.rel.RelDistribution;
 
 
 public class MailboxReceiveNode extends AbstractStageNode {
-
-  private final String _senderStageId;
+  private final int _senderStageId;
   private final RelDistribution.Type _exchangeType;
 
-  public MailboxReceiveNode(String stageId, String senderStageId, RelDistribution.Type exchangeType) {
+  public MailboxReceiveNode(int stageId, int senderStageId, RelDistribution.Type exchangeType) {
     super(stageId);
     _senderStageId = senderStageId;
     _exchangeType = exchangeType;
   }
 
-  @Override
-  public List<StageNode> getInputs() {
-    return Collections.emptyList();
-  }
-
-  @Override
-  public void addInput(StageNode stageNode) {
-    throw new UnsupportedOperationException("no input should be added to mailbox receive.");
-  }
-
-  public String getSenderStageId() {
+  public int getSenderStageId() {
     return _senderStageId;
   }
 
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java
index 6db9578f35..ea39ad3493 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java
@@ -18,34 +18,20 @@
  */
 package org.apache.pinot.query.planner.nodes;
 
-import java.util.Collections;
-import java.util.List;
 import org.apache.calcite.rel.RelDistribution;
 
 
 public class MailboxSendNode extends AbstractStageNode {
-  private final StageNode _stageRoot;
-  private final String _receiverStageId;
+  private final int _receiverStageId;
   private final RelDistribution.Type _exchangeType;
 
-  public MailboxSendNode(StageNode stageRoot, String receiverStageId, RelDistribution.Type exchangeType) {
-    super(stageRoot.getStageId());
-    _stageRoot = stageRoot;
+  public MailboxSendNode(int stageId, int receiverStageId, RelDistribution.Type exchangeType) {
+    super(stageId);
     _receiverStageId = receiverStageId;
     _exchangeType = exchangeType;
   }
 
-  @Override
-  public List<StageNode> getInputs() {
-    return Collections.singletonList(_stageRoot);
-  }
-
-  @Override
-  public void addInput(StageNode queryStageRoot) {
-    throw new UnsupportedOperationException("mailbox cannot be changed!");
-  }
-
-  public String getReceiverStageId() {
+  public int getReceiverStageId() {
     return _receiverStageId;
   }
 
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/StageNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/StageNode.java
index 8fcbb5e01a..cd34aca530 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/StageNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/StageNode.java
@@ -36,5 +36,5 @@ public interface StageNode extends Serializable {
 
   void addInput(StageNode stageNode);
 
-  String getStageId();
+  int getStageId();
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
index 0bb6e0f704..8d78ec6d0f 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
@@ -18,36 +18,20 @@
  */
 package org.apache.pinot.query.planner.nodes;
 
-import java.util.Collections;
 import java.util.List;
-import java.util.stream.Collectors;
-import org.apache.calcite.rel.logical.LogicalTableScan;
-import org.apache.calcite.rel.type.RelDataTypeField;
 
 
 public class TableScanNode extends AbstractStageNode {
-  private final List<String> _tableName;
+  private final String _tableName;
   private final List<String> _tableScanColumns;
 
-  public TableScanNode(LogicalTableScan tableScan, String stageId) {
+  public TableScanNode(int stageId, String tableName, List<String> tableScanColumns) {
     super(stageId);
-    _tableName = tableScan.getTable().getQualifiedName();
-    // TODO: optimize this, table field is not directly usable as name.
-    _tableScanColumns =
-        tableScan.getRowType().getFieldList().stream().map(RelDataTypeField::getName).collect(Collectors.toList());
+    _tableName = tableName;
+    _tableScanColumns = tableScanColumns;
   }
 
-  @Override
-  public List<StageNode> getInputs() {
-    return Collections.emptyList();
-  }
-
-  @Override
-  public void addInput(StageNode queryStageRoot) {
-    throw new UnsupportedOperationException("TableScanNode cannot add input as it is a leaf node");
-  }
-
-  public List<String> getTableName() {
+  public String getTableName() {
     return _tableName;
   }
 
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
index 9e0c776fd5..0b846e555c 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
@@ -18,13 +18,11 @@
  */
 package org.apache.pinot.query.planner.partitioning;
 
-import java.io.Serializable;
-
 
 /**
  * The {@code FieldSelectionKeySelector} simply extract a column value out from a row array {@link Object[]}.
  */
-public class FieldSelectionKeySelector implements KeySelector<Object[], Object>, Serializable {
+public class FieldSelectionKeySelector implements KeySelector<Object[], Object> {
 
   private int _columnIndex;
 
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
index 79dc987d9d..eaefb77604 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
@@ -18,6 +18,9 @@
  */
 package org.apache.pinot.query.planner.partitioning;
 
+import java.io.Serializable;
+
+
 /**
  * The {@code KeySelector} provides a partitioning function to encode a specific input data type into a key.
  *
@@ -25,7 +28,7 @@ package org.apache.pinot.query.planner.partitioning;
  *
  * <p>Key selector should always produce the same selection hash key when the same input is provided.
  */
-public interface KeySelector<IN, OUT> {
+public interface KeySelector<IN, OUT> extends Serializable {
 
   /**
    * Extract the key out of an input data construct.
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java
index a3102580b6..5b6ba6d804 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java
@@ -27,6 +27,7 @@ import java.util.Map;
 import org.apache.pinot.core.routing.RoutingManager;
 import org.apache.pinot.core.routing.RoutingTable;
 import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.planner.PlannerUtils;
 import org.apache.pinot.query.planner.StageMetadata;
 import org.apache.pinot.spi.config.table.TableType;
 import org.apache.pinot.spi.utils.CommonConstants;
@@ -56,14 +57,14 @@ public class WorkerManager {
     _routingManager = routingManager;
   }
 
-  public void assignWorkerToStage(String stageId, StageMetadata stageMetadata) {
+  public void assignWorkerToStage(int stageId, StageMetadata stageMetadata) {
     List<String> scannedTables = stageMetadata.getScannedTables();
     if (scannedTables.size() == 1) { // table scan stage, need to attach server as well as segment info.
       RoutingTable routingTable = getRoutingTable(scannedTables.get(0));
       Map<ServerInstance, List<String>> serverInstanceToSegmentsMap = routingTable.getServerInstanceToSegmentsMap();
       stageMetadata.setServerInstances(new ArrayList<>(serverInstanceToSegmentsMap.keySet()));
       stageMetadata.setServerInstanceToSegmentsMap(new HashMap<>(serverInstanceToSegmentsMap));
-    } else if (stageId.equalsIgnoreCase("ROOT")) {
+    } else if (PlannerUtils.isRootStage(stageId)) {
       // ROOT stage doesn't have a QueryServer as it is strictly only reducing results.
       // here we simply assign the worker instance with identical server/mailbox port number.
       stageMetadata.setServerInstances(Lists.newArrayList(new WorkerInstance(_hostName, _port, _port)));
diff --git a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
index 911469644b..60c7cd11af 100644
--- a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
+++ b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
@@ -35,6 +35,7 @@ import org.apache.pinot.core.routing.RoutingManager;
 import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.query.catalog.PinotCatalog;
 import org.apache.pinot.query.context.PlannerContext;
+import org.apache.pinot.query.planner.PlannerUtils;
 import org.apache.pinot.query.planner.QueryPlan;
 import org.apache.pinot.query.planner.StageMetadata;
 import org.apache.pinot.query.routing.WorkerManager;
@@ -72,7 +73,7 @@ public class QueryEnvironmentTest {
     QueryPlan queryPlan = _queryEnvironment.planQuery(query);
     Assert.assertEquals(queryPlan.getQueryStageMap().size(), 4);
     Assert.assertEquals(queryPlan.getStageMetadataMap().size(), 4);
-    for (Map.Entry<String, StageMetadata> e : queryPlan.getStageMetadataMap().entrySet()) {
+    for (Map.Entry<Integer, StageMetadata> e : queryPlan.getStageMetadataMap().entrySet()) {
       List<String> tables = e.getValue().getScannedTables();
       if (tables.size() == 1) {
         // table scan stages; for tableA it should have 2 hosts, for tableB it should have only 1
@@ -80,7 +81,7 @@ public class QueryEnvironmentTest {
             e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
             tables.get(0).equals("a") ? ImmutableList.of("Server_localhost_1", "Server_localhost_2")
                 : ImmutableList.of("Server_localhost_1"));
-      } else if (!e.getKey().equals("ROOT")) {
+      } else if (!PlannerUtils.isRootStage(e.getKey())) {
         // join stage should have both servers used.
         Assert.assertEquals(
             e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
diff --git a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTestUtils.java b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTestUtils.java
index cc0db385db..7585f9bd7c 100644
--- a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTestUtils.java
+++ b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTestUtils.java
@@ -112,11 +112,11 @@ public class QueryEnvironmentTestUtils {
     return mock;
   }
 
-  public static String getTestStageByServerCount(QueryPlan queryPlan, int serverCount) {
-    List<String> stageIds = queryPlan.getStageMetadataMap().entrySet().stream()
-        .filter(e -> !e.getKey().equals("ROOT") && e.getValue().getServerInstances().size() == serverCount)
+  public static int getTestStageByServerCount(QueryPlan queryPlan, int serverCount) {
+    List<Integer> stageIds = queryPlan.getStageMetadataMap().entrySet().stream()
+        .filter(e -> !e.getKey().equals(0) && e.getValue().getServerInstances().size() == serverCount)
         .map(Map.Entry::getKey).collect(Collectors.toList());
-    return stageIds.size() > 0 ? stageIds.get(0) : null;
+    return stageIds.size() > 0 ? stageIds.get(0) : -1;
   }
 
   public static int getAvailablePort() {
diff --git a/pinot-query-runtime/pom.xml b/pinot-query-runtime/pom.xml
new file mode 100644
index 0000000000..3607e51e31
--- /dev/null
+++ b/pinot-query-runtime/pom.xml
@@ -0,0 +1,83 @@
+<?xml version="1.0"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>pinot</artifactId>
+    <groupId>org.apache.pinot</groupId>
+    <version>0.10.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>pinot-query-runtime</artifactId>
+  <name>Pinot Query Runtime</name>
+  <url>https://pinot.apache.org/</url>
+
+  <properties>
+    <pinot.root>${basedir}/..</pinot.root>
+  </properties>
+
+  <dependencies>
+    <!-- Pinot dependencies -->
+    <dependency>
+      <groupId>org.apache.pinot</groupId>
+      <artifactId>pinot-core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.pinot</groupId>
+      <artifactId>pinot-common</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.pinot</groupId>
+      <artifactId>pinot-query-planner</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.testng</groupId>
+      <artifactId>testng</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.pinot</groupId>
+      <artifactId>pinot-common</artifactId>
+      <version>${project.version}</version>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.pinot</groupId>
+      <artifactId>pinot-query-planner</artifactId>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.pinot</groupId>
+      <artifactId>pinot-yammer</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/GrpcMailboxService.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/GrpcMailboxService.java
new file mode 100644
index 0000000000..30f0518a19
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/GrpcMailboxService.java
@@ -0,0 +1,101 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox;
+
+import io.grpc.ManagedChannel;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.pinot.common.proto.Mailbox.MailboxContent;
+import org.apache.pinot.query.mailbox.channel.ChannelManager;
+
+
+/**
+ * GRPC-based implementation of {@link MailboxService}.
+ *
+ * <p>It maintains a collection of connected mailbox servers and clients to remote hosts. All indexed by the
+ * mailboxID in the format of: <code>"jobId:partitionKey:senderHost:senderPort:receiverHost:receiverPort"</code>
+ *
+ * <p>Connections are established/initiated from the sender side and only tier-down from the sender side as well.
+ * In the event of exception or timed out, the connection is cloased based on a mutually agreed upon timeout period
+ * after the last successful message sent/received.
+ *
+ * <p>Noted that:
+ * <ul>
+ *   <li>the latter part of the mailboxID consist of the channelID.</li>
+ *   <li>the job_id should be uniquely identifying a send/receving pair, for example if one bundle job requires
+ *   to open 2 mailboxes, they should use {job_id}_1 and {job_id}_2 to distinguish the 2 different mailbox.</li>
+ * </ul>
+ */
+public class GrpcMailboxService implements MailboxService<MailboxContent> {
+  // channel manager
+  private final ChannelManager _channelManager;
+  private final String _hostname;
+  private final int _mailboxPort;
+
+  // maintaining a list of registered mailboxes.
+  private final ConcurrentHashMap<String, ReceivingMailbox<MailboxContent>> _receivingMailboxMap =
+      new ConcurrentHashMap<>();
+  private final ConcurrentHashMap<String, SendingMailbox<MailboxContent>> _sendingMailboxMap =
+      new ConcurrentHashMap<>();
+
+  public GrpcMailboxService(String hostname, int mailboxPort) {
+    _hostname = hostname;
+    _mailboxPort = mailboxPort;
+    _channelManager = new ChannelManager(this);
+  }
+
+  @Override
+  public void start() {
+    _channelManager.init();
+  }
+
+  @Override
+  public void shutdown() {
+    _channelManager.shutdown();
+  }
+
+  @Override
+  public String getHostname() {
+    return _hostname;
+  }
+
+  @Override
+  public int getMailboxPort() {
+    return _mailboxPort;
+  }
+
+  /**
+   * Register a mailbox, mailbox needs to be registered before use.
+   * @param mailboxId the id of the mailbox.
+   */
+  public SendingMailbox<MailboxContent> getSendingMailbox(String mailboxId) {
+    return _sendingMailboxMap.computeIfAbsent(mailboxId, (mId) -> new GrpcSendingMailbox(mId, this));
+  }
+
+  /**
+   * Register a mailbox, mailbox needs to be registered before use.
+   * @param mailboxId the id of the mailbox.
+   */
+  public ReceivingMailbox<MailboxContent> getReceivingMailbox(String mailboxId) {
+    return _receivingMailboxMap.computeIfAbsent(mailboxId, (mId) -> new GrpcReceivingMailbox(mId, this));
+  }
+
+  public ManagedChannel getChannel(String mailboxId) {
+    return _channelManager.getChannel(Utils.constructChannelId(mailboxId));
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/GrpcReceivingMailbox.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/GrpcReceivingMailbox.java
new file mode 100644
index 0000000000..2ce0e54a4e
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/GrpcReceivingMailbox.java
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.pinot.common.proto.Mailbox.MailboxContent;
+import org.apache.pinot.query.mailbox.channel.MailboxContentStreamObserver;
+
+
+/**
+ * GRPC implementation of the {@link ReceivingMailbox}.
+ */
+public class GrpcReceivingMailbox implements ReceivingMailbox<MailboxContent> {
+  private static final long DEFAULT_MAILBOX_INIT_TIMEOUT = 100L;
+  private final GrpcMailboxService _mailboxService;
+  private final String _mailboxId;
+  private final CountDownLatch _initializationLatch;
+  private final AtomicInteger _totalMsgReceived = new AtomicInteger(0);
+
+  private MailboxContentStreamObserver _contentStreamObserver;
+
+  public GrpcReceivingMailbox(String mailboxId, GrpcMailboxService mailboxService) {
+    _mailboxService = mailboxService;
+    _mailboxId = mailboxId;
+    _initializationLatch = new CountDownLatch(1);
+  }
+
+  public void init(MailboxContentStreamObserver streamObserver) {
+    if (_initializationLatch.getCount() > 0) {
+      _contentStreamObserver = streamObserver;
+      _initializationLatch.countDown();
+    }
+  }
+
+  @Override
+  public MailboxContent receive()
+      throws Exception {
+    MailboxContent mailboxContent = null;
+    if (waitForInitialize()) {
+      mailboxContent = _contentStreamObserver.poll();
+      _totalMsgReceived.incrementAndGet();
+    }
+    return mailboxContent;
+  }
+
+  @Override
+  public boolean isInitialized() {
+    return _initializationLatch.getCount() <= 0;
+  }
+
+  @Override
+  public boolean isClosed() {
+    return isInitialized() && _contentStreamObserver.isCompleted();
+  }
+
+  // TODO: fix busy wait. This should be guarded by timeout.
+  private boolean waitForInitialize()
+      throws Exception {
+    if (_initializationLatch.getCount() > 0) {
+      return _initializationLatch.await(DEFAULT_MAILBOX_INIT_TIMEOUT, TimeUnit.MILLISECONDS);
+    } else {
+      return true;
+    }
+  }
+
+  @Override
+  public String getMailboxId() {
+    return _mailboxId;
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/GrpcSendingMailbox.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/GrpcSendingMailbox.java
new file mode 100644
index 0000000000..2aff8a03e3
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/GrpcSendingMailbox.java
@@ -0,0 +1,74 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox;
+
+import io.grpc.ManagedChannel;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.pinot.common.proto.Mailbox.MailboxContent;
+import org.apache.pinot.common.proto.PinotMailboxGrpc;
+import org.apache.pinot.query.mailbox.channel.MailboxStatusStreamObserver;
+
+/**
+ * GRPC implementation of the {@link SendingMailbox}.
+ */
+public class GrpcSendingMailbox implements SendingMailbox<MailboxContent> {
+  private final GrpcMailboxService _mailboxService;
+  private final String _mailboxId;
+  private final AtomicBoolean _initialized = new AtomicBoolean(false);
+  private final AtomicInteger _totalMsgSent = new AtomicInteger(0);
+
+  private MailboxStatusStreamObserver _statusStreamObserver;
+
+  public GrpcSendingMailbox(String mailboxId, GrpcMailboxService mailboxService) {
+    _mailboxService = mailboxService;
+    _mailboxId = mailboxId;
+    _initialized.set(false);
+  }
+
+  public void init()
+      throws UnsupportedOperationException {
+    ManagedChannel channel = _mailboxService.getChannel(_mailboxId);
+    PinotMailboxGrpc.PinotMailboxStub stub = PinotMailboxGrpc.newStub(channel);
+    _statusStreamObserver = new MailboxStatusStreamObserver();
+    _statusStreamObserver.init(stub.open(_statusStreamObserver));
+    _initialized.set(true);
+  }
+
+  @Override
+  public void send(MailboxContent data)
+      throws UnsupportedOperationException {
+    if (!_initialized.get()) {
+      // initialization is special
+      init();
+    }
+    _statusStreamObserver.send(data);
+    _totalMsgSent.incrementAndGet();
+  }
+
+  @Override
+  public void complete() {
+    _statusStreamObserver.complete();
+  }
+
+  @Override
+  public String getMailboxId() {
+    return _mailboxId;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/MailboxIdentifier.java
similarity index 54%
copy from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
copy to pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/MailboxIdentifier.java
index 79dc987d9d..ee72494cdc 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/MailboxIdentifier.java
@@ -16,22 +16,42 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.partitioning;
+package org.apache.pinot.query.mailbox;
 
 /**
- * The {@code KeySelector} provides a partitioning function to encode a specific input data type into a key.
+ * {@link MailboxIdentifier} uniquely identify the mailbox that pairs a sender and a receiver.
  *
- * <p>This key selector is used for computation such as GROUP BY or equality JOINs.
- *
- * <p>Key selector should always produce the same selection hash key when the same input is provided.
+ * <p>It consists of the job_id and the partition key. as well as the component for a channelID.
  */
-public interface KeySelector<IN, OUT> {
+public interface MailboxIdentifier {
+
+  /**
+   * get the job identifier.
+   * @return job identifier.
+   */
+  String getJobId();
+
+  /**
+   * get the sender host.
+   * @return sender host
+   */
+  String getFromHost();
+
+  /**
+   * get the sender port.
+   * @return sender port
+   */
+  int getFromPort();
+
+  /**
+   * get the receiver host.
+   * @return receiver host
+   */
+  String getToHost();
 
   /**
-   * Extract the key out of an input data construct.
-   *
-   * @param input input data.
-   * @return the key of the input data.
+   * get the receiver port.
+   * @return receiver port
    */
-  OUT getKey(IN input);
+  int getToPort();
 }
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/MailboxService.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/MailboxService.java
new file mode 100644
index 0000000000..1943b12efb
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/MailboxService.java
@@ -0,0 +1,70 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox;
+
+/**
+ * Mailbox service that handles transfer for mailbox contents.
+ *
+ * @param <T> type of content supported by this mailbox service.
+ */
+public interface MailboxService<T> {
+
+  /**
+   * Starting the mailbox service.
+   */
+  void start();
+
+  /**
+   * Shutting down the mailbox service.s
+   */
+  void shutdown();
+
+  /**
+   * Get the host name on which this mailbox service is runnning on.
+   *
+   * @return the host.
+   */
+  String getHostname();
+
+  /**
+   * Get the host port that receives inbound mailbox message.
+   *
+   * @return the port.
+   */
+  int getMailboxPort();
+
+  /**
+   * Look up a receiving mailbox by {@link MailboxIdentifier}.
+   *
+   * <p>the acquired {@link ReceivingMailbox} will be constructed if not exist already, but it might not have been
+   * initialized.
+   *
+   * @param mailboxId mailbox identifier.
+   * @return a receiving mailbox.
+   */
+  ReceivingMailbox<T> getReceivingMailbox(String mailboxId);
+
+  /**
+   * Look up a sending mailbox by {@link MailboxIdentifier}.
+   *
+   * @param mailboxId mailbox identifier.
+   * @return a sending mailbox.
+   */
+  SendingMailbox<T> getSendingMailbox(String mailboxId);
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/ReceivingMailbox.java
similarity index 55%
copy from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
copy to pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/ReceivingMailbox.java
index 79dc987d9d..9e17d827f2 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/ReceivingMailbox.java
@@ -16,22 +16,41 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.partitioning;
+package org.apache.pinot.query.mailbox;
 
 /**
- * The {@code KeySelector} provides a partitioning function to encode a specific input data type into a key.
+ * Mailbox is used to send and receive data.
  *
- * <p>This key selector is used for computation such as GROUP BY or equality JOINs.
+ * Mailbox should be instantiated on both side of MailboxServer.
  *
- * <p>Key selector should always produce the same selection hash key when the same input is provided.
+ * @param <T> type of data carried over the mailbox.
  */
-public interface KeySelector<IN, OUT> {
+public interface ReceivingMailbox<T> {
 
   /**
-   * Extract the key out of an input data construct.
+   * get the unique identifier for the mailbox.
    *
-   * @param input input data.
-   * @return the key of the input data.
+   * @return Mailbox ID.
    */
-  OUT getKey(IN input);
+  String getMailboxId();
+
+  /**
+   * receive a data packet from the mailbox.
+   * @return data packet.
+   * @throws Exception
+   */
+  T receive()
+      throws Exception;
+
+  /**
+   * Check if receiving mailbox is initialized.
+   * @return
+   */
+  boolean isInitialized();
+
+  /**
+   * Check if mailbox is closed.
+   * @return
+   */
+  boolean isClosed();
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/SendingMailbox.java
similarity index 58%
copy from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
copy to pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/SendingMailbox.java
index 79dc987d9d..11cee29f0a 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/SendingMailbox.java
@@ -16,22 +16,34 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.partitioning;
+package org.apache.pinot.query.mailbox;
 
 /**
- * The {@code KeySelector} provides a partitioning function to encode a specific input data type into a key.
+ * Mailbox is used to send and receive data.
  *
- * <p>This key selector is used for computation such as GROUP BY or equality JOINs.
+ * Mailbox should be instantiated on both side of MailboxServer.
  *
- * <p>Key selector should always produce the same selection hash key when the same input is provided.
+ * @param <T> type of data carried over the mailbox.
  */
-public interface KeySelector<IN, OUT> {
+public interface SendingMailbox<T> {
 
   /**
-   * Extract the key out of an input data construct.
+   * get the unique identifier for the mailbox.
    *
-   * @param input input data.
-   * @return the key of the input data.
+   * @return Mailbox ID.
    */
-  OUT getKey(IN input);
+  String getMailboxId();
+
+  /**
+   * send a data packet through the mailbox.
+   * @param data
+   * @throws UnsupportedOperationException
+   */
+  void send(T data)
+      throws UnsupportedOperationException;
+
+  /**
+   * Complete delivery of the current mailbox.
+   */
+  void complete();
 }
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/StringMailboxIdentifier.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/StringMailboxIdentifier.java
new file mode 100644
index 0000000000..21712273e2
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/StringMailboxIdentifier.java
@@ -0,0 +1,100 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
+
+
+public class StringMailboxIdentifier implements MailboxIdentifier {
+  private static final Joiner JOINER = Joiner.on(':');
+
+  private final String _mailboxIdString;
+  private final String _jobId;
+  private final String _fromHost;
+  private final int _fromPort;
+  private final String _toHost;
+  private final int _toPort;
+
+  public StringMailboxIdentifier(String jobId, String fromHost, int fromPort, String toHost,
+      int toPort) {
+    _jobId = jobId;
+    _fromHost = fromHost;
+    _fromPort = fromPort;
+    _toHost = toHost;
+    _toPort = toPort;
+    _mailboxIdString = JOINER.join(_jobId, _fromHost, _fromPort, _toHost, _toPort);
+  }
+
+  public StringMailboxIdentifier(String mailboxId) {
+    _mailboxIdString = mailboxId;
+    String[] splits = mailboxId.split(":");
+    Preconditions.checkState(splits.length == 5);
+    _jobId = splits[0];
+    _fromHost = splits[1];
+    _fromPort = Integer.parseInt(splits[2]);
+    _toHost = splits[3];
+    _toPort = Integer.parseInt(splits[4]);
+
+    // assert that resulting string are identical.
+    Preconditions.checkState(
+        JOINER.join(_jobId, _fromHost, _fromPort, _toHost, _toPort).equals(_mailboxIdString));
+  }
+
+  @Override
+  public String getJobId() {
+    return _jobId;
+  }
+
+  @Override
+  public String getFromHost() {
+    return _fromHost;
+  }
+
+  @Override
+  public int getFromPort() {
+    return _fromPort;
+  }
+
+  @Override
+  public String getToHost() {
+    return _toHost;
+  }
+
+  @Override
+  public int getToPort() {
+    return _toPort;
+  }
+
+  @Override
+  public String toString() {
+    return _mailboxIdString;
+  }
+
+  @Override
+  public int hashCode() {
+    return _mailboxIdString.hashCode();
+  }
+
+  @Override
+  public boolean equals(Object that) {
+    return (that instanceof StringMailboxIdentifier) && _mailboxIdString.equals(
+        ((StringMailboxIdentifier) that)._mailboxIdString);
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/Utils.java
similarity index 55%
copy from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
copy to pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/Utils.java
index 71701df762..90a4070544 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/Utils.java
@@ -16,34 +16,28 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.mailbox;
 
-import java.util.ArrayList;
-import java.util.List;
+import com.google.common.base.Joiner;
 
 
-public abstract class AbstractStageNode implements StageNode {
+public final class Utils {
+  private static final Joiner JOINER = Joiner.on(':');
 
-  protected final String _stageId;
-  protected final List<StageNode> _inputs;
-
-  public AbstractStageNode(String stageId) {
-    _stageId = stageId;
-    _inputs = new ArrayList<>();
+  private Utils() {
+    // do not instantiate.
   }
 
-  @Override
-  public List<StageNode> getInputs() {
-    return _inputs;
+  public static String constructChannelId(String mailboxId) {
+    MailboxIdentifier mailboxIdentifier = toMailboxIdentifier(mailboxId);
+    return JOINER.join(mailboxIdentifier.getToHost(), mailboxIdentifier.getToPort());
   }
 
-  @Override
-  public void addInput(StageNode stageNode) {
-    _inputs.add(stageNode);
+  public static MailboxIdentifier toMailboxIdentifier(String mailboxId) {
+    return new StringMailboxIdentifier(mailboxId);
   }
 
-  @Override
-  public String getStageId() {
-    return _stageId;
+  public static String fromMailboxIdentifier(MailboxIdentifier mailboxId) {
+    return mailboxId.toString();
   }
 }
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/ChannelManager.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/ChannelManager.java
new file mode 100644
index 0000000000..54ff4bcfb7
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/ChannelManager.java
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox.channel;
+
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.pinot.query.mailbox.GrpcMailboxService;
+
+
+/**
+ * {@code ChannelManager} manages Grpc send/receive channels.
+ *
+ * <p>Grpc channels are managed centralized per Pinot component. Channels should be reused across different
+ * query/job/stages.
+ *
+ * <p>the channelId should be in the format of: <code>"senderHost:senderPort:receiverHost:receiverPort"</code>
+ */
+public class ChannelManager {
+  private static final int DEFAULT_MAX_INBOUND_MESSAGE_BYTES_SIZE = 128 * 1024 * 1024;
+
+  private final GrpcMailboxService _mailboxService;
+  private final GrpcMailboxServer _grpcMailboxServer;
+
+  private final ConcurrentHashMap<String, ManagedChannel> _channelMap = new ConcurrentHashMap<>();
+
+  public ChannelManager(GrpcMailboxService mailboxService) {
+    _mailboxService = mailboxService;
+    _grpcMailboxServer = new GrpcMailboxServer(_mailboxService, _mailboxService.getMailboxPort());
+  }
+
+  public void init() {
+    _grpcMailboxServer.start();
+  }
+
+  public void shutdown() {
+    _grpcMailboxServer.shutdown();
+  }
+
+  public ManagedChannel getChannel(String channelId) {
+    String[] channelParts = channelId.split(":");
+    return _channelMap.computeIfAbsent(channelId,
+        (id) -> ManagedChannelBuilder.forAddress(channelParts[0], Integer.parseInt(channelParts[1]))
+            .maxInboundMessageSize(DEFAULT_MAX_INBOUND_MESSAGE_BYTES_SIZE).usePlaintext().build());
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/GrpcMailboxServer.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/GrpcMailboxServer.java
new file mode 100644
index 0000000000..a260bbaeef
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/GrpcMailboxServer.java
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox.channel;
+
+import io.grpc.Server;
+import io.grpc.ServerBuilder;
+import io.grpc.stub.StreamObserver;
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+import org.apache.pinot.common.proto.Mailbox;
+import org.apache.pinot.common.proto.PinotMailboxGrpc;
+import org.apache.pinot.query.mailbox.GrpcMailboxService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * {@code GrpcMailboxServer} manages GRPC-based mailboxes by creating a stream-stream GRPC server.
+ *
+ * <p>This GRPC server is responsible for constructing {@link StreamObserver} out of an initial "open" request
+ * send by the sender of the sender/receiver pair.
+ */
+public class GrpcMailboxServer extends PinotMailboxGrpc.PinotMailboxImplBase {
+  private static final Logger LOGGER = LoggerFactory.getLogger(GrpcMailboxServer.class);
+  private static final long DEFAULT_GRPC_MAILBOX_SERVER_TIMEOUT = 10000L;
+
+  private final GrpcMailboxService _mailboxService;
+  private final Server _server;
+
+  public GrpcMailboxServer(GrpcMailboxService mailboxService, int port) {
+    _mailboxService = mailboxService;
+    _server = ServerBuilder.forPort(port).addService(this).build();
+    LOGGER.info("Initialized GrpcMailboxServer on port: {}", port);
+  }
+
+  public void start() {
+    try {
+      _server.start();
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  public void shutdown() {
+    try {
+      _server.shutdown().awaitTermination(DEFAULT_GRPC_MAILBOX_SERVER_TIMEOUT, TimeUnit.MILLISECONDS);
+    } catch (InterruptedException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Override
+  public StreamObserver<Mailbox.MailboxContent> open(StreamObserver<Mailbox.MailboxStatus> responseObserver) {
+    return new MailboxContentStreamObserver(_mailboxService, responseObserver);
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/MailboxContentStreamObserver.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/MailboxContentStreamObserver.java
new file mode 100644
index 0000000000..8aeb560c8f
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/MailboxContentStreamObserver.java
@@ -0,0 +1,114 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox.channel;
+
+import io.grpc.stub.StreamObserver;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.pinot.common.proto.Mailbox;
+import org.apache.pinot.query.mailbox.GrpcMailboxService;
+import org.apache.pinot.query.mailbox.GrpcReceivingMailbox;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * {@code MailboxContentStreamObserver} is the content streaming observer used to receive mailbox content.
+ *
+ * <p>When the observer onNext() is called (e.g. when data packet has arrived at the receiving end), it puts the
+ * mailbox content to the receiving mailbox buffer; response with the remaining buffer size of the receiving mailbox
+ * to the sender side.
+ */
+public class MailboxContentStreamObserver implements StreamObserver<Mailbox.MailboxContent> {
+  private static final Logger LOGGER = LoggerFactory.getLogger(MailboxContentStreamObserver.class);
+  private static final int DEFAULT_MAILBOX_QUEUE_CAPACITY = 5;
+  private static final long DEFAULT_MAILBOX_POLL_TIMEOUT = 1000L;
+  private final GrpcMailboxService _mailboxService;
+  private final StreamObserver<Mailbox.MailboxStatus> _responseObserver;
+  private final boolean _isEnabledFeedback;
+
+  private final AtomicBoolean _isCompleted = new AtomicBoolean(false);
+  private ArrayBlockingQueue<Mailbox.MailboxContent> _receivingBuffer;
+
+  public MailboxContentStreamObserver(GrpcMailboxService mailboxService,
+      StreamObserver<Mailbox.MailboxStatus> responseObserver) {
+    this(mailboxService, responseObserver, false);
+  }
+
+  public MailboxContentStreamObserver(GrpcMailboxService mailboxService,
+      StreamObserver<Mailbox.MailboxStatus> responseObserver, boolean isEnabledFeedback) {
+    _mailboxService = mailboxService;
+    _responseObserver = responseObserver;
+    _receivingBuffer = new ArrayBlockingQueue<>(DEFAULT_MAILBOX_QUEUE_CAPACITY);
+    _isEnabledFeedback = isEnabledFeedback;
+  }
+
+  public Mailbox.MailboxContent poll() {
+    while (!isCompleted()) {
+      try {
+        Mailbox.MailboxContent content = _receivingBuffer.poll(DEFAULT_MAILBOX_POLL_TIMEOUT, TimeUnit.MILLISECONDS);
+        if (content != null) {
+          return content;
+        }
+      } catch (InterruptedException e) {
+        LOGGER.error("Interrupt occurred while waiting for mailbox content", e);
+      }
+    }
+    return null;
+  }
+
+  public boolean isCompleted() {
+    return _isCompleted.get() && _receivingBuffer.isEmpty();
+  }
+
+  @Override
+  public void onNext(Mailbox.MailboxContent mailboxContent) {
+    GrpcReceivingMailbox receivingMailbox =
+        (GrpcReceivingMailbox) _mailboxService.getReceivingMailbox(mailboxContent.getMailboxId());
+    receivingMailbox.init(this);
+    // when the receiving end receives a message put it in the mailbox queue.
+    _receivingBuffer.offer(mailboxContent);
+    if (_isEnabledFeedback) {
+      // TODO: this has race conditions with onCompleted() because sender blindly closes connection channels once
+      // it has finished sending all the data packets.
+      int remainingCapacity = _receivingBuffer.remainingCapacity() - 1;
+      Mailbox.MailboxStatus.Builder builder =
+          Mailbox.MailboxStatus.newBuilder().setMailboxId(mailboxContent.getMailboxId())
+              .putMetadata("buffer.size", String.valueOf(remainingCapacity));
+      if (mailboxContent.getMetadataMap().get("finished") != null) {
+        builder.putMetadata("finished", "true");
+      }
+      Mailbox.MailboxStatus status = builder.build();
+      // returns the buffer available size to sender for rate controller / throttling.
+      _responseObserver.onNext(status);
+    }
+  }
+
+  @Override
+  public void onError(Throwable e) {
+    throw new RuntimeException(e);
+  }
+
+  @Override
+  public void onCompleted() {
+    _isCompleted.set(true);
+    _responseObserver.onCompleted();
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/MailboxStatusStreamObserver.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/MailboxStatusStreamObserver.java
new file mode 100644
index 0000000000..45233952e2
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/mailbox/channel/MailboxStatusStreamObserver.java
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox.channel;
+
+import io.grpc.stub.StreamObserver;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.pinot.common.proto.Mailbox;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * {@code MailboxStatusStreamObserver} is used by the SendingMailbox to send data over the wire.
+ *
+ * <p>Once {@link org.apache.pinot.query.mailbox.GrpcSendingMailbox#init()} is called, one instances of this class is
+ * created based on the opened GRPC connection returned {@link StreamObserver}. From this point, the sending mailbox
+ * can use the {@link MailboxStatusStreamObserver#send(Mailbox.MailboxContent)} API to send data packet to the receiving
+ * end.
+ */
+public class MailboxStatusStreamObserver implements StreamObserver<Mailbox.MailboxStatus> {
+  private static final Logger LOGGER = LoggerFactory.getLogger(MailboxStatusStreamObserver.class);
+  private static final int DEFAULT_MAILBOX_QUEUE_CAPACITY = 5;
+  private static final long DEFAULT_MAILBOX_POLL_TIMEOUT_MS = 1000L;
+  private final AtomicInteger _bufferSize = new AtomicInteger(5);
+  private final AtomicBoolean _isCompleted = new AtomicBoolean(false);
+
+  private StreamObserver<Mailbox.MailboxContent> _mailboxContentStreamObserver;
+
+  public MailboxStatusStreamObserver() {
+  }
+
+  public void init(StreamObserver<Mailbox.MailboxContent> mailboxContentStreamObserver) {
+    _mailboxContentStreamObserver = mailboxContentStreamObserver;
+  }
+
+  public void send(Mailbox.MailboxContent mailboxContent) {
+    _mailboxContentStreamObserver.onNext(mailboxContent);
+  }
+
+  public void complete() {
+    _mailboxContentStreamObserver.onCompleted();
+  }
+
+  @Override
+  public void onNext(Mailbox.MailboxStatus mailboxStatus) {
+    // when received a mailbox status from the receiving end, sending end update the known buffer size available
+    // so we can make better throughput send judgement. here is a simple example.
+    // TODO: this feedback info is not used to throttle the send speed. it is currently being discarded.
+    if (mailboxStatus.getMetadataMap().containsKey("buffer.size")) {
+      _bufferSize.set(Integer.parseInt(mailboxStatus.getMetadataMap().get("buffer.size")));
+    } else {
+      _bufferSize.set(DEFAULT_MAILBOX_QUEUE_CAPACITY); // DEFAULT_AVAILABILITY;
+    }
+  }
+
+  @Override
+  public void onError(Throwable e) {
+    _isCompleted.set(true);
+    shutdown();
+    throw new RuntimeException(e);
+  }
+
+  private void shutdown() {
+  }
+
+  @Override
+  public void onCompleted() {
+    _isCompleted.set(true);
+    shutdown();
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java
new file mode 100644
index 0000000000..2968ff9a23
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java
@@ -0,0 +1,118 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import org.apache.pinot.common.metrics.ServerMetrics;
+import org.apache.pinot.common.proto.Mailbox;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.core.data.manager.InstanceDataManager;
+import org.apache.pinot.core.query.executor.ServerQueryExecutorV1Impl;
+import org.apache.pinot.core.query.request.ServerQueryRequest;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.mailbox.GrpcMailboxService;
+import org.apache.pinot.query.mailbox.MailboxService;
+import org.apache.pinot.query.planner.StageMetadata;
+import org.apache.pinot.query.planner.nodes.MailboxSendNode;
+import org.apache.pinot.query.runtime.executor.WorkerQueryExecutor;
+import org.apache.pinot.query.runtime.operator.MailboxSendOperator;
+import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
+import org.apache.pinot.query.runtime.utils.ServerRequestUtils;
+import org.apache.pinot.query.service.QueryConfig;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import org.apache.pinot.spi.utils.CommonConstants;
+
+
+/**
+ * {@link QueryRunner} accepts a {@link DistributedStagePlan} and runs it.
+ */
+public class QueryRunner {
+  // This is a temporary before merging the 2 type of executor.
+  private ServerQueryExecutorV1Impl _serverExecutor;
+  private WorkerQueryExecutor _workerExecutor;
+  private MailboxService<Mailbox.MailboxContent> _mailboxService;
+  private String _hostname;
+  private int _port;
+
+  /**
+   * Initializes the query executor.
+   * <p>Should be called only once and before calling any other method.
+   */
+  public void init(PinotConfiguration config, InstanceDataManager instanceDataManager, ServerMetrics serverMetrics) {
+    String instanceName = config.getProperty(QueryConfig.KEY_OF_QUERY_RUNNER_HOSTNAME);
+    _hostname = instanceName.startsWith(CommonConstants.Helix.PREFIX_OF_SERVER_INSTANCE) ? instanceName.substring(
+        CommonConstants.Helix.SERVER_INSTANCE_PREFIX_LENGTH) : instanceName;
+    _port = config.getProperty(QueryConfig.KEY_OF_QUERY_RUNNER_PORT, QueryConfig.DEFAULT_QUERY_RUNNER_PORT);
+    try {
+      _mailboxService = new GrpcMailboxService(_hostname, _port);
+      _serverExecutor = new ServerQueryExecutorV1Impl();
+      _serverExecutor.init(config, instanceDataManager, serverMetrics);
+      _workerExecutor = new WorkerQueryExecutor();
+      _workerExecutor.init(config, serverMetrics, _mailboxService, _hostname, _port);
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  public void start() {
+    _mailboxService.start();
+    _serverExecutor.start();
+    _workerExecutor.start();
+  }
+
+  public void shutDown() {
+    _workerExecutor.shutDown();
+    _serverExecutor.shutDown();
+    _mailboxService.shutdown();
+  }
+
+  public void processQuery(DistributedStagePlan distributedStagePlan, ExecutorService executorService,
+      Map<String, String> requestMetadataMap) {
+    if (isLeafStage(distributedStagePlan)) {
+      // TODO: make server query request return via mailbox, this is a hack to gather the non-streaming data table
+      // and package it here for return. But we should really use a MailboxSendOperator directly put into the
+      // server executor.
+      ServerQueryRequest serverQueryRequest =
+          ServerRequestUtils.constructServerQueryRequest(distributedStagePlan, requestMetadataMap);
+
+      // send the data table via mailbox in one-off fashion (e.g. no block-level split, one data table/partition key)
+      DataTable dataTable = _serverExecutor.processQuery(serverQueryRequest, executorService, null);
+
+      MailboxSendNode sendNode = (MailboxSendNode) distributedStagePlan.getStageRoot();
+      StageMetadata receivingStageMetadata = distributedStagePlan.getMetadataMap().get(sendNode.getReceiverStageId());
+      MailboxSendOperator mailboxSendOperator =
+          new MailboxSendOperator(_mailboxService, dataTable, receivingStageMetadata.getServerInstances(),
+              sendNode.getExchangeType(), _hostname, _port, serverQueryRequest.getRequestId(),
+              sendNode.getStageId());
+      mailboxSendOperator.nextBlock();
+    } else {
+      _workerExecutor.processQuery(distributedStagePlan, requestMetadataMap, executorService);
+    }
+  }
+
+  private boolean isLeafStage(DistributedStagePlan distributedStagePlan) {
+    int stageId = distributedStagePlan.getStageId();
+    ServerInstance serverInstance = distributedStagePlan.getServerInstance();
+    StageMetadata stageMetadata = distributedStagePlan.getMetadataMap().get(stageId);
+    List<String> segments = stageMetadata.getServerInstanceToSegmentsMap().get(serverInstance);
+    return segments != null && segments.size() > 0;
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlock.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlock.java
new file mode 100644
index 0000000000..a8cc63c624
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlock.java
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import java.io.IOException;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.core.common.Block;
+import org.apache.pinot.core.common.BlockDocIdSet;
+import org.apache.pinot.core.common.BlockDocIdValueSet;
+import org.apache.pinot.core.common.BlockMetadata;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.operator.blocks.InstanceResponseBlock;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * A {@code DataTableBlock} is a row-based data block backed by a {@link DataTable}.
+ */
+public class DataTableBlock implements Block {
+  private static final Logger LOGGER = LoggerFactory.getLogger(InstanceResponseBlock.class);
+
+  private DataTable _dataTable;
+
+  public DataTableBlock(DataTable dataTable) {
+    _dataTable = dataTable;
+  }
+
+  public DataTable getDataTable() {
+    return _dataTable;
+  }
+
+  @Override
+  public BlockValSet getBlockValueSet() {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public BlockDocIdValueSet getBlockDocIdValueSet() {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public BlockDocIdSet getBlockDocIdSet() {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public BlockMetadata getMetadata() {
+    throw new UnsupportedOperationException();
+  }
+
+  public byte[] toBytes()
+      throws IOException {
+    return _dataTable.toBytes();
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlockUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlockUtils.java
new file mode 100644
index 0000000000..c429e2e014
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataTableBlockUtils.java
@@ -0,0 +1,71 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import org.apache.pinot.common.exception.QueryException;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.core.common.datatable.DataTableBuilder;
+
+
+public final class DataTableBlockUtils {
+  private DataTableBlockUtils() {
+    // do not instantiate.
+  }
+
+  // used to indicate a datatable block status
+  private static final DataSchema EMPTY_SCHEMA = new DataSchema(new String[0], new DataSchema.ColumnDataType[0]);
+  private static final DataTable EMPTY_DATATABLE = new DataTableBuilder(EMPTY_SCHEMA).build();
+  private static final DataTableBlock END_OF_STREAM_DATATABLE_BLOCK = new DataTableBlock(EMPTY_DATATABLE);
+
+  public static DataTableBlock getEndOfStreamDataTableBlock() {
+    return END_OF_STREAM_DATATABLE_BLOCK;
+  }
+
+  public static DataTable getEndOfStreamDataTable() {
+    return EMPTY_DATATABLE;
+  }
+
+  public static DataTable getErrorDataTable(Exception e) {
+    DataTable errorDataTable = new DataTableBuilder(EMPTY_SCHEMA).build();
+    errorDataTable.addException(QueryException.UNKNOWN_ERROR_CODE, e.getMessage());
+    return errorDataTable;
+  }
+
+  public static DataTableBlock getErrorDatatableBlock(Exception e) {
+    return new DataTableBlock(getErrorDataTable(e));
+  }
+
+  public static DataTable getEmptyDataTable(DataSchema dataSchema) {
+    if (dataSchema != null) {
+      return new DataTableBuilder(dataSchema).build();
+    } else {
+      return EMPTY_DATATABLE;
+    }
+  }
+
+  public static DataTableBlock getEmptyDataTableBlock(DataSchema dataSchema) {
+    return new DataTableBlock(getEmptyDataTable(dataSchema));
+  }
+
+  public static boolean isEndOfStream(DataTableBlock dataTableBlock) {
+    DataSchema dataSchema = dataTableBlock.getDataTable().getDataSchema();
+    return dataSchema.getColumnNames().length == 0 && dataSchema.getColumnDataTypes().length == 0;
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java
new file mode 100644
index 0000000000..85c0f108b4
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java
@@ -0,0 +1,122 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.executor;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import org.apache.calcite.rel.RelDistribution;
+import org.apache.pinot.common.metrics.ServerMetrics;
+import org.apache.pinot.common.proto.Mailbox;
+import org.apache.pinot.core.operator.BaseOperator;
+import org.apache.pinot.core.query.request.context.ThreadTimer;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.core.util.trace.TraceRunnable;
+import org.apache.pinot.query.mailbox.MailboxService;
+import org.apache.pinot.query.planner.StageMetadata;
+import org.apache.pinot.query.planner.nodes.JoinNode;
+import org.apache.pinot.query.planner.nodes.MailboxReceiveNode;
+import org.apache.pinot.query.planner.nodes.MailboxSendNode;
+import org.apache.pinot.query.planner.nodes.StageNode;
+import org.apache.pinot.query.runtime.blocks.DataTableBlock;
+import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+import org.apache.pinot.query.runtime.operator.BroadcastJoinOperator;
+import org.apache.pinot.query.runtime.operator.MailboxReceiveOperator;
+import org.apache.pinot.query.runtime.operator.MailboxSendOperator;
+import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * WorkerQueryExecutor is the v2 of the {@link org.apache.pinot.core.query.executor.QueryExecutor} API.
+ *
+ * It provides not only execution interface for {@link org.apache.pinot.core.query.request.ServerQueryRequest} but
+ * also a more general {@link DistributedStagePlan}.
+ */
+public class WorkerQueryExecutor {
+  private static final Logger LOGGER = LoggerFactory.getLogger(WorkerQueryExecutor.class);
+  private PinotConfiguration _config;
+  private ServerMetrics _serverMetrics;
+  private MailboxService<Mailbox.MailboxContent> _mailboxService;
+  private String _hostName;
+  private int _port;
+
+  public void init(PinotConfiguration config, ServerMetrics serverMetrics,
+      MailboxService<Mailbox.MailboxContent> mailboxService, String hostName, int port) {
+    _config = config;
+    _serverMetrics = serverMetrics;
+    _mailboxService = mailboxService;
+    _hostName = hostName;
+    _port = port;
+  }
+
+  public synchronized void start() {
+    LOGGER.info("Worker query executor started");
+  }
+
+  public synchronized void shutDown() {
+    LOGGER.info("Worker query executor shut down");
+  }
+
+  // TODO: split this execution from PhysicalPlanner
+  public void processQuery(DistributedStagePlan queryRequest, Map<String, String> requestMetadataMap,
+      ExecutorService executorService) {
+    long requestId = Long.parseLong(requestMetadataMap.get("REQUEST_ID"));
+    StageNode stageRoot = queryRequest.getStageRoot();
+    BaseOperator<DataTableBlock> rootOperator = getOperator(requestId, stageRoot, queryRequest.getMetadataMap());
+    executorService.submit(new TraceRunnable() {
+      @Override
+      public void runJob() {
+        ThreadTimer executionThreadTimer = new ThreadTimer();
+        while (!DataTableBlockUtils.isEndOfStream(rootOperator.nextBlock())) {
+          LOGGER.debug("Result Block acquired");
+        }
+        LOGGER.info("Execution time:" + executionThreadTimer.getThreadTimeNs());
+      }
+    });
+  }
+
+  // TODO: split this PhysicalPlanner into a separate module
+  private BaseOperator<DataTableBlock> getOperator(long requestId, StageNode stageNode,
+      Map<Integer, StageMetadata> metadataMap) {
+    // TODO: optimize this into a framework. (physical planner)
+    if (stageNode instanceof MailboxSendNode) {
+      MailboxSendNode sendNode = (MailboxSendNode) stageNode;
+      BaseOperator<DataTableBlock> nextOperator = getOperator(requestId, sendNode.getInputs().get(0), metadataMap);
+      StageMetadata receivingStageMetadata = metadataMap.get(sendNode.getReceiverStageId());
+      return new MailboxSendOperator(_mailboxService, nextOperator, receivingStageMetadata.getServerInstances(),
+          sendNode.getExchangeType(), _hostName, _port, requestId, sendNode.getStageId());
+    } else if (stageNode instanceof MailboxReceiveNode) {
+      MailboxReceiveNode receiveNode = (MailboxReceiveNode) stageNode;
+      List<ServerInstance> sendingInstances = metadataMap.get(receiveNode.getSenderStageId()).getServerInstances();
+      return new MailboxReceiveOperator(_mailboxService, RelDistribution.Type.ANY, sendingInstances, _hostName, _port,
+          requestId, receiveNode.getSenderStageId());
+    } else if (stageNode instanceof JoinNode) {
+      JoinNode joinNode = (JoinNode) stageNode;
+      BaseOperator<DataTableBlock> leftOperator = getOperator(requestId, joinNode.getInputs().get(0), metadataMap);
+      BaseOperator<DataTableBlock> rightOperator = getOperator(requestId, joinNode.getInputs().get(1), metadataMap);
+      return new BroadcastJoinOperator(leftOperator, rightOperator, joinNode.getCriteria());
+    } else {
+      throw new UnsupportedOperationException(
+          String.format("Stage node type %s is not supported!", stageNode.getClass().getSimpleName()));
+    }
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/BroadcastJoinOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/BroadcastJoinOperator.java
new file mode 100644
index 0000000000..db5bb5289d
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/BroadcastJoinOperator.java
@@ -0,0 +1,165 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.operator;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.core.operator.BaseOperator;
+import org.apache.pinot.core.query.selection.SelectionOperatorUtils;
+import org.apache.pinot.query.planner.nodes.JoinNode;
+import org.apache.pinot.query.planner.partitioning.KeySelector;
+import org.apache.pinot.query.runtime.blocks.DataTableBlock;
+import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+
+
+/**
+ * This basic {@code BroadcastJoinOperator} implement a basic broadcast join algorithm.
+ *
+ * <p>It takes the right table as the broadcast side and materialize a hash table. Then for each of the left table row,
+ * it looks up for the corresponding row(s) from the hash table and create a joint row.
+ *
+ * <p>For each of the data block received from the left table, it will generate a joint data block.
+ */
+public class BroadcastJoinOperator extends BaseOperator<DataTableBlock> {
+  private static final String OPERATOR_NAME = "BroadcastJoinOperator";
+  private static final String EXPLAIN_NAME = "BROADCAST_JOIN";
+
+  private final HashMap<Object, List<Object[]>> _broadcastHashTable;
+  private final BaseOperator<DataTableBlock> _leftTableOperator;
+  private final BaseOperator<DataTableBlock> _rightTableOperator;
+
+  private DataSchema _leftTableSchema;
+  private DataSchema _rightTableSchema;
+  private int _resultRowSize;
+  private boolean _isHashTableBuilt;
+  private KeySelector<Object[], Object> _leftKeySelector;
+  private KeySelector<Object[], Object> _rightKeySelector;
+
+  public BroadcastJoinOperator(BaseOperator<DataTableBlock> leftTableOperator,
+      BaseOperator<DataTableBlock> rightTableOperator, List<JoinNode.JoinClause> criteria) {
+    // TODO: this assumes right table is broadcast.
+    _leftKeySelector = criteria.get(0).getLeftJoinKeySelector();
+    _rightKeySelector = criteria.get(0).getRightJoinKeySelector();
+    _leftTableOperator = leftTableOperator;
+    _rightTableOperator = rightTableOperator;
+    _isHashTableBuilt = false;
+    _broadcastHashTable = new HashMap<>();
+  }
+
+  @Override
+  public String getOperatorName() {
+    return OPERATOR_NAME;
+  }
+
+  @Override
+  public List<Operator> getChildOperators() {
+    // WorkerExecutor doesn't use getChildOperators, returns null here.
+    return null;
+  }
+
+  @Nullable
+  @Override
+  public String toExplainString() {
+    return EXPLAIN_NAME;
+  }
+
+  @Override
+  protected DataTableBlock getNextBlock() {
+    buildBroadcastHashTable();
+    try {
+      return new DataTableBlock(buildJoinedDataTable(_leftTableOperator.nextBlock()));
+    } catch (Exception e) {
+      return DataTableBlockUtils.getErrorDatatableBlock(e);
+    }
+  }
+
+  private void buildBroadcastHashTable() {
+    if (!_isHashTableBuilt) {
+      DataTableBlock rightBlock = _rightTableOperator.nextBlock();
+      while (!DataTableBlockUtils.isEndOfStream(rightBlock)) {
+        DataTable dataTable = rightBlock.getDataTable();
+        _rightTableSchema = dataTable.getDataSchema();
+        int numRows = dataTable.getNumberOfRows();
+        // put all the rows into corresponding hash collections keyed by the key selector function.
+        for (int rowId = 0; rowId < numRows; rowId++) {
+          Object[] objects = SelectionOperatorUtils.extractRowFromDataTable(dataTable, rowId);
+          List<Object[]> hashCollection =
+              _broadcastHashTable.computeIfAbsent(_rightKeySelector.getKey(objects), k -> new ArrayList<>());
+          hashCollection.add(objects);
+        }
+        rightBlock = _rightTableOperator.nextBlock();
+      }
+      _isHashTableBuilt = true;
+    }
+  }
+
+  private DataTable buildJoinedDataTable(DataTableBlock block)
+      throws Exception {
+    if (DataTableBlockUtils.isEndOfStream(block)) {
+      return DataTableBlockUtils.getEndOfStreamDataTable();
+    }
+    List<Object[]> rows = new ArrayList<>();
+    DataTable dataTable = block.getDataTable();
+    _leftTableSchema = dataTable.getDataSchema();
+    _resultRowSize = _leftTableSchema.size() + _rightTableSchema.size();
+    int numRows = dataTable.getNumberOfRows();
+    for (int rowId = 0; rowId < numRows; rowId++) {
+      Object[] leftRow = SelectionOperatorUtils.extractRowFromDataTable(dataTable, rowId);
+      List<Object[]> hashCollection =
+          _broadcastHashTable.getOrDefault(_leftKeySelector.getKey(leftRow), Collections.emptyList());
+      for (Object[] rightRow : hashCollection) {
+        rows.add(joinRow(leftRow, rightRow));
+      }
+    }
+    return SelectionOperatorUtils.getDataTableFromRows(rows, computeSchema());
+  }
+
+  private Object[] joinRow(Object[] leftRow, Object[] rightRow) {
+    Object[] resultRow = new Object[_resultRowSize];
+    int idx = 0;
+    for (Object obj : leftRow) {
+      resultRow[idx++] = obj;
+    }
+    for (Object obj : rightRow) {
+      resultRow[idx++] = obj;
+    }
+    return resultRow;
+  }
+
+  private DataSchema computeSchema() {
+    String[] columnNames = new String[_resultRowSize];
+    DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[_resultRowSize];
+    int idx = 0;
+    for (int index = 0; index < _leftTableSchema.size(); index++) {
+      columnNames[idx] = _leftTableSchema.getColumnName(index);
+      columnDataTypes[idx++] = _leftTableSchema.getColumnDataType(index);
+    }
+    for (int index = 0; index < _rightTableSchema.size(); index++) {
+      columnNames[idx] = _rightTableSchema.getColumnName(index);
+      columnDataTypes[idx++] = _rightTableSchema.getColumnDataType(index);
+    }
+    return new DataSchema(columnNames, columnDataTypes);
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java
new file mode 100644
index 0000000000..d15a9fc1db
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java
@@ -0,0 +1,137 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.operator;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.calcite.rel.RelDistribution;
+import org.apache.pinot.common.proto.Mailbox;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.core.common.datatable.DataTableFactory;
+import org.apache.pinot.core.operator.BaseOperator;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.mailbox.MailboxService;
+import org.apache.pinot.query.mailbox.ReceivingMailbox;
+import org.apache.pinot.query.mailbox.StringMailboxIdentifier;
+import org.apache.pinot.query.runtime.blocks.DataTableBlock;
+import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This {@code MailboxReceiveOperator} receives data from a {@link ReceivingMailbox} and serve it out from the
+ * {@link BaseOperator#getNextBlock()} API.
+ */
+public class MailboxReceiveOperator extends BaseOperator<DataTableBlock> {
+  private static final Logger LOGGER = LoggerFactory.getLogger(MailboxReceiveOperator.class);
+  private static final String OPERATOR_NAME = "MailboxReceiveOperator";
+  private static final String EXPLAIN_NAME = "MAILBOX_RECEIVE";
+  private static final long DEFAULT_TIMEOUT_NANO = 10_000_000_000L;
+
+  private final MailboxService<Mailbox.MailboxContent> _mailboxService;
+  private final RelDistribution.Type _exchangeType;
+  private final List<ServerInstance> _sendingStageInstances;
+  private final String _hostName;
+  private final int _port;
+  private final long _jobId;
+  private final int _stageId;
+
+  public MailboxReceiveOperator(MailboxService<Mailbox.MailboxContent> mailboxService,
+      RelDistribution.Type exchangeType, List<ServerInstance> sendingStageInstances, String hostName, int port,
+      long jobId, int stageId) {
+    _mailboxService = mailboxService;
+    _exchangeType = exchangeType;
+    _sendingStageInstances = sendingStageInstances;
+    _hostName = hostName;
+    _port = port;
+    _jobId = jobId;
+    _stageId = stageId;
+  }
+
+  @Override
+  public String getOperatorName() {
+    return OPERATOR_NAME;
+  }
+
+  @Override
+  public List<Operator> getChildOperators() {
+    // WorkerExecutor doesn't use getChildOperators, returns null here.
+    return null;
+  }
+
+  @Nullable
+  @Override
+  public String toExplainString() {
+    return EXPLAIN_NAME;
+  }
+
+  @Override
+  protected DataTableBlock getNextBlock() {
+    // TODO: do a round robin check against all MailboxContentStreamObservers and find which one that has data.
+    boolean hasOpenedMailbox = true;
+    DataSchema dataSchema = null;
+    long timeoutWatermark = System.nanoTime() + DEFAULT_TIMEOUT_NANO;
+    while (hasOpenedMailbox && System.nanoTime() < timeoutWatermark) {
+      hasOpenedMailbox = false;
+      for (ServerInstance sendingInstance : _sendingStageInstances) {
+        try {
+          ReceivingMailbox<Mailbox.MailboxContent> receivingMailbox =
+              _mailboxService.getReceivingMailbox(toMailboxId(sendingInstance));
+          // TODO this is not threadsafe.
+          // make sure only one thread is checking receiving mailbox and calling receive() then close()
+          if (!receivingMailbox.isClosed()) {
+            hasOpenedMailbox = true;
+            Mailbox.MailboxContent mailboxContent = receivingMailbox.receive();
+            if (mailboxContent != null) {
+              ByteBuffer byteBuffer = mailboxContent.getPayload().asReadOnlyByteBuffer();
+              if (byteBuffer.hasRemaining()) {
+                DataTable dataTable = DataTableFactory.getDataTable(byteBuffer);
+                if (dataTable.getNumberOfRows() > 0) {
+                  // here we only return data table block when it is not empty.
+                  return new DataTableBlock(dataTable);
+                }
+              }
+            }
+          }
+        } catch (Exception e) {
+          LOGGER.error(String.format("Error receiving data from mailbox %s", sendingInstance), e);
+        }
+      }
+    }
+    if (System.nanoTime() >= timeoutWatermark) {
+      LOGGER.error("Timed out after polling mailboxes: {}", _sendingStageInstances);
+    }
+    // TODO: we need to at least return one data table with schema if there's no error.
+    // we need to condition this on whether there's already things being returned or not.
+    return DataTableBlockUtils.getEndOfStreamDataTableBlock();
+  }
+
+  public RelDistribution.Type getExchangeType() {
+    return _exchangeType;
+  }
+
+  private String toMailboxId(ServerInstance serverInstance) {
+    return new StringMailboxIdentifier(String.format("%s_%s", _jobId, _stageId), serverInstance.getHostname(),
+        serverInstance.getGrpcPort(), _hostName, _port).toString();
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
new file mode 100644
index 0000000000..3971dfb325
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
@@ -0,0 +1,182 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.operator;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+import com.google.protobuf.ByteString;
+import java.io.IOException;
+import java.util.List;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.calcite.rel.RelDistribution;
+import org.apache.pinot.common.proto.Mailbox;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.core.operator.BaseOperator;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.mailbox.MailboxService;
+import org.apache.pinot.query.mailbox.SendingMailbox;
+import org.apache.pinot.query.mailbox.StringMailboxIdentifier;
+import org.apache.pinot.query.runtime.blocks.DataTableBlock;
+import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This {@code MailboxSendOperator} is created to send {@link DataTableBlock}s to the receiving end.
+ */
+public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
+  private static final Logger LOGGER = LoggerFactory.getLogger(MailboxSendOperator.class);
+  private static final String OPERATOR_NAME = "MailboxSendOperator";
+  private static final String EXPLAIN_NAME = "MAILBOX_SEND";
+  private static final Set<RelDistribution.Type> SUPPORTED_EXCHANGE_TYPE =
+      ImmutableSet.of(RelDistribution.Type.SINGLETON, RelDistribution.Type.RANDOM_DISTRIBUTED,
+          RelDistribution.Type.BROADCAST_DISTRIBUTED);
+
+  private final List<ServerInstance> _receivingStageInstances;
+  private final RelDistribution.Type _exchangeType;
+  private final String _serverHostName;
+  private final int _serverPort;
+  private final long _jobId;
+  private final int _stageId;
+  private final MailboxService<Mailbox.MailboxContent> _mailboxService;
+  private BaseOperator<DataTableBlock> _dataTableBlockBaseOperator;
+  private DataTable _dataTable;
+
+  public MailboxSendOperator(MailboxService<Mailbox.MailboxContent> mailboxService,
+      BaseOperator<DataTableBlock> dataTableBlockBaseOperator, List<ServerInstance> receivingStageInstances,
+      RelDistribution.Type exchangeType, String hostName, int port, long jobId, int stageId) {
+    _mailboxService = mailboxService;
+    _dataTableBlockBaseOperator = dataTableBlockBaseOperator;
+    _receivingStageInstances = receivingStageInstances;
+    _exchangeType = exchangeType;
+    _serverHostName = hostName;
+    _serverPort = port;
+    _jobId = jobId;
+    _stageId = stageId;
+    Preconditions.checkState(SUPPORTED_EXCHANGE_TYPE.contains(_exchangeType),
+        String.format("Exchange type '%s' is not supported yet", _exchangeType));
+  }
+
+  /**
+   * This is a temporary interface for connecting with server API. remove/merge with InstanceResponseOperator once
+   * we create a {@link org.apache.pinot.core.query.executor.ServerQueryExecutorV1Impl} that can handle the
+   * creation of MailboxSendOperator we should not use this API.
+   */
+  public MailboxSendOperator(MailboxService<Mailbox.MailboxContent> mailboxService, DataTable dataTable,
+      List<ServerInstance> receivingStageInstances, RelDistribution.Type exchangeType, String hostName, int port,
+      long jobId, int stageId) {
+    _mailboxService = mailboxService;
+    _dataTable = dataTable;
+    _receivingStageInstances = receivingStageInstances;
+    _exchangeType = exchangeType;
+    _serverHostName = hostName;
+    _serverPort = port;
+    _jobId = jobId;
+    _stageId = stageId;
+  }
+
+  @Override
+  public String getOperatorName() {
+    return OPERATOR_NAME;
+  }
+
+  @Override
+  public List<Operator> getChildOperators() {
+    // WorkerExecutor doesn't use getChildOperators, returns null here.
+    return null;
+  }
+
+  @Nullable
+  @Override
+  public String toExplainString() {
+    return EXPLAIN_NAME;
+  }
+
+  @Override
+  protected DataTableBlock getNextBlock() {
+    DataTable dataTable;
+    DataTableBlock dataTableBlock = null;
+    if (_dataTableBlockBaseOperator != null) {
+      dataTableBlock = _dataTableBlockBaseOperator.nextBlock();
+      dataTable = dataTableBlock.getDataTable();
+    } else {
+      dataTable = _dataTable;
+    }
+    boolean isEndOfStream = dataTableBlock == null || DataTableBlockUtils.isEndOfStream(dataTableBlock);
+    try {
+      switch (_exchangeType) {
+        // TODO: random and singleton distribution should've been selected in planning phase.
+        case SINGLETON:
+        case RANDOM_DISTRIBUTED:
+          // TODO: make random distributed actually random, this impl only sends data to the first instances.
+          for (ServerInstance serverInstance : _receivingStageInstances) {
+            sendDataTableBlock(serverInstance, dataTable, isEndOfStream);
+            // we no longer need to send data to the rest of the receiving instances, but we still need to transfer
+            // the dataTable over indicating that we are a potential sender. thus next time a random server is selected
+            // it might still be useful.
+            dataTable = DataTableBlockUtils.getEmptyDataTable(dataTable.getDataSchema());
+          }
+          break;
+        case BROADCAST_DISTRIBUTED:
+          for (ServerInstance serverInstance : _receivingStageInstances) {
+            sendDataTableBlock(serverInstance, dataTable, isEndOfStream);
+          }
+          break;
+        case HASH_DISTRIBUTED:
+        case RANGE_DISTRIBUTED:
+        case ROUND_ROBIN_DISTRIBUTED:
+        case ANY:
+        default:
+          throw new UnsupportedOperationException("Unsupported mailbox exchange type: " + _exchangeType);
+      }
+    } catch (Exception e) {
+      LOGGER.error("Exception occur while sending data via mailbox", e);
+    }
+    return dataTableBlock;
+  }
+
+  private void sendDataTableBlock(ServerInstance serverInstance, DataTable dataTable, boolean isEndOfStream)
+      throws IOException {
+    String mailboxId = toMailboxId(serverInstance);
+    SendingMailbox<Mailbox.MailboxContent> sendingMailbox = _mailboxService.getSendingMailbox(mailboxId);
+    Mailbox.MailboxContent mailboxContent = toMailboxContent(mailboxId, dataTable, isEndOfStream);
+    sendingMailbox.send(mailboxContent);
+    if (mailboxContent.getMetadataMap().containsKey("finished")) {
+      sendingMailbox.complete();
+    }
+  }
+
+  private Mailbox.MailboxContent toMailboxContent(String mailboxId, DataTable dataTable, boolean isEndOfStream)
+      throws IOException {
+    Mailbox.MailboxContent.Builder builder = Mailbox.MailboxContent.newBuilder().setMailboxId(mailboxId)
+        .setPayload(ByteString.copyFrom(new DataTableBlock(dataTable).toBytes()));
+    if (isEndOfStream) {
+      builder.putMetadata("finished", "true");
+    }
+    return builder.build();
+  }
+
+  private String toMailboxId(ServerInstance serverInstance) {
+    return new StringMailboxIdentifier(String.format("%s_%s", _jobId, _stageId), _serverHostName, _serverPort,
+        serverInstance.getHostname(), serverInstance.getGrpcPort()).toString();
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/DistributedStagePlan.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/DistributedStagePlan.java
new file mode 100644
index 0000000000..f9ecf7f089
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/DistributedStagePlan.java
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.plan;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.planner.StageMetadata;
+import org.apache.pinot.query.planner.nodes.StageNode;
+
+
+/**
+ * {@code DistributedStagePlan} is the deserialized version of the
+ * {@link org.apache.pinot.common.proto.Worker.StagePlan}.
+ *
+ * <p>It is also the extended version of the {@link org.apache.pinot.core.query.request.ServerQueryRequest}.
+ */
+public class DistributedStagePlan {
+  private int _stageId;
+  private ServerInstance _serverInstance;
+  private StageNode _stageRoot;
+  private Map<Integer, StageMetadata> _metadataMap;
+
+  public DistributedStagePlan(int stageId) {
+    _stageId = stageId;
+    _metadataMap = new HashMap<>();
+  }
+
+  public DistributedStagePlan(int stageId, ServerInstance serverInstance, StageNode stageRoot,
+      Map<Integer, StageMetadata> metadataMap) {
+    _stageId = stageId;
+    _serverInstance = serverInstance;
+    _stageRoot = stageRoot;
+    _metadataMap = metadataMap;
+  }
+
+  public int getStageId() {
+    return _stageId;
+  }
+
+  public ServerInstance getServerInstance() {
+    return _serverInstance;
+  }
+
+  public StageNode getStageRoot() {
+    return _stageRoot;
+  }
+
+  public Map<Integer, StageMetadata> getMetadataMap() {
+    return _metadataMap;
+  }
+
+  public void setServerInstance(ServerInstance serverInstance) {
+    _serverInstance = serverInstance;
+  }
+
+  public void setStageRoot(StageNode stageRoot) {
+    _stageRoot = stageRoot;
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java
new file mode 100644
index 0000000000..1b4ecbbdc0
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java
@@ -0,0 +1,108 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.plan.serde;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.common.proto.Worker;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.planner.StageMetadata;
+import org.apache.pinot.query.routing.WorkerInstance;
+import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
+
+
+/**
+ * This utility class serialize/deserialize between {@link Worker.StagePlan} elements to Planner elements.
+ */
+public class QueryPlanSerDeUtils {
+
+  private QueryPlanSerDeUtils() {
+    // do not instantiate.
+  }
+
+  public static DistributedStagePlan deserialize(Worker.StagePlan stagePlan) {
+    DistributedStagePlan distributedStagePlan = new DistributedStagePlan(stagePlan.getStageId());
+    distributedStagePlan.setServerInstance(stringToInstance(stagePlan.getInstanceId()));
+    distributedStagePlan.setStageRoot(StageNodeSerDeUtils.deserializeStageRoot(stagePlan.getSerializedStageRoot()));
+    Map<Integer, Worker.StageMetadata> metadataMap = stagePlan.getStageMetadataMap();
+    distributedStagePlan.getMetadataMap().putAll(protoMapToStageMetadataMap(metadataMap));
+    return distributedStagePlan;
+  }
+
+  public static Worker.StagePlan serialize(DistributedStagePlan distributedStagePlan) {
+    return Worker.StagePlan.newBuilder()
+        .setStageId(distributedStagePlan.getStageId())
+        .setInstanceId(instanceToString(distributedStagePlan.getServerInstance()))
+        .setSerializedStageRoot(StageNodeSerDeUtils.serializeStageRoot(distributedStagePlan.getStageRoot()))
+        .putAllStageMetadata(stageMetadataMapToProtoMap(distributedStagePlan.getMetadataMap())).build();
+  }
+
+  public static ServerInstance stringToInstance(String serverInstanceString) {
+    String[] s = StringUtils.split(serverInstanceString, '_');
+    return new WorkerInstance(s[0], Integer.parseInt(s[1]), Integer.parseInt(s[2]));
+  }
+
+  public static String instanceToString(ServerInstance serverInstance) {
+    return StringUtils.join(serverInstance.getHostname(), '_', serverInstance.getPort(), '_',
+        serverInstance.getGrpcPort());
+  }
+
+  public static Map<Integer, StageMetadata> protoMapToStageMetadataMap(Map<Integer, Worker.StageMetadata> protoMap) {
+    Map<Integer, StageMetadata> metadataMap = new HashMap<>();
+    for (Map.Entry<Integer, Worker.StageMetadata> e : protoMap.entrySet()) {
+      metadataMap.put(e.getKey(), fromWorkerStageMetadata(e.getValue()));
+    }
+    return metadataMap;
+  }
+
+  private static StageMetadata fromWorkerStageMetadata(Worker.StageMetadata workerStageMetadata) {
+    StageMetadata stageMetadata = new StageMetadata();
+    stageMetadata.getScannedTables().addAll(workerStageMetadata.getDataSourcesList());
+    for (String serverInstanceString : workerStageMetadata.getInstancesList()) {
+      stageMetadata.getServerInstances().add(stringToInstance(serverInstanceString));
+    }
+    for (Map.Entry<String, Worker.SegmentList> e : workerStageMetadata.getInstanceToSegmentListMap().entrySet()) {
+      stageMetadata.getServerInstanceToSegmentsMap().put(stringToInstance(e.getKey()), e.getValue().getSegmentsList());
+    }
+    return stageMetadata;
+  }
+
+  public static Map<Integer, Worker.StageMetadata> stageMetadataMapToProtoMap(Map<Integer, StageMetadata> metadataMap) {
+    Map<Integer, Worker.StageMetadata> protoMap = new HashMap<>();
+    for (Map.Entry<Integer, StageMetadata> e : metadataMap.entrySet()) {
+      protoMap.put(e.getKey(), toWorkerStageMetadata(e.getValue()));
+    }
+    return protoMap;
+  }
+
+  private static Worker.StageMetadata toWorkerStageMetadata(StageMetadata stageMetadata) {
+    Worker.StageMetadata.Builder builder = Worker.StageMetadata.newBuilder();
+    builder.addAllDataSources(stageMetadata.getScannedTables());
+    for (ServerInstance serverInstance : stageMetadata.getServerInstances()) {
+      builder.addInstances(instanceToString(serverInstance));
+    }
+    for (Map.Entry<ServerInstance, List<String>> e : stageMetadata.getServerInstanceToSegmentsMap().entrySet()) {
+      builder.putInstanceToSegmentList(instanceToString(e.getKey()),
+          Worker.SegmentList.newBuilder().addAllSegments(e.getValue()).build());
+    }
+    return builder.build();
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/StageNodeSerDeUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/StageNodeSerDeUtils.java
new file mode 100644
index 0000000000..80370128cf
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/StageNodeSerDeUtils.java
@@ -0,0 +1,56 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.plan.serde;
+
+import com.google.common.base.Preconditions;
+import com.google.protobuf.ByteString;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import org.apache.pinot.query.planner.nodes.StageNode;
+
+
+public class StageNodeSerDeUtils {
+  private StageNodeSerDeUtils() {
+    // do not instantiate.
+  }
+
+  public static StageNode deserializeStageRoot(ByteString serializeStagePlan) {
+    try (ByteArrayInputStream bs = new ByteArrayInputStream(serializeStagePlan.toByteArray());
+        ObjectInputStream is = new ObjectInputStream(bs)) {
+      Object o = is.readObject();
+      Preconditions.checkState(o instanceof StageNode, "invalid worker query request object");
+      return (StageNode) o;
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  public static ByteString serializeStageRoot(StageNode stageRoot) {
+    try (ByteArrayOutputStream bs = new ByteArrayOutputStream();
+        ObjectOutputStream os = new ObjectOutputStream(bs)) {
+      os.writeObject(stageRoot);
+      return ByteString.copyFrom(bs.toByteArray());
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/utils/ServerRequestUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/utils/ServerRequestUtils.java
new file mode 100644
index 0000000000..306faa2598
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/utils/ServerRequestUtils.java
@@ -0,0 +1,109 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.utils;
+
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.pinot.common.metrics.PinotMetricUtils;
+import org.apache.pinot.common.metrics.ServerMetrics;
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.common.request.DataSource;
+import org.apache.pinot.common.request.InstanceRequest;
+import org.apache.pinot.common.request.PinotQuery;
+import org.apache.pinot.common.request.QuerySource;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import org.apache.pinot.core.query.request.ServerQueryRequest;
+import org.apache.pinot.query.planner.nodes.CalcNode;
+import org.apache.pinot.query.planner.nodes.MailboxReceiveNode;
+import org.apache.pinot.query.planner.nodes.MailboxSendNode;
+import org.apache.pinot.query.planner.nodes.StageNode;
+import org.apache.pinot.query.planner.nodes.TableScanNode;
+import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
+
+
+/**
+ * {@code ServerRequestUtils} converts the {@link DistributedStagePlan} into a {@link ServerQueryRequest}.
+ *
+ * <p>In order to reuse the current pinot {@link org.apache.pinot.core.query.executor.ServerQueryExecutorV1Impl}, a
+ * conversion step is needed so that the V2 query plan can be converted into a compatible format to run V1 executor.
+ */
+public class ServerRequestUtils {
+
+  private ServerRequestUtils() {
+    // do not instantiate.
+  }
+
+  // TODO: This is a hack, make an actual ServerQueryRequest converter.
+  public static ServerQueryRequest constructServerQueryRequest(DistributedStagePlan distributedStagePlan,
+      Map<String, String> requestMetadataMap) {
+    InstanceRequest instanceRequest = new InstanceRequest();
+    instanceRequest.setRequestId(Long.parseLong(requestMetadataMap.get("REQUEST_ID")));
+    instanceRequest.setBrokerId("unknown");
+    instanceRequest.setEnableTrace(false);
+    instanceRequest.setSearchSegments(
+        distributedStagePlan.getMetadataMap().get(distributedStagePlan.getStageId()).getServerInstanceToSegmentsMap()
+            .get(distributedStagePlan.getServerInstance()));
+    instanceRequest.setQuery(constructBrokerRequest(distributedStagePlan));
+    return new ServerQueryRequest(instanceRequest, new ServerMetrics(PinotMetricUtils.getPinotMetricsRegistry()),
+        System.currentTimeMillis());
+  }
+
+  // TODO: this is a hack, create a broker request object should not be needed because we rewrite the entire
+  // query into stages already.
+  public static BrokerRequest constructBrokerRequest(DistributedStagePlan distributedStagePlan) {
+    PinotQuery pinotQuery = constructPinotQuery(distributedStagePlan);
+    BrokerRequest brokerRequest = new BrokerRequest();
+    brokerRequest.setPinotQuery(pinotQuery);
+    // Set table name in broker request because it is used for access control, query routing etc.
+    DataSource dataSource = pinotQuery.getDataSource();
+    if (dataSource != null) {
+      QuerySource querySource = new QuerySource();
+      querySource.setTableName(dataSource.getTableName());
+      brokerRequest.setQuerySource(querySource);
+    }
+    return brokerRequest;
+  }
+
+  public static PinotQuery constructPinotQuery(DistributedStagePlan distributedStagePlan) {
+    PinotQuery pinotQuery = new PinotQuery();
+    pinotQuery.setExplain(false);
+    walkStageTree(distributedStagePlan.getStageRoot(), pinotQuery);
+    return pinotQuery;
+  }
+
+  private static void walkStageTree(StageNode node, PinotQuery pinotQuery) {
+    if (node instanceof CalcNode) {
+      // TODO: add conversion for CalcNode, specifically filter/alias/...
+    } else if (node instanceof TableScanNode) {
+      TableScanNode tableScanNode = (TableScanNode) node;
+      DataSource dataSource = new DataSource();
+      dataSource.setTableName(tableScanNode.getTableName());
+      pinotQuery.setDataSource(dataSource);
+      pinotQuery.setSelectList(tableScanNode.getTableScanColumns().stream().map(RequestUtils::getIdentifierExpression)
+          .collect(Collectors.toList()));
+    } else if (node instanceof MailboxSendNode || node instanceof MailboxReceiveNode) {
+      // ignore for now. continue to child.
+    } else {
+      throw new UnsupportedOperationException("Unsupported logical plan node: " + node);
+    }
+    for (StageNode child : node.getInputs()) {
+      walkStageTree(child, pinotQuery);
+    }
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryConfig.java
similarity index 55%
copy from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
copy to pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryConfig.java
index 9e0c776fd5..8de432f3ea 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryConfig.java
@@ -16,24 +16,22 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.partitioning;
-
-import java.io.Serializable;
-
+package org.apache.pinot.query.service;
 
 /**
- * The {@code FieldSelectionKeySelector} simply extract a column value out from a row array {@link Object[]}.
+ * Configuration for setting up query runtime.
  */
-public class FieldSelectionKeySelector implements KeySelector<Object[], Object>, Serializable {
+public class QueryConfig {
+  public static final String KEY_OF_QUERY_SERVER_PORT = "pinot.query.server.port";
+  public static final int DEFAULT_QUERY_SERVER_PORT = -1;
 
-  private int _columnIndex;
-
-  public FieldSelectionKeySelector(int columnIndex) {
-    _columnIndex = columnIndex;
-  }
+  public static final String KEY_OF_QUERY_RUNNER_HOSTNAME = "pinot.query.runner.hostname";
+  public static final String DEFAULT_QUERY_RUNNER_HOSTNAME = "localhost";
+  // query runner port is the mailbox port.
+  public static final String KEY_OF_QUERY_RUNNER_PORT = "pinot.query.runner.port";
+  public static final int DEFAULT_QUERY_RUNNER_PORT = -1;
 
-  @Override
-  public Object getKey(Object[] input) {
-    return input[_columnIndex];
+  private QueryConfig() {
+    // do not instantiate.
   }
 }
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java
new file mode 100644
index 0000000000..3200b317bb
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java
@@ -0,0 +1,143 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.service;
+
+import io.grpc.ManagedChannelBuilder;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.calcite.rel.RelDistribution;
+import org.apache.pinot.common.proto.Mailbox;
+import org.apache.pinot.common.proto.PinotQueryWorkerGrpc;
+import org.apache.pinot.common.proto.Worker;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.mailbox.MailboxService;
+import org.apache.pinot.query.planner.QueryPlan;
+import org.apache.pinot.query.planner.StageMetadata;
+import org.apache.pinot.query.planner.nodes.MailboxReceiveNode;
+import org.apache.pinot.query.runtime.blocks.DataTableBlock;
+import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+import org.apache.pinot.query.runtime.operator.MailboxReceiveOperator;
+import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
+import org.apache.pinot.query.runtime.plan.serde.QueryPlanSerDeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * {@code QueryDispatcher} dispatch a query to different workers.
+ */
+public class QueryDispatcher {
+  private static final Logger LOGGER = LoggerFactory.getLogger(QueryDispatcher.class);
+
+  private final Map<String, DispatchClient> _dispatchClientMap = new ConcurrentHashMap<>();
+
+  public QueryDispatcher() {
+  }
+
+  public List<DataTable> submitAndReduce(long requestId, QueryPlan queryPlan,
+      MailboxService<Mailbox.MailboxContent> mailboxService, long timeoutNano)
+      throws Exception {
+    // submit all the distributed stages.
+    int reduceStageId = submit(requestId, queryPlan);
+
+    // run reduce stage.
+    MailboxReceiveNode reduceNode = (MailboxReceiveNode) queryPlan.getQueryStageMap().get(reduceStageId);
+    MailboxReceiveOperator mailboxReceiveOperator = createReduceStageOperator(mailboxService,
+        queryPlan.getStageMetadataMap().get(reduceNode.getSenderStageId()).getServerInstances(),
+        requestId, reduceNode.getSenderStageId(), mailboxService.getHostname(),
+        mailboxService.getMailboxPort());
+
+    List<DataTable> queryResults = new ArrayList<>();
+    long timeoutWatermark = System.nanoTime() + timeoutNano;
+    while (System.nanoTime() < timeoutWatermark) {
+      DataTableBlock dataTableBlock = mailboxReceiveOperator.nextBlock();
+      queryResults.add(dataTableBlock.getDataTable());
+      if (DataTableBlockUtils.isEndOfStream(dataTableBlock)) {
+        break;
+      }
+    }
+    return queryResults;
+  }
+
+  public int submit(long requestId, QueryPlan queryPlan)
+      throws Exception {
+    int reduceStageId = -1;
+    for (Map.Entry<Integer, StageMetadata> stage : queryPlan.getStageMetadataMap().entrySet()) {
+      int stageId = stage.getKey();
+      // stage rooting at a mailbox receive node means reduce stage.
+      if (queryPlan.getQueryStageMap().get(stageId) instanceof MailboxReceiveNode) {
+        reduceStageId = stageId;
+      } else {
+        List<ServerInstance> serverInstances = stage.getValue().getServerInstances();
+        for (ServerInstance serverInstance : serverInstances) {
+          String host = serverInstance.getHostname();
+          int port = serverInstance.getPort();
+          DispatchClient client = getOrCreateDispatchClient(host, port);
+          Worker.QueryResponse response = client.submit(Worker.QueryRequest.newBuilder()
+              .setStagePlan(QueryPlanSerDeUtils.serialize(constructDistributedStagePlan(queryPlan, stageId,
+                  serverInstance)))
+              .putMetadata("REQUEST_ID", String.valueOf(requestId))
+              .putMetadata("SERVER_INSTANCE_HOST", serverInstance.getHostname())
+              .putMetadata("SERVER_INSTANCE_PORT", String.valueOf(serverInstance.getGrpcPort())).build());
+          if (response.containsMetadata("ERROR")) {
+            throw new RuntimeException(
+                String.format("Unable to execute query plan at stage %s on server %s: ERROR: %s", stageId,
+                    serverInstance, response));
+          }
+        }
+      }
+    }
+    return reduceStageId;
+  }
+
+  protected MailboxReceiveOperator createReduceStageOperator(MailboxService<Mailbox.MailboxContent> mailboxService,
+      List<ServerInstance> sendingInstances, long jobId, int stageId, String hostname, int port) {
+    MailboxReceiveOperator mailboxReceiveOperator =
+        new MailboxReceiveOperator(mailboxService, RelDistribution.Type.ANY, sendingInstances, hostname, port, jobId,
+            stageId);
+    return mailboxReceiveOperator;
+  }
+
+  public static DistributedStagePlan constructDistributedStagePlan(QueryPlan queryPlan, int stageId,
+      ServerInstance serverInstance) {
+    return new DistributedStagePlan(stageId, serverInstance, queryPlan.getQueryStageMap().get(stageId),
+        queryPlan.getStageMetadataMap());
+  }
+
+  private DispatchClient getOrCreateDispatchClient(String host, int port) {
+    String key = String.format("%s_%d", host, port);
+    return _dispatchClientMap.computeIfAbsent(key, k -> new DispatchClient(host, port));
+  }
+
+  public static class DispatchClient {
+    private final PinotQueryWorkerGrpc.PinotQueryWorkerBlockingStub _blockingStub;
+
+    public DispatchClient(String host, int port) {
+      ManagedChannelBuilder managedChannelBuilder = ManagedChannelBuilder.forAddress(host, port).usePlaintext();
+      _blockingStub = PinotQueryWorkerGrpc.newBlockingStub(managedChannelBuilder.build());
+    }
+
+    public Worker.QueryResponse submit(Worker.QueryRequest request) {
+      return _blockingStub.submit(request);
+    }
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryServer.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryServer.java
new file mode 100644
index 0000000000..0a74ee820b
--- /dev/null
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryServer.java
@@ -0,0 +1,115 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.service;
+
+import io.grpc.Context;
+import io.grpc.Server;
+import io.grpc.ServerBuilder;
+import io.grpc.Status;
+import io.grpc.stub.StreamObserver;
+import java.io.IOException;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import org.apache.pinot.common.proto.PinotQueryWorkerGrpc;
+import org.apache.pinot.common.proto.Worker;
+import org.apache.pinot.common.utils.NamedThreadFactory;
+import org.apache.pinot.core.query.scheduler.resources.ResourceManager;
+import org.apache.pinot.core.transport.grpc.GrpcQueryServer;
+import org.apache.pinot.query.runtime.QueryRunner;
+import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
+import org.apache.pinot.query.runtime.plan.serde.QueryPlanSerDeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * {@link QueryServer} is the GRPC server that accepts query plan requests sent from {@link QueryDispatcher}.
+ */
+public class QueryServer extends PinotQueryWorkerGrpc.PinotQueryWorkerImplBase {
+  private static final Logger LOGGER = LoggerFactory.getLogger(GrpcQueryServer.class);
+
+  private final Server _server;
+  private final QueryRunner _queryRunner;
+  private final ExecutorService _executorService;
+
+  public QueryServer(int port, QueryRunner queryRunner) {
+    _server = ServerBuilder.forPort(port).addService(this).build();
+    _executorService = Executors.newFixedThreadPool(ResourceManager.DEFAULT_QUERY_WORKER_THREADS,
+        new NamedThreadFactory("query_worker_on_" + port + "_port"));
+    _queryRunner = queryRunner;
+    LOGGER.info("Initialized QueryWorker on port: {} with numWorkerThreads: {}", port,
+        ResourceManager.DEFAULT_QUERY_WORKER_THREADS);
+  }
+
+  public void start() {
+    LOGGER.info("Starting QueryWorker");
+    try {
+      _queryRunner.start();
+      _server.start();
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  public void shutdown() {
+    LOGGER.info("Shutting down QueryWorker");
+    try {
+      _queryRunner.shutDown();
+      _server.shutdown().awaitTermination();
+    } catch (InterruptedException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Override
+  public void submit(Worker.QueryRequest request, StreamObserver<Worker.QueryResponse> responseObserver) {
+    // Deserialize the request
+    DistributedStagePlan distributedStagePlan;
+    Map<String, String> requestMetadataMap;
+    try {
+      distributedStagePlan = QueryPlanSerDeUtils.deserialize(request.getStagePlan());
+      requestMetadataMap = request.getMetadataMap();
+    } catch (Exception e) {
+      LOGGER.error("Caught exception while deserializing the request: {}", request, e);
+      responseObserver.onError(Status.INVALID_ARGUMENT.withDescription("Bad request").withCause(e).asException());
+      return;
+    }
+
+    // return dispatch successful.
+    // TODO: return meaningful value here.
+    responseObserver.onNext(Worker.QueryResponse.newBuilder().putMetadata("OK", "OK").build());
+    responseObserver.onCompleted();
+
+    // start a new GRPC ctx has all the values as the current context, but won't be cancelled
+    Context ctx = Context.current().fork();
+    // Set ctx as the current context within the Runnable can start asynchronous work here that will not
+    // be cancelled when submit returns
+    ctx.run(() -> {
+      // Process the query
+      try {
+        // TODO: break this into parsing and execution, so that responseObserver can return upon parsing complete.
+        _queryRunner.processQuery(distributedStagePlan, _executorService, requestMetadataMap);
+      } catch (Exception e) {
+        LOGGER.error("Caught exception while processing request", e);
+        throw new RuntimeException(e);
+      }
+    });
+  }
+}
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/QueryServerEnclosure.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/QueryServerEnclosure.java
new file mode 100644
index 0000000000..0e60a4a533
--- /dev/null
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/QueryServerEnclosure.java
@@ -0,0 +1,192 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.stream.Collectors;
+import org.apache.commons.io.FileUtils;
+import org.apache.pinot.common.metrics.ServerMetrics;
+import org.apache.pinot.common.utils.NamedThreadFactory;
+import org.apache.pinot.core.data.manager.InstanceDataManager;
+import org.apache.pinot.core.data.manager.offline.ImmutableSegmentDataManager;
+import org.apache.pinot.query.runtime.QueryRunner;
+import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
+import org.apache.pinot.query.service.QueryConfig;
+import org.apache.pinot.segment.local.data.manager.SegmentDataManager;
+import org.apache.pinot.segment.local.data.manager.TableDataManager;
+import org.apache.pinot.segment.local.indexsegment.immutable.ImmutableSegmentLoader;
+import org.apache.pinot.segment.local.segment.creator.impl.SegmentIndexCreationDriverImpl;
+import org.apache.pinot.segment.local.segment.readers.GenericRowRecordReader;
+import org.apache.pinot.segment.spi.ImmutableSegment;
+import org.apache.pinot.segment.spi.creator.SegmentGeneratorConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.data.readers.RecordReader;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import org.apache.pinot.spi.utils.ReadMode;
+import org.apache.pinot.spi.utils.builder.TableConfigBuilder;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.matches;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+
+/**
+ * Query server enclosure for testing Pinot query planner & runtime.
+ *
+ * This enclosure simulates a deployable component of Pinot that serves
+ *   - regular Pinot query server (that serves segment-based queries)
+ *   - intermediate stage queries (such as JOIN operator that awaits data scanned from left/right tables)
+ *
+ * Inside this construct it runs a regular pinot QueryExecutor as well as the new runtime - WorkerExecutor
+ * Depending on the query request type it gets routed to either one of the two for execution.
+ *
+ * It also runs a GRPC Mailbox service that runs the new transport layer protocol as the backbone for all
+ * multi-stage query communication.
+ */
+public class QueryServerEnclosure {
+  private static final int NUM_ROWS = 5;
+  private static final int DEFAULT_EXECUTOR_THREAD_NUM = 5;
+  private static final String[] STRING_FIELD_LIST = new String[]{"foo", "bar", "alice", "bob", "charlie"};
+  private static final int[] INT_FIELD_LIST = new int[]{1, 2, 42};
+
+  private final ExecutorService _testExecutor;
+  private final int _queryRunnerPort;
+  private final Map<String, Object> _runnerConfig = new HashMap<>();
+  private final Map<String, List<ImmutableSegment>> _segmentMap = new HashMap<>();
+  private final InstanceDataManager _instanceDataManager;
+  private final Map<String, TableDataManager> _tableDataManagers = new HashMap<>();
+  private final Map<String, File> _indexDirs;
+
+  private QueryRunner _queryRunner;
+
+  public QueryServerEnclosure(List<String> tables, Map<String, File> indexDirs, Map<String, List<String>> segments) {
+    _indexDirs = indexDirs;
+    try {
+      for (int i = 0; i < tables.size(); i++) {
+        String tableName = tables.get(i);
+        File indexDir = indexDirs.get(tableName);
+        FileUtils.deleteQuietly(indexDir);
+        List<ImmutableSegment> segmentList = new ArrayList<>();
+        for (String segmentName : segments.get(tableName)) {
+          segmentList.add(buildSegment(indexDir, tableName, segmentName));
+        }
+        _segmentMap.put(tableName, segmentList);
+      }
+      _instanceDataManager = mockInstanceDataManager();
+      _queryRunnerPort = QueryEnvironmentTestUtils.getAvailablePort();
+      _runnerConfig.put(QueryConfig.KEY_OF_QUERY_RUNNER_PORT, _queryRunnerPort);
+      _runnerConfig.put(QueryConfig.KEY_OF_QUERY_RUNNER_HOSTNAME,
+          String.format("Server_%s", QueryConfig.DEFAULT_QUERY_RUNNER_HOSTNAME));
+      _queryRunner = new QueryRunner();
+      _testExecutor = Executors.newFixedThreadPool(DEFAULT_EXECUTOR_THREAD_NUM,
+          new NamedThreadFactory("test_query_server_enclosure_on_" + _queryRunnerPort + "_port"));
+    } catch (Exception e) {
+      throw new RuntimeException("Test Failed!", e);
+    }
+  }
+
+  public ServerMetrics mockServiceMetrics() {
+    return mock(ServerMetrics.class);
+  }
+
+  public InstanceDataManager mockInstanceDataManager() {
+    InstanceDataManager instanceDataManager = mock(InstanceDataManager.class);
+    for (Map.Entry<String, List<ImmutableSegment>> e : _segmentMap.entrySet()) {
+      TableDataManager tableDataManager = mockTableDataManager(e.getValue());
+      _tableDataManagers.put(e.getKey(), tableDataManager);
+    }
+    for (Map.Entry<String, TableDataManager> e : _tableDataManagers.entrySet()) {
+      when(instanceDataManager.getTableDataManager(matches(String.format("%s.*", e.getKey())))).thenReturn(
+          e.getValue());
+    }
+    return instanceDataManager;
+  }
+
+  public TableDataManager mockTableDataManager(List<ImmutableSegment> segmentList) {
+    List<SegmentDataManager> tableSegmentDataManagers =
+        segmentList.stream().map(ImmutableSegmentDataManager::new).collect(Collectors.toList());
+    TableDataManager tableDataManager = mock(TableDataManager.class);
+    when(tableDataManager.acquireSegments(any(), any())).thenReturn(tableSegmentDataManagers);
+    return tableDataManager;
+  }
+
+  public ImmutableSegment buildSegment(File indexDir, String tableName, String segmentName)
+      throws Exception {
+    List<GenericRow> rows = new ArrayList<>(NUM_ROWS);
+    for (int i = 0; i < NUM_ROWS; i++) {
+      GenericRow row = new GenericRow();
+      row.putValue("col1", STRING_FIELD_LIST[i % STRING_FIELD_LIST.length]);
+      row.putValue("col2", STRING_FIELD_LIST[(i + 2) % STRING_FIELD_LIST.length]);
+      row.putValue("col3", INT_FIELD_LIST[i % INT_FIELD_LIST.length]);
+      row.putValue("ts", System.currentTimeMillis());
+      rows.add(row);
+    }
+
+    TableConfig tableConfig =
+        new TableConfigBuilder(TableType.OFFLINE).setTableName(tableName).setTimeColumnName("ts").build();
+    Schema schema = QueryEnvironmentTestUtils.SCHEMA_BUILDER.setSchemaName(tableName).build();
+    SegmentGeneratorConfig config = new SegmentGeneratorConfig(tableConfig, schema);
+    config.setOutDir(indexDir.getPath());
+    config.setTableName(tableName);
+    config.setSegmentName(segmentName);
+
+    SegmentIndexCreationDriverImpl driver = new SegmentIndexCreationDriverImpl();
+    try (RecordReader recordReader = new GenericRowRecordReader(rows)) {
+      driver.init(config, recordReader);
+    }
+    driver.build();
+    return ImmutableSegmentLoader.load(new File(indexDir, segmentName), ReadMode.mmap);
+  }
+
+  public int getPort() {
+    return _queryRunnerPort;
+  }
+
+  public void start()
+      throws Exception {
+    PinotConfiguration configuration = new PinotConfiguration(_runnerConfig);
+    _queryRunner = new QueryRunner();
+    _queryRunner.init(configuration, _instanceDataManager, mockServiceMetrics());
+    _queryRunner.start();
+  }
+
+  public void shutDown() {
+    _queryRunner.shutDown();
+    for (Map.Entry<String, List<ImmutableSegment>> e : _segmentMap.entrySet()) {
+      for (ImmutableSegment segment : e.getValue()) {
+        segment.destroy();
+      }
+      FileUtils.deleteQuietly(_indexDirs.get(e.getKey()));
+    }
+  }
+
+  public void processQuery(DistributedStagePlan distributedStagePlan, Map<String, String> requestMetadataMap) {
+    _queryRunner.processQuery(distributedStagePlan, _testExecutor, requestMetadataMap);
+  }
+}
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcMailboxServiceTest.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcMailboxServiceTest.java
new file mode 100644
index 0000000000..f1863bc339
--- /dev/null
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcMailboxServiceTest.java
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
+import com.google.protobuf.ByteString;
+import java.io.IOException;
+import java.util.Map;
+import org.apache.pinot.common.proto.Mailbox;
+import org.apache.pinot.core.common.datatable.DataTableBuilder;
+import org.apache.pinot.query.runtime.blocks.DataTableBlock;
+import org.apache.pinot.util.TestUtils;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class GrpcMailboxServiceTest extends GrpcMailboxServiceTestBase {
+
+  @Test
+  public void testHappyPath()
+      throws Exception {
+    Preconditions.checkState(_mailboxServices.size() >= 2);
+    Map.Entry<Integer, GrpcMailboxService> sender = _mailboxServices.firstEntry();
+    Map.Entry<Integer, GrpcMailboxService> receiver = _mailboxServices.lastEntry();
+    String mailboxId =
+        String.format("happyPath:localhost:%d:localhost:%d", sender.getKey(), receiver.getKey());
+    SendingMailbox<Mailbox.MailboxContent> sendingMailbox = sender.getValue().getSendingMailbox(mailboxId);
+    ReceivingMailbox<Mailbox.MailboxContent> receivingMailbox = receiver.getValue().getReceivingMailbox(mailboxId);
+
+    // create mock object
+    Mailbox.MailboxContent testContent = getTestMailboxContent(mailboxId);
+    sendingMailbox.send(testContent);
+
+    // wait for receiving mailbox to be created.
+    TestUtils.waitForCondition(aVoid -> {
+      return receivingMailbox.isInitialized();
+    }, 5000L, "Receiving mailbox initialize failed!");
+
+    Mailbox.MailboxContent receivedContent = receivingMailbox.receive();
+    Assert.assertEquals(receivedContent, testContent);
+
+    sendingMailbox.complete();
+
+    TestUtils.waitForCondition(aVoid -> {
+      return receivingMailbox.isClosed();
+    }, 5000L, "Receiving mailbox is not closed properly!");
+  }
+
+  private Mailbox.MailboxContent getTestMailboxContent(String mailboxId)
+      throws IOException {
+    return Mailbox.MailboxContent.newBuilder().setMailboxId(mailboxId)
+        .putAllMetadata(ImmutableMap.of("key", "value", "finished", "true"))
+        .setPayload(ByteString.copyFrom(new DataTableBlock(DataTableBuilder.getEmptyDataTable()).toBytes())).build();
+  }
+}
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcMailboxServiceTestBase.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcMailboxServiceTestBase.java
new file mode 100644
index 0000000000..8a5badd36a
--- /dev/null
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/mailbox/GrpcMailboxServiceTestBase.java
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.mailbox;
+
+import java.util.TreeMap;
+import org.apache.pinot.query.QueryEnvironmentTestUtils;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+
+
+public abstract class GrpcMailboxServiceTestBase {
+  protected static final int MAILBOX_TEST_SIZE = 2;
+  protected TreeMap<Integer, GrpcMailboxService> _mailboxServices = new TreeMap<>();
+
+  @BeforeClass
+  public void setUp()
+      throws Exception {
+    for (int i = 0; i < MAILBOX_TEST_SIZE; i++) {
+      int availablePort = QueryEnvironmentTestUtils.getAvailablePort();
+      GrpcMailboxService grpcMailboxService = new GrpcMailboxService("localhost", availablePort);
+      grpcMailboxService.start();
+      _mailboxServices.put(availablePort, grpcMailboxService);
+    }
+  }
+
+  @AfterClass
+  public void tearDown() {
+    for (GrpcMailboxService service : _mailboxServices.values()) {
+      service.shutdown();
+    }
+  }
+}
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java
new file mode 100644
index 0000000000..9de2db834f
--- /dev/null
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java
@@ -0,0 +1,278 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import org.apache.calcite.rel.RelDistribution;
+import org.apache.commons.io.FileUtils;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.QueryEnvironment;
+import org.apache.pinot.query.QueryEnvironmentTestUtils;
+import org.apache.pinot.query.QueryServerEnclosure;
+import org.apache.pinot.query.mailbox.GrpcMailboxService;
+import org.apache.pinot.query.planner.QueryPlan;
+import org.apache.pinot.query.planner.nodes.MailboxReceiveNode;
+import org.apache.pinot.query.routing.WorkerInstance;
+import org.apache.pinot.query.runtime.blocks.DataTableBlock;
+import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
+import org.apache.pinot.query.runtime.operator.MailboxReceiveOperator;
+import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
+import org.apache.pinot.query.service.QueryConfig;
+import org.apache.pinot.query.service.QueryDispatcher;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.apache.pinot.core.query.selection.SelectionOperatorUtils.extractRowFromDataTable;
+
+
+public class QueryRunnerTest {
+  private static final Random RANDOM_REQUEST_ID_GEN = new Random();
+  private static final File INDEX_DIR_S1_A = new File(FileUtils.getTempDirectory(), "QueryRunnerTest_server1_tableA");
+  private static final File INDEX_DIR_S1_B = new File(FileUtils.getTempDirectory(), "QueryRunnerTest_server1_tableB");
+  private static final File INDEX_DIR_S2_A = new File(FileUtils.getTempDirectory(), "QueryRunnerTest_server2_tableA");
+  private static final File INDEX_DIR_S1_C = new File(FileUtils.getTempDirectory(), "QueryRunnerTest_server1_tableC");
+  private static final File INDEX_DIR_S2_C = new File(FileUtils.getTempDirectory(), "QueryRunnerTest_server2_tableC");
+
+  private QueryEnvironment _queryEnvironment;
+  private String _reducerHostname;
+  private int _reducerGrpcPort;
+  private Map<ServerInstance, QueryServerEnclosure> _servers = new HashMap<>();
+  private GrpcMailboxService _mailboxService;
+
+  @BeforeClass
+  public void setUp()
+      throws Exception {
+    QueryServerEnclosure server1 = new QueryServerEnclosure(Lists.newArrayList("a", "b", "c"),
+        ImmutableMap.of("a", INDEX_DIR_S1_A, "b", INDEX_DIR_S1_B, "c", INDEX_DIR_S1_C),
+        QueryEnvironmentTestUtils.SERVER1_SEGMENTS);
+    QueryServerEnclosure server2 = new QueryServerEnclosure(Lists.newArrayList("a", "c"),
+        ImmutableMap.of("a", INDEX_DIR_S2_A, "c", INDEX_DIR_S2_C), QueryEnvironmentTestUtils.SERVER2_SEGMENTS);
+
+    _reducerGrpcPort = QueryEnvironmentTestUtils.getAvailablePort();
+    _reducerHostname = String.format("Broker_%s", QueryConfig.DEFAULT_QUERY_RUNNER_HOSTNAME);
+    Map<String, Object> reducerConfig = new HashMap<>();
+    reducerConfig.put(QueryConfig.KEY_OF_QUERY_RUNNER_PORT, _reducerGrpcPort);
+    reducerConfig.put(QueryConfig.KEY_OF_QUERY_RUNNER_HOSTNAME, _reducerHostname);
+    _mailboxService = new GrpcMailboxService(_reducerHostname, _reducerGrpcPort);
+    _mailboxService.start();
+
+    _queryEnvironment =
+        QueryEnvironmentTestUtils.getQueryEnvironment(_reducerGrpcPort, server1.getPort(), server2.getPort());
+    server1.start();
+    server2.start();
+    // this doesn't test the QueryServer functionality so the server port can be the same as the mailbox port.
+    // this is only use for test identifier purpose.
+    _servers.put(new WorkerInstance("localhost", server1.getPort(), server1.getPort()), server1);
+    _servers.put(new WorkerInstance("localhost", server2.getPort(), server2.getPort()), server2);
+  }
+
+  @AfterClass
+  public void tearDown() {
+    for (QueryServerEnclosure server : _servers.values()) {
+      server.shutDown();
+    }
+    _mailboxService.shutdown();
+  }
+
+  @Test
+  public void testRunningTableScanOnlyQuery()
+      throws Exception {
+    QueryPlan queryPlan = _queryEnvironment.planQuery("SELECT * FROM b");
+    int stageRoodId = QueryEnvironmentTestUtils.getTestStageByServerCount(queryPlan, 1);
+    Map<String, String> requestMetadataMap =
+        ImmutableMap.of("REQUEST_ID", String.valueOf(RANDOM_REQUEST_ID_GEN.nextLong()));
+
+    ServerInstance serverInstance = queryPlan.getStageMetadataMap().get(stageRoodId).getServerInstances().get(0);
+    DistributedStagePlan distributedStagePlan =
+        QueryDispatcher.constructDistributedStagePlan(queryPlan, stageRoodId, serverInstance);
+
+    MailboxReceiveOperator mailboxReceiveOperator =
+        createReduceStageOperator(queryPlan.getStageMetadataMap().get(stageRoodId).getServerInstances(),
+            Long.parseLong(requestMetadataMap.get("REQUEST_ID")), stageRoodId, _reducerGrpcPort);
+
+    // execute this single stage.
+    _servers.get(serverInstance).processQuery(distributedStagePlan, requestMetadataMap);
+
+    DataTableBlock dataTableBlock;
+    // get the block back and it should have 5 rows
+    dataTableBlock = mailboxReceiveOperator.nextBlock();
+    Assert.assertEquals(dataTableBlock.getDataTable().getNumberOfRows(), 5);
+    // next block should be null as all servers finished sending.
+    dataTableBlock = mailboxReceiveOperator.nextBlock();
+    Assert.assertTrue(DataTableBlockUtils.isEndOfStream(dataTableBlock));
+  }
+
+  @Test
+  public void testRunningTableScanMultipleServer()
+      throws Exception {
+    QueryPlan queryPlan = _queryEnvironment.planQuery("SELECT * FROM a");
+    int stageRoodId = QueryEnvironmentTestUtils.getTestStageByServerCount(queryPlan, 2);
+    Map<String, String> requestMetadataMap =
+        ImmutableMap.of("REQUEST_ID", String.valueOf(RANDOM_REQUEST_ID_GEN.nextLong()));
+
+    for (ServerInstance serverInstance : queryPlan.getStageMetadataMap().get(stageRoodId).getServerInstances()) {
+      DistributedStagePlan distributedStagePlan =
+          QueryDispatcher.constructDistributedStagePlan(queryPlan, stageRoodId, serverInstance);
+
+      // execute this single stage.
+      _servers.get(serverInstance).processQuery(distributedStagePlan, requestMetadataMap);
+    }
+
+    MailboxReceiveOperator mailboxReceiveOperator =
+        createReduceStageOperator(queryPlan.getStageMetadataMap().get(stageRoodId).getServerInstances(),
+            Long.parseLong(requestMetadataMap.get("REQUEST_ID")), stageRoodId, _reducerGrpcPort);
+
+    int count = 0;
+    int rowCount = 0;
+    DataTableBlock dataTableBlock;
+    while (count < 2) { // we have 2 servers sending data.
+      dataTableBlock = mailboxReceiveOperator.nextBlock();
+      rowCount += dataTableBlock.getDataTable().getNumberOfRows();
+      count++;
+    }
+    // assert that all table A segments returned successfully.
+    Assert.assertEquals(rowCount, 15);
+    // assert that the next block is null (e.g. finished receiving).
+    dataTableBlock = mailboxReceiveOperator.nextBlock();
+    Assert.assertTrue(DataTableBlockUtils.isEndOfStream(dataTableBlock));
+  }
+
+  @Test
+  public void testJoin()
+      throws Exception {
+    QueryPlan queryPlan = _queryEnvironment.planQuery("SELECT * FROM a JOIN b on a.col1 = b.col2");
+    Map<String, String> requestMetadataMap =
+        ImmutableMap.of("REQUEST_ID", String.valueOf(RANDOM_REQUEST_ID_GEN.nextLong()));
+    MailboxReceiveOperator mailboxReceiveOperator = null;
+    for (int stageId : queryPlan.getStageMetadataMap().keySet()) {
+      if (queryPlan.getQueryStageMap().get(stageId) instanceof MailboxReceiveNode) {
+        MailboxReceiveNode reduceNode = (MailboxReceiveNode) queryPlan.getQueryStageMap().get(stageId);
+        mailboxReceiveOperator = createReduceStageOperator(
+            queryPlan.getStageMetadataMap().get(reduceNode.getSenderStageId()).getServerInstances(),
+            Long.parseLong(requestMetadataMap.get("REQUEST_ID")), reduceNode.getSenderStageId(), _reducerGrpcPort);
+      } else {
+        for (ServerInstance serverInstance : queryPlan.getStageMetadataMap().get(stageId).getServerInstances()) {
+          DistributedStagePlan distributedStagePlan =
+              QueryDispatcher.constructDistributedStagePlan(queryPlan, stageId, serverInstance);
+          _servers.get(serverInstance).processQuery(distributedStagePlan, requestMetadataMap);
+        }
+      }
+    }
+    Preconditions.checkNotNull(mailboxReceiveOperator);
+
+    int count = 0;
+    int rowCount = 0;
+    List<Object[]> resultRows = new ArrayList<>();
+    DataTableBlock dataTableBlock;
+    while (count < 2) { // we have 2 servers sending data.
+      dataTableBlock = mailboxReceiveOperator.nextBlock();
+      if (dataTableBlock.getDataTable() != null) {
+        DataTable dataTable = dataTableBlock.getDataTable();
+        int numRows = dataTable.getNumberOfRows();
+        for (int rowId = 0; rowId < numRows; rowId++) {
+          resultRows.add(extractRowFromDataTable(dataTable, rowId));
+        }
+        rowCount += numRows;
+      }
+      count++;
+    }
+
+    // Assert that each of the 5 categories from left table is joined with right table.
+    // Specifically table A has 15 rows (10 on server1 and 5 on server2) and table B has 5 rows (all on server1),
+    // thus the final JOIN result will be 15 x 1 = 15.
+    Assert.assertEquals(rowCount, 15);
+
+    // assert that the next block is null (e.g. finished receiving).
+    dataTableBlock = mailboxReceiveOperator.nextBlock();
+    Assert.assertTrue(DataTableBlockUtils.isEndOfStream(dataTableBlock));
+  }
+
+  @Test
+  public void testMultipleJoin()
+      throws Exception {
+    QueryPlan queryPlan =
+        _queryEnvironment.planQuery("SELECT * FROM a JOIN b ON a.col1 = b.col2 " + "JOIN c ON a.col3 = c.col3");
+    Map<String, String> requestMetadataMap =
+        ImmutableMap.of("REQUEST_ID", String.valueOf(RANDOM_REQUEST_ID_GEN.nextLong()));
+    MailboxReceiveOperator mailboxReceiveOperator = null;
+    for (int stageId : queryPlan.getStageMetadataMap().keySet()) {
+      if (queryPlan.getQueryStageMap().get(stageId) instanceof MailboxReceiveNode) {
+        MailboxReceiveNode reduceNode = (MailboxReceiveNode) queryPlan.getQueryStageMap().get(stageId);
+        mailboxReceiveOperator = createReduceStageOperator(
+            queryPlan.getStageMetadataMap().get(reduceNode.getSenderStageId()).getServerInstances(),
+            Long.parseLong(requestMetadataMap.get("REQUEST_ID")), reduceNode.getSenderStageId(), _reducerGrpcPort);
+      } else {
+        for (ServerInstance serverInstance : queryPlan.getStageMetadataMap().get(stageId).getServerInstances()) {
+          DistributedStagePlan distributedStagePlan =
+              QueryDispatcher.constructDistributedStagePlan(queryPlan, stageId, serverInstance);
+          _servers.get(serverInstance).processQuery(distributedStagePlan, requestMetadataMap);
+        }
+      }
+    }
+    Preconditions.checkNotNull(mailboxReceiveOperator);
+
+    int count = 0;
+    int rowCount = 0;
+    List<Object[]> resultRows = new ArrayList<>();
+    DataTableBlock dataTableBlock;
+    while (count < 2) { // we have 2 servers sending data.
+      dataTableBlock = mailboxReceiveOperator.nextBlock();
+      if (dataTableBlock.getDataTable() != null) {
+        DataTable dataTable = dataTableBlock.getDataTable();
+        int numRows = dataTable.getNumberOfRows();
+        for (int rowId = 0; rowId < numRows; rowId++) {
+          resultRows.add(extractRowFromDataTable(dataTable, rowId));
+        }
+        rowCount += numRows;
+      }
+      count++;
+    }
+
+    // Assert that each of the 5 categories from left table is joined with right table.
+    // Specifically table A has 15 rows (10 on server1 and 5 on server2) and table B has 5 rows (all on server1),
+    // thus the final JOIN result will be 15 x 1 = 15.
+    // Next join with table C which has (5 on server1 and 10 on server2), since data is identical. each of the row of
+    // the A JOIN B will have identical value of col3 as table C.col3 has. Since the values are cycling between
+    // (1, 2, 42, 1, 2). we will have 6 1s, 6 2s, and 3 42s, total result count will be 36 + 36 + 9 = 81
+    Assert.assertEquals(rowCount, 81);
+
+    // assert that the next block is null (e.g. finished receiving).
+    dataTableBlock = mailboxReceiveOperator.nextBlock();
+    Assert.assertTrue(DataTableBlockUtils.isEndOfStream(dataTableBlock));
+  }
+
+  protected MailboxReceiveOperator createReduceStageOperator(List<ServerInstance> sendingInstances, long jobId,
+      int stageId, int port) {
+    MailboxReceiveOperator mailboxReceiveOperator =
+        new MailboxReceiveOperator(_mailboxService, RelDistribution.Type.ANY, sendingInstances, "localhost", port,
+            jobId, stageId);
+    return mailboxReceiveOperator;
+  }
+}
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java
new file mode 100644
index 0000000000..539d5d9a53
--- /dev/null
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java
@@ -0,0 +1,107 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.service;
+
+import com.google.common.collect.Lists;
+import io.grpc.ManagedChannelBuilder;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.proto.PinotQueryWorkerGrpc;
+import org.apache.pinot.common.proto.Worker;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.QueryEnvironment;
+import org.apache.pinot.query.QueryEnvironmentTestUtils;
+import org.apache.pinot.query.planner.QueryPlan;
+import org.apache.pinot.query.routing.WorkerInstance;
+import org.apache.pinot.query.runtime.QueryRunner;
+import org.apache.pinot.query.runtime.plan.serde.QueryPlanSerDeUtils;
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+
+public class QueryServerTest {
+  private static final int QUERY_SERVER_COUNT = 2;
+  private Map<Integer, QueryServer> _queryServerMap = new HashMap<>();
+  private Map<Integer, ServerInstance> _queryServerInstanceMap = new HashMap<>();
+
+  private QueryEnvironment _queryEnvironment;
+
+  @BeforeClass
+  public void setUp()
+      throws Exception {
+
+    for (int i = 0; i < QUERY_SERVER_COUNT; i++) {
+      int availablePort = QueryEnvironmentTestUtils.getAvailablePort();
+      QueryServer queryServer = new QueryServer(availablePort, Mockito.mock(QueryRunner.class));
+      queryServer.start();
+      _queryServerMap.put(availablePort, queryServer);
+      // this only test the QueryServer functionality so the server port can be the same as the mailbox port.
+      // this is only use for test identifier purpose.
+      _queryServerInstanceMap.put(availablePort, new WorkerInstance("localhost", availablePort, availablePort));
+    }
+
+    List<Integer> portList = Lists.newArrayList(_queryServerMap.keySet());
+
+    // reducer port doesn't matter, we are testing the worker instance not GRPC.
+    _queryEnvironment = QueryEnvironmentTestUtils.getQueryEnvironment(1, portList.get(0), portList.get(1));
+  }
+
+  @AfterClass
+  public void tearDown() {
+    for (QueryServer worker : _queryServerMap.values()) {
+      worker.shutdown();
+    }
+  }
+
+  @Test
+  public void testWorkerAcceptsWorkerRequestCorrect()
+      throws Exception {
+    QueryPlan queryPlan = _queryEnvironment.planQuery("SELECT * FROM a JOIN b ON a.col1 = b.col2");
+
+    int singleServerStageId = QueryEnvironmentTestUtils.getTestStageByServerCount(queryPlan, 1);
+
+    Worker.QueryRequest queryRequest = getQueryRequest(queryPlan, singleServerStageId);
+
+    // submit the request for testing.
+    submitRequest(queryRequest);
+  }
+
+  private void submitRequest(Worker.QueryRequest queryRequest) {
+    String host = queryRequest.getMetadataMap().get("SERVER_INSTANCE_HOST");
+    int port = Integer.parseInt(queryRequest.getMetadataMap().get("SERVER_INSTANCE_PORT"));
+    PinotQueryWorkerGrpc.PinotQueryWorkerBlockingStub stub =
+        PinotQueryWorkerGrpc.newBlockingStub(ManagedChannelBuilder.forAddress(host, port).usePlaintext().build());
+    Worker.QueryResponse resp = stub.submit(queryRequest);
+    // TODO: validate meaningful return value
+    Assert.assertNotNull(resp.getMetadataMap().get("OK"));
+  }
+
+  private Worker.QueryRequest getQueryRequest(QueryPlan queryPlan, int stageId) {
+    ServerInstance serverInstance = queryPlan.getStageMetadataMap().get(stageId).getServerInstances().get(0);
+
+    return Worker.QueryRequest.newBuilder().setStagePlan(QueryPlanSerDeUtils.serialize(
+            QueryDispatcher.constructDistributedStagePlan(queryPlan, stageId, serverInstance)))
+        .putMetadata("SERVER_INSTANCE_HOST", serverInstance.getHostname())
+        .putMetadata("SERVER_INSTANCE_PORT", String.valueOf(serverInstance.getPort())).build();
+  }
+}
diff --git a/pom.xml b/pom.xml
index 56e6ade7e9..a53cd97617 100644
--- a/pom.xml
+++ b/pom.xml
@@ -59,6 +59,7 @@
     <module>contrib/pinot-fmpp-maven-plugin</module>
 
     <module>pinot-query-planner</module>
+    <module>pinot-query-runtime</module>
   </modules>
 
   <licenses>
@@ -395,6 +396,24 @@
         <version>${project.version}</version>
         <type>test-jar</type>
       </dependency>
+
+      <dependency>
+        <groupId>org.apache.pinot</groupId>
+        <artifactId>pinot-query-planner</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.pinot</groupId>
+        <artifactId>pinot-query-planner</artifactId>
+        <version>${project.version}</version>
+        <type>test-jar</type>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.pinot</groupId>
+        <artifactId>pinot-query-runtime</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+
       <dependency>
         <groupId>nl.jqno.equalsverifier</groupId>
         <artifactId>equalsverifier</artifactId>


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[pinot] 05/09: add support for project/filter pushdown (#8558)

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rongr pushed a commit to branch multi_stage_query_engine
in repository https://gitbox.apache.org/repos/asf/pinot.git

commit 3b01b2dec835267abb7db59559270e427e8b3b7c
Author: Rong Rong <ro...@apache.org>
AuthorDate: Fri Apr 22 08:57:29 2022 -0700

    add support for project/filter pushdown (#8558)
    
    * make planner node ready for project/filter pushdown
    
    making filter expression compilation work, generating operator
    
    add ProjectNode compilation as well
    
    * fixing comments from previous PR
    
    - add float type
    - change serde javadoc
    - change function name for StageNode serializable
    - change function name in proto utils
    
    * add distributed hash join capability
    
    * fixing serde
    
    * refactor calcite components into sql/rex/expression 3 subclasses
    
    * address diff comments
    
    1. renamed BroadcastJoin to HashJoin
    2. relocate packages inside planner to planner.node to planner.logical/serde/stage based on their functionalities
    3. added SqlHint strategy to planner so that it can do either hash or broadcast join
    
    * additional comment address
    
    1. remove CalciteExpressionParser as it is not used
    2. remove rowType in construction for AbstractStagerNode
    
    * rename RelHint
    
    Co-authored-by: Rong Rong <ro...@startree.ai>
---
 pinot-common/src/main/proto/plan.proto             |   5 +-
 pinot-query-planner/pom.xml                        |   4 +-
 .../org/apache/pinot/query/QueryEnvironment.java   |  12 +-
 .../query/parser/CalciteExpressionParser.java      | 502 ---------------------
 .../query/parser/CalciteRexExpressionParser.java   | 217 +++++++++
 .../pinot/query/parser/CalciteSqlParser.java       |   2 +-
 .../org/apache/pinot/query/parser/ParserUtils.java |  55 +++
 .../org/apache/pinot/query/planner/QueryPlan.java  |   3 +-
 .../apache/pinot/query/planner/StageMetadata.java  |   4 +-
 .../PinotRelationalHints.java}                     |  24 +-
 .../planner/{ => logical}/LogicalPlanner.java      |   2 +-
 .../planner/{ => logical}/RelToStageConverter.java |  39 +-
 .../pinot/query/planner/logical/RexExpression.java | 157 +++++++
 .../query/planner/{ => logical}/StagePlanner.java  |  31 +-
 .../partitioning/FieldSelectionKeySelector.java    |   3 +
 .../query/planner/partitioning/KeySelector.java    |   4 +-
 .../ProtoProperties.java}                          |  19 +-
 .../{nodes => }/serde/ProtoSerializable.java       |  20 +-
 .../{nodes => }/serde/ProtoSerializationUtils.java | 117 +++--
 .../{nodes => stage}/AbstractStageNode.java        |  24 +-
 .../{nodes/CalcNode.java => stage/FilterNode.java} |  24 +-
 .../query/planner/{nodes => stage}/JoinNode.java   |  12 +-
 .../{nodes => stage}/MailboxReceiveNode.java       |   9 +-
 .../planner/{nodes => stage}/MailboxSendNode.java  |  24 +-
 .../TableScanNode.java => stage/ProjectNode.java}  |  32 +-
 .../query/planner/{nodes => stage}/StageNode.java  |   2 +-
 .../StageNodeSerDeUtils.java}                      |  16 +-
 .../planner/{nodes => stage}/TableScanNode.java    |   9 +-
 .../query/rules/PinotExchangeNodeInsertRule.java   |  24 +-
 .../pinot/query/rules/PinotQueryRuleSets.java      |   2 +
 .../apache/pinot/query/QueryEnvironmentTest.java   |  80 ++--
 .../pinot/query/QueryEnvironmentTestBase.java      |  52 +++
 .../pinot/query/planner/stage/SerDeUtilsTest.java  |  81 ++++
 .../apache/pinot/query/runtime/QueryRunner.java    |   6 +-
 .../runtime/executor/WorkerQueryExecutor.java      |  33 +-
 ...castJoinOperator.java => HashJoinOperator.java} |   6 +-
 .../runtime/operator/MailboxSendOperator.java      |  52 ++-
 .../query/runtime/plan/DistributedStagePlan.java   |   2 +-
 .../runtime/plan/serde/QueryPlanSerDeUtils.java    |   8 +-
 .../query/runtime/utils/ServerRequestUtils.java    |  33 +-
 .../pinot/query/service/QueryDispatcher.java       |   2 +-
 .../pinot/query/runtime/QueryRunnerTest.java       | 170 ++-----
 .../pinot/query/service/QueryServerTest.java       |  40 +-
 43 files changed, 1071 insertions(+), 892 deletions(-)

diff --git a/pinot-common/src/main/proto/plan.proto b/pinot-common/src/main/proto/plan.proto
index 47018197fc..8e75a31a42 100644
--- a/pinot-common/src/main/proto/plan.proto
+++ b/pinot-common/src/main/proto/plan.proto
@@ -76,8 +76,9 @@ message LiteralField {
     bool boolField = 1;
     int32 intField = 2;
     int64 longField = 3;
-    double doubleField = 4;
-    string stringField = 5;
+    float floatField = 4;
+    double doubleField = 5;
+    string stringField = 6;
   }
 }
 
diff --git a/pinot-query-planner/pom.xml b/pinot-query-planner/pom.xml
index 05b9461cdc..68ee3a2057 100644
--- a/pinot-query-planner/pom.xml
+++ b/pinot-query-planner/pom.xml
@@ -70,12 +70,12 @@
     <dependency>
       <groupId>org.codehaus.janino</groupId>
       <artifactId>janino</artifactId>
-      <version>3.0.9</version>
+      <version>3.1.6</version>
     </dependency>
     <dependency>
       <groupId>org.codehaus.janino</groupId>
       <artifactId>commons-compiler</artifactId>
-      <version>3.0.9</version>
+      <version>3.1.6</version>
     </dependency>
 
     <dependency>
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java
index 215c0051d7..3a19156287 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java
@@ -31,6 +31,7 @@ import org.apache.calcite.prepare.PlannerImpl;
 import org.apache.calcite.prepare.Prepare;
 import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.RelRoot;
+import org.apache.calcite.rel.hint.HintStrategyTable;
 import org.apache.calcite.rel.type.RelDataTypeFactory;
 import org.apache.calcite.rex.RexBuilder;
 import org.apache.calcite.sql.SqlKind;
@@ -43,9 +44,9 @@ import org.apache.calcite.tools.FrameworkConfig;
 import org.apache.calcite.tools.Frameworks;
 import org.apache.pinot.query.context.PlannerContext;
 import org.apache.pinot.query.parser.CalciteSqlParser;
-import org.apache.pinot.query.planner.LogicalPlanner;
 import org.apache.pinot.query.planner.QueryPlan;
-import org.apache.pinot.query.planner.StagePlanner;
+import org.apache.pinot.query.planner.logical.LogicalPlanner;
+import org.apache.pinot.query.planner.logical.StagePlanner;
 import org.apache.pinot.query.routing.WorkerManager;
 import org.apache.pinot.query.rules.PinotQueryRuleSets;
 import org.apache.pinot.query.type.TypeFactory;
@@ -157,10 +158,15 @@ public class QueryEnvironment {
     RelOptCluster cluster = RelOptCluster.create(_relOptPlanner, rexBuilder);
     SqlToRelConverter sqlToRelConverter =
         new SqlToRelConverter(_planner, _validator, _catalogReader, cluster, StandardConvertletTable.INSTANCE,
-            SqlToRelConverter.config());
+            SqlToRelConverter.config().withHintStrategyTable(getHintStrategyTable(plannerContext)));
     return sqlToRelConverter.convertQuery(parsed, false, true);
   }
 
+  // TODO: add hint strategy table based on plannerContext.
+  private HintStrategyTable getHintStrategyTable(PlannerContext plannerContext) {
+    return HintStrategyTable.builder().build();
+  }
+
   protected RelNode optimize(RelRoot relRoot, PlannerContext plannerContext) {
     // 4. optimize relNode
     // TODO: add support for traits, cost factory.
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteExpressionParser.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteExpressionParser.java
deleted file mode 100644
index fc75efb1ae..0000000000
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteExpressionParser.java
+++ /dev/null
@@ -1,502 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.pinot.query.parser;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-import org.apache.calcite.sql.SqlBasicCall;
-import org.apache.calcite.sql.SqlDataTypeSpec;
-import org.apache.calcite.sql.SqlIdentifier;
-import org.apache.calcite.sql.SqlKind;
-import org.apache.calcite.sql.SqlLiteral;
-import org.apache.calcite.sql.SqlNode;
-import org.apache.calcite.sql.SqlNodeList;
-import org.apache.calcite.sql.fun.SqlCase;
-import org.apache.calcite.sql.parser.SqlParseException;
-import org.apache.calcite.sql.parser.SqlParser;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.pinot.common.request.Expression;
-import org.apache.pinot.common.request.ExpressionType;
-import org.apache.pinot.common.request.Function;
-import org.apache.pinot.common.utils.request.RequestUtils;
-import org.apache.pinot.segment.spi.AggregationFunctionType;
-import org.apache.pinot.sql.parsers.SqlCompilationException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-
-/**
- * Calcite parser to convert SQL expressions into {@link Expression}.
- *
- * <p>This class is extracted from {@link org.apache.pinot.sql.parsers.CalciteSqlParser}. It only contains the
- * {@link Expression} related info, this is used for ingestion and query rewrite.
- */
-public class CalciteExpressionParser {
-  private static final Logger LOGGER = LoggerFactory.getLogger(CalciteExpressionParser.class);
-
-  private CalciteExpressionParser() {
-    // do not instantiate.
-  }
-
-  private static List<Expression> getAliasLeftExpressionsFromDistinctExpression(Function function) {
-    List<Expression> operands = function.getOperands();
-    List<Expression> expressions = new ArrayList<>(operands.size());
-    for (Expression operand : operands) {
-      if (isAsFunction(operand)) {
-        expressions.add(operand.getFunctionCall().getOperands().get(0));
-      } else {
-        expressions.add(operand);
-      }
-    }
-    return expressions;
-  }
-
-  public static boolean isAggregateExpression(Expression expression) {
-    Function functionCall = expression.getFunctionCall();
-    if (functionCall != null) {
-      String operator = functionCall.getOperator();
-      try {
-        AggregationFunctionType.getAggregationFunctionType(operator);
-        return true;
-      } catch (IllegalArgumentException e) {
-      }
-      if (functionCall.getOperandsSize() > 0) {
-        for (Expression operand : functionCall.getOperands()) {
-          if (isAggregateExpression(operand)) {
-            return true;
-          }
-        }
-      }
-    }
-    return false;
-  }
-
-  public static boolean isAsFunction(Expression expression) {
-    return expression.getFunctionCall() != null && expression.getFunctionCall().getOperator().equalsIgnoreCase("AS");
-  }
-
-  /**
-   * Extract all the identifiers from given expressions.
-   *
-   * @param expressions
-   * @param excludeAs if true, ignores the right side identifier for AS function.
-   * @return all the identifier names.
-   */
-  public static Set<String> extractIdentifiers(List<Expression> expressions, boolean excludeAs) {
-    Set<String> identifiers = new HashSet<>();
-    for (Expression expression : expressions) {
-      if (expression.getIdentifier() != null) {
-        identifiers.add(expression.getIdentifier().getName());
-      } else if (expression.getFunctionCall() != null) {
-        if (excludeAs && expression.getFunctionCall().getOperator().equalsIgnoreCase("AS")) {
-          identifiers.addAll(
-              extractIdentifiers(Arrays.asList(expression.getFunctionCall().getOperands().get(0)), true));
-          continue;
-        } else {
-          identifiers.addAll(extractIdentifiers(expression.getFunctionCall().getOperands(), excludeAs));
-        }
-      }
-    }
-    return identifiers;
-  }
-
-  /**
-   * Compiles a String expression into {@link Expression}.
-   *
-   * @param expression String expression.
-   * @return {@link Expression} equivalent of the string.
-   *
-   * @throws SqlCompilationException if String is not a valid expression.
-   */
-  public static Expression compileToExpression(String expression) {
-    SqlParser sqlParser = SqlParser.create(expression, ParserUtils.PARSER_CONFIG);
-    SqlNode sqlNode;
-    try {
-      sqlNode = sqlParser.parseExpression();
-    } catch (SqlParseException e) {
-      throw new SqlCompilationException("Caught exception while parsing expression: " + expression, e);
-    }
-    return toExpression(sqlNode);
-  }
-
-  private static List<Expression> convertDistinctSelectList(SqlNodeList selectList) {
-    List<Expression> selectExpr = new ArrayList<>();
-    selectExpr.add(convertDistinctAndSelectListToFunctionExpression(selectList));
-    return selectExpr;
-  }
-
-  private static List<Expression> convertSelectList(SqlNodeList selectList) {
-    List<Expression> selectExpr = new ArrayList<>();
-
-    final Iterator<SqlNode> iterator = selectList.iterator();
-    while (iterator.hasNext()) {
-      final SqlNode next = iterator.next();
-      selectExpr.add(toExpression(next));
-    }
-
-    return selectExpr;
-  }
-
-  private static List<Expression> convertOrderByList(SqlNodeList orderList) {
-    List<Expression> orderByExpr = new ArrayList<>();
-    final Iterator<SqlNode> iterator = orderList.iterator();
-    while (iterator.hasNext()) {
-      final SqlNode next = iterator.next();
-      orderByExpr.add(convertOrderBy(next));
-    }
-    return orderByExpr;
-  }
-
-  private static Expression convertOrderBy(SqlNode node) {
-    final SqlKind kind = node.getKind();
-    Expression expression;
-    switch (kind) {
-      case DESCENDING:
-        SqlBasicCall basicCall = (SqlBasicCall) node;
-        expression = RequestUtils.getFunctionExpression("DESC");
-        expression.getFunctionCall().addToOperands(toExpression(basicCall.getOperandList().get(0)));
-        break;
-      case IDENTIFIER:
-      default:
-        expression = RequestUtils.getFunctionExpression("ASC");
-        expression.getFunctionCall().addToOperands(toExpression(node));
-        break;
-    }
-    return expression;
-  }
-
-  /**
-   * DISTINCT is implemented as an aggregation function so need to take the select list items
-   * and convert them into a single function expression for handing over to execution engine
-   * either as a PinotQuery or BrokerRequest via conversion
-   * @param selectList select list items
-   * @return DISTINCT function expression
-   */
-  private static Expression convertDistinctAndSelectListToFunctionExpression(SqlNodeList selectList) {
-    String functionName = AggregationFunctionType.DISTINCT.getName();
-    Expression functionExpression = RequestUtils.getFunctionExpression(functionName);
-    for (SqlNode node : selectList) {
-      Expression columnExpression = toExpression(node);
-      if (columnExpression.getType() == ExpressionType.IDENTIFIER && columnExpression.getIdentifier().getName()
-          .equals("*")) {
-        throw new SqlCompilationException(
-            "Syntax error: Pinot currently does not support DISTINCT with *. Please specify each column name after "
-                + "DISTINCT keyword");
-      } else if (columnExpression.getType() == ExpressionType.FUNCTION) {
-        Function functionCall = columnExpression.getFunctionCall();
-        String function = functionCall.getOperator();
-        if (AggregationFunctionType.isAggregationFunction(function)) {
-          throw new SqlCompilationException(
-              "Syntax error: Use of DISTINCT with aggregation functions is not supported");
-        }
-      }
-      functionExpression.getFunctionCall().addToOperands(columnExpression);
-    }
-    return functionExpression;
-  }
-
-  private static Expression toExpression(SqlNode node) {
-    LOGGER.debug("Current processing SqlNode: {}, node.getKind(): {}", node, node.getKind());
-    switch (node.getKind()) {
-      case IDENTIFIER:
-        if (((SqlIdentifier) node).isStar()) {
-          return RequestUtils.getIdentifierExpression("*");
-        }
-        if (((SqlIdentifier) node).isSimple()) {
-          return RequestUtils.getIdentifierExpression(((SqlIdentifier) node).getSimple());
-        }
-        return RequestUtils.getIdentifierExpression(node.toString());
-      case LITERAL:
-        return RequestUtils.getLiteralExpression((SqlLiteral) node);
-      case AS:
-        SqlBasicCall asFuncSqlNode = (SqlBasicCall) node;
-        List<SqlNode> operands = asFuncSqlNode.getOperandList();
-        Expression leftExpr = toExpression(operands.get(0));
-        SqlNode aliasSqlNode = operands.get(1);
-        String aliasName;
-        switch (aliasSqlNode.getKind()) {
-          case IDENTIFIER:
-            aliasName = ((SqlIdentifier) aliasSqlNode).getSimple();
-            break;
-          case LITERAL:
-            aliasName = ((SqlLiteral) aliasSqlNode).toValue();
-            break;
-          default:
-            throw new SqlCompilationException("Unsupported Alias sql node - " + aliasSqlNode);
-        }
-        Expression rightExpr = RequestUtils.getIdentifierExpression(aliasName);
-        // Just return left identifier if both sides are the same identifier.
-        if (leftExpr.isSetIdentifier() && rightExpr.isSetIdentifier()) {
-          if (leftExpr.getIdentifier().getName().equals(rightExpr.getIdentifier().getName())) {
-            return leftExpr;
-          }
-        }
-        final Expression asFuncExpr = RequestUtils.getFunctionExpression(SqlKind.AS.toString());
-        asFuncExpr.getFunctionCall().addToOperands(leftExpr);
-        asFuncExpr.getFunctionCall().addToOperands(rightExpr);
-        return asFuncExpr;
-      case CASE:
-        // CASE WHEN Statement is model as a function with variable length parameters.
-        // Assume N is number of WHEN Statements, total number of parameters is (2 * N + 1).
-        // - N: Convert each WHEN Statement into a function Expression;
-        // - N: Convert each THEN Statement into an Expression;
-        // - 1: Convert ELSE Statement into an Expression.
-        SqlCase caseSqlNode = (SqlCase) node;
-        SqlNodeList whenOperands = caseSqlNode.getWhenOperands();
-        SqlNodeList thenOperands = caseSqlNode.getThenOperands();
-        SqlNode elseOperand = caseSqlNode.getElseOperand();
-        Expression caseFuncExpr = RequestUtils.getFunctionExpression(SqlKind.CASE.name());
-        for (SqlNode whenSqlNode : whenOperands.getList()) {
-          Expression whenExpression = toExpression(whenSqlNode);
-          if (isAggregateExpression(whenExpression)) {
-            throw new SqlCompilationException(
-                "Aggregation functions inside WHEN Clause is not supported - " + whenSqlNode);
-          }
-          caseFuncExpr.getFunctionCall().addToOperands(whenExpression);
-        }
-        for (SqlNode thenSqlNode : thenOperands.getList()) {
-          Expression thenExpression = toExpression(thenSqlNode);
-          if (isAggregateExpression(thenExpression)) {
-            throw new SqlCompilationException(
-                "Aggregation functions inside THEN Clause is not supported - " + thenSqlNode);
-          }
-          caseFuncExpr.getFunctionCall().addToOperands(thenExpression);
-        }
-        Expression elseExpression = toExpression(elseOperand);
-        if (isAggregateExpression(elseExpression)) {
-          throw new SqlCompilationException(
-              "Aggregation functions inside ELSE Clause is not supported - " + elseExpression);
-        }
-        caseFuncExpr.getFunctionCall().addToOperands(elseExpression);
-        return caseFuncExpr;
-      default:
-        if (node instanceof SqlDataTypeSpec) {
-          // This is to handle expression like: CAST(col AS INT)
-          return RequestUtils.getLiteralExpression(((SqlDataTypeSpec) node).getTypeName().getSimple());
-        } else {
-          return compileFunctionExpression((SqlBasicCall) node);
-        }
-    }
-  }
-
-  private static Expression compileFunctionExpression(SqlBasicCall functionNode) {
-    SqlKind functionKind = functionNode.getKind();
-    String functionName;
-    switch (functionKind) {
-      case AND:
-        return compileAndExpression(functionNode);
-      case OR:
-        return compileOrExpression(functionNode);
-      case COUNT:
-        SqlLiteral functionQuantifier = functionNode.getFunctionQuantifier();
-        if (functionQuantifier != null && functionQuantifier.toValue().equalsIgnoreCase("DISTINCT")) {
-          functionName = AggregationFunctionType.DISTINCTCOUNT.name();
-        } else {
-          functionName = AggregationFunctionType.COUNT.name();
-        }
-        break;
-      case OTHER:
-      case OTHER_FUNCTION:
-      case DOT:
-        functionName = functionNode.getOperator().getName().toUpperCase();
-        if (functionName.equals("ITEM") || functionName.equals("DOT")) {
-          // Calcite parses path expression such as "data[0][1].a.b[0]" into a chain of ITEM and/or DOT
-          // functions. Collapse this chain into an identifier.
-          StringBuffer path = new StringBuffer();
-          compilePathExpression(functionName, functionNode, path);
-          return RequestUtils.getIdentifierExpression(path.toString());
-        }
-        break;
-      default:
-        functionName = functionKind.name();
-        break;
-    }
-    // When there is no argument, set an empty list as the operands
-    List<SqlNode> childNodes = functionNode.getOperandList();
-    List<Expression> operands = new ArrayList<>(childNodes.size());
-    for (SqlNode childNode : childNodes) {
-      if (childNode instanceof SqlNodeList) {
-        for (SqlNode node : (SqlNodeList) childNode) {
-          operands.add(toExpression(node));
-        }
-      } else {
-        operands.add(toExpression(childNode));
-      }
-    }
-    validateFunction(functionName, operands);
-    Expression functionExpression = RequestUtils.getFunctionExpression(functionName);
-    functionExpression.getFunctionCall().setOperands(operands);
-    return functionExpression;
-  }
-
-  /**
-   * Convert Calcite operator tree made up of ITEM and DOT functions to an identifier. For example, the operator tree
-   * shown below will be converted to IDENTIFIER "jsoncolumn.data[0][1].a.b[0]".
-   *
-   * ├── ITEM(jsoncolumn.data[0][1].a.b[0])
-   *      ├── LITERAL (0)
-   *      └── DOT (jsoncolumn.daa[0][1].a.b)
-   *            ├── IDENTIFIER (b)
-   *            └── DOT (jsoncolumn.data[0][1].a)
-   *                  ├── IDENTIFIER (a)
-   *                  └── ITEM (jsoncolumn.data[0][1])
-   *                        ├── LITERAL (1)
-   *                        └── ITEM (jsoncolumn.data[0])
-   *                              ├── LITERAL (1)
-   *                              └── IDENTIFIER (jsoncolumn.data)
-   *
-   * @param functionName Name of the function ("DOT" or "ITEM")
-   * @param functionNode Root node of the DOT and/or ITEM operator function chain.
-   * @param path String representation of path represented by DOT and/or ITEM function chain.
-   */
-  private static void compilePathExpression(String functionName, SqlBasicCall functionNode, StringBuffer path) {
-    List<SqlNode> operands = functionNode.getOperandList();
-
-    // Compile first operand of the function (either an identifier or another DOT and/or ITEM function).
-    SqlKind kind0 = operands.get(0).getKind();
-    if (kind0 == SqlKind.IDENTIFIER) {
-      path.append(operands.get(0).toString());
-    } else if (kind0 == SqlKind.DOT || kind0 == SqlKind.OTHER_FUNCTION) {
-      SqlBasicCall function0 = (SqlBasicCall) operands.get(0);
-      String name0 = function0.getOperator().getName();
-      if (name0.equals("ITEM") || name0.equals("DOT")) {
-        compilePathExpression(name0, function0, path);
-      } else {
-        throw new SqlCompilationException("SELECT list item has bad path expression.");
-      }
-    } else {
-      throw new SqlCompilationException("SELECT list item has bad path expression.");
-    }
-
-    // Compile second operand of the function (either an identifier or literal).
-    SqlKind kind1 = operands.get(1).getKind();
-    if (kind1 == SqlKind.IDENTIFIER) {
-      path.append(".").append(((SqlIdentifier) operands.get(1)).getSimple());
-    } else if (kind1 == SqlKind.LITERAL) {
-      path.append("[").append(((SqlLiteral) operands.get(1)).toValue()).append("]");
-    } else {
-      throw new SqlCompilationException("SELECT list item has bad path expression.");
-    }
-  }
-
-  public static String canonicalize(String functionName) {
-    return StringUtils.remove(functionName, '_').toLowerCase();
-  }
-
-  public static boolean isSameFunction(String function1, String function2) {
-    return canonicalize(function1).equals(canonicalize(function2));
-  }
-
-  private static void validateFunction(String functionName, List<Expression> operands) {
-    switch (canonicalize(functionName)) {
-      case "jsonextractscalar":
-        validateJsonExtractScalarFunction(operands);
-        break;
-      case "jsonextractkey":
-        validateJsonExtractKeyFunction(operands);
-        break;
-      default:
-        break;
-    }
-  }
-
-  private static void validateJsonExtractScalarFunction(List<Expression> operands) {
-    int numOperands = operands.size();
-
-    // Check that there are exactly 3 or 4 arguments
-    if (numOperands != 3 && numOperands != 4) {
-      throw new SqlCompilationException(
-          "Expect 3 or 4 arguments for transform function: jsonExtractScalar(jsonFieldName, 'jsonPath', "
-              + "'resultsType', ['defaultValue'])");
-    }
-    if (!operands.get(1).isSetLiteral() || !operands.get(2).isSetLiteral() || (numOperands == 4 && !operands.get(3)
-        .isSetLiteral())) {
-      throw new SqlCompilationException(
-          "Expect the 2nd/3rd/4th argument of transform function: jsonExtractScalar(jsonFieldName, 'jsonPath',"
-              + " 'resultsType', ['defaultValue']) to be a single-quoted literal value.");
-    }
-  }
-
-  private static void validateJsonExtractKeyFunction(List<Expression> operands) {
-    // Check that there are exactly 2 arguments
-    if (operands.size() != 2) {
-      throw new SqlCompilationException(
-          "Expect 2 arguments are required for transform function: jsonExtractKey(jsonFieldName, 'jsonPath')");
-    }
-    if (!operands.get(1).isSetLiteral()) {
-      throw new SqlCompilationException(
-          "Expect the 2nd argument for transform function: jsonExtractKey(jsonFieldName, 'jsonPath') to be a "
-              + "single-quoted literal value.");
-    }
-  }
-
-  /**
-   * Helper method to flatten the operands for the AND expression.
-   */
-  private static Expression compileAndExpression(SqlBasicCall andNode) {
-    List<Expression> operands = new ArrayList<>();
-    for (SqlNode childNode : andNode.getOperandList()) {
-      if (childNode.getKind() == SqlKind.AND) {
-        Expression childAndExpression = compileAndExpression((SqlBasicCall) childNode);
-        operands.addAll(childAndExpression.getFunctionCall().getOperands());
-      } else {
-        operands.add(toExpression(childNode));
-      }
-    }
-    Expression andExpression = RequestUtils.getFunctionExpression(SqlKind.AND.name());
-    andExpression.getFunctionCall().setOperands(operands);
-    return andExpression;
-  }
-
-  /**
-   * Helper method to flatten the operands for the OR expression.
-   */
-  private static Expression compileOrExpression(SqlBasicCall orNode) {
-    List<Expression> operands = new ArrayList<>();
-    for (SqlNode childNode : orNode.getOperandList()) {
-      if (childNode.getKind() == SqlKind.OR) {
-        Expression childAndExpression = compileOrExpression((SqlBasicCall) childNode);
-        operands.addAll(childAndExpression.getFunctionCall().getOperands());
-      } else {
-        operands.add(toExpression(childNode));
-      }
-    }
-    Expression andExpression = RequestUtils.getFunctionExpression(SqlKind.OR.name());
-    andExpression.getFunctionCall().setOperands(operands);
-    return andExpression;
-  }
-
-  public static boolean isLiteralOnlyExpression(Expression e) {
-    if (e.getType() == ExpressionType.LITERAL) {
-      return true;
-    }
-    if (e.getType() == ExpressionType.FUNCTION) {
-      Function functionCall = e.getFunctionCall();
-      if (functionCall.getOperator().equalsIgnoreCase(SqlKind.AS.toString())) {
-        return isLiteralOnlyExpression(functionCall.getOperands().get(0));
-      }
-      return false;
-    }
-    return false;
-  }
-}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteRexExpressionParser.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteRexExpressionParser.java
new file mode 100644
index 0000000000..91d573c89a
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteRexExpressionParser.java
@@ -0,0 +1,217 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.parser;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.ExpressionType;
+import org.apache.pinot.common.request.Function;
+import org.apache.pinot.common.request.PinotQuery;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import org.apache.pinot.query.planner.logical.RexExpression;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+import org.apache.pinot.sql.parsers.SqlCompilationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Calcite parser to convert SQL expressions into {@link Expression}.
+ *
+ * <p>This class is extracted from {@link org.apache.pinot.sql.parsers.CalciteSqlParser}. It contains the logic
+ * to parsed {@link org.apache.calcite.rex.RexNode}, in the format of {@link RexExpression} and convert them into
+ * Thrift {@link Expression} format.
+ */
+public class CalciteRexExpressionParser {
+  private static final Logger LOGGER = LoggerFactory.getLogger(CalciteRexExpressionParser.class);
+
+  private CalciteRexExpressionParser() {
+    // do not instantiate.
+  }
+
+  // --------------------------------------------------------------------------
+  // Relational conversion Utils
+  // --------------------------------------------------------------------------
+
+  public static List<Expression> convertSelectList(List<RexExpression> rexNodeList, PinotQuery pinotQuery) {
+    List<Expression> selectExpr = new ArrayList<>();
+
+    final Iterator<RexExpression> iterator = rexNodeList.iterator();
+    while (iterator.hasNext()) {
+      final RexExpression next = iterator.next();
+      selectExpr.add(toExpression(next, pinotQuery));
+    }
+
+    return selectExpr;
+  }
+
+  private static List<Expression> convertDistinctSelectList(RexExpression.FunctionCall rexCall, PinotQuery pinotQuery) {
+    List<Expression> selectExpr = new ArrayList<>();
+    selectExpr.add(convertDistinctAndSelectListToFunctionExpression(rexCall, pinotQuery));
+    return selectExpr;
+  }
+
+  private static List<Expression> convertOrderByList(RexExpression.FunctionCall rexCall, PinotQuery pinotQuery) {
+    Preconditions.checkState(rexCall.getKind() == SqlKind.ORDER_BY);
+    List<Expression> orderByExpr = new ArrayList<>();
+
+    final Iterator<RexExpression> iterator = rexCall.getFunctionOperands().iterator();
+    while (iterator.hasNext()) {
+      final RexExpression next = iterator.next();
+      orderByExpr.add(convertOrderBy(next, pinotQuery));
+    }
+    return orderByExpr;
+  }
+
+  private static Expression convertOrderBy(RexExpression rexNode, PinotQuery pinotQuery) {
+    final SqlKind kind = rexNode.getKind();
+    Expression expression;
+    switch (kind) {
+      case DESCENDING:
+        RexExpression.FunctionCall rexCall = (RexExpression.FunctionCall) rexNode;
+        expression = RequestUtils.getFunctionExpression("DESC");
+        expression.getFunctionCall().addToOperands(toExpression(rexCall.getFunctionOperands().get(0), pinotQuery));
+        break;
+      case IDENTIFIER:
+      default:
+        expression = RequestUtils.getFunctionExpression("ASC");
+        expression.getFunctionCall().addToOperands(toExpression(rexNode, pinotQuery));
+        break;
+    }
+    return expression;
+  }
+
+  private static Expression convertDistinctAndSelectListToFunctionExpression(RexExpression.FunctionCall rexCall,
+      PinotQuery pinotQuery) {
+    String functionName = AggregationFunctionType.DISTINCT.getName();
+    Expression functionExpression = RequestUtils.getFunctionExpression(functionName);
+    for (RexExpression node : rexCall.getFunctionOperands()) {
+      Expression columnExpression = toExpression(node, pinotQuery);
+      if (columnExpression.getType() == ExpressionType.IDENTIFIER && columnExpression.getIdentifier().getName()
+          .equals("*")) {
+        throw new SqlCompilationException(
+            "Syntax error: Pinot currently does not support DISTINCT with *. Please specify each column name after "
+                + "DISTINCT keyword");
+      } else if (columnExpression.getType() == ExpressionType.FUNCTION) {
+        Function functionCall = columnExpression.getFunctionCall();
+        String function = functionCall.getOperator();
+        if (AggregationFunctionType.isAggregationFunction(function)) {
+          throw new SqlCompilationException(
+              "Syntax error: Use of DISTINCT with aggregation functions is not supported");
+        }
+      }
+      functionExpression.getFunctionCall().addToOperands(columnExpression);
+    }
+    return functionExpression;
+  }
+
+  public static Expression toExpression(RexExpression rexNode, PinotQuery pinotQuery) {
+    LOGGER.debug("Current processing RexNode: {}, node.getKind(): {}", rexNode, rexNode.getKind());
+    switch (rexNode.getKind()) {
+      case INPUT_REF:
+        return inputRefToIdentifier((RexExpression.InputRef) rexNode, pinotQuery);
+      case LITERAL:
+        return rexLiteralToExpression((RexExpression.Literal) rexNode);
+      default:
+        return compileFunctionExpression((RexExpression.FunctionCall) rexNode, pinotQuery);
+    }
+  }
+
+  private static Expression rexLiteralToExpression(RexExpression.Literal rexLiteral) {
+    RelDataType type = rexLiteral.getDataType();
+    switch (type.getSqlTypeName()) {
+      default:
+        return RequestUtils.getLiteralExpression(rexLiteral.getValue());
+    }
+  }
+
+  private static Expression inputRefToIdentifier(RexExpression.InputRef inputRef, PinotQuery pinotQuery) {
+    List<Expression> selectList = pinotQuery.getSelectList();
+    return selectList.get(inputRef.getIndex());
+  }
+
+  private static Expression compileFunctionExpression(RexExpression.FunctionCall rexCall, PinotQuery pinotQuery) {
+    SqlKind functionKind = rexCall.getKind();
+    String functionName;
+    switch (functionKind) {
+      case AND:
+        return compileAndExpression(rexCall, pinotQuery);
+      case OR:
+        return compileOrExpression(rexCall, pinotQuery);
+      case COUNT:
+      case OTHER:
+      case OTHER_FUNCTION:
+      case DOT:
+      default:
+        functionName = functionKind.name();
+        break;
+    }
+    // When there is no argument, set an empty list as the operands
+    List<RexExpression> childNodes = rexCall.getFunctionOperands();
+    List<Expression> operands = new ArrayList<>(childNodes.size());
+    for (RexExpression childNode : childNodes) {
+      operands.add(toExpression(childNode, pinotQuery));
+    }
+    ParserUtils.validateFunction(functionName, operands);
+    Expression functionExpression = RequestUtils.getFunctionExpression(functionName);
+    functionExpression.getFunctionCall().setOperands(operands);
+    return functionExpression;
+  }
+
+  /**
+   * Helper method to flatten the operands for the AND expression.
+   */
+  private static Expression compileAndExpression(RexExpression.FunctionCall andNode, PinotQuery pinotQuery) {
+    List<Expression> operands = new ArrayList<>();
+    for (RexExpression childNode : andNode.getFunctionOperands()) {
+      if (childNode.getKind() == SqlKind.AND) {
+        Expression childAndExpression = compileAndExpression((RexExpression.FunctionCall) childNode, pinotQuery);
+        operands.addAll(childAndExpression.getFunctionCall().getOperands());
+      } else {
+        operands.add(toExpression(childNode, pinotQuery));
+      }
+    }
+    Expression andExpression = RequestUtils.getFunctionExpression(SqlKind.AND.name());
+    andExpression.getFunctionCall().setOperands(operands);
+    return andExpression;
+  }
+
+  /**
+   * Helper method to flatten the operands for the OR expression.
+   */
+  private static Expression compileOrExpression(RexExpression.FunctionCall orNode, PinotQuery pinotQuery) {
+    List<Expression> operands = new ArrayList<>();
+    for (RexExpression childNode : orNode.getFunctionOperands()) {
+      if (childNode.getKind() == SqlKind.OR) {
+        Expression childAndExpression = compileOrExpression((RexExpression.FunctionCall) childNode, pinotQuery);
+        operands.addAll(childAndExpression.getFunctionCall().getOperands());
+      } else {
+        operands.add(toExpression(childNode, pinotQuery));
+      }
+    }
+    Expression andExpression = RequestUtils.getFunctionExpression(SqlKind.OR.name());
+    andExpression.getFunctionCall().setOperands(operands);
+    return andExpression;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteSqlParser.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteSqlParser.java
index d67896f9b9..d2fd054833 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteSqlParser.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteSqlParser.java
@@ -57,7 +57,7 @@ import org.slf4j.LoggerFactory;
  * This class provide API to parse a SQL string into Pinot query {@link SqlNode}.
  *
  * <p>This class is extracted from {@link org.apache.pinot.sql.parsers.CalciteSqlParser}. It contains the logic
- * to parsed SQL into {@link SqlNode} and use {@link QueryRewriter} to rewrite the query with Pinot specific
+ * to parsed SQL string into {@link SqlNode} and use {@link QueryRewriter} to rewrite the query with Pinot specific
  * contextual info.
  */
 public class CalciteSqlParser {
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/ParserUtils.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/ParserUtils.java
index 5422382509..ec6494fea2 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/ParserUtils.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/ParserUtils.java
@@ -18,11 +18,15 @@
  */
 package org.apache.pinot.query.parser;
 
+import java.util.List;
 import java.util.regex.Pattern;
 import org.apache.calcite.config.Lex;
 import org.apache.calcite.sql.parser.SqlParser;
 import org.apache.calcite.sql.parser.babel.SqlBabelParserImpl;
 import org.apache.calcite.sql.validate.SqlConformanceEnum;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.sql.parsers.SqlCompilationException;
 
 
 /**
@@ -60,4 +64,55 @@ final class ParserUtils {
   private ParserUtils() {
     // do not instantiate.
   }
+
+  public static String canonicalize(String functionName) {
+    return StringUtils.remove(functionName, '_').toLowerCase();
+  }
+
+  public static boolean isSameFunction(String function1, String function2) {
+    return canonicalize(function1).equals(canonicalize(function2));
+  }
+
+  public static void validateFunction(String functionName, List<Expression> operands) {
+    switch (canonicalize(functionName)) {
+      case "jsonextractscalar":
+        validateJsonExtractScalarFunction(operands);
+        break;
+      case "jsonextractkey":
+        validateJsonExtractKeyFunction(operands);
+        break;
+      default:
+        break;
+    }
+  }
+
+  private static void validateJsonExtractScalarFunction(List<Expression> operands) {
+    int numOperands = operands.size();
+
+    // Check that there are exactly 3 or 4 arguments
+    if (numOperands != 3 && numOperands != 4) {
+      throw new SqlCompilationException(
+          "Expect 3 or 4 arguments for transform function: jsonExtractScalar(jsonFieldName, 'jsonPath', "
+              + "'resultsType', ['defaultValue'])");
+    }
+    if (!operands.get(1).isSetLiteral() || !operands.get(2).isSetLiteral() || (numOperands == 4 && !operands.get(3)
+        .isSetLiteral())) {
+      throw new SqlCompilationException(
+          "Expect the 2nd/3rd/4th argument of transform function: jsonExtractScalar(jsonFieldName, 'jsonPath',"
+              + " 'resultsType', ['defaultValue']) to be a single-quoted literal value.");
+    }
+  }
+
+  private static void validateJsonExtractKeyFunction(List<Expression> operands) {
+    // Check that there are exactly 2 arguments
+    if (operands.size() != 2) {
+      throw new SqlCompilationException(
+          "Expect 2 arguments are required for transform function: jsonExtractKey(jsonFieldName, 'jsonPath')");
+    }
+    if (!operands.get(1).isSetLiteral()) {
+      throw new SqlCompilationException(
+          "Expect the 2nd argument for transform function: jsonExtractKey(jsonFieldName, 'jsonPath') to be a "
+              + "single-quoted literal value.");
+    }
+  }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java
index cb075bb9f3..a93bddc4f7 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java
@@ -19,7 +19,8 @@
 package org.apache.pinot.query.planner;
 
 import java.util.Map;
-import org.apache.pinot.query.planner.nodes.StageNode;
+import org.apache.pinot.query.planner.logical.LogicalPlanner;
+import org.apache.pinot.query.planner.stage.StageNode;
 
 
 /**
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java
index 90dd22a8aa..8e691003c9 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java
@@ -24,8 +24,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import org.apache.pinot.core.transport.ServerInstance;
-import org.apache.pinot.query.planner.nodes.StageNode;
-import org.apache.pinot.query.planner.nodes.TableScanNode;
+import org.apache.pinot.query.planner.stage.StageNode;
+import org.apache.pinot.query.planner.stage.TableScanNode;
 
 
 /**
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/hints/PinotRelationalHints.java
similarity index 62%
copy from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
copy to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/hints/PinotRelationalHints.java
index 0aa8c94ec8..19a9daa54f 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/hints/PinotRelationalHints.java
@@ -16,23 +16,19 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner.hints;
 
+import org.apache.calcite.rel.hint.RelHint;
 
 
-public class CalcNode extends AbstractStageNode {
-  private String _expression;
-
-  public CalcNode(int stageId) {
-    super(stageId);
-  }
-
-  public CalcNode(int stageId, String expression) {
-    super(stageId);
-    _expression = expression;
-  }
+/**
+ * Provide certain relational hint to query planner for better optimization.
+ */
+public class PinotRelationalHints {
+  public static final RelHint USE_HASH_DISTRIBUTE = RelHint.builder("USE_HASH_DISTRIBUTE").build();
+  public static final RelHint USE_BROADCAST_DISTRIBUTE = RelHint.builder("USE_BROADCAST_DISTRIBUTE").build();
 
-  public String getExpression() {
-    return _expression;
+  private PinotRelationalHints() {
+    // do not instantiate.
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/LogicalPlanner.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/LogicalPlanner.java
similarity index 97%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/LogicalPlanner.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/LogicalPlanner.java
index 9844916490..0e317560e9 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/LogicalPlanner.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/LogicalPlanner.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner;
+package org.apache.pinot.query.planner.logical;
 
 import java.util.ArrayList;
 import java.util.List;
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToStageConverter.java
similarity index 75%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToStageConverter.java
index 572302ef92..3750437f7f 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToStageConverter.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner;
+package org.apache.pinot.query.planner.logical;
 
 import com.google.common.base.Preconditions;
 import java.util.Collections;
@@ -24,19 +24,21 @@ import java.util.List;
 import java.util.stream.Collectors;
 import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.core.JoinRelType;
-import org.apache.calcite.rel.logical.LogicalCalc;
+import org.apache.calcite.rel.logical.LogicalFilter;
 import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rel.logical.LogicalProject;
 import org.apache.calcite.rel.logical.LogicalTableScan;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rel.type.RelDataTypeField;
 import org.apache.calcite.rex.RexCall;
 import org.apache.calcite.rex.RexInputRef;
 import org.apache.calcite.sql.SqlKind;
-import org.apache.pinot.query.planner.nodes.CalcNode;
-import org.apache.pinot.query.planner.nodes.JoinNode;
-import org.apache.pinot.query.planner.nodes.StageNode;
-import org.apache.pinot.query.planner.nodes.TableScanNode;
 import org.apache.pinot.query.planner.partitioning.FieldSelectionKeySelector;
+import org.apache.pinot.query.planner.stage.FilterNode;
+import org.apache.pinot.query.planner.stage.JoinNode;
+import org.apache.pinot.query.planner.stage.ProjectNode;
+import org.apache.pinot.query.planner.stage.StageNode;
+import org.apache.pinot.query.planner.stage.TableScanNode;
 
 
 /**
@@ -57,27 +59,32 @@ public final class RelToStageConverter {
    * @return stage node.
    */
   public static StageNode toStageNode(RelNode node, int currentStageId) {
-    if (node instanceof LogicalCalc) {
-      return convertLogicalCal((LogicalCalc) node, currentStageId);
-    } else if (node instanceof LogicalTableScan) {
+    if (node instanceof LogicalTableScan) {
       return convertLogicalTableScan((LogicalTableScan) node, currentStageId);
     } else if (node instanceof LogicalJoin) {
       return convertLogicalJoin((LogicalJoin) node, currentStageId);
+    } else if (node instanceof LogicalProject) {
+      return convertLogicalProject((LogicalProject) node, currentStageId);
+    } else if (node instanceof LogicalFilter) {
+      return convertLogicalFilter((LogicalFilter) node, currentStageId);
     } else {
       throw new UnsupportedOperationException("Unsupported logical plan node: " + node);
     }
   }
 
+  private static StageNode convertLogicalProject(LogicalProject node, int currentStageId) {
+    return new ProjectNode(currentStageId, node.getRowType(), node.getProjects());
+  }
+
+  private static StageNode convertLogicalFilter(LogicalFilter node, int currentStageId) {
+    return new FilterNode(currentStageId, node.getRowType(), node.getCondition());
+  }
+
   private static StageNode convertLogicalTableScan(LogicalTableScan node, int currentStageId) {
     String tableName = node.getTable().getQualifiedName().get(0);
     List<String> columnNames = node.getRowType().getFieldList().stream()
         .map(RelDataTypeField::getName).collect(Collectors.toList());
-    return new TableScanNode(currentStageId, tableName, columnNames);
-  }
-
-  private static StageNode convertLogicalCal(LogicalCalc node, int currentStageId) {
-    // TODO: support actual calcNode
-    return new CalcNode(currentStageId, node.getDigest());
+    return new TableScanNode(currentStageId, node.getRowType(), tableName, columnNames);
   }
 
   private static StageNode convertLogicalJoin(LogicalJoin node, int currentStageId) {
@@ -95,7 +102,7 @@ public final class RelToStageConverter {
     FieldSelectionKeySelector leftFieldSelectionKeySelector = new FieldSelectionKeySelector(leftOperandIndex);
     FieldSelectionKeySelector rightFieldSelectionKeySelector =
           new FieldSelectionKeySelector(rightOperandIndex - leftRowType.getFieldNames().size());
-    return new JoinNode(currentStageId, joinType, Collections.singletonList(new JoinNode.JoinClause(
+    return new JoinNode(currentStageId, node.getRowType(), joinType, Collections.singletonList(new JoinNode.JoinClause(
         leftFieldSelectionKeySelector, rightFieldSelectionKeySelector)));
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RexExpression.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RexExpression.java
new file mode 100644
index 0000000000..899636ab4d
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RexExpression.java
@@ -0,0 +1,157 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.logical;
+
+import java.math.BigDecimal;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.util.NlsString;
+import org.apache.pinot.query.planner.serde.ProtoProperties;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+
+/**
+ * {@code RexExpression} is the serializable format of the {@link RexNode}.
+ */
+public abstract class RexExpression {
+  @ProtoProperties
+  protected SqlKind _sqlKind;
+  @ProtoProperties
+  protected RelDataType _dataType;
+
+  public SqlKind getKind() {
+    return _sqlKind;
+  }
+
+  public RelDataType getDataType() {
+    return _dataType;
+  }
+
+  public static RexExpression toRexExpression(RexNode rexNode) {
+    if (rexNode instanceof RexInputRef) {
+      return new RexExpression.InputRef(((RexInputRef) rexNode).getIndex());
+    } else if (rexNode instanceof RexLiteral) {
+      RexLiteral rexLiteral = ((RexLiteral) rexNode);
+      return new RexExpression.Literal(rexLiteral.getType(), rexLiteral.getTypeName(), rexLiteral.getValue());
+    } else if (rexNode instanceof RexCall) {
+      RexCall rexCall = (RexCall) rexNode;
+      List<RexExpression> operands = rexCall.getOperands().stream().map(RexExpression::toRexExpression)
+          .collect(Collectors.toList());
+      return new RexExpression.FunctionCall(rexCall.getKind(), rexCall.getType(), rexCall.getOperator().getName(),
+          operands);
+    } else {
+      throw new IllegalArgumentException("Unsupported RexNode type with SqlKind: " + rexNode.getKind());
+    }
+  }
+
+  private static Comparable convertLiteral(Comparable value, SqlTypeName sqlTypeName, RelDataType dataType) {
+    switch (sqlTypeName) {
+      case BOOLEAN:
+        return (boolean) value;
+      case DECIMAL:
+        switch (dataType.getSqlTypeName()) {
+          case INTEGER:
+            return ((BigDecimal) value).intValue();
+          case BIGINT:
+            return ((BigDecimal) value).longValue();
+          case FLOAT:
+            return ((BigDecimal) value).floatValue();
+          case DOUBLE:
+          default:
+            return ((BigDecimal) value).doubleValue();
+        }
+      case CHAR:
+        switch (dataType.getSqlTypeName()) {
+          case VARCHAR:
+            return ((NlsString) value).getValue();
+          default:
+            return value;
+        }
+      default:
+        return value;
+    }
+  }
+
+  public static class InputRef extends RexExpression {
+    @ProtoProperties
+    private int _index;
+
+    public InputRef() {
+    }
+
+    public InputRef(int index) {
+      _sqlKind = SqlKind.INPUT_REF;
+      _index = index;
+    }
+
+    public int getIndex() {
+      return _index;
+    }
+  }
+
+  public static class Literal extends RexExpression {
+    @ProtoProperties
+    private Object _value;
+
+    public Literal() {
+    }
+
+    public Literal(RelDataType dataType, SqlTypeName sqlTypeName, @Nullable Comparable value) {
+      _sqlKind = SqlKind.LITERAL;
+      _dataType = dataType;
+      _value = convertLiteral(value, sqlTypeName, dataType);
+    }
+
+    public Object getValue() {
+      return _value;
+    }
+  }
+
+  public static class FunctionCall extends RexExpression {
+    @ProtoProperties
+    private String _functionName;
+    @ProtoProperties
+    private List<RexExpression> _functionOperands;
+
+    public FunctionCall() {
+    }
+
+    public FunctionCall(SqlKind sqlKind, RelDataType type, String functionName, List<RexExpression> functionOperands) {
+      _sqlKind = sqlKind;
+      _dataType = type;
+      _functionName = functionName;
+      _functionOperands = functionOperands;
+    }
+
+    public String getFunctionName() {
+      return _functionName;
+    }
+
+    public List<RexExpression> getFunctionOperands() {
+      return _functionOperands;
+    }
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StagePlanner.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/StagePlanner.java
similarity index 79%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StagePlanner.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/StagePlanner.java
index f6ec38738c..b5dfab8689 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StagePlanner.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/StagePlanner.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner;
+package org.apache.pinot.query.planner.logical;
 
 import java.util.HashMap;
 import java.util.List;
@@ -26,9 +26,12 @@ import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.RelRoot;
 import org.apache.calcite.rel.logical.LogicalExchange;
 import org.apache.pinot.query.context.PlannerContext;
-import org.apache.pinot.query.planner.nodes.MailboxReceiveNode;
-import org.apache.pinot.query.planner.nodes.MailboxSendNode;
-import org.apache.pinot.query.planner.nodes.StageNode;
+import org.apache.pinot.query.planner.QueryPlan;
+import org.apache.pinot.query.planner.StageMetadata;
+import org.apache.pinot.query.planner.partitioning.FieldSelectionKeySelector;
+import org.apache.pinot.query.planner.stage.MailboxReceiveNode;
+import org.apache.pinot.query.planner.stage.MailboxSendNode;
+import org.apache.pinot.query.planner.stage.StageNode;
 import org.apache.pinot.query.routing.WorkerManager;
 
 
@@ -66,12 +69,13 @@ public class StagePlanner {
     // walk the plan and create stages.
     StageNode globalStageRoot = walkRelPlan(relRoot, getNewStageId());
 
-    // global root needs to send results back to the ROOT, a.k.a. the client response node.
-    // the last stage is always a broadcast-gather.
+    // global root needs to send results back to the ROOT, a.k.a. the client response node. the last stage only has one
+    // receiver so doesn't matter what the exchange type is. setting it to SINGLETON by default.
     StageNode globalReceiverNode =
-        new MailboxReceiveNode(0, globalStageRoot.getStageId(), RelDistribution.Type.BROADCAST_DISTRIBUTED);
-    StageNode globalSenderNode = new MailboxSendNode(globalStageRoot.getStageId(), globalReceiverNode.getStageId(),
-        RelDistribution.Type.BROADCAST_DISTRIBUTED);
+        new MailboxReceiveNode(0, relRoot.getRowType(), globalStageRoot.getStageId(),
+            RelDistribution.Type.SINGLETON);
+    StageNode globalSenderNode = new MailboxSendNode(globalStageRoot.getStageId(), relRoot.getRowType(),
+        globalReceiverNode.getStageId(), RelDistribution.Type.SINGLETON);
     globalSenderNode.addInput(globalStageRoot);
     _queryStageMap.put(globalSenderNode.getStageId(), globalSenderNode);
     StageMetadata stageMetadata = _stageMetadataMap.get(globalSenderNode.getStageId());
@@ -95,12 +99,15 @@ public class StagePlanner {
     if (isExchangeNode(node)) {
       // 1. exchangeNode always have only one input, get its input converted as a new stage root.
       StageNode nextStageRoot = walkRelPlan(node.getInput(0), getNewStageId());
-      RelDistribution.Type exchangeType = ((LogicalExchange) node).distribution.getType();
+      RelDistribution distribution = ((LogicalExchange) node).getDistribution();
+      RelDistribution.Type exchangeType = distribution.getType();
 
       // 2. make an exchange sender and receiver node pair
-      StageNode mailboxReceiver = new MailboxReceiveNode(currentStageId, nextStageRoot.getStageId(), exchangeType);
-      StageNode mailboxSender = new MailboxSendNode(nextStageRoot.getStageId(), mailboxReceiver.getStageId(),
+      StageNode mailboxReceiver = new MailboxReceiveNode(currentStageId, node.getRowType(), nextStageRoot.getStageId(),
           exchangeType);
+      StageNode mailboxSender = new MailboxSendNode(nextStageRoot.getStageId(), node.getRowType(),
+          mailboxReceiver.getStageId(), exchangeType, exchangeType == RelDistribution.Type.HASH_DISTRIBUTED
+          ? new FieldSelectionKeySelector(distribution.getKeys().get(0)) : null);
       mailboxSender.addInput(nextStageRoot);
 
       // 3. put the sender side as a completed stage.
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
index 95991d558b..14f263c44f 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
@@ -18,12 +18,15 @@
  */
 package org.apache.pinot.query.planner.partitioning;
 
+import org.apache.pinot.query.planner.serde.ProtoProperties;
+
 
 /**
  * The {@code FieldSelectionKeySelector} simply extract a column value out from a row array {@link Object[]}.
  */
 public class FieldSelectionKeySelector implements KeySelector<Object[], Object> {
 
+  @ProtoProperties
   private int _columnIndex;
 
   public FieldSelectionKeySelector() {
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
index eaefb77604..e6b6e598a2 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
@@ -18,8 +18,6 @@
  */
 package org.apache.pinot.query.planner.partitioning;
 
-import java.io.Serializable;
-
 
 /**
  * The {@code KeySelector} provides a partitioning function to encode a specific input data type into a key.
@@ -28,7 +26,7 @@ import java.io.Serializable;
  *
  * <p>Key selector should always produce the same selection hash key when the same input is provided.
  */
-public interface KeySelector<IN, OUT> extends Serializable {
+public interface KeySelector<IN, OUT> {
 
   /**
    * Extract the key out of an input data construct.
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializable.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/serde/ProtoProperties.java
similarity index 63%
copy from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializable.java
copy to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/serde/ProtoProperties.java
index 2b99003e87..5a10b91941 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializable.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/serde/ProtoProperties.java
@@ -22,14 +22,19 @@
  * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
  *  @generated
  */
-package org.apache.pinot.query.planner.nodes.serde;
+package org.apache.pinot.query.planner.serde;
 
-import org.apache.pinot.common.proto.Plan;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
 
 
-public interface ProtoSerializable {
-
-  void setObjectField(Plan.ObjectField objFields);
-
-  Plan.ObjectField getObjectField();
+/**
+ * Annotation {@code ProtoProperties} indicates whether a field defined in a
+ * {@link org.apache.pinot.query.planner.stage.StageNode} should be serialized.
+ */
+@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ProtoProperties {
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializable.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/serde/ProtoSerializable.java
similarity index 62%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializable.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/serde/ProtoSerializable.java
index 2b99003e87..f10cb9dd35 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializable.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/serde/ProtoSerializable.java
@@ -22,14 +22,28 @@
  * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
  *  @generated
  */
-package org.apache.pinot.query.planner.nodes.serde;
+package org.apache.pinot.query.planner.serde;
 
 import org.apache.pinot.common.proto.Plan;
 
 
+/**
+ * Interface to convert between proto serialized payload and object.
+ *
+ * <p>Classes that implement {@code ProtoSerializable} should provide methods to convert to and from
+ * {@link Plan.ObjectField}.
+ */
 public interface ProtoSerializable {
 
-  void setObjectField(Plan.ObjectField objFields);
+  /**
+   * Setting object's own member variable from a serialized {@link Plan.ObjectField}.
+   * @param objFields the serialized ObjectField.
+   */
+  void fromObjectField(Plan.ObjectField objFields);
 
-  Plan.ObjectField getObjectField();
+  /**
+   * convert the object to a serialized {@link Plan.ObjectField}.
+   * @return the serialized ObjectField.
+   */
+  Plan.ObjectField toObjectField();
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializationUtils.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/serde/ProtoSerializationUtils.java
similarity index 61%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializationUtils.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/serde/ProtoSerializationUtils.java
index c30295a101..d0d3b1dc3e 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializationUtils.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/serde/ProtoSerializationUtils.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes.serde;
+package org.apache.pinot.query.planner.serde;
 
 import com.google.common.base.Preconditions;
 import java.lang.reflect.Field;
@@ -28,48 +28,75 @@ import java.util.Set;
 import org.apache.pinot.common.proto.Plan;
 
 
+/**
+ * Utils to convert automatically from/to object that's implementing {@link ProtoSerializable}.
+ */
 @SuppressWarnings({"rawtypes", "unchecked"})
 public class ProtoSerializationUtils {
   private static final String ENUM_VALUE_KEY = "ENUM_VALUE_KEY";
+  private static final String NULL_OBJECT_CLASSNAME = "null";
+  private static final Plan.ObjectField NULL_OBJECT_VALUE = Plan.ObjectField.newBuilder()
+      .setObjectClassName(NULL_OBJECT_CLASSNAME).build();
 
   private ProtoSerializationUtils() {
     // do not instantiate.
   }
 
-  public static void fromObjectField(Object object, Plan.ObjectField objectField) {
+  /**
+   * Reflectively set object's field based on {@link Plan.ObjectField} provided.
+   *
+   * @param object the object to be set.
+   * @param objectField the proto ObjectField from which the object will be set.
+   */
+  public static void setObjectFieldToObject(Object object, Plan.ObjectField objectField) {
     Map<String, Plan.MemberVariableField> memberVariablesMap = objectField.getMemberVariablesMap();
-    try {
-      for (Map.Entry<String, Plan.MemberVariableField> e : memberVariablesMap.entrySet()) {
-        Object memberVarObject = constructMemberVariable(e.getValue());
-        if (memberVarObject != null) {
-          Field declaredField = object.getClass().getDeclaredField(e.getKey());
-          declaredField.setAccessible(true);
-          declaredField.set(object, memberVarObject);
+    for (Map.Entry<String, Plan.MemberVariableField> e : memberVariablesMap.entrySet()) {
+      try {
+        Field declaredField = object.getClass().getDeclaredField(e.getKey());
+        if (declaredField.isAnnotationPresent(ProtoProperties.class)) {
+          Object memberVarObject = constructMemberVariable(e.getValue());
+          if (memberVarObject != null) {
+            declaredField.setAccessible(true);
+            declaredField.set(object, memberVarObject);
+          }
         }
+      } catch (NoSuchFieldException | IllegalAccessException ex) {
+        throw new IllegalStateException("Unable to set Object " + object.getClass() + " on field " + e.getKey()
+            + "with object of type: " + objectField.getObjectClassName(), ex);
       }
-    } catch (NoSuchFieldException | IllegalAccessException e) {
-      throw new IllegalStateException("Unable to set Object field for: " + objectField.getObjectClassName(), e);
     }
   }
 
-  public static Plan.ObjectField toObjectField(Object object) {
-    Plan.ObjectField.Builder builder = Plan.ObjectField.newBuilder();
-    builder.setObjectClassName(object.getClass().getName());
-    // special handling for enum
-    if (object instanceof Enum) {
-      builder.putMemberVariables(ENUM_VALUE_KEY, serializeMemberVariable(((Enum) object).name()));
-    } else {
-      try {
-        for (Field field : object.getClass().getDeclaredFields()) {
-          field.setAccessible(true);
-          Object fieldObject = field.get(object);
-          builder.putMemberVariables(field.getName(), serializeMemberVariable(fieldObject));
+  /**
+   * Convert object into a proto {@link Plan.ObjectField}.
+   *
+   * @param object object to be converted.
+   * @return the converted proto ObjectField.
+   */
+  public static Plan.ObjectField convertObjectToObjectField(Object object) {
+    if (object != null) {
+      Plan.ObjectField.Builder builder = Plan.ObjectField.newBuilder();
+      builder.setObjectClassName(object.getClass().getName());
+      // special handling for enum
+      if (object instanceof Enum) {
+        builder.putMemberVariables(ENUM_VALUE_KEY, serializeMemberVariable(((Enum) object).name()));
+      } else {
+        try {
+          for (Field field : object.getClass().getDeclaredFields()) {
+            if (field.isAnnotationPresent(ProtoProperties.class)) {
+              field.setAccessible(true);
+              Object fieldObject = field.get(object);
+              builder.putMemberVariables(field.getName(), serializeMemberVariable(fieldObject));
+            }
+          }
+        } catch (IllegalAccessException e) {
+          throw new IllegalStateException("Unable to serialize Object: " + object.getClass(), e);
         }
-      } catch (IllegalAccessException e) {
-        throw new IllegalStateException("Unable to serialize Object: " + object.getClass(), e);
       }
+      return builder.build();
+    } else {
+      return NULL_OBJECT_VALUE;
     }
-    return builder.build();
   }
 
   // --------------------------------------------------------------------------
@@ -88,6 +115,10 @@ public class ProtoSerializationUtils {
     return Plan.LiteralField.newBuilder().setLongField(val).build();
   }
 
+  private static Plan.LiteralField floatField(float val) {
+    return Plan.LiteralField.newBuilder().setFloatField(val).build();
+  }
+
   private static Plan.LiteralField doubleField(double val) {
     return Plan.LiteralField.newBuilder().setDoubleField(val).build();
   }
@@ -104,6 +135,8 @@ public class ProtoSerializationUtils {
       builder.setLiteralField(intField((Integer) fieldObject));
     } else if (fieldObject instanceof Long) {
       builder.setLiteralField(longField((Long) fieldObject));
+    } else if (fieldObject instanceof Float) {
+      builder.setLiteralField(floatField((Float) fieldObject));
     } else if (fieldObject instanceof Double) {
       builder.setLiteralField(doubleField((Double) fieldObject));
     } else if (fieldObject instanceof String) {
@@ -113,7 +146,7 @@ public class ProtoSerializationUtils {
     } else if (fieldObject instanceof Map) {
       builder.setMapField(serializeMapMemberVariable(fieldObject));
     } else {
-      builder.setObjectField(toObjectField(fieldObject));
+      builder.setObjectField(convertObjectToObjectField(fieldObject));
     }
     return builder.build();
   }
@@ -165,6 +198,8 @@ public class ProtoSerializationUtils {
         return literalField.getIntField();
       case LONGFIELD:
         return literalField.getLongField();
+      case FLOATFIELD:
+        return literalField.getFloatField();
       case DOUBLEFIELD:
         return literalField.getDoubleField();
       case STRINGFIELD:
@@ -183,7 +218,7 @@ public class ProtoSerializationUtils {
     return list;
   }
 
-  private static Object constructMap(Plan.MapField mapField) {
+  private static Map constructMap(Plan.MapField mapField) {
     Map map = new HashMap();
     for (Map.Entry<String, Plan.MemberVariableField> e : mapField.getContentMap().entrySet()) {
       map.put(e.getKey(), constructMemberVariable(e.getValue()));
@@ -192,18 +227,22 @@ public class ProtoSerializationUtils {
   }
 
   private static Object constructObject(Plan.ObjectField objectField) {
-    try {
-      Class<?> clazz = Class.forName(objectField.getObjectClassName());
-      if (clazz.isEnum()) {
-        return Enum.valueOf((Class<Enum>) clazz,
-            objectField.getMemberVariablesOrDefault(ENUM_VALUE_KEY, null).getLiteralField().getStringField());
-      } else {
-        Object obj = clazz.newInstance();
-        fromObjectField(obj, objectField);
-        return obj;
+    if (!NULL_OBJECT_CLASSNAME.equals(objectField.getObjectClassName())) {
+      try {
+        Class<?> clazz = Class.forName(objectField.getObjectClassName());
+        if (clazz.isEnum()) {
+          return Enum.valueOf((Class<Enum>) clazz,
+              objectField.getMemberVariablesOrDefault(ENUM_VALUE_KEY, null).getLiteralField().getStringField());
+        } else {
+          Object obj = clazz.newInstance();
+          setObjectFieldToObject(obj, objectField);
+          return obj;
+        }
+      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
+        throw new IllegalStateException("Unable to create Object of type: " + objectField.getObjectClassName(), e);
       }
-    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
-      throw new IllegalStateException("Unable to create Object of type: " + objectField.getObjectClassName(), e);
+    } else {
+      return null;
     }
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/AbstractStageNode.java
similarity index 67%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/AbstractStageNode.java
index ed1fc9ba3e..bdcccea355 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/AbstractStageNode.java
@@ -16,19 +16,25 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner.stage;
 
 import java.util.ArrayList;
 import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
 import org.apache.pinot.common.proto.Plan;
-import org.apache.pinot.query.planner.nodes.serde.ProtoSerializable;
-import org.apache.pinot.query.planner.nodes.serde.ProtoSerializationUtils;
+import org.apache.pinot.query.planner.serde.ProtoProperties;
+import org.apache.pinot.query.planner.serde.ProtoSerializable;
+import org.apache.pinot.query.planner.serde.ProtoSerializationUtils;
 
 
 public abstract class AbstractStageNode implements StageNode, ProtoSerializable {
 
+  @ProtoProperties
   protected final int _stageId;
+  @ProtoProperties
   protected final List<StageNode> _inputs;
+  @ProtoProperties
+  protected RelDataType _rowType;
 
   public AbstractStageNode(int stageId) {
     _stageId = stageId;
@@ -51,12 +57,16 @@ public abstract class AbstractStageNode implements StageNode, ProtoSerializable
   }
 
   @Override
-  public void setObjectField(Plan.ObjectField objectField) {
-    ProtoSerializationUtils.fromObjectField(this, objectField);
+  public void fromObjectField(Plan.ObjectField objectField) {
+    ProtoSerializationUtils.setObjectFieldToObject(this, objectField);
   }
 
   @Override
-  public Plan.ObjectField getObjectField() {
-    return ProtoSerializationUtils.toObjectField(this);
+  public Plan.ObjectField toObjectField() {
+    return ProtoSerializationUtils.convertObjectToObjectField(this);
+  }
+
+  public RelDataType getRowType() {
+    return _rowType;
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/FilterNode.java
similarity index 56%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/FilterNode.java
index 0aa8c94ec8..52df4ed5d3 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/FilterNode.java
@@ -16,23 +16,29 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner.stage;
 
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexNode;
+import org.apache.pinot.query.planner.logical.RexExpression;
+import org.apache.pinot.query.planner.serde.ProtoProperties;
 
 
-public class CalcNode extends AbstractStageNode {
-  private String _expression;
+public class FilterNode extends AbstractStageNode {
+  @ProtoProperties
+  private RexExpression _condition;
 
-  public CalcNode(int stageId) {
+  public FilterNode(int stageId) {
     super(stageId);
   }
 
-  public CalcNode(int stageId, String expression) {
-    super(stageId);
-    _expression = expression;
+  public FilterNode(int currentStageId, RelDataType rowType, RexNode condition) {
+    super(currentStageId);
+    super._rowType = rowType;
+    _condition = RexExpression.toRexExpression(condition);
   }
 
-  public String getExpression() {
-    return _expression;
+  public RexExpression getCondition() {
+    return _condition;
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/JoinNode.java
similarity index 84%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/JoinNode.java
index bf380639d8..96b6c43a95 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/JoinNode.java
@@ -16,25 +16,29 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner.stage;
 
 import java.util.List;
 import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.type.RelDataType;
 import org.apache.pinot.query.planner.partitioning.FieldSelectionKeySelector;
 import org.apache.pinot.query.planner.partitioning.KeySelector;
+import org.apache.pinot.query.planner.serde.ProtoProperties;
 
 
 public class JoinNode extends AbstractStageNode {
+  @ProtoProperties
   private JoinRelType _joinRelType;
+  @ProtoProperties
   private List<JoinClause> _criteria;
 
   public JoinNode(int stageId) {
     super(stageId);
   }
 
-  public JoinNode(int stageId, JoinRelType joinRelType, List<JoinClause> criteria
-  ) {
+  public JoinNode(int stageId, RelDataType rowType, JoinRelType joinRelType, List<JoinClause> criteria) {
     super(stageId);
+    super._rowType = rowType;
     _joinRelType = joinRelType;
     _criteria = criteria;
   }
@@ -48,7 +52,9 @@ public class JoinNode extends AbstractStageNode {
   }
 
   public static class JoinClause {
+    @ProtoProperties
     private KeySelector<Object[], Object> _leftJoinKeySelector;
+    @ProtoProperties
     private KeySelector<Object[], Object> _rightJoinKeySelector;
 
     public JoinClause() {
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxReceiveNode.java
similarity index 79%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxReceiveNode.java
index 8f0c619b79..1c01d8de5c 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxReceiveNode.java
@@ -16,21 +16,26 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner.stage;
 
 import org.apache.calcite.rel.RelDistribution;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.pinot.query.planner.serde.ProtoProperties;
 
 
 public class MailboxReceiveNode extends AbstractStageNode {
+  @ProtoProperties
   private int _senderStageId;
+  @ProtoProperties
   private RelDistribution.Type _exchangeType;
 
   public MailboxReceiveNode(int stageId) {
     super(stageId);
   }
 
-  public MailboxReceiveNode(int stageId, int senderStageId, RelDistribution.Type exchangeType) {
+  public MailboxReceiveNode(int stageId, RelDataType rowType, int senderStageId, RelDistribution.Type exchangeType) {
     super(stageId);
+    super._rowType = rowType;
     _senderStageId = senderStageId;
     _exchangeType = exchangeType;
   }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxSendNode.java
similarity index 56%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxSendNode.java
index 9867a16f61..c3f540aa0e 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxSendNode.java
@@ -16,23 +16,39 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner.stage;
 
+import javax.annotation.Nullable;
 import org.apache.calcite.rel.RelDistribution;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.pinot.query.planner.partitioning.KeySelector;
+import org.apache.pinot.query.planner.serde.ProtoProperties;
 
 
 public class MailboxSendNode extends AbstractStageNode {
+  @ProtoProperties
   private int _receiverStageId;
+  @ProtoProperties
   private RelDistribution.Type _exchangeType;
+  @ProtoProperties
+  private KeySelector<Object[], Object> _partitionKeySelector;
 
   public MailboxSendNode(int stageId) {
     super(stageId);
   }
 
-  public MailboxSendNode(int stageId, int receiverStageId, RelDistribution.Type exchangeType) {
+  public MailboxSendNode(int stageId, RelDataType rowType, int receiverStageId, RelDistribution.Type exchangeType) {
+    // When exchangeType is not HASH_DISTRIBUTE, no partitionKeySelector is needed.
+    this(stageId, rowType, receiverStageId, exchangeType, null);
+  }
+
+  public MailboxSendNode(int stageId, RelDataType rowType, int receiverStageId, RelDistribution.Type exchangeType,
+      @Nullable KeySelector<Object[], Object> partitionKeySelector) {
     super(stageId);
+    super._rowType = rowType;
     _receiverStageId = receiverStageId;
     _exchangeType = exchangeType;
+    _partitionKeySelector = partitionKeySelector;
   }
 
   public int getReceiverStageId() {
@@ -42,4 +58,8 @@ public class MailboxSendNode extends AbstractStageNode {
   public RelDistribution.Type getExchangeType() {
     return _exchangeType;
   }
+
+  public KeySelector<Object[], Object> getPartitionKeySelector() {
+    return _partitionKeySelector;
+  }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/ProjectNode.java
similarity index 51%
copy from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
copy to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/ProjectNode.java
index 9375a7e986..4ee9be6c0a 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/ProjectNode.java
@@ -16,30 +16,34 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner.stage;
 
 import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexNode;
+import org.apache.pinot.query.planner.logical.RexExpression;
+import org.apache.pinot.query.planner.serde.ProtoProperties;
 
 
-public class TableScanNode extends AbstractStageNode {
-  private String _tableName;
-  private List<String> _tableScanColumns;
+public class ProjectNode extends AbstractStageNode {
+  @ProtoProperties
+  private List<RexExpression> _projects;
 
-  public TableScanNode(int stageId) {
+  public ProjectNode(int stageId) {
     super(stageId);
   }
-
-  public TableScanNode(int stageId, String tableName, List<String> tableScanColumns) {
-    super(stageId);
-    _tableName = tableName;
-    _tableScanColumns = tableScanColumns;
+  public ProjectNode(int currentStageId, RelDataType rowType, List<RexNode> projects) {
+    super(currentStageId);
+    super._rowType = rowType;
+    _projects = projects.stream().map(RexExpression::toRexExpression).collect(Collectors.toList());
   }
 
-  public String getTableName() {
-    return _tableName;
+  public List<RexExpression> getProjects() {
+    return _projects;
   }
 
-  public List<String> getTableScanColumns() {
-    return _tableScanColumns;
+  public RelDataType getRowType() {
+    return _rowType;
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/StageNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/StageNode.java
similarity index 96%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/StageNode.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/StageNode.java
index cd34aca530..45e65a8c21 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/StageNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/StageNode.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner.stage;
 
 import java.io.Serializable;
 import java.util.List;
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/SerDeUtils.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/StageNodeSerDeUtils.java
similarity index 85%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/SerDeUtils.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/StageNodeSerDeUtils.java
index ad7184cdb1..3d34f6effb 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/SerDeUtils.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/StageNodeSerDeUtils.java
@@ -16,19 +16,19 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner.stage;
 
 import org.apache.pinot.common.proto.Plan;
 
 
-public final class SerDeUtils {
-  private SerDeUtils() {
+public final class StageNodeSerDeUtils {
+  private StageNodeSerDeUtils() {
     // do not instantiate.
   }
 
   public static AbstractStageNode deserializeStageNode(Plan.StageNode protoNode) {
     AbstractStageNode stageNode = newNodeInstance(protoNode.getNodeName(), protoNode.getStageId());
-    stageNode.setObjectField(protoNode.getObjectField());
+    stageNode.fromObjectField(protoNode.getObjectField());
     for (Plan.StageNode protoChild : protoNode.getInputsList()) {
       stageNode.addInput(deserializeStageNode(protoChild));
     }
@@ -39,7 +39,7 @@ public final class SerDeUtils {
     Plan.StageNode.Builder builder = Plan.StageNode.newBuilder()
         .setStageId(stageNode.getStageId())
         .setNodeName(stageNode.getClass().getSimpleName())
-        .setObjectField(stageNode.getObjectField());
+        .setObjectField(stageNode.toObjectField());
     for (StageNode childNode : stageNode.getInputs()) {
       builder.addInputs(serializeStageNode((AbstractStageNode) childNode));
     }
@@ -52,8 +52,10 @@ public final class SerDeUtils {
         return new TableScanNode(stageId);
       case "JoinNode":
         return new JoinNode(stageId);
-      case "CalcNode":
-        return new CalcNode(stageId);
+      case "ProjectNode":
+        return new ProjectNode(stageId);
+      case "FilterNode":
+        return new FilterNode(stageId);
       case "MailboxSendNode":
         return new MailboxSendNode(stageId);
       case "MailboxReceiveNode":
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/TableScanNode.java
similarity index 79%
rename from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
rename to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/TableScanNode.java
index 9375a7e986..9ba36d34f3 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/TableScanNode.java
@@ -16,21 +16,26 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+package org.apache.pinot.query.planner.stage;
 
 import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.pinot.query.planner.serde.ProtoProperties;
 
 
 public class TableScanNode extends AbstractStageNode {
+  @ProtoProperties
   private String _tableName;
+  @ProtoProperties
   private List<String> _tableScanColumns;
 
   public TableScanNode(int stageId) {
     super(stageId);
   }
 
-  public TableScanNode(int stageId, String tableName, List<String> tableScanColumns) {
+  public TableScanNode(int stageId, RelDataType rowType, String tableName, List<String> tableScanColumns) {
     super(stageId);
+    super._rowType = rowType;
     _tableName = tableName;
     _tableScanColumns = tableScanColumns;
   }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotExchangeNodeInsertRule.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotExchangeNodeInsertRule.java
index 2b35613f7a..e7ef083ded 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotExchangeNodeInsertRule.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotExchangeNodeInsertRule.java
@@ -19,6 +19,8 @@
 package org.apache.pinot.query.rules;
 
 import com.google.common.collect.ImmutableList;
+import java.util.Collections;
+import java.util.List;
 import org.apache.calcite.plan.RelOptRule;
 import org.apache.calcite.plan.RelOptRuleCall;
 import org.apache.calcite.plan.hep.HepRelVertex;
@@ -27,9 +29,13 @@ import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.core.Exchange;
 import org.apache.calcite.rel.core.Join;
 import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rel.hint.RelHint;
 import org.apache.calcite.rel.logical.LogicalExchange;
 import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
 import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.pinot.query.planner.hints.PinotRelationalHints;
 
 
 /**
@@ -57,12 +63,26 @@ public class PinotExchangeNodeInsertRule extends RelOptRule {
 
   @Override
   public void onMatch(RelOptRuleCall call) {
+    // TODO: this only works for single equality JOIN. add generic condition parser
     Join join = call.rel(0);
     RelNode leftInput = join.getInput(0);
     RelNode rightInput = join.getInput(1);
 
-    RelNode leftExchange = LogicalExchange.create(leftInput, RelDistributions.SINGLETON);
-    RelNode rightExchange = LogicalExchange.create(rightInput, RelDistributions.BROADCAST_DISTRIBUTED);
+    RelNode leftExchange;
+    RelNode rightExchange;
+    List<RelHint> hints = join.getHints();
+    if (hints.contains(PinotRelationalHints.USE_HASH_DISTRIBUTE)) {
+      int leftOperandIndex = ((RexInputRef) ((RexCall) join.getCondition()).getOperands().get(0)).getIndex();
+      int rightOperandIndex = ((RexInputRef) ((RexCall) join.getCondition()).getOperands().get(1)).getIndex()
+          - join.getLeft().getRowType().getFieldNames().size();
+      leftExchange = LogicalExchange.create(leftInput,
+          RelDistributions.hash(Collections.singletonList(leftOperandIndex)));
+      rightExchange = LogicalExchange.create(rightInput,
+          RelDistributions.hash(Collections.singletonList(rightOperandIndex)));
+    } else { // if (hints.contains(PinotRelationalHints.USE_BROADCAST_JOIN))
+      leftExchange = LogicalExchange.create(leftInput, RelDistributions.SINGLETON);
+      rightExchange = LogicalExchange.create(rightInput, RelDistributions.BROADCAST_DISTRIBUTED);
+    }
 
     RelNode newJoinNode =
         new LogicalJoin(join.getCluster(), join.getTraitSet(), leftExchange, rightExchange, join.getCondition(),
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotQueryRuleSets.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotQueryRuleSets.java
index 1b4e0850ac..63c2fd799f 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotQueryRuleSets.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotQueryRuleSets.java
@@ -45,6 +45,8 @@ public class PinotQueryRuleSets {
           CoreRules.FILTER_AGGREGATE_TRANSPOSE,
           // push filter through set operation
           CoreRules.FILTER_SET_OP_TRANSPOSE,
+          // push project through join,
+          CoreRules.PROJECT_JOIN_TRANSPOSE,
           // push project through set operation
           CoreRules.PROJECT_SET_OP_TRANSPOSE,
 
diff --git a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
index 60c7cd11af..9f4778743b 100644
--- a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
+++ b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
@@ -19,56 +19,45 @@
 package org.apache.pinot.query;
 
 import com.google.common.collect.ImmutableList;
-import java.io.PrintWriter;
-import java.io.StringWriter;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
-import org.apache.calcite.jdbc.CalciteSchemaBuilder;
-import org.apache.calcite.rel.RelNode;
-import org.apache.calcite.rel.RelRoot;
-import org.apache.calcite.rel.RelWriter;
-import org.apache.calcite.rel.externalize.RelXmlWriter;
-import org.apache.calcite.sql.SqlExplainLevel;
 import org.apache.calcite.sql.SqlNode;
-import org.apache.pinot.core.routing.RoutingManager;
 import org.apache.pinot.core.transport.ServerInstance;
-import org.apache.pinot.query.catalog.PinotCatalog;
 import org.apache.pinot.query.context.PlannerContext;
 import org.apache.pinot.query.planner.PlannerUtils;
 import org.apache.pinot.query.planner.QueryPlan;
 import org.apache.pinot.query.planner.StageMetadata;
-import org.apache.pinot.query.routing.WorkerManager;
-import org.apache.pinot.query.type.TypeFactory;
-import org.apache.pinot.query.type.TypeSystem;
 import org.testng.Assert;
-import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 
-public class QueryEnvironmentTest {
-  private QueryEnvironment _queryEnvironment;
+public class QueryEnvironmentTest extends QueryEnvironmentTestBase {
 
-  @BeforeClass
-  public void setUp() {
-    // the port doesn't matter as we are not actually making a server call.
-    RoutingManager routingManager = QueryEnvironmentTestUtils.getMockRoutingManager(1, 2);
-    _queryEnvironment = new QueryEnvironment(new TypeFactory(new TypeSystem()),
-        CalciteSchemaBuilder.asRootSchema(new PinotCatalog(QueryEnvironmentTestUtils.mockTableCache())),
-        new WorkerManager("localhost", 3, routingManager));
+  @Test(dataProvider = "testQueryParserDataProvider")
+  public void testQueryParser(String query, String digest)
+      throws Exception {
+    PlannerContext plannerContext = new PlannerContext();
+    SqlNode sqlNode = _queryEnvironment.parse(query, plannerContext);
+    _queryEnvironment.validate(sqlNode);
+    Assert.assertEquals(sqlNode.toString(), digest);
   }
 
-  @Test
-  public void testSqlStrings()
+  @Test(dataProvider = "testQueryDataProvider")
+  public void testQueryToRel(String query)
       throws Exception {
-    testQueryParsing("SELECT * FROM a JOIN b ON a.col1 = b.col2 WHERE a.col3 >= 0",
-        "SELECT *\n" + "FROM `a`\n" + "INNER JOIN `b` ON `a`.`col1` = `b`.`col2`\n" + "WHERE `a`.`col3` >= 0");
+    try {
+      QueryPlan queryPlan = _queryEnvironment.planQuery(query);
+      Assert.assertNotNull(queryPlan);
+    } catch (RuntimeException e) {
+      Assert.fail("failed to plan query: " + query, e);
+    }
   }
 
   @Test
-  public void testQueryToStages()
+  public void testQueryAndAssertStageContentForJoin()
       throws Exception {
-    PlannerContext plannerContext = new PlannerContext();
     String query = "SELECT * FROM a JOIN b ON a.col1 = b.col2";
     QueryPlan queryPlan = _queryEnvironment.planQuery(query);
     Assert.assertEquals(queryPlan.getQueryStageMap().size(), 4);
@@ -96,28 +85,19 @@ public class QueryEnvironmentTest {
   }
 
   @Test
-  public void testQueryToRel()
-      throws Exception {
-    PlannerContext plannerContext = new PlannerContext();
-    String query = "SELECT * FROM a JOIN b ON a.col1 = b.col2 WHERE a.col3 >= 0";
-    SqlNode parsed = _queryEnvironment.parse(query, plannerContext);
-    SqlNode validated = _queryEnvironment.validate(parsed);
-    RelRoot relRoot = _queryEnvironment.toRelation(validated, plannerContext);
-    RelNode optimized = _queryEnvironment.optimize(relRoot, plannerContext);
-
-    // Assert that relational plan can be written into a ALL-ATTRIBUTE digest.
-    StringWriter sw = new StringWriter();
-    PrintWriter pw = new PrintWriter(sw);
-    RelWriter planWriter = new RelXmlWriter(pw, SqlExplainLevel.ALL_ATTRIBUTES);
-    optimized.explain(planWriter);
-    Assert.assertNotNull(sw.toString());
+  public void testQueryProjectFilterPushdownForJoin() {
+    String query = "SELECT a.col1, a.ts, b.col2, b.col3 FROM a JOIN b ON a.col1 = b.col2 "
+        + "WHERE a.col3 >= 0 AND a.col2 IN  ('a', 'b') AND b.col3 < 0";
+    QueryPlan queryPlan = _queryEnvironment.planQuery(query);
+    Assert.assertEquals(queryPlan.getQueryStageMap().size(), 4);
+    Assert.assertEquals(queryPlan.getStageMetadataMap().size(), 4);
   }
 
-  private void testQueryParsing(String query, String digest)
-      throws Exception {
-    PlannerContext plannerContext = new PlannerContext();
-    SqlNode sqlNode = _queryEnvironment.parse(query, plannerContext);
-    _queryEnvironment.validate(sqlNode);
-    Assert.assertEquals(sqlNode.toString(), digest);
+  @DataProvider(name = "testQueryParserDataProvider")
+  private Object[][] provideQueriesAndDigest() {
+    return new Object[][] {
+        new Object[]{"SELECT * FROM a JOIN b ON a.col1 = b.col2 WHERE a.col3 >= 0",
+            "SELECT *\n" + "FROM `a`\n" + "INNER JOIN `b` ON `a`.`col1` = `b`.`col2`\n" + "WHERE `a`.`col3` >= 0"},
+    };
   }
 }
diff --git a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTestBase.java b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTestBase.java
new file mode 100644
index 0000000000..40841d7c72
--- /dev/null
+++ b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTestBase.java
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query;
+
+import org.apache.calcite.jdbc.CalciteSchemaBuilder;
+import org.apache.pinot.core.routing.RoutingManager;
+import org.apache.pinot.query.catalog.PinotCatalog;
+import org.apache.pinot.query.routing.WorkerManager;
+import org.apache.pinot.query.type.TypeFactory;
+import org.apache.pinot.query.type.TypeSystem;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+
+
+public class QueryEnvironmentTestBase {
+  protected QueryEnvironment _queryEnvironment;
+
+  @BeforeClass
+  public void setUp() {
+    // the port doesn't matter as we are not actually making a server call.
+    RoutingManager routingManager = QueryEnvironmentTestUtils.getMockRoutingManager(1, 2);
+    _queryEnvironment = new QueryEnvironment(new TypeFactory(new TypeSystem()),
+        CalciteSchemaBuilder.asRootSchema(new PinotCatalog(QueryEnvironmentTestUtils.mockTableCache())),
+        new WorkerManager("localhost", 3, routingManager));
+  }
+
+  @DataProvider(name = "testQueryDataProvider")
+  protected Object[][] provideQueries() {
+    return new Object[][] {
+        new Object[]{"SELECT * FROM a JOIN b ON a.col1 = b.col2"},
+        new Object[]{"SELECT * FROM a JOIN b ON a.col1 = b.col2 WHERE a.col3 >= 0"},
+        new Object[]{"SELECT a.col1, a.ts, b.col3 FROM a JOIN b ON a.col1 = b.col2 "
+            + "WHERE a.col3 >= 0 AND a.col2 = 'a' AND b.col3 < 0"},
+    };
+  }
+}
diff --git a/pinot-query-planner/src/test/java/org/apache/pinot/query/planner/stage/SerDeUtilsTest.java b/pinot-query-planner/src/test/java/org/apache/pinot/query/planner/stage/SerDeUtilsTest.java
new file mode 100644
index 0000000000..21031cd303
--- /dev/null
+++ b/pinot-query-planner/src/test/java/org/apache/pinot/query/planner/stage/SerDeUtilsTest.java
@@ -0,0 +1,81 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.stage;
+
+import java.lang.reflect.Field;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.proto.Plan;
+import org.apache.pinot.query.QueryEnvironmentTestBase;
+import org.apache.pinot.query.planner.QueryPlan;
+import org.apache.pinot.query.planner.serde.ProtoProperties;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class SerDeUtilsTest extends QueryEnvironmentTestBase {
+
+  @Test(dataProvider = "testQueryDataProvider")
+  public void testQueryStagePlanSerDe(String query)
+      throws Exception {
+    QueryPlan queryPlan = _queryEnvironment.planQuery(query);
+    for (StageNode stageNode : queryPlan.getQueryStageMap().values()) {
+      Plan.StageNode serializedStageNode = StageNodeSerDeUtils.serializeStageNode((AbstractStageNode) stageNode);
+      StageNode deserializedStageNode = StageNodeSerDeUtils.deserializeStageNode(serializedStageNode);
+      Assert.assertTrue(isObjectEqual(stageNode, deserializedStageNode));
+    }
+  }
+
+  @SuppressWarnings({"rawtypes"})
+  private boolean isObjectEqual(Object left, Object right)
+      throws IllegalAccessException {
+    Class<?> clazz = left.getClass();
+    for (Field field : clazz.getDeclaredFields()) {
+      if (field.isAnnotationPresent(ProtoProperties.class)) {
+        field.setAccessible(true);
+        Object l = field.get(left);
+        Object r = field.get(right);
+        if (l instanceof List) {
+          if (((List) l).size() != ((List) r).size()) {
+            return false;
+          }
+          for (int i = 0; i < ((List) l).size(); i++) {
+            if (!isObjectEqual(((List) l).get(i), ((List) r).get(i))) {
+              return false;
+            }
+          }
+        } else if (l instanceof Map) {
+          if (((Map) l).size() != ((Map) r).size()) {
+            return false;
+          }
+          for (Object key : ((Map) l).keySet()) {
+            if (!isObjectEqual(((Map) l).get(key), ((Map) r).get(key))) {
+              return false;
+            }
+          }
+        } else {
+          if (!(l == null && r == null || l != null && l.equals(r) || isObjectEqual(l, r))) {
+            return false;
+          }
+        }
+      }
+    }
+    return true;
+  }
+}
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java
index 2968ff9a23..f33cde43c2 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java
@@ -31,7 +31,7 @@ import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.query.mailbox.GrpcMailboxService;
 import org.apache.pinot.query.mailbox.MailboxService;
 import org.apache.pinot.query.planner.StageMetadata;
-import org.apache.pinot.query.planner.nodes.MailboxSendNode;
+import org.apache.pinot.query.planner.stage.MailboxSendNode;
 import org.apache.pinot.query.runtime.executor.WorkerQueryExecutor;
 import org.apache.pinot.query.runtime.operator.MailboxSendOperator;
 import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
@@ -100,8 +100,8 @@ public class QueryRunner {
       StageMetadata receivingStageMetadata = distributedStagePlan.getMetadataMap().get(sendNode.getReceiverStageId());
       MailboxSendOperator mailboxSendOperator =
           new MailboxSendOperator(_mailboxService, dataTable, receivingStageMetadata.getServerInstances(),
-              sendNode.getExchangeType(), _hostname, _port, serverQueryRequest.getRequestId(),
-              sendNode.getStageId());
+              sendNode.getExchangeType(), sendNode.getPartitionKeySelector(), _hostname, _port,
+              serverQueryRequest.getRequestId(), sendNode.getStageId());
       mailboxSendOperator.nextBlock();
     } else {
       _workerExecutor.processQuery(distributedStagePlan, requestMetadataMap, executorService);
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java
index 85c0f108b4..9a66bf86c1 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/executor/WorkerQueryExecutor.java
@@ -30,13 +30,15 @@ import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.core.util.trace.TraceRunnable;
 import org.apache.pinot.query.mailbox.MailboxService;
 import org.apache.pinot.query.planner.StageMetadata;
-import org.apache.pinot.query.planner.nodes.JoinNode;
-import org.apache.pinot.query.planner.nodes.MailboxReceiveNode;
-import org.apache.pinot.query.planner.nodes.MailboxSendNode;
-import org.apache.pinot.query.planner.nodes.StageNode;
+import org.apache.pinot.query.planner.stage.FilterNode;
+import org.apache.pinot.query.planner.stage.JoinNode;
+import org.apache.pinot.query.planner.stage.MailboxReceiveNode;
+import org.apache.pinot.query.planner.stage.MailboxSendNode;
+import org.apache.pinot.query.planner.stage.ProjectNode;
+import org.apache.pinot.query.planner.stage.StageNode;
 import org.apache.pinot.query.runtime.blocks.DataTableBlock;
 import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
-import org.apache.pinot.query.runtime.operator.BroadcastJoinOperator;
+import org.apache.pinot.query.runtime.operator.HashJoinOperator;
 import org.apache.pinot.query.runtime.operator.MailboxReceiveOperator;
 import org.apache.pinot.query.runtime.operator.MailboxSendOperator;
 import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
@@ -98,22 +100,27 @@ public class WorkerQueryExecutor {
   private BaseOperator<DataTableBlock> getOperator(long requestId, StageNode stageNode,
       Map<Integer, StageMetadata> metadataMap) {
     // TODO: optimize this into a framework. (physical planner)
-    if (stageNode instanceof MailboxSendNode) {
-      MailboxSendNode sendNode = (MailboxSendNode) stageNode;
-      BaseOperator<DataTableBlock> nextOperator = getOperator(requestId, sendNode.getInputs().get(0), metadataMap);
-      StageMetadata receivingStageMetadata = metadataMap.get(sendNode.getReceiverStageId());
-      return new MailboxSendOperator(_mailboxService, nextOperator, receivingStageMetadata.getServerInstances(),
-          sendNode.getExchangeType(), _hostName, _port, requestId, sendNode.getStageId());
-    } else if (stageNode instanceof MailboxReceiveNode) {
+    if (stageNode instanceof MailboxReceiveNode) {
       MailboxReceiveNode receiveNode = (MailboxReceiveNode) stageNode;
       List<ServerInstance> sendingInstances = metadataMap.get(receiveNode.getSenderStageId()).getServerInstances();
       return new MailboxReceiveOperator(_mailboxService, RelDistribution.Type.ANY, sendingInstances, _hostName, _port,
           requestId, receiveNode.getSenderStageId());
+    } else if (stageNode instanceof MailboxSendNode) {
+      MailboxSendNode sendNode = (MailboxSendNode) stageNode;
+      BaseOperator<DataTableBlock> nextOperator = getOperator(requestId, sendNode.getInputs().get(0), metadataMap);
+      StageMetadata receivingStageMetadata = metadataMap.get(sendNode.getReceiverStageId());
+      return new MailboxSendOperator(_mailboxService, nextOperator, receivingStageMetadata.getServerInstances(),
+          sendNode.getExchangeType(), sendNode.getPartitionKeySelector(), _hostName, _port, requestId,
+          sendNode.getStageId());
     } else if (stageNode instanceof JoinNode) {
       JoinNode joinNode = (JoinNode) stageNode;
       BaseOperator<DataTableBlock> leftOperator = getOperator(requestId, joinNode.getInputs().get(0), metadataMap);
       BaseOperator<DataTableBlock> rightOperator = getOperator(requestId, joinNode.getInputs().get(1), metadataMap);
-      return new BroadcastJoinOperator(leftOperator, rightOperator, joinNode.getCriteria());
+      return new HashJoinOperator(leftOperator, rightOperator, joinNode.getCriteria());
+    } else if (stageNode instanceof FilterNode) {
+      throw new UnsupportedOperationException("Unsupported!");
+    } else if (stageNode instanceof ProjectNode) {
+      throw new UnsupportedOperationException("Unsupported!");
     } else {
       throw new UnsupportedOperationException(
           String.format("Stage node type %s is not supported!", stageNode.getClass().getSimpleName()));
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/BroadcastJoinOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java
similarity index 96%
rename from pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/BroadcastJoinOperator.java
rename to pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java
index db5bb5289d..6cf9fddd12 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/BroadcastJoinOperator.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java
@@ -28,8 +28,8 @@ import org.apache.pinot.common.utils.DataTable;
 import org.apache.pinot.core.common.Operator;
 import org.apache.pinot.core.operator.BaseOperator;
 import org.apache.pinot.core.query.selection.SelectionOperatorUtils;
-import org.apache.pinot.query.planner.nodes.JoinNode;
 import org.apache.pinot.query.planner.partitioning.KeySelector;
+import org.apache.pinot.query.planner.stage.JoinNode;
 import org.apache.pinot.query.runtime.blocks.DataTableBlock;
 import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
 
@@ -42,7 +42,7 @@ import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
  *
  * <p>For each of the data block received from the left table, it will generate a joint data block.
  */
-public class BroadcastJoinOperator extends BaseOperator<DataTableBlock> {
+public class HashJoinOperator extends BaseOperator<DataTableBlock> {
   private static final String OPERATOR_NAME = "BroadcastJoinOperator";
   private static final String EXPLAIN_NAME = "BROADCAST_JOIN";
 
@@ -57,7 +57,7 @@ public class BroadcastJoinOperator extends BaseOperator<DataTableBlock> {
   private KeySelector<Object[], Object> _leftKeySelector;
   private KeySelector<Object[], Object> _rightKeySelector;
 
-  public BroadcastJoinOperator(BaseOperator<DataTableBlock> leftTableOperator,
+  public HashJoinOperator(BaseOperator<DataTableBlock> leftTableOperator,
       BaseOperator<DataTableBlock> rightTableOperator, List<JoinNode.JoinClause> criteria) {
     // TODO: this assumes right table is broadcast.
     _leftKeySelector = criteria.get(0).getLeftJoinKeySelector();
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
index 3971dfb325..3dd9c5ff77 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
@@ -22,6 +22,7 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableSet;
 import com.google.protobuf.ByteString;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 import javax.annotation.Nullable;
@@ -30,10 +31,12 @@ import org.apache.pinot.common.proto.Mailbox;
 import org.apache.pinot.common.utils.DataTable;
 import org.apache.pinot.core.common.Operator;
 import org.apache.pinot.core.operator.BaseOperator;
+import org.apache.pinot.core.query.selection.SelectionOperatorUtils;
 import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.query.mailbox.MailboxService;
 import org.apache.pinot.query.mailbox.SendingMailbox;
 import org.apache.pinot.query.mailbox.StringMailboxIdentifier;
+import org.apache.pinot.query.planner.partitioning.KeySelector;
 import org.apache.pinot.query.runtime.blocks.DataTableBlock;
 import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
 import org.slf4j.Logger;
@@ -49,10 +52,11 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
   private static final String EXPLAIN_NAME = "MAILBOX_SEND";
   private static final Set<RelDistribution.Type> SUPPORTED_EXCHANGE_TYPE =
       ImmutableSet.of(RelDistribution.Type.SINGLETON, RelDistribution.Type.RANDOM_DISTRIBUTED,
-          RelDistribution.Type.BROADCAST_DISTRIBUTED);
+          RelDistribution.Type.BROADCAST_DISTRIBUTED, RelDistribution.Type.HASH_DISTRIBUTED);
 
   private final List<ServerInstance> _receivingStageInstances;
   private final RelDistribution.Type _exchangeType;
+  private final KeySelector<Object[], Object> _keySelector;
   private final String _serverHostName;
   private final int _serverPort;
   private final long _jobId;
@@ -63,11 +67,13 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
 
   public MailboxSendOperator(MailboxService<Mailbox.MailboxContent> mailboxService,
       BaseOperator<DataTableBlock> dataTableBlockBaseOperator, List<ServerInstance> receivingStageInstances,
-      RelDistribution.Type exchangeType, String hostName, int port, long jobId, int stageId) {
+      RelDistribution.Type exchangeType, KeySelector<Object[], Object> keySelector, String hostName, int port,
+      long jobId, int stageId) {
     _mailboxService = mailboxService;
     _dataTableBlockBaseOperator = dataTableBlockBaseOperator;
     _receivingStageInstances = receivingStageInstances;
     _exchangeType = exchangeType;
+    _keySelector = keySelector;
     _serverHostName = hostName;
     _serverPort = port;
     _jobId = jobId;
@@ -82,12 +88,13 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
    * creation of MailboxSendOperator we should not use this API.
    */
   public MailboxSendOperator(MailboxService<Mailbox.MailboxContent> mailboxService, DataTable dataTable,
-      List<ServerInstance> receivingStageInstances, RelDistribution.Type exchangeType, String hostName, int port,
-      long jobId, int stageId) {
+      List<ServerInstance> receivingStageInstances, RelDistribution.Type exchangeType,
+      KeySelector<Object[], Object> keySelector, String hostName, int port, long jobId, int stageId) {
     _mailboxService = mailboxService;
     _dataTable = dataTable;
     _receivingStageInstances = receivingStageInstances;
     _exchangeType = exchangeType;
+    _keySelector = keySelector;
     _serverHostName = hostName;
     _serverPort = port;
     _jobId = jobId;
@@ -115,13 +122,16 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
   protected DataTableBlock getNextBlock() {
     DataTable dataTable;
     DataTableBlock dataTableBlock = null;
+    boolean isEndOfStream;
     if (_dataTableBlockBaseOperator != null) {
       dataTableBlock = _dataTableBlockBaseOperator.nextBlock();
       dataTable = dataTableBlock.getDataTable();
+      isEndOfStream = DataTableBlockUtils.isEndOfStream(dataTableBlock);
     } else {
       dataTable = _dataTable;
+      isEndOfStream = true;
     }
-    boolean isEndOfStream = dataTableBlock == null || DataTableBlockUtils.isEndOfStream(dataTableBlock);
+
     try {
       switch (_exchangeType) {
         // TODO: random and singleton distribution should've been selected in planning phase.
@@ -142,6 +152,13 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
           }
           break;
         case HASH_DISTRIBUTED:
+          // TODO: ensure that server instance list is sorted using same function in sender.
+          List<DataTable> dataTableList = constructPartitionedDataBlock(dataTable, _keySelector,
+              _receivingStageInstances.size());
+          for (int i = 0; i < _receivingStageInstances.size(); i++) {
+            sendDataTableBlock(_receivingStageInstances.get(i), dataTableList.get(i), isEndOfStream);
+          }
+          break;
         case RANGE_DISTRIBUTED:
         case ROUND_ROBIN_DISTRIBUTED:
         case ANY:
@@ -154,6 +171,31 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
     return dataTableBlock;
   }
 
+  private static List<DataTable> constructPartitionedDataBlock(DataTable dataTable,
+      KeySelector<Object[], Object> keySelector, int partitionSize)
+      throws Exception {
+    List<List<Object[]>> temporaryRows = new ArrayList<>(partitionSize);
+    for (int i = 0; i < partitionSize; i++) {
+      temporaryRows.add(new ArrayList<>());
+    }
+    for (int rowId = 0; rowId < dataTable.getNumberOfRows(); rowId++) {
+      Object[] row = SelectionOperatorUtils.extractRowFromDataTable(dataTable, rowId);
+      Object key = keySelector.getKey(row);
+      // TODO: support other partitioning algorithm
+      temporaryRows.get(hashToIndex(key, partitionSize)).add(row);
+    }
+    List<DataTable> dataTableList = new ArrayList<>(partitionSize);
+    for (int i = 0; i < partitionSize; i++) {
+      List<Object[]> objects = temporaryRows.get(i);
+      dataTableList.add(SelectionOperatorUtils.getDataTableFromRows(objects, dataTable.getDataSchema()));
+    }
+    return dataTableList;
+  }
+
+  private static int hashToIndex(Object key, int partitionSize) {
+    return (key.hashCode()) % partitionSize;
+  }
+
   private void sendDataTableBlock(ServerInstance serverInstance, DataTable dataTable, boolean isEndOfStream)
       throws IOException {
     String mailboxId = toMailboxId(serverInstance);
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/DistributedStagePlan.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/DistributedStagePlan.java
index f9ecf7f089..828a969dd0 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/DistributedStagePlan.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/DistributedStagePlan.java
@@ -22,7 +22,7 @@ import java.util.HashMap;
 import java.util.Map;
 import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.query.planner.StageMetadata;
-import org.apache.pinot.query.planner.nodes.StageNode;
+import org.apache.pinot.query.planner.stage.StageNode;
 
 
 /**
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java
index 358ebb8465..034bf561a2 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java
@@ -25,8 +25,8 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.pinot.common.proto.Worker;
 import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.query.planner.StageMetadata;
-import org.apache.pinot.query.planner.nodes.AbstractStageNode;
-import org.apache.pinot.query.planner.nodes.SerDeUtils;
+import org.apache.pinot.query.planner.stage.AbstractStageNode;
+import org.apache.pinot.query.planner.stage.StageNodeSerDeUtils;
 import org.apache.pinot.query.routing.WorkerInstance;
 import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
 
@@ -43,7 +43,7 @@ public class QueryPlanSerDeUtils {
   public static DistributedStagePlan deserialize(Worker.StagePlan stagePlan) {
     DistributedStagePlan distributedStagePlan = new DistributedStagePlan(stagePlan.getStageId());
     distributedStagePlan.setServerInstance(stringToInstance(stagePlan.getInstanceId()));
-    distributedStagePlan.setStageRoot(SerDeUtils.deserializeStageNode(stagePlan.getStageRoot()));
+    distributedStagePlan.setStageRoot(StageNodeSerDeUtils.deserializeStageNode(stagePlan.getStageRoot()));
     Map<Integer, Worker.StageMetadata> metadataMap = stagePlan.getStageMetadataMap();
     distributedStagePlan.getMetadataMap().putAll(protoMapToStageMetadataMap(metadataMap));
     return distributedStagePlan;
@@ -53,7 +53,7 @@ public class QueryPlanSerDeUtils {
     return Worker.StagePlan.newBuilder()
         .setStageId(distributedStagePlan.getStageId())
         .setInstanceId(instanceToString(distributedStagePlan.getServerInstance()))
-        .setStageRoot(SerDeUtils.serializeStageNode((AbstractStageNode) distributedStagePlan.getStageRoot()))
+        .setStageRoot(StageNodeSerDeUtils.serializeStageNode((AbstractStageNode) distributedStagePlan.getStageRoot()))
         .putAllStageMetadata(stageMetadataMapToProtoMap(distributedStagePlan.getMetadataMap())).build();
   }
 
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/utils/ServerRequestUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/utils/ServerRequestUtils.java
index 306faa2598..5f17e83305 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/utils/ServerRequestUtils.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/utils/ServerRequestUtils.java
@@ -29,11 +29,12 @@ import org.apache.pinot.common.request.PinotQuery;
 import org.apache.pinot.common.request.QuerySource;
 import org.apache.pinot.common.utils.request.RequestUtils;
 import org.apache.pinot.core.query.request.ServerQueryRequest;
-import org.apache.pinot.query.planner.nodes.CalcNode;
-import org.apache.pinot.query.planner.nodes.MailboxReceiveNode;
-import org.apache.pinot.query.planner.nodes.MailboxSendNode;
-import org.apache.pinot.query.planner.nodes.StageNode;
-import org.apache.pinot.query.planner.nodes.TableScanNode;
+import org.apache.pinot.query.parser.CalciteRexExpressionParser;
+import org.apache.pinot.query.planner.stage.FilterNode;
+import org.apache.pinot.query.planner.stage.MailboxSendNode;
+import org.apache.pinot.query.planner.stage.ProjectNode;
+import org.apache.pinot.query.planner.stage.StageNode;
+import org.apache.pinot.query.planner.stage.TableScanNode;
 import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
 
 
@@ -88,22 +89,28 @@ public class ServerRequestUtils {
   }
 
   private static void walkStageTree(StageNode node, PinotQuery pinotQuery) {
-    if (node instanceof CalcNode) {
-      // TODO: add conversion for CalcNode, specifically filter/alias/...
-    } else if (node instanceof TableScanNode) {
+    // this walkStageTree should only be a sequential walk.
+    for (StageNode child : node.getInputs()) {
+      walkStageTree(child, pinotQuery);
+    }
+    if (node instanceof TableScanNode) {
       TableScanNode tableScanNode = (TableScanNode) node;
       DataSource dataSource = new DataSource();
       dataSource.setTableName(tableScanNode.getTableName());
       pinotQuery.setDataSource(dataSource);
       pinotQuery.setSelectList(tableScanNode.getTableScanColumns().stream().map(RequestUtils::getIdentifierExpression)
           .collect(Collectors.toList()));
-    } else if (node instanceof MailboxSendNode || node instanceof MailboxReceiveNode) {
-      // ignore for now. continue to child.
+    } else if (node instanceof FilterNode) {
+      pinotQuery.setFilterExpression(CalciteRexExpressionParser.toExpression(
+          ((FilterNode) node).getCondition(), pinotQuery));
+    } else if (node instanceof ProjectNode) {
+      pinotQuery.setSelectList(CalciteRexExpressionParser.convertSelectList(
+          ((ProjectNode) node).getProjects(), pinotQuery));
+    } else if (node instanceof MailboxSendNode) {
+      // TODO: MailboxSendNode should be the root of the leaf stage. but ignore for now since it is handle seperately
+      // in QueryRunner as a single step sender.
     } else {
       throw new UnsupportedOperationException("Unsupported logical plan node: " + node);
     }
-    for (StageNode child : node.getInputs()) {
-      walkStageTree(child, pinotQuery);
-    }
   }
 }
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java
index 3200b317bb..7ea69b8d21 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/service/QueryDispatcher.java
@@ -32,7 +32,7 @@ import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.query.mailbox.MailboxService;
 import org.apache.pinot.query.planner.QueryPlan;
 import org.apache.pinot.query.planner.StageMetadata;
-import org.apache.pinot.query.planner.nodes.MailboxReceiveNode;
+import org.apache.pinot.query.planner.stage.MailboxReceiveNode;
 import org.apache.pinot.query.runtime.blocks.DataTableBlock;
 import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
 import org.apache.pinot.query.runtime.operator.MailboxReceiveOperator;
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java
index 9de2db834f..2316195bce 100644
--- a/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/runtime/QueryRunnerTest.java
@@ -36,7 +36,7 @@ import org.apache.pinot.query.QueryEnvironmentTestUtils;
 import org.apache.pinot.query.QueryServerEnclosure;
 import org.apache.pinot.query.mailbox.GrpcMailboxService;
 import org.apache.pinot.query.planner.QueryPlan;
-import org.apache.pinot.query.planner.nodes.MailboxReceiveNode;
+import org.apache.pinot.query.planner.stage.MailboxReceiveNode;
 import org.apache.pinot.query.routing.WorkerInstance;
 import org.apache.pinot.query.runtime.blocks.DataTableBlock;
 import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
@@ -47,6 +47,7 @@ import org.apache.pinot.query.service.QueryDispatcher;
 import org.testng.Assert;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 import static org.apache.pinot.core.query.selection.SelectionOperatorUtils.extractRowFromDataTable;
@@ -101,73 +102,9 @@ public class QueryRunnerTest {
     _mailboxService.shutdown();
   }
 
-  @Test
-  public void testRunningTableScanOnlyQuery()
-      throws Exception {
-    QueryPlan queryPlan = _queryEnvironment.planQuery("SELECT * FROM b");
-    int stageRoodId = QueryEnvironmentTestUtils.getTestStageByServerCount(queryPlan, 1);
-    Map<String, String> requestMetadataMap =
-        ImmutableMap.of("REQUEST_ID", String.valueOf(RANDOM_REQUEST_ID_GEN.nextLong()));
-
-    ServerInstance serverInstance = queryPlan.getStageMetadataMap().get(stageRoodId).getServerInstances().get(0);
-    DistributedStagePlan distributedStagePlan =
-        QueryDispatcher.constructDistributedStagePlan(queryPlan, stageRoodId, serverInstance);
-
-    MailboxReceiveOperator mailboxReceiveOperator =
-        createReduceStageOperator(queryPlan.getStageMetadataMap().get(stageRoodId).getServerInstances(),
-            Long.parseLong(requestMetadataMap.get("REQUEST_ID")), stageRoodId, _reducerGrpcPort);
-
-    // execute this single stage.
-    _servers.get(serverInstance).processQuery(distributedStagePlan, requestMetadataMap);
-
-    DataTableBlock dataTableBlock;
-    // get the block back and it should have 5 rows
-    dataTableBlock = mailboxReceiveOperator.nextBlock();
-    Assert.assertEquals(dataTableBlock.getDataTable().getNumberOfRows(), 5);
-    // next block should be null as all servers finished sending.
-    dataTableBlock = mailboxReceiveOperator.nextBlock();
-    Assert.assertTrue(DataTableBlockUtils.isEndOfStream(dataTableBlock));
-  }
-
-  @Test
-  public void testRunningTableScanMultipleServer()
-      throws Exception {
-    QueryPlan queryPlan = _queryEnvironment.planQuery("SELECT * FROM a");
-    int stageRoodId = QueryEnvironmentTestUtils.getTestStageByServerCount(queryPlan, 2);
-    Map<String, String> requestMetadataMap =
-        ImmutableMap.of("REQUEST_ID", String.valueOf(RANDOM_REQUEST_ID_GEN.nextLong()));
-
-    for (ServerInstance serverInstance : queryPlan.getStageMetadataMap().get(stageRoodId).getServerInstances()) {
-      DistributedStagePlan distributedStagePlan =
-          QueryDispatcher.constructDistributedStagePlan(queryPlan, stageRoodId, serverInstance);
-
-      // execute this single stage.
-      _servers.get(serverInstance).processQuery(distributedStagePlan, requestMetadataMap);
-    }
-
-    MailboxReceiveOperator mailboxReceiveOperator =
-        createReduceStageOperator(queryPlan.getStageMetadataMap().get(stageRoodId).getServerInstances(),
-            Long.parseLong(requestMetadataMap.get("REQUEST_ID")), stageRoodId, _reducerGrpcPort);
-
-    int count = 0;
-    int rowCount = 0;
-    DataTableBlock dataTableBlock;
-    while (count < 2) { // we have 2 servers sending data.
-      dataTableBlock = mailboxReceiveOperator.nextBlock();
-      rowCount += dataTableBlock.getDataTable().getNumberOfRows();
-      count++;
-    }
-    // assert that all table A segments returned successfully.
-    Assert.assertEquals(rowCount, 15);
-    // assert that the next block is null (e.g. finished receiving).
-    dataTableBlock = mailboxReceiveOperator.nextBlock();
-    Assert.assertTrue(DataTableBlockUtils.isEndOfStream(dataTableBlock));
-  }
-
-  @Test
-  public void testJoin()
-      throws Exception {
-    QueryPlan queryPlan = _queryEnvironment.planQuery("SELECT * FROM a JOIN b on a.col1 = b.col2");
+  @Test(dataProvider = "testDataWithSqlToFinalRowCount")
+  public void testSqlWithFinalRowCountChecker(String sql, int expectedRowCount) {
+    QueryPlan queryPlan = _queryEnvironment.planQuery(sql);
     Map<String, String> requestMetadataMap =
         ImmutableMap.of("REQUEST_ID", String.valueOf(RANDOM_REQUEST_ID_GEN.nextLong()));
     MailboxReceiveOperator mailboxReceiveOperator = null;
@@ -187,85 +124,52 @@ public class QueryRunnerTest {
     }
     Preconditions.checkNotNull(mailboxReceiveOperator);
 
-    int count = 0;
-    int rowCount = 0;
-    List<Object[]> resultRows = new ArrayList<>();
-    DataTableBlock dataTableBlock;
-    while (count < 2) { // we have 2 servers sending data.
-      dataTableBlock = mailboxReceiveOperator.nextBlock();
-      if (dataTableBlock.getDataTable() != null) {
-        DataTable dataTable = dataTableBlock.getDataTable();
-        int numRows = dataTable.getNumberOfRows();
-        for (int rowId = 0; rowId < numRows; rowId++) {
-          resultRows.add(extractRowFromDataTable(dataTable, rowId));
-        }
-        rowCount += numRows;
-      }
-      count++;
-    }
-
-    // Assert that each of the 5 categories from left table is joined with right table.
-    // Specifically table A has 15 rows (10 on server1 and 5 on server2) and table B has 5 rows (all on server1),
-    // thus the final JOIN result will be 15 x 1 = 15.
-    Assert.assertEquals(rowCount, 15);
-
-    // assert that the next block is null (e.g. finished receiving).
-    dataTableBlock = mailboxReceiveOperator.nextBlock();
-    Assert.assertTrue(DataTableBlockUtils.isEndOfStream(dataTableBlock));
+    List<Object[]> resultRows = reduceMailboxReceive(mailboxReceiveOperator);
+    Assert.assertEquals(resultRows.size(), expectedRowCount);
   }
 
-  @Test
-  public void testMultipleJoin()
-      throws Exception {
-    QueryPlan queryPlan =
-        _queryEnvironment.planQuery("SELECT * FROM a JOIN b ON a.col1 = b.col2 " + "JOIN c ON a.col3 = c.col3");
-    Map<String, String> requestMetadataMap =
-        ImmutableMap.of("REQUEST_ID", String.valueOf(RANDOM_REQUEST_ID_GEN.nextLong()));
-    MailboxReceiveOperator mailboxReceiveOperator = null;
-    for (int stageId : queryPlan.getStageMetadataMap().keySet()) {
-      if (queryPlan.getQueryStageMap().get(stageId) instanceof MailboxReceiveNode) {
-        MailboxReceiveNode reduceNode = (MailboxReceiveNode) queryPlan.getQueryStageMap().get(stageId);
-        mailboxReceiveOperator = createReduceStageOperator(
-            queryPlan.getStageMetadataMap().get(reduceNode.getSenderStageId()).getServerInstances(),
-            Long.parseLong(requestMetadataMap.get("REQUEST_ID")), reduceNode.getSenderStageId(), _reducerGrpcPort);
-      } else {
-        for (ServerInstance serverInstance : queryPlan.getStageMetadataMap().get(stageId).getServerInstances()) {
-          DistributedStagePlan distributedStagePlan =
-              QueryDispatcher.constructDistributedStagePlan(queryPlan, stageId, serverInstance);
-          _servers.get(serverInstance).processQuery(distributedStagePlan, requestMetadataMap);
-        }
-      }
-    }
-    Preconditions.checkNotNull(mailboxReceiveOperator);
+  @DataProvider(name = "testDataWithSqlToFinalRowCount")
+  private Object[][] provideTestSqlAndRowCount() {
+    return new Object[][] {
+        new Object[]{"SELECT * FROM b", 5},
+        new Object[]{"SELECT * FROM a", 15},
+
+        // Specifically table A has 15 rows (10 on server1 and 5 on server2) and table B has 5 rows (all on server1),
+        // thus the final JOIN result will be 15 x 1 = 15.
+        // Next join with table C which has (5 on server1 and 10 on server2), since data is identical. each of the row
+        // of the A JOIN B will have identical value of col3 as table C.col3 has. Since the values are cycling between
+        // (1, 2, 42, 1, 2). we will have 6 1s, 6 2s, and 3 42s, total result count will be 36 + 36 + 9 = 81
+        new Object[]{"SELECT * FROM a JOIN b ON a.col1 = b.col2 JOIN c ON a.col3 = c.col3", 81},
+
+        // Specifically table A has 15 rows (10 on server1 and 5 on server2) and table B has 5 rows (all on server1),
+        // thus the final JOIN result will be 15 x 1 = 15.
+        new Object[]{"SELECT * FROM a JOIN b on a.col1 = b.col2", 15},
+
+        // Specifically table A has 15 rows (10 on server1 and 5 on server2) and table B has 5 rows (all on server1),
+        // but only 1 out of 5 rows from table A will be selected out; and all in table B will be selected.
+        // thus the final JOIN result will be 1 x 3 x 1 = 3.
+        new Object[]{"SELECT a.col1, a.ts, b.col2, b.col3 FROM a JOIN b ON a.col1 = b.col2 "
+            + " WHERE a.col3 >= 0 AND a.col2 = 'foo' AND b.col3 >= 0", 3},
+    };
+  }
 
-    int count = 0;
-    int rowCount = 0;
+  protected static List<Object[]> reduceMailboxReceive(MailboxReceiveOperator mailboxReceiveOperator) {
     List<Object[]> resultRows = new ArrayList<>();
     DataTableBlock dataTableBlock;
-    while (count < 2) { // we have 2 servers sending data.
+    while (true) {
       dataTableBlock = mailboxReceiveOperator.nextBlock();
+      if (DataTableBlockUtils.isEndOfStream(dataTableBlock)) {
+        break;
+      }
       if (dataTableBlock.getDataTable() != null) {
         DataTable dataTable = dataTableBlock.getDataTable();
         int numRows = dataTable.getNumberOfRows();
         for (int rowId = 0; rowId < numRows; rowId++) {
           resultRows.add(extractRowFromDataTable(dataTable, rowId));
         }
-        rowCount += numRows;
       }
-      count++;
     }
-
-    // Assert that each of the 5 categories from left table is joined with right table.
-    // Specifically table A has 15 rows (10 on server1 and 5 on server2) and table B has 5 rows (all on server1),
-    // thus the final JOIN result will be 15 x 1 = 15.
-    // Next join with table C which has (5 on server1 and 10 on server2), since data is identical. each of the row of
-    // the A JOIN B will have identical value of col3 as table C.col3 has. Since the values are cycling between
-    // (1, 2, 42, 1, 2). we will have 6 1s, 6 2s, and 3 42s, total result count will be 36 + 36 + 9 = 81
-    Assert.assertEquals(rowCount, 81);
-
-    // assert that the next block is null (e.g. finished receiving).
-    dataTableBlock = mailboxReceiveOperator.nextBlock();
-    Assert.assertTrue(DataTableBlockUtils.isEndOfStream(dataTableBlock));
+    return resultRows;
   }
 
   protected MailboxReceiveOperator createReduceStageOperator(List<ServerInstance> sendingInstances, long jobId,
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java
index eeed6d20d9..735c54dd0c 100644
--- a/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java
@@ -24,6 +24,7 @@ import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Random;
 import java.util.concurrent.ExecutorService;
 import org.apache.pinot.common.proto.PinotQueryWorkerGrpc;
 import org.apache.pinot.common.proto.Worker;
@@ -32,7 +33,7 @@ import org.apache.pinot.query.QueryEnvironment;
 import org.apache.pinot.query.QueryEnvironmentTestUtils;
 import org.apache.pinot.query.planner.QueryPlan;
 import org.apache.pinot.query.planner.StageMetadata;
-import org.apache.pinot.query.planner.nodes.StageNode;
+import org.apache.pinot.query.planner.stage.StageNode;
 import org.apache.pinot.query.routing.WorkerInstance;
 import org.apache.pinot.query.runtime.QueryRunner;
 import org.apache.pinot.query.runtime.plan.serde.QueryPlanSerDeUtils;
@@ -41,12 +42,14 @@ import org.mockito.Mockito;
 import org.testng.Assert;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 import static org.mockito.ArgumentMatchers.any;
 
 
 public class QueryServerTest {
+  private static final Random RANDOM_REQUEST_ID_GEN = new Random();
   private static final int QUERY_SERVER_COUNT = 2;
   private final Map<Integer, QueryServer> _queryServerMap = new HashMap<>();
   private final Map<Integer, ServerInstance> _queryServerInstanceMap = new HashMap<>();
@@ -84,13 +87,14 @@ public class QueryServerTest {
   }
 
   @SuppressWarnings("unchecked")
-  @Test
-  public void testWorkerAcceptsWorkerRequestCorrect()
+  @Test(dataProvider = "testDataWithSqlToCompiledAsWorkerRequest")
+  public void testWorkerAcceptsWorkerRequestCorrect(String sql)
       throws Exception {
-    QueryPlan queryPlan = _queryEnvironment.planQuery("SELECT * FROM a JOIN b ON a.col1 = b.col2");
+    QueryPlan queryPlan = _queryEnvironment.planQuery(sql);
 
     for (int stageId : queryPlan.getStageMetadataMap().keySet()) {
       if (stageId > 0) { // we do not test reduce stage.
+        // only get one worker request out.
         Worker.QueryRequest queryRequest = getQueryRequest(queryPlan, stageId);
 
         // submit the request for testing.
@@ -99,24 +103,39 @@ public class QueryServerTest {
         StageMetadata stageMetadata = queryPlan.getStageMetadataMap().get(stageId);
 
         // ensure mock query runner received correctly deserialized payload.
+        QueryRunner mockRunner = _queryRunnerMap.get(
+            Integer.parseInt(queryRequest.getMetadataOrThrow("SERVER_INSTANCE_PORT")));
+        String requestIdStr = queryRequest.getMetadataOrThrow("REQUEST_ID");
+
         // since submitRequest is async, we need to wait for the mockRunner to receive the query payload.
-        QueryRunner mockRunner = _queryRunnerMap.get(stageMetadata.getServerInstances().get(0).getPort());
         TestUtils.waitForCondition(aVoid -> {
           try {
             Mockito.verify(mockRunner).processQuery(Mockito.argThat(distributedStagePlan -> {
               StageNode stageNode = queryPlan.getQueryStageMap().get(stageId);
-              return isStageNodesEqual(stageNode, distributedStagePlan.getStageRoot()) && isMetadataMapsEqual(
-                  stageMetadata, distributedStagePlan.getMetadataMap().get(stageId));
-            }), any(ExecutorService.class), any(Map.class));
+              return isStageNodesEqual(stageNode, distributedStagePlan.getStageRoot())
+                  && isMetadataMapsEqual(stageMetadata, distributedStagePlan.getMetadataMap().get(stageId));
+            }), any(ExecutorService.class), Mockito.argThat(requestMetadataMap ->
+                requestIdStr.equals(requestMetadataMap.get("REQUEST_ID"))));
             return true;
           } catch (Throwable t) {
             return false;
           }
-        }, 1000L, "Error verifying mock QueryRunner intercepted query payload!");
+        }, 10000L, "Error verifying mock QueryRunner intercepted query payload!");
       }
     }
   }
 
+  @DataProvider(name = "testDataWithSqlToCompiledAsWorkerRequest")
+  private Object[][] provideTestSqlToCompiledToWorkerRequest() {
+    return new Object[][] {
+        new Object[]{"SELECT * FROM b"},
+        new Object[]{"SELECT * FROM a"},
+        new Object[]{"SELECT * FROM a JOIN b ON a.col3 = b.col3"},
+        new Object[]{"SELECT a.col1, a.ts, c.col2, c.col3 FROM a JOIN c ON a.col1 = c.col2 "
+            + " WHERE (a.col3 >= 0 OR a.col2 = 'foo') AND c.col3 >= 0"},
+    };
+  }
+
   private static boolean isMetadataMapsEqual(StageMetadata left, StageMetadata right) {
     return left.getServerInstances().equals(right.getServerInstances())
         && left.getServerInstanceToSegmentsMap().equals(right.getServerInstanceToSegmentsMap())
@@ -124,6 +143,8 @@ public class QueryServerTest {
   }
 
   private static boolean isStageNodesEqual(StageNode left, StageNode right) {
+    // This only checks the stage tree structure is correct. because the input/stageId fields are not
+    // part of the generic proto ser/de; which is tested in query planner.
     if (left.getStageId() != right.getStageId() || left.getClass() != right.getClass()
         || left.getInputs().size() != right.getInputs().size()) {
       return false;
@@ -153,6 +174,7 @@ public class QueryServerTest {
 
     return Worker.QueryRequest.newBuilder().setStagePlan(QueryPlanSerDeUtils.serialize(
             QueryDispatcher.constructDistributedStagePlan(queryPlan, stageId, serverInstance)))
+        .putMetadata("REQUEST_ID", String.valueOf(RANDOM_REQUEST_ID_GEN.nextLong()))
         .putMetadata("SERVER_INSTANCE_HOST", serverInstance.getHostname())
         .putMetadata("SERVER_INSTANCE_PORT", String.valueOf(serverInstance.getPort())).build();
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[pinot] 09/09: [hotfix] multi stage engine serde (#8689)

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rongr pushed a commit to branch multi_stage_query_engine
in repository https://gitbox.apache.org/repos/asf/pinot.git

commit fa2b2fa7cfa5410b2d5d5451942d1404ab72209e
Author: Rong Rong <ro...@apache.org>
AuthorDate: Thu May 12 15:00:54 2022 -0700

    [hotfix] multi stage engine serde (#8689)
    
    * [hotfix] serde issue with RelDataType from Calcite
    
    - fix serde issue with calcite data object
    - adding in correct serde test
    
    * remove rowType, as it is encoded in RexExpression already
    
    Co-authored-by: Rong Rong <ro...@startree.ai>
---
 .../query/parser/CalciteRexExpressionParser.java   |   7 +-
 .../query/planner/logical/RelToStageConverter.java |   8 +-
 .../pinot/query/planner/logical/RexExpression.java | 131 ++++++++++++++-------
 .../pinot/query/planner/logical/StagePlanner.java  |  14 +--
 .../query/planner/stage/AbstractStageNode.java     |  10 --
 .../pinot/query/planner/stage/FilterNode.java      |   4 +-
 .../apache/pinot/query/planner/stage/JoinNode.java |   4 +-
 .../query/planner/stage/MailboxReceiveNode.java    |   5 +-
 .../pinot/query/planner/stage/MailboxSendNode.java |  11 +-
 .../pinot/query/planner/stage/ProjectNode.java     |   8 +-
 .../pinot/query/planner/stage/TableScanNode.java   |   4 +-
 .../pinot/query/planner/stage/SerDeUtilsTest.java  |  50 ++++----
 12 files changed, 136 insertions(+), 120 deletions(-)

diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteRexExpressionParser.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteRexExpressionParser.java
index 91d573c89a..a4f5753557 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteRexExpressionParser.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteRexExpressionParser.java
@@ -22,7 +22,6 @@ import com.google.common.base.Preconditions;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.sql.SqlKind;
 import org.apache.pinot.common.request.Expression;
 import org.apache.pinot.common.request.ExpressionType;
@@ -139,11 +138,7 @@ public class CalciteRexExpressionParser {
   }
 
   private static Expression rexLiteralToExpression(RexExpression.Literal rexLiteral) {
-    RelDataType type = rexLiteral.getDataType();
-    switch (type.getSqlTypeName()) {
-      default:
-        return RequestUtils.getLiteralExpression(rexLiteral.getValue());
-    }
+    return RequestUtils.getLiteralExpression(rexLiteral.getValue());
   }
 
   private static Expression inputRefToIdentifier(RexExpression.InputRef inputRef, PinotQuery pinotQuery) {
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToStageConverter.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToStageConverter.java
index 3750437f7f..297dce68e5 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToStageConverter.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToStageConverter.java
@@ -73,18 +73,18 @@ public final class RelToStageConverter {
   }
 
   private static StageNode convertLogicalProject(LogicalProject node, int currentStageId) {
-    return new ProjectNode(currentStageId, node.getRowType(), node.getProjects());
+    return new ProjectNode(currentStageId, node.getProjects());
   }
 
   private static StageNode convertLogicalFilter(LogicalFilter node, int currentStageId) {
-    return new FilterNode(currentStageId, node.getRowType(), node.getCondition());
+    return new FilterNode(currentStageId, node.getCondition());
   }
 
   private static StageNode convertLogicalTableScan(LogicalTableScan node, int currentStageId) {
     String tableName = node.getTable().getQualifiedName().get(0);
     List<String> columnNames = node.getRowType().getFieldList().stream()
         .map(RelDataTypeField::getName).collect(Collectors.toList());
-    return new TableScanNode(currentStageId, node.getRowType(), tableName, columnNames);
+    return new TableScanNode(currentStageId, tableName, columnNames);
   }
 
   private static StageNode convertLogicalJoin(LogicalJoin node, int currentStageId) {
@@ -102,7 +102,7 @@ public final class RelToStageConverter {
     FieldSelectionKeySelector leftFieldSelectionKeySelector = new FieldSelectionKeySelector(leftOperandIndex);
     FieldSelectionKeySelector rightFieldSelectionKeySelector =
           new FieldSelectionKeySelector(rightOperandIndex - leftRowType.getFieldNames().size());
-    return new JoinNode(currentStageId, node.getRowType(), joinType, Collections.singletonList(new JoinNode.JoinClause(
+    return new JoinNode(currentStageId, joinType, Collections.singletonList(new JoinNode.JoinClause(
         leftFieldSelectionKeySelector, rightFieldSelectionKeySelector)));
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RexExpression.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RexExpression.java
index 899636ab4d..17e472c811 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RexExpression.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RexExpression.java
@@ -30,72 +30,80 @@ import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.util.NlsString;
 import org.apache.pinot.query.planner.serde.ProtoProperties;
+import org.apache.pinot.spi.data.FieldSpec;
 import org.checkerframework.checker.nullness.qual.Nullable;
 
 
 /**
  * {@code RexExpression} is the serializable format of the {@link RexNode}.
  */
-public abstract class RexExpression {
-  @ProtoProperties
-  protected SqlKind _sqlKind;
-  @ProtoProperties
-  protected RelDataType _dataType;
-
-  public SqlKind getKind() {
-    return _sqlKind;
-  }
+public interface RexExpression {
 
-  public RelDataType getDataType() {
-    return _dataType;
-  }
+  SqlKind getKind();
 
-  public static RexExpression toRexExpression(RexNode rexNode) {
+  FieldSpec.DataType getDataType();
+
+  static RexExpression toRexExpression(RexNode rexNode) {
     if (rexNode instanceof RexInputRef) {
       return new RexExpression.InputRef(((RexInputRef) rexNode).getIndex());
     } else if (rexNode instanceof RexLiteral) {
       RexLiteral rexLiteral = ((RexLiteral) rexNode);
-      return new RexExpression.Literal(rexLiteral.getType(), rexLiteral.getTypeName(), rexLiteral.getValue());
+      FieldSpec.DataType dataType = toDataType(rexLiteral.getType());
+      return new RexExpression.Literal(dataType, rexLiteral.getTypeName(),
+          toRexValue(dataType, rexLiteral.getValue()));
     } else if (rexNode instanceof RexCall) {
       RexCall rexCall = (RexCall) rexNode;
       List<RexExpression> operands = rexCall.getOperands().stream().map(RexExpression::toRexExpression)
           .collect(Collectors.toList());
-      return new RexExpression.FunctionCall(rexCall.getKind(), rexCall.getType(), rexCall.getOperator().getName(),
-          operands);
+      return new RexExpression.FunctionCall(rexCall.getKind(), toDataType(rexCall.getType()),
+          rexCall.getOperator().getName(), operands);
     } else {
       throw new IllegalArgumentException("Unsupported RexNode type with SqlKind: " + rexNode.getKind());
     }
   }
 
-  private static Comparable convertLiteral(Comparable value, SqlTypeName sqlTypeName, RelDataType dataType) {
-    switch (sqlTypeName) {
-      case BOOLEAN:
-        return (boolean) value;
-      case DECIMAL:
-        switch (dataType.getSqlTypeName()) {
-          case INTEGER:
-            return ((BigDecimal) value).intValue();
-          case BIGINT:
-            return ((BigDecimal) value).longValue();
-          case FLOAT:
-            return ((BigDecimal) value).floatValue();
-          case DOUBLE:
-          default:
-            return ((BigDecimal) value).doubleValue();
-        }
-      case CHAR:
-        switch (dataType.getSqlTypeName()) {
-          case VARCHAR:
-            return ((NlsString) value).getValue();
-          default:
-            return value;
-        }
+  static Object toRexValue(FieldSpec.DataType dataType, Comparable value) {
+    switch (dataType) {
+      case INT:
+        return ((BigDecimal) value).intValue();
+      case LONG:
+        return ((BigDecimal) value).longValue();
+      case FLOAT:
+        return ((BigDecimal) value).floatValue();
+      case DOUBLE:
+        return ((BigDecimal) value).doubleValue();
+      case STRING:
+        return ((NlsString) value).getValue();
       default:
         return value;
     }
   }
 
-  public static class InputRef extends RexExpression {
+  static FieldSpec.DataType toDataType(RelDataType type) {
+    switch (type.getSqlTypeName()) {
+      case INTEGER:
+        return FieldSpec.DataType.INT;
+      case BIGINT:
+        return FieldSpec.DataType.LONG;
+      case FLOAT:
+        return FieldSpec.DataType.FLOAT;
+      case DOUBLE:
+        return FieldSpec.DataType.DOUBLE;
+      case VARCHAR:
+        return FieldSpec.DataType.STRING;
+      case BOOLEAN:
+        return FieldSpec.DataType.BOOLEAN;
+      default:
+        // TODO: do not assume byte type.
+        return FieldSpec.DataType.BYTES;
+    }
+  }
+
+  class InputRef implements RexExpression {
+    @ProtoProperties
+    private SqlKind _sqlKind;
+    @ProtoProperties
+    private FieldSpec.DataType _dataType;
     @ProtoProperties
     private int _index;
 
@@ -110,27 +118,51 @@ public abstract class RexExpression {
     public int getIndex() {
       return _index;
     }
+
+    public SqlKind getKind() {
+      return _sqlKind;
+    }
+
+    public FieldSpec.DataType getDataType() {
+      return _dataType;
+    }
   }
 
-  public static class Literal extends RexExpression {
+  class Literal implements RexExpression {
+    @ProtoProperties
+    private SqlKind _sqlKind;
+    @ProtoProperties
+    private FieldSpec.DataType _dataType;
     @ProtoProperties
     private Object _value;
 
     public Literal() {
     }
 
-    public Literal(RelDataType dataType, SqlTypeName sqlTypeName, @Nullable Comparable value) {
+    public Literal(FieldSpec.DataType dataType, SqlTypeName sqlTypeName, @Nullable Object value) {
       _sqlKind = SqlKind.LITERAL;
       _dataType = dataType;
-      _value = convertLiteral(value, sqlTypeName, dataType);
+      _value = value;
     }
 
     public Object getValue() {
       return _value;
     }
+
+    public SqlKind getKind() {
+      return _sqlKind;
+    }
+
+    public FieldSpec.DataType getDataType() {
+      return _dataType;
+    }
   }
 
-  public static class FunctionCall extends RexExpression {
+  class FunctionCall implements RexExpression {
+    @ProtoProperties
+    private SqlKind _sqlKind;
+    @ProtoProperties
+    private FieldSpec.DataType _dataType;
     @ProtoProperties
     private String _functionName;
     @ProtoProperties
@@ -139,7 +171,8 @@ public abstract class RexExpression {
     public FunctionCall() {
     }
 
-    public FunctionCall(SqlKind sqlKind, RelDataType type, String functionName, List<RexExpression> functionOperands) {
+    public FunctionCall(SqlKind sqlKind, FieldSpec.DataType type, String functionName,
+        List<RexExpression> functionOperands) {
       _sqlKind = sqlKind;
       _dataType = type;
       _functionName = functionName;
@@ -153,5 +186,13 @@ public abstract class RexExpression {
     public List<RexExpression> getFunctionOperands() {
       return _functionOperands;
     }
+
+    public SqlKind getKind() {
+      return _sqlKind;
+    }
+
+    public FieldSpec.DataType getDataType() {
+      return _dataType;
+    }
   }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/StagePlanner.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/StagePlanner.java
index b5dfab8689..db7fd63d83 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/StagePlanner.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/StagePlanner.java
@@ -72,10 +72,9 @@ public class StagePlanner {
     // global root needs to send results back to the ROOT, a.k.a. the client response node. the last stage only has one
     // receiver so doesn't matter what the exchange type is. setting it to SINGLETON by default.
     StageNode globalReceiverNode =
-        new MailboxReceiveNode(0, relRoot.getRowType(), globalStageRoot.getStageId(),
-            RelDistribution.Type.SINGLETON);
-    StageNode globalSenderNode = new MailboxSendNode(globalStageRoot.getStageId(), relRoot.getRowType(),
-        globalReceiverNode.getStageId(), RelDistribution.Type.SINGLETON);
+        new MailboxReceiveNode(0, globalStageRoot.getStageId(), RelDistribution.Type.SINGLETON);
+    StageNode globalSenderNode = new MailboxSendNode(globalStageRoot.getStageId(), globalReceiverNode.getStageId(),
+        RelDistribution.Type.SINGLETON);
     globalSenderNode.addInput(globalStageRoot);
     _queryStageMap.put(globalSenderNode.getStageId(), globalSenderNode);
     StageMetadata stageMetadata = _stageMetadataMap.get(globalSenderNode.getStageId());
@@ -103,10 +102,9 @@ public class StagePlanner {
       RelDistribution.Type exchangeType = distribution.getType();
 
       // 2. make an exchange sender and receiver node pair
-      StageNode mailboxReceiver = new MailboxReceiveNode(currentStageId, node.getRowType(), nextStageRoot.getStageId(),
-          exchangeType);
-      StageNode mailboxSender = new MailboxSendNode(nextStageRoot.getStageId(), node.getRowType(),
-          mailboxReceiver.getStageId(), exchangeType, exchangeType == RelDistribution.Type.HASH_DISTRIBUTED
+      StageNode mailboxReceiver = new MailboxReceiveNode(currentStageId, nextStageRoot.getStageId(), exchangeType);
+      StageNode mailboxSender = new MailboxSendNode(nextStageRoot.getStageId(), mailboxReceiver.getStageId(),
+          exchangeType, exchangeType == RelDistribution.Type.HASH_DISTRIBUTED
           ? new FieldSelectionKeySelector(distribution.getKeys().get(0)) : null);
       mailboxSender.addInput(nextStageRoot);
 
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/AbstractStageNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/AbstractStageNode.java
index bdcccea355..0f84a10f68 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/AbstractStageNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/AbstractStageNode.java
@@ -20,21 +20,15 @@ package org.apache.pinot.query.planner.stage;
 
 import java.util.ArrayList;
 import java.util.List;
-import org.apache.calcite.rel.type.RelDataType;
 import org.apache.pinot.common.proto.Plan;
-import org.apache.pinot.query.planner.serde.ProtoProperties;
 import org.apache.pinot.query.planner.serde.ProtoSerializable;
 import org.apache.pinot.query.planner.serde.ProtoSerializationUtils;
 
 
 public abstract class AbstractStageNode implements StageNode, ProtoSerializable {
 
-  @ProtoProperties
   protected final int _stageId;
-  @ProtoProperties
   protected final List<StageNode> _inputs;
-  @ProtoProperties
-  protected RelDataType _rowType;
 
   public AbstractStageNode(int stageId) {
     _stageId = stageId;
@@ -65,8 +59,4 @@ public abstract class AbstractStageNode implements StageNode, ProtoSerializable
   public Plan.ObjectField toObjectField() {
     return ProtoSerializationUtils.convertObjectToObjectField(this);
   }
-
-  public RelDataType getRowType() {
-    return _rowType;
-  }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/FilterNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/FilterNode.java
index 52df4ed5d3..c169a61970 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/FilterNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/FilterNode.java
@@ -18,7 +18,6 @@
  */
 package org.apache.pinot.query.planner.stage;
 
-import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rex.RexNode;
 import org.apache.pinot.query.planner.logical.RexExpression;
 import org.apache.pinot.query.planner.serde.ProtoProperties;
@@ -32,9 +31,8 @@ public class FilterNode extends AbstractStageNode {
     super(stageId);
   }
 
-  public FilterNode(int currentStageId, RelDataType rowType, RexNode condition) {
+  public FilterNode(int currentStageId, RexNode condition) {
     super(currentStageId);
-    super._rowType = rowType;
     _condition = RexExpression.toRexExpression(condition);
   }
 
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/JoinNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/JoinNode.java
index 96b6c43a95..223f0addee 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/JoinNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/JoinNode.java
@@ -20,7 +20,6 @@ package org.apache.pinot.query.planner.stage;
 
 import java.util.List;
 import org.apache.calcite.rel.core.JoinRelType;
-import org.apache.calcite.rel.type.RelDataType;
 import org.apache.pinot.query.planner.partitioning.FieldSelectionKeySelector;
 import org.apache.pinot.query.planner.partitioning.KeySelector;
 import org.apache.pinot.query.planner.serde.ProtoProperties;
@@ -36,9 +35,8 @@ public class JoinNode extends AbstractStageNode {
     super(stageId);
   }
 
-  public JoinNode(int stageId, RelDataType rowType, JoinRelType joinRelType, List<JoinClause> criteria) {
+  public JoinNode(int stageId, JoinRelType joinRelType, List<JoinClause> criteria) {
     super(stageId);
-    super._rowType = rowType;
     _joinRelType = joinRelType;
     _criteria = criteria;
   }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxReceiveNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxReceiveNode.java
index 1c01d8de5c..edadf30570 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxReceiveNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxReceiveNode.java
@@ -19,7 +19,6 @@
 package org.apache.pinot.query.planner.stage;
 
 import org.apache.calcite.rel.RelDistribution;
-import org.apache.calcite.rel.type.RelDataType;
 import org.apache.pinot.query.planner.serde.ProtoProperties;
 
 
@@ -33,9 +32,9 @@ public class MailboxReceiveNode extends AbstractStageNode {
     super(stageId);
   }
 
-  public MailboxReceiveNode(int stageId, RelDataType rowType, int senderStageId, RelDistribution.Type exchangeType) {
+  public MailboxReceiveNode(int stageId, int senderStageId,
+      RelDistribution.Type exchangeType) {
     super(stageId);
-    super._rowType = rowType;
     _senderStageId = senderStageId;
     _exchangeType = exchangeType;
   }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxSendNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxSendNode.java
index c3f540aa0e..2d7eb816f7 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxSendNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/MailboxSendNode.java
@@ -20,7 +20,6 @@ package org.apache.pinot.query.planner.stage;
 
 import javax.annotation.Nullable;
 import org.apache.calcite.rel.RelDistribution;
-import org.apache.calcite.rel.type.RelDataType;
 import org.apache.pinot.query.planner.partitioning.KeySelector;
 import org.apache.pinot.query.planner.serde.ProtoProperties;
 
@@ -37,15 +36,15 @@ public class MailboxSendNode extends AbstractStageNode {
     super(stageId);
   }
 
-  public MailboxSendNode(int stageId, RelDataType rowType, int receiverStageId, RelDistribution.Type exchangeType) {
+  public MailboxSendNode(int stageId, int receiverStageId,
+      RelDistribution.Type exchangeType) {
     // When exchangeType is not HASH_DISTRIBUTE, no partitionKeySelector is needed.
-    this(stageId, rowType, receiverStageId, exchangeType, null);
+    this(stageId, receiverStageId, exchangeType, null);
   }
 
-  public MailboxSendNode(int stageId, RelDataType rowType, int receiverStageId, RelDistribution.Type exchangeType,
-      @Nullable KeySelector<Object[], Object> partitionKeySelector) {
+  public MailboxSendNode(int stageId, int receiverStageId,
+      RelDistribution.Type exchangeType, @Nullable KeySelector<Object[], Object> partitionKeySelector) {
     super(stageId);
-    super._rowType = rowType;
     _receiverStageId = receiverStageId;
     _exchangeType = exchangeType;
     _partitionKeySelector = partitionKeySelector;
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/ProjectNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/ProjectNode.java
index 4ee9be6c0a..9a026aae18 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/ProjectNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/ProjectNode.java
@@ -20,7 +20,6 @@ package org.apache.pinot.query.planner.stage;
 
 import java.util.List;
 import java.util.stream.Collectors;
-import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rex.RexNode;
 import org.apache.pinot.query.planner.logical.RexExpression;
 import org.apache.pinot.query.planner.serde.ProtoProperties;
@@ -33,17 +32,12 @@ public class ProjectNode extends AbstractStageNode {
   public ProjectNode(int stageId) {
     super(stageId);
   }
-  public ProjectNode(int currentStageId, RelDataType rowType, List<RexNode> projects) {
+  public ProjectNode(int currentStageId, List<RexNode> projects) {
     super(currentStageId);
-    super._rowType = rowType;
     _projects = projects.stream().map(RexExpression::toRexExpression).collect(Collectors.toList());
   }
 
   public List<RexExpression> getProjects() {
     return _projects;
   }
-
-  public RelDataType getRowType() {
-    return _rowType;
-  }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/TableScanNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/TableScanNode.java
index 9ba36d34f3..7151f84f56 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/TableScanNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/stage/TableScanNode.java
@@ -19,7 +19,6 @@
 package org.apache.pinot.query.planner.stage;
 
 import java.util.List;
-import org.apache.calcite.rel.type.RelDataType;
 import org.apache.pinot.query.planner.serde.ProtoProperties;
 
 
@@ -33,9 +32,8 @@ public class TableScanNode extends AbstractStageNode {
     super(stageId);
   }
 
-  public TableScanNode(int stageId, RelDataType rowType, String tableName, List<String> tableScanColumns) {
+  public TableScanNode(int stageId, String tableName, List<String> tableScanColumns) {
     super(stageId);
-    super._rowType = rowType;
     _tableName = tableName;
     _tableScanColumns = tableScanColumns;
   }
diff --git a/pinot-query-planner/src/test/java/org/apache/pinot/query/planner/stage/SerDeUtilsTest.java b/pinot-query-planner/src/test/java/org/apache/pinot/query/planner/stage/SerDeUtilsTest.java
index 21031cd303..db9294252c 100644
--- a/pinot-query-planner/src/test/java/org/apache/pinot/query/planner/stage/SerDeUtilsTest.java
+++ b/pinot-query-planner/src/test/java/org/apache/pinot/query/planner/stage/SerDeUtilsTest.java
@@ -46,35 +46,41 @@ public class SerDeUtilsTest extends QueryEnvironmentTestBase {
   private boolean isObjectEqual(Object left, Object right)
       throws IllegalAccessException {
     Class<?> clazz = left.getClass();
-    for (Field field : clazz.getDeclaredFields()) {
-      if (field.isAnnotationPresent(ProtoProperties.class)) {
-        field.setAccessible(true);
-        Object l = field.get(left);
-        Object r = field.get(right);
-        if (l instanceof List) {
-          if (((List) l).size() != ((List) r).size()) {
-            return false;
-          }
-          for (int i = 0; i < ((List) l).size(); i++) {
-            if (!isObjectEqual(((List) l).get(i), ((List) r).get(i))) {
+    while (Object.class != clazz) {
+      for (Field field : clazz.getDeclaredFields()) {
+        if (field.isAnnotationPresent(ProtoProperties.class)) {
+          field.setAccessible(true);
+          Object l = field.get(left);
+          Object r = field.get(right);
+          if (l instanceof List) {
+            if (((List) l).size() != ((List) r).size()) {
               return false;
             }
-          }
-        } else if (l instanceof Map) {
-          if (((Map) l).size() != ((Map) r).size()) {
-            return false;
-          }
-          for (Object key : ((Map) l).keySet()) {
-            if (!isObjectEqual(((Map) l).get(key), ((Map) r).get(key))) {
+            for (int i = 0; i < ((List) l).size(); i++) {
+              if (!isObjectEqual(((List) l).get(i), ((List) r).get(i))) {
+                return false;
+              }
+            }
+          } else if (l instanceof Map) {
+            if (((Map) l).size() != ((Map) r).size()) {
+              return false;
+            }
+            for (Object key : ((Map) l).keySet()) {
+              if (!isObjectEqual(((Map) l).get(key), ((Map) r).get(key))) {
+                return false;
+              }
+            }
+          } else {
+            if (l == null && r != null || l != null && r == null) {
+              return false;
+            }
+            if (!(l == null && r == null || l != null && l.equals(r) || isObjectEqual(l, r))) {
               return false;
             }
-          }
-        } else {
-          if (!(l == null && r == null || l != null && l.equals(r) || isObjectEqual(l, r))) {
-            return false;
           }
         }
       }
+      clazz = clazz.getSuperclass();
     }
     return true;
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[pinot] 02/09: Add pinot-query-planner module (#8340)

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rongr pushed a commit to branch multi_stage_query_engine
in repository https://gitbox.apache.org/repos/asf/pinot.git

commit 8c8380005b6d833ee9d20ad83487aac1439e0019
Author: Rong Rong <ro...@apache.org>
AuthorDate: Thu Mar 24 21:13:51 2022 -0700

    Add pinot-query-planner module (#8340)
    
    * add pinot-query-planner
    
    - fix calcite upgrade compilation issue
    - fix query compilation runtime after calcite 1.29 upgrade
    - linter
    
    * address diff comments and add more TODOs
    
    Co-authored-by: Rong Rong <ro...@startree.ai>
---
 .../pinot/common/config/provider/TableCache.java   |  10 +
 pinot-query-planner/pom.xml                        |  92 ++++
 .../apache/calcite/jdbc/CalciteSchemaBuilder.java  |  52 +++
 .../org/apache/pinot/query/QueryEnvironment.java   | 181 ++++++++
 .../apache/pinot/query/catalog/PinotCatalog.java   | 122 +++++
 .../org/apache/pinot/query/catalog/PinotTable.java |  61 +++
 .../apache/pinot/query/context/PlannerContext.java |  40 ++
 .../query/parser/CalciteExpressionParser.java      | 502 +++++++++++++++++++++
 .../pinot/query/parser/CalciteSqlParser.java       | 148 ++++++
 .../org/apache/pinot/query/parser/ParserUtils.java |  63 +++
 .../apache/pinot/query/parser/QueryRewriter.java   |  46 ++
 .../apache/pinot/query/planner/LogicalPlanner.java |  63 +++
 .../org/apache/pinot/query/planner/QueryPlan.java  |  60 +++
 .../pinot/query/planner/RelToStageConverter.java   |  71 +++
 .../apache/pinot/query/planner/StageMetadata.java  |  85 ++++
 .../apache/pinot/query/planner/StagePlanner.java   | 126 ++++++
 .../query/planner/nodes/AbstractStageNode.java     |  49 ++
 .../apache/pinot/query/planner/nodes/CalcNode.java |  40 ++
 .../apache/pinot/query/planner/nodes/JoinNode.java |  86 ++++
 .../query/planner/nodes/MailboxReceiveNode.java    |  54 +++
 .../pinot/query/planner/nodes/MailboxSendNode.java |  55 +++
 .../pinot/query/planner/nodes/StageNode.java       |  40 ++
 .../pinot/query/planner/nodes/TableScanNode.java   |  57 +++
 .../partitioning/FieldSelectionKeySelector.java    |  39 ++
 .../query/planner/partitioning/KeySelector.java    |  37 ++
 .../apache/pinot/query/routing/WorkerInstance.java |  51 +++
 .../apache/pinot/query/routing/WorkerManager.java  |  95 ++++
 .../query/rules/PinotExchangeNodeInsertRule.java   |  82 ++++
 .../pinot/query/rules/PinotQueryRuleSets.java      |  91 ++++
 .../org/apache/pinot/query/type/TypeFactory.java   |  84 ++++
 .../org/apache/pinot/query/type/TypeSystem.java    |  30 ++
 .../org/apache/pinot/query/validate/Validator.java |  36 ++
 .../apache/pinot/query/QueryEnvironmentTest.java   | 122 +++++
 .../pinot/query/QueryEnvironmentTestUtils.java     | 131 ++++++
 pom.xml                                            |   6 +-
 35 files changed, 2903 insertions(+), 4 deletions(-)

diff --git a/pinot-common/src/main/java/org/apache/pinot/common/config/provider/TableCache.java b/pinot-common/src/main/java/org/apache/pinot/common/config/provider/TableCache.java
index 17fbf73cd3..005fd65615 100644
--- a/pinot-common/src/main/java/org/apache/pinot/common/config/provider/TableCache.java
+++ b/pinot-common/src/main/java/org/apache/pinot/common/config/provider/TableCache.java
@@ -227,6 +227,16 @@ public class TableCache implements PinotConfigProvider {
     }
   }
 
+  /**
+   * Return a map between lower-case table name and their canonicalized form. Key-value pair are only different in
+   * case-sensitive environment.
+   *
+   * @return the table name map.
+   */
+  public Map<String, String> getTableNameMap() {
+    return _tableNameMap;
+  }
+
   private void addTableConfigs(List<String> paths) {
     // Subscribe data changes before reading the data to avoid missing changes
     for (String path : paths) {
diff --git a/pinot-query-planner/pom.xml b/pinot-query-planner/pom.xml
new file mode 100644
index 0000000000..8d1af64a19
--- /dev/null
+++ b/pinot-query-planner/pom.xml
@@ -0,0 +1,92 @@
+<?xml version="1.0"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>pinot</artifactId>
+    <groupId>org.apache.pinot</groupId>
+    <version>0.10.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>pinot-query-planner</artifactId>
+  <name>Pinot Query Planner</name>
+  <url>https://pinot.apache.org/</url>
+
+  <properties>
+    <pinot.root>${basedir}/..</pinot.root>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-jar-plugin</artifactId>
+        <executions>
+          <execution>
+            <goals>
+              <goal>test-jar</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <!-- Pinot dependencies -->
+    <dependency>
+      <groupId>org.apache.pinot</groupId>
+      <artifactId>pinot-core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.pinot</groupId>
+      <artifactId>pinot-common</artifactId>
+    </dependency>
+
+    <!-- Calcite dependencies -->
+    <dependency>
+      <groupId>org.apache.calcite</groupId>
+      <artifactId>calcite-core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.janino</groupId>
+      <artifactId>janino</artifactId>
+      <version>3.0.9</version>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.janino</groupId>
+      <artifactId>commons-compiler</artifactId>
+      <version>3.0.9</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.testng</groupId>
+      <artifactId>testng</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/pinot-query-planner/src/main/java/org/apache/calcite/jdbc/CalciteSchemaBuilder.java b/pinot-query-planner/src/main/java/org/apache/calcite/jdbc/CalciteSchemaBuilder.java
new file mode 100644
index 0000000000..ce3d1c99f9
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/calcite/jdbc/CalciteSchemaBuilder.java
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.calcite.jdbc;
+
+import org.apache.calcite.schema.Schema;
+
+
+/**
+ * This class is used to create a {@link CalciteSchema} with a given {@link Schema} as the root.
+ *
+ * <p>This class resides in calcite.jdbc namespace because there's no complex logic we have in terms of catalog-based
+ * schema construct. We instead create a {@link SimpleCalciteSchema} that's package protected.
+ */
+public class CalciteSchemaBuilder {
+
+  private CalciteSchemaBuilder() {
+    // do not instantiate.
+  }
+
+  /**
+   * Creates a {@link CalciteSchema} with a given {@link Schema} as the root.
+   *
+   * <p>Calcite creates two layer of abstraction, the {@link CalciteSchema} is use internally for planner and
+   * {@link Schema} is user-facing with overrides. In our case we don't have a complex internal wrapper extension
+   * so we only reuse the package protected {@link SimpleCalciteSchema}.
+   *
+   * <p>If there's need to extend this feature for planner functionalities we should create our own extension to the
+   * {@link CalciteSchema}.
+   *
+   * @param root schema to use as a root schema
+   * @return calcite schema with given schema as the root
+   */
+  public static CalciteSchema asRootSchema(Schema root) {
+    return new SimpleCalciteSchema(null, root, "");
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java
new file mode 100644
index 0000000000..215c0051d7
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java
@@ -0,0 +1,181 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query;
+
+import java.util.Collection;
+import org.apache.calcite.jdbc.CalciteSchema;
+import org.apache.calcite.plan.Contexts;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.plan.hep.HepProgramBuilder;
+import org.apache.calcite.prepare.CalciteCatalogReader;
+import org.apache.calcite.prepare.PlannerImpl;
+import org.apache.calcite.prepare.Prepare;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelRoot;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql2rel.SqlToRelConverter;
+import org.apache.calcite.sql2rel.StandardConvertletTable;
+import org.apache.calcite.tools.FrameworkConfig;
+import org.apache.calcite.tools.Frameworks;
+import org.apache.pinot.query.context.PlannerContext;
+import org.apache.pinot.query.parser.CalciteSqlParser;
+import org.apache.pinot.query.planner.LogicalPlanner;
+import org.apache.pinot.query.planner.QueryPlan;
+import org.apache.pinot.query.planner.StagePlanner;
+import org.apache.pinot.query.routing.WorkerManager;
+import org.apache.pinot.query.rules.PinotQueryRuleSets;
+import org.apache.pinot.query.type.TypeFactory;
+import org.apache.pinot.query.validate.Validator;
+
+
+/**
+ * The {@code QueryEnvironment} contains the main entrypoint for query planning.
+ *
+ * <p>It provide the higher level entry interface to convert a SQL string into a {@link QueryPlan}.
+ */
+public class QueryEnvironment {
+  // Calcite configurations
+  private final FrameworkConfig _config;
+
+  // Calcite extension/plugins
+  private final CalciteSchema _rootSchema;
+  private final PlannerImpl _planner;
+  private final Prepare.CatalogReader _catalogReader;
+  private final RelDataTypeFactory _typeFactory;
+  private final RelOptPlanner _relOptPlanner;
+  private final SqlValidator _validator;
+
+  // Pinot extensions
+  private final Collection<RelOptRule> _logicalRuleSet;
+  private final WorkerManager _workerManager;
+
+  public QueryEnvironment(TypeFactory typeFactory, CalciteSchema rootSchema, WorkerManager workerManager) {
+    _typeFactory = typeFactory;
+    _rootSchema = rootSchema;
+    _workerManager = workerManager;
+    _config = Frameworks.newConfigBuilder().traitDefs().build();
+
+    // Planner is not thread-safe. must be reset() after each use.
+    _planner = new PlannerImpl(_config);
+
+    // catalog
+    _catalogReader = new CalciteCatalogReader(_rootSchema, _rootSchema.path(null), _typeFactory, null);
+    _validator = new Validator(SqlStdOperatorTable.instance(), _catalogReader, _typeFactory);
+
+    // optimizer rules
+    _logicalRuleSet = PinotQueryRuleSets.LOGICAL_OPT_RULES;
+
+    // optimizer
+    HepProgramBuilder hepProgramBuilder = new HepProgramBuilder();
+    for (RelOptRule relOptRule : _logicalRuleSet) {
+      hepProgramBuilder.addRuleInstance(relOptRule);
+    }
+    _relOptPlanner = new LogicalPlanner(hepProgramBuilder.build(), Contexts.EMPTY_CONTEXT);
+  }
+
+  /**
+   * Plan a SQL query.
+   *
+   * <p>Noted that since Calcite's {@link org.apache.calcite.tools.Planner} is not threadsafe.
+   * Only one query query can be planned at a time. Afterwards planner needs to be reset in order to clear the
+   * state for the next planning.
+   *
+   * <p>In order for faster planning, we pre-constructed all the planner objects and use the plan-then-reset
+   * model. Thusn when using {@code QueryEnvironment#planQuery(String)}, caller should ensure that no concurrent
+   * plan execution occurs.
+   *
+   * TODO: follow benchmark and profile to measure whether it make sense for the latency-concurrency trade-off
+   * between reusing plannerImpl vs. create a new planner for each query.
+   *
+   * @param sqlQuery SQL query string.
+   * @return a dispatchable query plan
+   */
+  public QueryPlan planQuery(String sqlQuery) {
+    PlannerContext plannerContext = new PlannerContext();
+    try {
+      SqlNode parsed = parse(sqlQuery, plannerContext);
+      SqlNode validated = validate(parsed);
+      RelRoot relation = toRelation(validated, plannerContext);
+      RelNode optimized = optimize(relation, plannerContext);
+      return toDispatchablePlan(optimized, plannerContext);
+    } catch (Exception e) {
+      throw new RuntimeException("Error composing query plan for: " + sqlQuery, e);
+    } finally {
+      _planner.close();
+      _planner.reset();
+    }
+  }
+
+  // --------------------------------------------------------------------------
+  // steps
+  // --------------------------------------------------------------------------
+
+  protected SqlNode parse(String query, PlannerContext plannerContext)
+      throws Exception {
+    // 1. invoke CalciteSqlParser to parse out SqlNode;
+    return CalciteSqlParser.compile(query, plannerContext);
+  }
+
+  protected SqlNode validate(SqlNode parsed)
+      throws Exception {
+    // 2. validator to validate.
+    SqlNode validated = _validator.validate(parsed);
+    if (null == validated || !validated.getKind().belongsTo(SqlKind.QUERY)) {
+      throw new IllegalArgumentException(
+          String.format("unsupported SQL query, cannot validate out a valid sql from:\n%s", parsed));
+    }
+    return validated;
+  }
+
+  protected RelRoot toRelation(SqlNode parsed, PlannerContext plannerContext) {
+    // 3. convert sqlNode to relNode.
+    RexBuilder rexBuilder = new RexBuilder(_typeFactory);
+    RelOptCluster cluster = RelOptCluster.create(_relOptPlanner, rexBuilder);
+    SqlToRelConverter sqlToRelConverter =
+        new SqlToRelConverter(_planner, _validator, _catalogReader, cluster, StandardConvertletTable.INSTANCE,
+            SqlToRelConverter.config());
+    return sqlToRelConverter.convertQuery(parsed, false, true);
+  }
+
+  protected RelNode optimize(RelRoot relRoot, PlannerContext plannerContext) {
+    // 4. optimize relNode
+    // TODO: add support for traits, cost factory.
+    try {
+      _relOptPlanner.setRoot(relRoot.rel);
+      return _relOptPlanner.findBestExp();
+    } catch (Exception e) {
+      throw new UnsupportedOperationException(
+          "Cannot generate a valid execution plan for the given query: " + RelOptUtil.toString(relRoot.rel), e);
+    }
+  }
+
+  protected QueryPlan toDispatchablePlan(RelNode relRoot, PlannerContext plannerContext) {
+    // 5. construct a dispatchable query plan.
+    StagePlanner queryStagePlanner = new StagePlanner(plannerContext, _workerManager);
+    return queryStagePlanner.makePlan(relRoot);
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/catalog/PinotCatalog.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/catalog/PinotCatalog.java
new file mode 100644
index 0000000000..34673a274b
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/catalog/PinotCatalog.java
@@ -0,0 +1,122 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.catalog;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.calcite.linq4j.tree.Expression;
+import org.apache.calcite.rel.type.RelProtoDataType;
+import org.apache.calcite.schema.Function;
+import org.apache.calcite.schema.Schema;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.SchemaVersion;
+import org.apache.calcite.schema.Schemas;
+import org.apache.calcite.schema.Table;
+import org.apache.pinot.common.config.provider.TableCache;
+import org.apache.pinot.spi.utils.builder.TableNameBuilder;
+
+import static java.util.Objects.requireNonNull;
+
+
+/**
+ * Simple Catalog that only contains list of tables. Backed by {@link TableCache}.
+ *
+ * <p>Catalog is needed for utilizing Apache Calcite's validator, which requires a root schema to store the
+ * entire catalog. In Pinot, since we don't have nested sub-catalog concept, we just return a flat list of schemas.
+ */
+public class PinotCatalog implements Schema {
+
+  private final TableCache _tableCache;
+
+  /**
+   * PinotCatalog needs have access to the actual {@link TableCache} object because TableCache hosts the actual
+   * table available for query and processes table/segment metadata updates when cluster status changes.
+   */
+  public PinotCatalog(TableCache tableCache) {
+    _tableCache = tableCache;
+  }
+
+  /**
+   * Acquire a table by its name.
+   * @param name name of the table.
+   * @return table object used by calcite planner.
+   */
+  @Override
+  public Table getTable(String name) {
+    String tableName = TableNameBuilder.extractRawTableName(name);
+    return new PinotTable(_tableCache.getSchema(tableName));
+  }
+
+  /**
+   * acquire a set of available table names.
+   * @return the set of table names at the time of query planning.
+   */
+  @Override
+  public Set<String> getTableNames() {
+    return _tableCache.getTableNameMap().keySet();
+  }
+
+  @Override
+  public RelProtoDataType getType(String name) {
+    return null;
+  }
+
+  @Override
+  public Set<String> getTypeNames() {
+    return Collections.emptySet();
+  }
+
+  @Override
+  public Collection<Function> getFunctions(String name) {
+    return Collections.emptyList();
+  }
+
+  @Override
+  public Set<String> getFunctionNames() {
+    return Collections.emptySet();
+  }
+
+  @Override
+  public Schema getSubSchema(String name) {
+    return null;
+  }
+
+  @Override
+  public Set<String> getSubSchemaNames() {
+    return Collections.emptySet();
+  }
+
+  @Override
+  public Expression getExpression(@Nullable SchemaPlus parentSchema, String name) {
+    requireNonNull(parentSchema, "parentSchema");
+    return Schemas.subSchemaExpression(parentSchema, name, getClass());
+  }
+
+  @Override
+  public boolean isMutable() {
+    return false;
+  }
+
+  @Override
+  public Schema snapshot(SchemaVersion version) {
+    return this;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/catalog/PinotTable.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/catalog/PinotTable.java
new file mode 100644
index 0000000000..23e6444e90
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/catalog/PinotTable.java
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.catalog;
+
+import com.clearspring.analytics.util.Preconditions;
+import org.apache.calcite.DataContext;
+import org.apache.calcite.linq4j.Enumerable;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.ScannableTable;
+import org.apache.calcite.schema.impl.AbstractTable;
+import org.apache.pinot.query.type.TypeFactory;
+import org.apache.pinot.spi.data.Schema;
+
+
+/**
+ * Wrapper for pinot internal info for a table.
+ *
+ * <p>This construct is used to connect a Pinot table to Apache Calcite's relational planner by providing a
+ * {@link RelDataType} of the table to the planner.
+ */
+public class PinotTable extends AbstractTable implements ScannableTable {
+  private Schema _schema;
+
+  public PinotTable(Schema schema) {
+    _schema = schema;
+  }
+
+  @Override
+  public RelDataType getRowType(RelDataTypeFactory relDataTypeFactory) {
+    Preconditions.checkState(relDataTypeFactory instanceof TypeFactory);
+    TypeFactory typeFactory = (TypeFactory) relDataTypeFactory;
+    return typeFactory.createRelDataTypeFromSchema(_schema);
+  }
+
+  @Override
+  public boolean isRolledUp(String s) {
+    return false;
+  }
+
+  @Override
+  public Enumerable<Object[]> scan(DataContext dataContext) {
+    return null;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/context/PlannerContext.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/context/PlannerContext.java
new file mode 100644
index 0000000000..1997d1f370
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/context/PlannerContext.java
@@ -0,0 +1,40 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.context;
+
+import java.util.Map;
+
+
+/**
+ * PlannerContext is an object that holds all contextual information during planning phase.
+ *
+ * TODO: currently the planner context is not used since we don't support option or query rewrite. This construct is
+ * here as a placeholder for the parsed out options.
+ */
+public class PlannerContext {
+  private Map<String, String> _options;
+
+  public void setOptions(Map<String, String> options) {
+    _options = options;
+  }
+
+  public Map<String, String> getOptions() {
+    return _options;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteExpressionParser.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteExpressionParser.java
new file mode 100644
index 0000000000..fc75efb1ae
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteExpressionParser.java
@@ -0,0 +1,502 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.parser;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlDataTypeSpec;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.fun.SqlCase;
+import org.apache.calcite.sql.parser.SqlParseException;
+import org.apache.calcite.sql.parser.SqlParser;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.ExpressionType;
+import org.apache.pinot.common.request.Function;
+import org.apache.pinot.common.utils.request.RequestUtils;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+import org.apache.pinot.sql.parsers.SqlCompilationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Calcite parser to convert SQL expressions into {@link Expression}.
+ *
+ * <p>This class is extracted from {@link org.apache.pinot.sql.parsers.CalciteSqlParser}. It only contains the
+ * {@link Expression} related info, this is used for ingestion and query rewrite.
+ */
+public class CalciteExpressionParser {
+  private static final Logger LOGGER = LoggerFactory.getLogger(CalciteExpressionParser.class);
+
+  private CalciteExpressionParser() {
+    // do not instantiate.
+  }
+
+  private static List<Expression> getAliasLeftExpressionsFromDistinctExpression(Function function) {
+    List<Expression> operands = function.getOperands();
+    List<Expression> expressions = new ArrayList<>(operands.size());
+    for (Expression operand : operands) {
+      if (isAsFunction(operand)) {
+        expressions.add(operand.getFunctionCall().getOperands().get(0));
+      } else {
+        expressions.add(operand);
+      }
+    }
+    return expressions;
+  }
+
+  public static boolean isAggregateExpression(Expression expression) {
+    Function functionCall = expression.getFunctionCall();
+    if (functionCall != null) {
+      String operator = functionCall.getOperator();
+      try {
+        AggregationFunctionType.getAggregationFunctionType(operator);
+        return true;
+      } catch (IllegalArgumentException e) {
+      }
+      if (functionCall.getOperandsSize() > 0) {
+        for (Expression operand : functionCall.getOperands()) {
+          if (isAggregateExpression(operand)) {
+            return true;
+          }
+        }
+      }
+    }
+    return false;
+  }
+
+  public static boolean isAsFunction(Expression expression) {
+    return expression.getFunctionCall() != null && expression.getFunctionCall().getOperator().equalsIgnoreCase("AS");
+  }
+
+  /**
+   * Extract all the identifiers from given expressions.
+   *
+   * @param expressions
+   * @param excludeAs if true, ignores the right side identifier for AS function.
+   * @return all the identifier names.
+   */
+  public static Set<String> extractIdentifiers(List<Expression> expressions, boolean excludeAs) {
+    Set<String> identifiers = new HashSet<>();
+    for (Expression expression : expressions) {
+      if (expression.getIdentifier() != null) {
+        identifiers.add(expression.getIdentifier().getName());
+      } else if (expression.getFunctionCall() != null) {
+        if (excludeAs && expression.getFunctionCall().getOperator().equalsIgnoreCase("AS")) {
+          identifiers.addAll(
+              extractIdentifiers(Arrays.asList(expression.getFunctionCall().getOperands().get(0)), true));
+          continue;
+        } else {
+          identifiers.addAll(extractIdentifiers(expression.getFunctionCall().getOperands(), excludeAs));
+        }
+      }
+    }
+    return identifiers;
+  }
+
+  /**
+   * Compiles a String expression into {@link Expression}.
+   *
+   * @param expression String expression.
+   * @return {@link Expression} equivalent of the string.
+   *
+   * @throws SqlCompilationException if String is not a valid expression.
+   */
+  public static Expression compileToExpression(String expression) {
+    SqlParser sqlParser = SqlParser.create(expression, ParserUtils.PARSER_CONFIG);
+    SqlNode sqlNode;
+    try {
+      sqlNode = sqlParser.parseExpression();
+    } catch (SqlParseException e) {
+      throw new SqlCompilationException("Caught exception while parsing expression: " + expression, e);
+    }
+    return toExpression(sqlNode);
+  }
+
+  private static List<Expression> convertDistinctSelectList(SqlNodeList selectList) {
+    List<Expression> selectExpr = new ArrayList<>();
+    selectExpr.add(convertDistinctAndSelectListToFunctionExpression(selectList));
+    return selectExpr;
+  }
+
+  private static List<Expression> convertSelectList(SqlNodeList selectList) {
+    List<Expression> selectExpr = new ArrayList<>();
+
+    final Iterator<SqlNode> iterator = selectList.iterator();
+    while (iterator.hasNext()) {
+      final SqlNode next = iterator.next();
+      selectExpr.add(toExpression(next));
+    }
+
+    return selectExpr;
+  }
+
+  private static List<Expression> convertOrderByList(SqlNodeList orderList) {
+    List<Expression> orderByExpr = new ArrayList<>();
+    final Iterator<SqlNode> iterator = orderList.iterator();
+    while (iterator.hasNext()) {
+      final SqlNode next = iterator.next();
+      orderByExpr.add(convertOrderBy(next));
+    }
+    return orderByExpr;
+  }
+
+  private static Expression convertOrderBy(SqlNode node) {
+    final SqlKind kind = node.getKind();
+    Expression expression;
+    switch (kind) {
+      case DESCENDING:
+        SqlBasicCall basicCall = (SqlBasicCall) node;
+        expression = RequestUtils.getFunctionExpression("DESC");
+        expression.getFunctionCall().addToOperands(toExpression(basicCall.getOperandList().get(0)));
+        break;
+      case IDENTIFIER:
+      default:
+        expression = RequestUtils.getFunctionExpression("ASC");
+        expression.getFunctionCall().addToOperands(toExpression(node));
+        break;
+    }
+    return expression;
+  }
+
+  /**
+   * DISTINCT is implemented as an aggregation function so need to take the select list items
+   * and convert them into a single function expression for handing over to execution engine
+   * either as a PinotQuery or BrokerRequest via conversion
+   * @param selectList select list items
+   * @return DISTINCT function expression
+   */
+  private static Expression convertDistinctAndSelectListToFunctionExpression(SqlNodeList selectList) {
+    String functionName = AggregationFunctionType.DISTINCT.getName();
+    Expression functionExpression = RequestUtils.getFunctionExpression(functionName);
+    for (SqlNode node : selectList) {
+      Expression columnExpression = toExpression(node);
+      if (columnExpression.getType() == ExpressionType.IDENTIFIER && columnExpression.getIdentifier().getName()
+          .equals("*")) {
+        throw new SqlCompilationException(
+            "Syntax error: Pinot currently does not support DISTINCT with *. Please specify each column name after "
+                + "DISTINCT keyword");
+      } else if (columnExpression.getType() == ExpressionType.FUNCTION) {
+        Function functionCall = columnExpression.getFunctionCall();
+        String function = functionCall.getOperator();
+        if (AggregationFunctionType.isAggregationFunction(function)) {
+          throw new SqlCompilationException(
+              "Syntax error: Use of DISTINCT with aggregation functions is not supported");
+        }
+      }
+      functionExpression.getFunctionCall().addToOperands(columnExpression);
+    }
+    return functionExpression;
+  }
+
+  private static Expression toExpression(SqlNode node) {
+    LOGGER.debug("Current processing SqlNode: {}, node.getKind(): {}", node, node.getKind());
+    switch (node.getKind()) {
+      case IDENTIFIER:
+        if (((SqlIdentifier) node).isStar()) {
+          return RequestUtils.getIdentifierExpression("*");
+        }
+        if (((SqlIdentifier) node).isSimple()) {
+          return RequestUtils.getIdentifierExpression(((SqlIdentifier) node).getSimple());
+        }
+        return RequestUtils.getIdentifierExpression(node.toString());
+      case LITERAL:
+        return RequestUtils.getLiteralExpression((SqlLiteral) node);
+      case AS:
+        SqlBasicCall asFuncSqlNode = (SqlBasicCall) node;
+        List<SqlNode> operands = asFuncSqlNode.getOperandList();
+        Expression leftExpr = toExpression(operands.get(0));
+        SqlNode aliasSqlNode = operands.get(1);
+        String aliasName;
+        switch (aliasSqlNode.getKind()) {
+          case IDENTIFIER:
+            aliasName = ((SqlIdentifier) aliasSqlNode).getSimple();
+            break;
+          case LITERAL:
+            aliasName = ((SqlLiteral) aliasSqlNode).toValue();
+            break;
+          default:
+            throw new SqlCompilationException("Unsupported Alias sql node - " + aliasSqlNode);
+        }
+        Expression rightExpr = RequestUtils.getIdentifierExpression(aliasName);
+        // Just return left identifier if both sides are the same identifier.
+        if (leftExpr.isSetIdentifier() && rightExpr.isSetIdentifier()) {
+          if (leftExpr.getIdentifier().getName().equals(rightExpr.getIdentifier().getName())) {
+            return leftExpr;
+          }
+        }
+        final Expression asFuncExpr = RequestUtils.getFunctionExpression(SqlKind.AS.toString());
+        asFuncExpr.getFunctionCall().addToOperands(leftExpr);
+        asFuncExpr.getFunctionCall().addToOperands(rightExpr);
+        return asFuncExpr;
+      case CASE:
+        // CASE WHEN Statement is model as a function with variable length parameters.
+        // Assume N is number of WHEN Statements, total number of parameters is (2 * N + 1).
+        // - N: Convert each WHEN Statement into a function Expression;
+        // - N: Convert each THEN Statement into an Expression;
+        // - 1: Convert ELSE Statement into an Expression.
+        SqlCase caseSqlNode = (SqlCase) node;
+        SqlNodeList whenOperands = caseSqlNode.getWhenOperands();
+        SqlNodeList thenOperands = caseSqlNode.getThenOperands();
+        SqlNode elseOperand = caseSqlNode.getElseOperand();
+        Expression caseFuncExpr = RequestUtils.getFunctionExpression(SqlKind.CASE.name());
+        for (SqlNode whenSqlNode : whenOperands.getList()) {
+          Expression whenExpression = toExpression(whenSqlNode);
+          if (isAggregateExpression(whenExpression)) {
+            throw new SqlCompilationException(
+                "Aggregation functions inside WHEN Clause is not supported - " + whenSqlNode);
+          }
+          caseFuncExpr.getFunctionCall().addToOperands(whenExpression);
+        }
+        for (SqlNode thenSqlNode : thenOperands.getList()) {
+          Expression thenExpression = toExpression(thenSqlNode);
+          if (isAggregateExpression(thenExpression)) {
+            throw new SqlCompilationException(
+                "Aggregation functions inside THEN Clause is not supported - " + thenSqlNode);
+          }
+          caseFuncExpr.getFunctionCall().addToOperands(thenExpression);
+        }
+        Expression elseExpression = toExpression(elseOperand);
+        if (isAggregateExpression(elseExpression)) {
+          throw new SqlCompilationException(
+              "Aggregation functions inside ELSE Clause is not supported - " + elseExpression);
+        }
+        caseFuncExpr.getFunctionCall().addToOperands(elseExpression);
+        return caseFuncExpr;
+      default:
+        if (node instanceof SqlDataTypeSpec) {
+          // This is to handle expression like: CAST(col AS INT)
+          return RequestUtils.getLiteralExpression(((SqlDataTypeSpec) node).getTypeName().getSimple());
+        } else {
+          return compileFunctionExpression((SqlBasicCall) node);
+        }
+    }
+  }
+
+  private static Expression compileFunctionExpression(SqlBasicCall functionNode) {
+    SqlKind functionKind = functionNode.getKind();
+    String functionName;
+    switch (functionKind) {
+      case AND:
+        return compileAndExpression(functionNode);
+      case OR:
+        return compileOrExpression(functionNode);
+      case COUNT:
+        SqlLiteral functionQuantifier = functionNode.getFunctionQuantifier();
+        if (functionQuantifier != null && functionQuantifier.toValue().equalsIgnoreCase("DISTINCT")) {
+          functionName = AggregationFunctionType.DISTINCTCOUNT.name();
+        } else {
+          functionName = AggregationFunctionType.COUNT.name();
+        }
+        break;
+      case OTHER:
+      case OTHER_FUNCTION:
+      case DOT:
+        functionName = functionNode.getOperator().getName().toUpperCase();
+        if (functionName.equals("ITEM") || functionName.equals("DOT")) {
+          // Calcite parses path expression such as "data[0][1].a.b[0]" into a chain of ITEM and/or DOT
+          // functions. Collapse this chain into an identifier.
+          StringBuffer path = new StringBuffer();
+          compilePathExpression(functionName, functionNode, path);
+          return RequestUtils.getIdentifierExpression(path.toString());
+        }
+        break;
+      default:
+        functionName = functionKind.name();
+        break;
+    }
+    // When there is no argument, set an empty list as the operands
+    List<SqlNode> childNodes = functionNode.getOperandList();
+    List<Expression> operands = new ArrayList<>(childNodes.size());
+    for (SqlNode childNode : childNodes) {
+      if (childNode instanceof SqlNodeList) {
+        for (SqlNode node : (SqlNodeList) childNode) {
+          operands.add(toExpression(node));
+        }
+      } else {
+        operands.add(toExpression(childNode));
+      }
+    }
+    validateFunction(functionName, operands);
+    Expression functionExpression = RequestUtils.getFunctionExpression(functionName);
+    functionExpression.getFunctionCall().setOperands(operands);
+    return functionExpression;
+  }
+
+  /**
+   * Convert Calcite operator tree made up of ITEM and DOT functions to an identifier. For example, the operator tree
+   * shown below will be converted to IDENTIFIER "jsoncolumn.data[0][1].a.b[0]".
+   *
+   * ├── ITEM(jsoncolumn.data[0][1].a.b[0])
+   *      ├── LITERAL (0)
+   *      └── DOT (jsoncolumn.daa[0][1].a.b)
+   *            ├── IDENTIFIER (b)
+   *            └── DOT (jsoncolumn.data[0][1].a)
+   *                  ├── IDENTIFIER (a)
+   *                  └── ITEM (jsoncolumn.data[0][1])
+   *                        ├── LITERAL (1)
+   *                        └── ITEM (jsoncolumn.data[0])
+   *                              ├── LITERAL (1)
+   *                              └── IDENTIFIER (jsoncolumn.data)
+   *
+   * @param functionName Name of the function ("DOT" or "ITEM")
+   * @param functionNode Root node of the DOT and/or ITEM operator function chain.
+   * @param path String representation of path represented by DOT and/or ITEM function chain.
+   */
+  private static void compilePathExpression(String functionName, SqlBasicCall functionNode, StringBuffer path) {
+    List<SqlNode> operands = functionNode.getOperandList();
+
+    // Compile first operand of the function (either an identifier or another DOT and/or ITEM function).
+    SqlKind kind0 = operands.get(0).getKind();
+    if (kind0 == SqlKind.IDENTIFIER) {
+      path.append(operands.get(0).toString());
+    } else if (kind0 == SqlKind.DOT || kind0 == SqlKind.OTHER_FUNCTION) {
+      SqlBasicCall function0 = (SqlBasicCall) operands.get(0);
+      String name0 = function0.getOperator().getName();
+      if (name0.equals("ITEM") || name0.equals("DOT")) {
+        compilePathExpression(name0, function0, path);
+      } else {
+        throw new SqlCompilationException("SELECT list item has bad path expression.");
+      }
+    } else {
+      throw new SqlCompilationException("SELECT list item has bad path expression.");
+    }
+
+    // Compile second operand of the function (either an identifier or literal).
+    SqlKind kind1 = operands.get(1).getKind();
+    if (kind1 == SqlKind.IDENTIFIER) {
+      path.append(".").append(((SqlIdentifier) operands.get(1)).getSimple());
+    } else if (kind1 == SqlKind.LITERAL) {
+      path.append("[").append(((SqlLiteral) operands.get(1)).toValue()).append("]");
+    } else {
+      throw new SqlCompilationException("SELECT list item has bad path expression.");
+    }
+  }
+
+  public static String canonicalize(String functionName) {
+    return StringUtils.remove(functionName, '_').toLowerCase();
+  }
+
+  public static boolean isSameFunction(String function1, String function2) {
+    return canonicalize(function1).equals(canonicalize(function2));
+  }
+
+  private static void validateFunction(String functionName, List<Expression> operands) {
+    switch (canonicalize(functionName)) {
+      case "jsonextractscalar":
+        validateJsonExtractScalarFunction(operands);
+        break;
+      case "jsonextractkey":
+        validateJsonExtractKeyFunction(operands);
+        break;
+      default:
+        break;
+    }
+  }
+
+  private static void validateJsonExtractScalarFunction(List<Expression> operands) {
+    int numOperands = operands.size();
+
+    // Check that there are exactly 3 or 4 arguments
+    if (numOperands != 3 && numOperands != 4) {
+      throw new SqlCompilationException(
+          "Expect 3 or 4 arguments for transform function: jsonExtractScalar(jsonFieldName, 'jsonPath', "
+              + "'resultsType', ['defaultValue'])");
+    }
+    if (!operands.get(1).isSetLiteral() || !operands.get(2).isSetLiteral() || (numOperands == 4 && !operands.get(3)
+        .isSetLiteral())) {
+      throw new SqlCompilationException(
+          "Expect the 2nd/3rd/4th argument of transform function: jsonExtractScalar(jsonFieldName, 'jsonPath',"
+              + " 'resultsType', ['defaultValue']) to be a single-quoted literal value.");
+    }
+  }
+
+  private static void validateJsonExtractKeyFunction(List<Expression> operands) {
+    // Check that there are exactly 2 arguments
+    if (operands.size() != 2) {
+      throw new SqlCompilationException(
+          "Expect 2 arguments are required for transform function: jsonExtractKey(jsonFieldName, 'jsonPath')");
+    }
+    if (!operands.get(1).isSetLiteral()) {
+      throw new SqlCompilationException(
+          "Expect the 2nd argument for transform function: jsonExtractKey(jsonFieldName, 'jsonPath') to be a "
+              + "single-quoted literal value.");
+    }
+  }
+
+  /**
+   * Helper method to flatten the operands for the AND expression.
+   */
+  private static Expression compileAndExpression(SqlBasicCall andNode) {
+    List<Expression> operands = new ArrayList<>();
+    for (SqlNode childNode : andNode.getOperandList()) {
+      if (childNode.getKind() == SqlKind.AND) {
+        Expression childAndExpression = compileAndExpression((SqlBasicCall) childNode);
+        operands.addAll(childAndExpression.getFunctionCall().getOperands());
+      } else {
+        operands.add(toExpression(childNode));
+      }
+    }
+    Expression andExpression = RequestUtils.getFunctionExpression(SqlKind.AND.name());
+    andExpression.getFunctionCall().setOperands(operands);
+    return andExpression;
+  }
+
+  /**
+   * Helper method to flatten the operands for the OR expression.
+   */
+  private static Expression compileOrExpression(SqlBasicCall orNode) {
+    List<Expression> operands = new ArrayList<>();
+    for (SqlNode childNode : orNode.getOperandList()) {
+      if (childNode.getKind() == SqlKind.OR) {
+        Expression childAndExpression = compileOrExpression((SqlBasicCall) childNode);
+        operands.addAll(childAndExpression.getFunctionCall().getOperands());
+      } else {
+        operands.add(toExpression(childNode));
+      }
+    }
+    Expression andExpression = RequestUtils.getFunctionExpression(SqlKind.OR.name());
+    andExpression.getFunctionCall().setOperands(operands);
+    return andExpression;
+  }
+
+  public static boolean isLiteralOnlyExpression(Expression e) {
+    if (e.getType() == ExpressionType.LITERAL) {
+      return true;
+    }
+    if (e.getType() == ExpressionType.FUNCTION) {
+      Function functionCall = e.getFunctionCall();
+      if (functionCall.getOperator().equalsIgnoreCase(SqlKind.AS.toString())) {
+        return isLiteralOnlyExpression(functionCall.getOperands().get(0));
+      }
+      return false;
+    }
+    return false;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteSqlParser.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteSqlParser.java
new file mode 100644
index 0000000000..d67896f9b9
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteSqlParser.java
@@ -0,0 +1,148 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.parser;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOrderBy;
+import org.apache.calcite.sql.SqlSelect;
+import org.apache.calcite.sql.parser.SqlParseException;
+import org.apache.calcite.sql.parser.SqlParser;
+import org.apache.pinot.query.context.PlannerContext;
+import org.apache.pinot.sql.parsers.SqlCompilationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class provide API to parse a SQL string into Pinot query {@link SqlNode}.
+ *
+ * <p>This class is extracted from {@link org.apache.pinot.sql.parsers.CalciteSqlParser}. It contains the logic
+ * to parsed SQL into {@link SqlNode} and use {@link QueryRewriter} to rewrite the query with Pinot specific
+ * contextual info.
+ */
+public class CalciteSqlParser {
+  private static final Logger LOGGER = LoggerFactory.getLogger(CalciteSqlParser.class);
+
+  private CalciteSqlParser() {
+    // do not instantiate.
+  }
+
+  /**
+   * entrypoint for Sql Parser.
+   */
+  public static SqlNode compile(String sql, PlannerContext plannerContext)
+      throws SqlCompilationException {
+    // Extract OPTION statements from sql as Calcite Parser doesn't parse it.
+    // TODO: use parser syntax extension instead.
+    Map<String, String> options = parseOptions(extractOptionsFromSql(sql));
+    plannerContext.setOptions(options);
+    if (!options.isEmpty()) {
+      sql = removeOptionsFromSql(sql);
+    }
+    // Compile Sql without OPTION statements.
+    SqlNode parsed = parse(sql);
+
+    // query rewrite.
+    return QueryRewriter.rewrite(parsed, plannerContext);
+  }
+
+  // ==========================================================================
+  // Static utils to parse the SQL.
+  // ==========================================================================
+
+  private static Map<String, String> parseOptions(List<String> optionsStatements) {
+    if (optionsStatements.isEmpty()) {
+      return Collections.emptyMap();
+    }
+    Map<String, String> options = new HashMap<>();
+    for (String optionsStatement : optionsStatements) {
+      for (String option : optionsStatement.split(",")) {
+        final String[] splits = option.split("=");
+        if (splits.length != 2) {
+          throw new SqlCompilationException("OPTION statement requires two parts separated by '='");
+        }
+        options.put(splits[0].trim(), splits[1].trim());
+      }
+    }
+    return options;
+  }
+
+  private static SqlNode parse(String sql) {
+    SqlParser sqlParser = SqlParser.create(sql, ParserUtils.PARSER_CONFIG);
+    SqlNode sqlNode;
+    try {
+      sqlNode = sqlParser.parseQuery();
+    } catch (SqlParseException e) {
+      throw new SqlCompilationException("Caught exception while parsing query: " + sql, e);
+    }
+
+    // This is a special rewrite,
+    // TODO: move it to planner later.
+    SqlSelect selectNode;
+    if (sqlNode instanceof SqlOrderBy) {
+      // Store order-by info into the select sql node
+      SqlOrderBy orderByNode = (SqlOrderBy) sqlNode;
+      selectNode = (SqlSelect) orderByNode.query;
+      selectNode.setOrderBy(orderByNode.orderList);
+      selectNode.setFetch(orderByNode.fetch);
+      selectNode.setOffset(orderByNode.offset);
+    } else {
+      selectNode = (SqlSelect) sqlNode;
+    }
+    return selectNode;
+  }
+
+  private static List<String> extractOptionsFromSql(String sql) {
+    List<String> results = new ArrayList<>();
+    Matcher matcher = ParserUtils.OPTIONS_REGEX_PATTEN.matcher(sql);
+    while (matcher.find()) {
+      results.add(matcher.group(1));
+    }
+    return results;
+  }
+
+  private static String removeOptionsFromSql(String sql) {
+    Matcher matcher = ParserUtils.OPTIONS_REGEX_PATTEN.matcher(sql);
+    return matcher.replaceAll("");
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/ParserUtils.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/ParserUtils.java
new file mode 100644
index 0000000000..5422382509
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/ParserUtils.java
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.parser;
+
+import java.util.regex.Pattern;
+import org.apache.calcite.config.Lex;
+import org.apache.calcite.sql.parser.SqlParser;
+import org.apache.calcite.sql.parser.babel.SqlBabelParserImpl;
+import org.apache.calcite.sql.validate.SqlConformanceEnum;
+
+
+/**
+ * Utility provided to Calcite parser.
+ *
+ * <p>This class is extracted from {@link org.apache.pinot.sql.parsers.CalciteSqlParser} for its static constructs.
+ */
+final class ParserUtils {
+  /** Lexical policy similar to MySQL with ANSI_QUOTES option enabled. (To be
+   * precise: MySQL on Windows; MySQL on Linux uses case-sensitive matching,
+   * like the Linux file system.) The case of identifiers is preserved whether
+   * or not they quoted; after which, identifiers are matched
+   * case-insensitively. Double quotes allow identifiers to contain
+   * non-alphanumeric characters. */
+  static final Lex PINOT_LEX = Lex.MYSQL_ANSI;
+
+  // BABEL is a very liberal conformance value that allows anything supported by any dialect
+  static final SqlParser.Config PARSER_CONFIG =
+      SqlParser.configBuilder().setLex(PINOT_LEX).setConformance(SqlConformanceEnum.BABEL)
+          .setParserFactory(SqlBabelParserImpl.FACTORY).build();
+
+  // TODO: move this to use parser syntax extension.
+  // To Keep the backward compatibility with 'OPTION' Functionality in PQL, which is used to
+  // provide more hints for query processing.
+  //
+  // PQL syntax is: `OPTION (<key> = <value>)`
+  //
+  // Multiple OPTIONs is also supported by:
+  // either
+  //   `OPTION (<k1> = <v1>, <k2> = <v2>, <k3> = <v3>)`
+  // or
+  //   `OPTION (<k1> = <v1>) OPTION (<k2> = <v2>) OPTION (<k3> = <v3>)`
+  static final Pattern OPTIONS_REGEX_PATTEN = Pattern.compile("option\\s*\\(([^\\)]+)\\)", Pattern.CASE_INSENSITIVE);
+
+  private ParserUtils() {
+    // do not instantiate.
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/QueryRewriter.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/QueryRewriter.java
new file mode 100644
index 0000000000..8cb0060416
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/QueryRewriter.java
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.parser;
+
+import org.apache.calcite.sql.SqlNode;
+import org.apache.pinot.query.context.PlannerContext;
+
+/**
+ * Rewrites query based on user option as well as built-in rules.
+ */
+public class QueryRewriter {
+
+  private QueryRewriter() {
+    // do not instantiate.
+  }
+
+  /**
+   * Entrypoint to execute the query rewrite.
+   *
+   * It should be functionally identical to running {@link org.apache.pinot.sql.parsers.rewriter.QueryRewriter}.
+   * But it operates on a {@link SqlNode} tree instead of a flat pinot query object.
+   *
+   * @param sqlNodeRoot root of the sqlNode tree
+   * @param plannerContext planner context
+   * @return rewritten sqlNode.
+   */
+  public static SqlNode rewrite(SqlNode sqlNodeRoot, PlannerContext plannerContext) {
+    return sqlNodeRoot;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/LogicalPlanner.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/LogicalPlanner.java
new file mode 100644
index 0000000000..9844916490
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/LogicalPlanner.java
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.calcite.plan.Context;
+import org.apache.calcite.plan.RelTraitDef;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.plan.hep.HepPlanner;
+import org.apache.calcite.plan.hep.HepProgram;
+
+
+/**
+ * The {@code LogicalPlanner} is an extended implementation of the Calcite's {@link HepPlanner}.
+ */
+public class LogicalPlanner extends HepPlanner {
+
+  private List<RelTraitDef> _traitDefs;
+
+  public LogicalPlanner(HepProgram program, Context context) {
+    super(program, context);
+    _traitDefs = new ArrayList();
+  }
+
+  @Override
+  public boolean addRelTraitDef(RelTraitDef relTraitDef) {
+    return !_traitDefs.contains(relTraitDef) && _traitDefs.add(relTraitDef);
+  }
+
+  @Override
+  public List<RelTraitDef> getRelTraitDefs() {
+    return _traitDefs;
+  }
+
+  @Override
+  public RelTraitSet emptyTraitSet() {
+    RelTraitSet traitSet = super.emptyTraitSet();
+    for (RelTraitDef traitDef : _traitDefs) {
+      if (traitDef.multiple()) {
+        // not supported
+      }
+      traitSet = traitSet.plus(traitDef.getDefault());
+    }
+    return traitSet;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java
new file mode 100644
index 0000000000..bdaba4ac70
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/QueryPlan.java
@@ -0,0 +1,60 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner;
+
+import java.util.Map;
+import org.apache.pinot.query.planner.nodes.StageNode;
+
+
+/**
+ * The {@code QueryPlan} is the dispatchable query execution plan from the result of {@link LogicalPlanner}.
+ *
+ * <p>QueryPlan should contain the necessary stage boundary information and the cross exchange information
+ * for:
+ * <ul>
+ *   <li>dispatch individual stages to executor.</li>
+ *   <li>instruct stage executor to establish connection channels to other stages.</li>
+ *   <li>encode data blocks for transfer between stages based on partitioning scheme.</li>
+ * </ul>
+ */
+public class QueryPlan {
+  private Map<String, StageNode> _queryStageMap;
+  private Map<String, StageMetadata> _stageMetadataMap;
+
+  public QueryPlan(Map<String, StageNode> queryStageMap, Map<String, StageMetadata> stageMetadataMap) {
+    _queryStageMap = queryStageMap;
+    _stageMetadataMap = stageMetadataMap;
+  }
+
+  /**
+   * Get the map between stageID and the stage plan root node.
+   * @return stage plan map.
+   */
+  public Map<String, StageNode> getQueryStageMap() {
+    return _queryStageMap;
+  }
+
+  /**
+   * Get the stage metadata information.
+   * @return stage metadata info.
+   */
+  public Map<String, StageMetadata> getStageMetadataMap() {
+    return _stageMetadataMap;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java
new file mode 100644
index 0000000000..d167521f20
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java
@@ -0,0 +1,71 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner;
+
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.logical.LogicalCalc;
+import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rel.logical.LogicalTableScan;
+import org.apache.pinot.query.planner.nodes.CalcNode;
+import org.apache.pinot.query.planner.nodes.JoinNode;
+import org.apache.pinot.query.planner.nodes.StageNode;
+import org.apache.pinot.query.planner.nodes.TableScanNode;
+
+
+/**
+ * The {@code StageNodeConverter} converts a logical {@link RelNode} to a {@link StageNode}.
+ */
+public final class RelToStageConverter {
+
+  private RelToStageConverter() {
+    // do not instantiate.
+  }
+
+  /**
+   * convert a normal relation node into stage node with just the expression piece.
+   *
+   * TODO: we should convert this to a more structured pattern once we determine the serialization format used.
+   *
+   * @param node relational node
+   * @return stage node.
+   */
+  public static StageNode toStageNode(RelNode node, String currentStageId) {
+    if (node instanceof LogicalCalc) {
+      return convertLogicalCal((LogicalCalc) node, currentStageId);
+    } else if (node instanceof LogicalTableScan) {
+      return convertLogicalTableScan((LogicalTableScan) node, currentStageId);
+    } else if (node instanceof LogicalJoin) {
+      return convertLogicalJoin((LogicalJoin) node, currentStageId);
+    } else {
+      throw new UnsupportedOperationException("Unsupported logical plan node: " + node);
+    }
+  }
+
+  private static StageNode convertLogicalTableScan(LogicalTableScan node, String currentStageId) {
+    return new TableScanNode(node, currentStageId);
+  }
+
+  private static StageNode convertLogicalCal(LogicalCalc node, String currentStageId) {
+    return new CalcNode(node, currentStageId);
+  }
+
+  private static StageNode convertLogicalJoin(LogicalJoin node, String currentStageId) {
+    return new JoinNode(node, currentStageId);
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java
new file mode 100644
index 0000000000..91eb5b77ca
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StageMetadata.java
@@ -0,0 +1,85 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.planner.nodes.StageNode;
+import org.apache.pinot.query.planner.nodes.TableScanNode;
+
+
+/**
+ * The {@code StageMetadata} info contains the information for dispatching a particular stage.
+ *
+ * <p>It contains information aboute:
+ * <ul>
+ *   <li>the tables it is suppose to scan for</li>
+ *   <li>the underlying segments a stage requires to execute upon.</li>
+ *   <li>the server instances to which this stage should be execute on</li>
+ * </ul>
+ */
+public class StageMetadata implements Serializable {
+  private List<String> _scannedTables;
+
+  // used for assigning server/worker nodes.
+  private List<ServerInstance> _serverInstances;
+
+  // used for table scan stage.
+  private Map<ServerInstance, List<String>> _serverInstanceToSegmentsMap;
+
+  public StageMetadata() {
+    _scannedTables = new ArrayList<>();
+    _serverInstances = new ArrayList<>();
+    _serverInstanceToSegmentsMap = new HashMap<>();
+  }
+
+  public void attach(StageNode stageNode) {
+    if (stageNode instanceof TableScanNode) {
+      _scannedTables.add(((TableScanNode) stageNode).getTableName().get(0));
+    }
+  }
+
+  public List<String> getScannedTables() {
+    return _scannedTables;
+  }
+
+  // -----------------------------------------------
+  // attached physical plan context.
+  // -----------------------------------------------
+
+  public Map<ServerInstance, List<String>> getServerInstanceToSegmentsMap() {
+    return _serverInstanceToSegmentsMap;
+  }
+
+  public void setServerInstanceToSegmentsMap(Map<ServerInstance, List<String>> serverInstanceToSegmentsMap) {
+    _serverInstanceToSegmentsMap = serverInstanceToSegmentsMap;
+  }
+
+  public List<ServerInstance> getServerInstances() {
+    return _serverInstances;
+  }
+
+  public void setServerInstances(List<ServerInstance> serverInstances) {
+    _serverInstances = serverInstances;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StagePlanner.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StagePlanner.java
new file mode 100644
index 0000000000..175e77f6ef
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/StagePlanner.java
@@ -0,0 +1,126 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.calcite.rel.RelDistribution;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelRoot;
+import org.apache.calcite.rel.logical.LogicalExchange;
+import org.apache.pinot.query.context.PlannerContext;
+import org.apache.pinot.query.planner.nodes.MailboxReceiveNode;
+import org.apache.pinot.query.planner.nodes.MailboxSendNode;
+import org.apache.pinot.query.planner.nodes.StageNode;
+import org.apache.pinot.query.routing.WorkerManager;
+
+
+/**
+ * QueryPlanMaker walks top-down from {@link RelRoot} and construct a forest of trees with {@link StageNode}.
+ *
+ * This class is non-threadsafe. Do not reuse the stage planner for multiple query plans.
+ */
+public class StagePlanner {
+  private final PlannerContext _plannerContext;
+  private final WorkerManager _workerManager;
+
+  private Map<String, StageNode> _queryStageMap;
+  private Map<String, StageMetadata> _stageMetadataMap;
+  private int _stageIdCounter;
+
+  public StagePlanner(PlannerContext plannerContext, WorkerManager workerManager) {
+    _plannerContext = plannerContext;
+    _workerManager = workerManager;
+  }
+
+  /**
+   * Construct the dispatchable plan from relational logical plan.
+   *
+   * @param relRoot relational plan root.
+   * @return dispatchable plan.
+   */
+  public QueryPlan makePlan(RelNode relRoot) {
+    // clear the state
+    _queryStageMap = new HashMap<>();
+    _stageMetadataMap = new HashMap<>();
+    _stageIdCounter = 0;
+
+    // walk the plan and create stages.
+    StageNode globalStageRoot = walkRelPlan(relRoot, getNewStageId());
+
+    // global root needs to send results back to the ROOT, a.k.a. the client response node.
+    // the last stage is always a broadcast-gather.
+    StageNode globalReceiverNode =
+        new MailboxReceiveNode("ROOT", globalStageRoot.getStageId(), RelDistribution.Type.BROADCAST_DISTRIBUTED);
+    StageNode globalSenderNode = new MailboxSendNode(globalStageRoot, globalReceiverNode.getStageId(),
+        RelDistribution.Type.BROADCAST_DISTRIBUTED);
+    _queryStageMap.put(globalSenderNode.getStageId(), globalSenderNode);
+    StageMetadata stageMetadata = _stageMetadataMap.get(globalSenderNode.getStageId());
+    stageMetadata.attach(globalSenderNode);
+
+    _queryStageMap.put(globalReceiverNode.getStageId(), globalReceiverNode);
+    StageMetadata globalReceivingStageMetadata = new StageMetadata();
+    globalReceivingStageMetadata.attach(globalReceiverNode);
+    _stageMetadataMap.put(globalReceiverNode.getStageId(), globalReceivingStageMetadata);
+
+    // assign workers to each stage.
+    for (Map.Entry<String, StageMetadata> e : _stageMetadataMap.entrySet()) {
+      _workerManager.assignWorkerToStage(e.getKey(), e.getValue());
+    }
+
+    return new QueryPlan(_queryStageMap, _stageMetadataMap);
+  }
+
+  // non-threadsafe
+  private StageNode walkRelPlan(RelNode node, String currentStageId) {
+    if (isExchangeNode(node)) {
+      // 1. exchangeNode always have only one input, get its input converted as a new stage root.
+      StageNode nextStageRoot = walkRelPlan(node.getInput(0), getNewStageId());
+      RelDistribution.Type exchangeType = ((LogicalExchange) node).distribution.getType();
+
+      // 2. make an exchange sender and receiver node pair
+      StageNode mailboxReceiver = new MailboxReceiveNode(currentStageId, nextStageRoot.getStageId(), exchangeType);
+      StageNode mailboxSender = new MailboxSendNode(nextStageRoot, mailboxReceiver.getStageId(), exchangeType);
+
+      // 3. put the sender side as a completed stage.
+      _queryStageMap.put(mailboxSender.getStageId(), mailboxSender);
+
+      // 4. return the receiver (this is considered as a "virtual table scan" node for its parent.
+      return mailboxReceiver;
+    } else {
+      StageNode stageNode = RelToStageConverter.toStageNode(node, currentStageId);
+      List<RelNode> inputs = node.getInputs();
+      for (RelNode input : inputs) {
+        stageNode.addInput(walkRelPlan(input, currentStageId));
+      }
+      StageMetadata stageMetadata = _stageMetadataMap.computeIfAbsent(currentStageId, (id) -> new StageMetadata());
+      stageMetadata.attach(stageNode);
+      return stageNode;
+    }
+  }
+
+  private boolean isExchangeNode(RelNode node) {
+    return (node instanceof LogicalExchange);
+  }
+
+  private String getNewStageId() {
+    return String.valueOf(_stageIdCounter++);
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
new file mode 100644
index 0000000000..71701df762
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.nodes;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+public abstract class AbstractStageNode implements StageNode {
+
+  protected final String _stageId;
+  protected final List<StageNode> _inputs;
+
+  public AbstractStageNode(String stageId) {
+    _stageId = stageId;
+    _inputs = new ArrayList<>();
+  }
+
+  @Override
+  public List<StageNode> getInputs() {
+    return _inputs;
+  }
+
+  @Override
+  public void addInput(StageNode stageNode) {
+    _inputs.add(stageNode);
+  }
+
+  @Override
+  public String getStageId() {
+    return _stageId;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
new file mode 100644
index 0000000000..4b4ca9165e
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
@@ -0,0 +1,40 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.nodes;
+
+import org.apache.calcite.rel.logical.LogicalCalc;
+
+
+public class CalcNode extends AbstractStageNode {
+  private final String _expression;
+
+  public CalcNode(LogicalCalc node, String currentStageId) {
+    super(currentStageId);
+    _expression = toExpression(node);
+  }
+
+  public String getExpression() {
+    return _expression;
+  }
+
+  private String toExpression(LogicalCalc node) {
+    // TODO: make it real.
+    return node.getDigest();
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java
new file mode 100644
index 0000000000..520af693d6
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.nodes;
+
+import com.google.common.base.Preconditions;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.pinot.query.planner.partitioning.FieldSelectionKeySelector;
+
+
+public class JoinNode extends AbstractStageNode {
+  private final JoinRelType _joinType;
+  private final int _leftOperandIndex;
+  private final int _rightOperandIndex;
+  private final FieldSelectionKeySelector _leftFieldSelectionKeySelector;
+  private final FieldSelectionKeySelector _rightFieldSelectionKeySelector;
+
+  private transient final RelDataType _leftRowType;
+  private transient final RelDataType _rightRowType;
+
+  public JoinNode(LogicalJoin node, String currentStageId) {
+    super(currentStageId);
+    _joinType = node.getJoinType();
+    RexCall joinCondition = (RexCall) node.getCondition();
+    Preconditions.checkState(
+        joinCondition.getOperator().getKind().equals(SqlKind.EQUALS) && joinCondition.getOperands().size() == 2,
+        "only equality JOIN is supported");
+    Preconditions.checkState(joinCondition.getOperands().get(0) instanceof RexInputRef, "only reference supported");
+    Preconditions.checkState(joinCondition.getOperands().get(1) instanceof RexInputRef, "only reference supported");
+    _leftRowType = node.getLeft().getRowType();
+    _rightRowType = node.getRight().getRowType();
+    _leftOperandIndex = ((RexInputRef) joinCondition.getOperands().get(0)).getIndex();
+    _rightOperandIndex = ((RexInputRef) joinCondition.getOperands().get(1)).getIndex();
+    _leftFieldSelectionKeySelector = new FieldSelectionKeySelector(_leftOperandIndex);
+    _rightFieldSelectionKeySelector =
+        new FieldSelectionKeySelector(_rightOperandIndex - _leftRowType.getFieldNames().size());
+  }
+
+  public JoinRelType getJoinType() {
+    return _joinType;
+  }
+
+  public RelDataType getLeftRowType() {
+    return _leftRowType;
+  }
+
+  public RelDataType getRightRowType() {
+    return _rightRowType;
+  }
+
+  public int getLeftOperandIndex() {
+    return _leftOperandIndex;
+  }
+
+  public int getRightOperandIndex() {
+    return _rightOperandIndex;
+  }
+
+  public FieldSelectionKeySelector getLeftJoinKeySelector() {
+    return _leftFieldSelectionKeySelector;
+  }
+
+  public FieldSelectionKeySelector getRightJoinKeySelector() {
+    return _rightFieldSelectionKeySelector;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java
new file mode 100644
index 0000000000..947d449383
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java
@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.nodes;
+
+import java.util.Collections;
+import java.util.List;
+import org.apache.calcite.rel.RelDistribution;
+
+
+public class MailboxReceiveNode extends AbstractStageNode {
+
+  private final String _senderStageId;
+  private final RelDistribution.Type _exchangeType;
+
+  public MailboxReceiveNode(String stageId, String senderStageId, RelDistribution.Type exchangeType) {
+    super(stageId);
+    _senderStageId = senderStageId;
+    _exchangeType = exchangeType;
+  }
+
+  @Override
+  public List<StageNode> getInputs() {
+    return Collections.emptyList();
+  }
+
+  @Override
+  public void addInput(StageNode stageNode) {
+    throw new UnsupportedOperationException("no input should be added to mailbox receive.");
+  }
+
+  public String getSenderStageId() {
+    return _senderStageId;
+  }
+
+  public RelDistribution.Type getExchangeType() {
+    return _exchangeType;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java
new file mode 100644
index 0000000000..6db9578f35
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.nodes;
+
+import java.util.Collections;
+import java.util.List;
+import org.apache.calcite.rel.RelDistribution;
+
+
+public class MailboxSendNode extends AbstractStageNode {
+  private final StageNode _stageRoot;
+  private final String _receiverStageId;
+  private final RelDistribution.Type _exchangeType;
+
+  public MailboxSendNode(StageNode stageRoot, String receiverStageId, RelDistribution.Type exchangeType) {
+    super(stageRoot.getStageId());
+    _stageRoot = stageRoot;
+    _receiverStageId = receiverStageId;
+    _exchangeType = exchangeType;
+  }
+
+  @Override
+  public List<StageNode> getInputs() {
+    return Collections.singletonList(_stageRoot);
+  }
+
+  @Override
+  public void addInput(StageNode queryStageRoot) {
+    throw new UnsupportedOperationException("mailbox cannot be changed!");
+  }
+
+  public String getReceiverStageId() {
+    return _receiverStageId;
+  }
+
+  public RelDistribution.Type getExchangeType() {
+    return _exchangeType;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/StageNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/StageNode.java
new file mode 100644
index 0000000000..8fcbb5e01a
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/StageNode.java
@@ -0,0 +1,40 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.nodes;
+
+import java.io.Serializable;
+import java.util.List;
+
+
+/**
+ * Stage Node is a serializable version of the {@link org.apache.calcite.rel.RelNode}.
+ *
+ * TODO: stage node currently uses java.io.Serializable as its serialization format.
+ * We should experiment with other type of serialization format for better performance.
+ * Essentially what we need is a way to exclude the planner context from the RelNode but only keeps the
+ * constructed relational content because we will no longer revisit the planner after stage is created.
+ */
+public interface StageNode extends Serializable {
+
+  List<StageNode> getInputs();
+
+  void addInput(StageNode stageNode);
+
+  String getStageId();
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
new file mode 100644
index 0000000000..0bb6e0f704
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.nodes;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.calcite.rel.logical.LogicalTableScan;
+import org.apache.calcite.rel.type.RelDataTypeField;
+
+
+public class TableScanNode extends AbstractStageNode {
+  private final List<String> _tableName;
+  private final List<String> _tableScanColumns;
+
+  public TableScanNode(LogicalTableScan tableScan, String stageId) {
+    super(stageId);
+    _tableName = tableScan.getTable().getQualifiedName();
+    // TODO: optimize this, table field is not directly usable as name.
+    _tableScanColumns =
+        tableScan.getRowType().getFieldList().stream().map(RelDataTypeField::getName).collect(Collectors.toList());
+  }
+
+  @Override
+  public List<StageNode> getInputs() {
+    return Collections.emptyList();
+  }
+
+  @Override
+  public void addInput(StageNode queryStageRoot) {
+    throw new UnsupportedOperationException("TableScanNode cannot add input as it is a leaf node");
+  }
+
+  public List<String> getTableName() {
+    return _tableName;
+  }
+
+  public List<String> getTableScanColumns() {
+    return _tableScanColumns;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
new file mode 100644
index 0000000000..9e0c776fd5
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.partitioning;
+
+import java.io.Serializable;
+
+
+/**
+ * The {@code FieldSelectionKeySelector} simply extract a column value out from a row array {@link Object[]}.
+ */
+public class FieldSelectionKeySelector implements KeySelector<Object[], Object>, Serializable {
+
+  private int _columnIndex;
+
+  public FieldSelectionKeySelector(int columnIndex) {
+    _columnIndex = columnIndex;
+  }
+
+  @Override
+  public Object getKey(Object[] input) {
+    return input[_columnIndex];
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
new file mode 100644
index 0000000000..79dc987d9d
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/KeySelector.java
@@ -0,0 +1,37 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.partitioning;
+
+/**
+ * The {@code KeySelector} provides a partitioning function to encode a specific input data type into a key.
+ *
+ * <p>This key selector is used for computation such as GROUP BY or equality JOINs.
+ *
+ * <p>Key selector should always produce the same selection hash key when the same input is provided.
+ */
+public interface KeySelector<IN, OUT> {
+
+  /**
+   * Extract the key out of an input data construct.
+   *
+   * @param input input data.
+   * @return the key of the input data.
+   */
+  OUT getKey(IN input);
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerInstance.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerInstance.java
new file mode 100644
index 0000000000..7ad6ee0b9c
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerInstance.java
@@ -0,0 +1,51 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.routing;
+
+import java.util.Map;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.model.InstanceConfig;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.spi.utils.CommonConstants;
+
+
+/**
+ * WorkerInstance is a wrapper around {@link ServerInstance}.
+ *
+ * <p>This can be considered as a simplified version which directly enable host-port initialization.
+ */
+public class WorkerInstance extends ServerInstance {
+
+  public WorkerInstance(InstanceConfig instanceConfig) {
+    super(instanceConfig);
+  }
+
+  public WorkerInstance(String hostname, int serverPort, int mailboxPort) {
+    super(toInstanceConfig(hostname, serverPort, mailboxPort));
+  }
+
+  private static InstanceConfig toInstanceConfig(String hostname, int serverPort, int mailboxPort) {
+    String server = String.format("%s_%d", hostname, serverPort);
+    InstanceConfig instanceConfig = InstanceConfig.toInstanceConfig(server);
+    ZNRecord znRecord = instanceConfig.getRecord();
+    Map<String, String> simpleFields = znRecord.getSimpleFields();
+    simpleFields.put(CommonConstants.Helix.Instance.GRPC_PORT_KEY, String.valueOf(mailboxPort));
+    return instanceConfig;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java
new file mode 100644
index 0000000000..a3102580b6
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java
@@ -0,0 +1,95 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.routing;
+
+import com.google.common.collect.Lists;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.routing.RoutingManager;
+import org.apache.pinot.core.routing.RoutingTable;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.planner.StageMetadata;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.apache.pinot.spi.utils.builder.TableNameBuilder;
+import org.apache.pinot.sql.parsers.CalciteSqlCompiler;
+
+
+/**
+ * The {@code WorkerManager} manages stage to worker assignment.
+ *
+ * <p>It contains the logic to assign worker to a particular stages. If it is a leaf stage the logic fallback to
+ * how Pinot server assigned server and server-segment mapping.
+ *
+ * TODO: Currently it is implemented by wrapping routing manager from Pinot Broker. however we can abstract out
+ * the worker manager later when we split out the query-spi layer.
+ */
+public class WorkerManager {
+  private static final CalciteSqlCompiler CALCITE_SQL_COMPILER = new CalciteSqlCompiler();
+
+  private final String _hostName;
+  private final int _port;
+  private final RoutingManager _routingManager;
+
+  public WorkerManager(String hostName, int port, RoutingManager routingManager) {
+    _hostName = hostName;
+    _port = port;
+    _routingManager = routingManager;
+  }
+
+  public void assignWorkerToStage(String stageId, StageMetadata stageMetadata) {
+    List<String> scannedTables = stageMetadata.getScannedTables();
+    if (scannedTables.size() == 1) { // table scan stage, need to attach server as well as segment info.
+      RoutingTable routingTable = getRoutingTable(scannedTables.get(0));
+      Map<ServerInstance, List<String>> serverInstanceToSegmentsMap = routingTable.getServerInstanceToSegmentsMap();
+      stageMetadata.setServerInstances(new ArrayList<>(serverInstanceToSegmentsMap.keySet()));
+      stageMetadata.setServerInstanceToSegmentsMap(new HashMap<>(serverInstanceToSegmentsMap));
+    } else if (stageId.equalsIgnoreCase("ROOT")) {
+      // ROOT stage doesn't have a QueryServer as it is strictly only reducing results.
+      // here we simply assign the worker instance with identical server/mailbox port number.
+      stageMetadata.setServerInstances(Lists.newArrayList(new WorkerInstance(_hostName, _port, _port)));
+    } else {
+      stageMetadata.setServerInstances(filterServers(_routingManager.getEnabledServerInstanceMap().values()));
+    }
+  }
+
+  private static List<ServerInstance> filterServers(Collection<ServerInstance> servers) {
+    List<ServerInstance> serverInstances = new ArrayList<>();
+    for (ServerInstance server : servers) {
+      String hostname = server.getHostname();
+      if (!hostname.startsWith(CommonConstants.Helix.PREFIX_OF_BROKER_INSTANCE) && !hostname.startsWith(
+          CommonConstants.Helix.PREFIX_OF_CONTROLLER_INSTANCE) && !hostname.startsWith(
+          CommonConstants.Helix.PREFIX_OF_MINION_INSTANCE) && server.getGrpcPort() > 0) {
+        serverInstances.add(server);
+      }
+    }
+    return serverInstances;
+  }
+
+  private RoutingTable getRoutingTable(String tableName) {
+    String rawTableName = TableNameBuilder.extractRawTableName(tableName);
+    // TODO: support both offline and realtime, now we hard code offline table.
+    String tableNameWithType = TableNameBuilder.forType(TableType.OFFLINE).tableNameWithType(rawTableName);
+    return _routingManager.getRoutingTable(CALCITE_SQL_COMPILER.compileToBrokerRequest(
+        "SELECT * FROM " + tableNameWithType));
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotExchangeNodeInsertRule.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotExchangeNodeInsertRule.java
new file mode 100644
index 0000000000..2b35613f7a
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotExchangeNodeInsertRule.java
@@ -0,0 +1,82 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.rules;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.hep.HepRelVertex;
+import org.apache.calcite.rel.RelDistributions;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Exchange;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rel.logical.LogicalExchange;
+import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.tools.RelBuilderFactory;
+
+
+/**
+ * Special rule for Pinot, always insert exchange after JOIN
+ */
+public class PinotExchangeNodeInsertRule extends RelOptRule {
+  public static final PinotExchangeNodeInsertRule INSTANCE =
+      new PinotExchangeNodeInsertRule(RelFactories.LOGICAL_BUILDER);
+
+  public PinotExchangeNodeInsertRule(RelBuilderFactory factory) {
+    super(operand(LogicalJoin.class, any()), factory, null);
+  }
+
+  @Override
+  public boolean matches(RelOptRuleCall call) {
+    if (call.rels.length < 1) {
+      return false;
+    }
+    if (call.rel(0) instanceof Join) {
+      Join join = call.rel(0);
+      return !isExchange(join.getLeft()) && !isExchange(join.getRight());
+    }
+    return false;
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    Join join = call.rel(0);
+    RelNode leftInput = join.getInput(0);
+    RelNode rightInput = join.getInput(1);
+
+    RelNode leftExchange = LogicalExchange.create(leftInput, RelDistributions.SINGLETON);
+    RelNode rightExchange = LogicalExchange.create(rightInput, RelDistributions.BROADCAST_DISTRIBUTED);
+
+    RelNode newJoinNode =
+        new LogicalJoin(join.getCluster(), join.getTraitSet(), leftExchange, rightExchange, join.getCondition(),
+            join.getVariablesSet(), join.getJoinType(), join.isSemiJoinDone(),
+            ImmutableList.copyOf(join.getSystemFieldList()));
+
+    call.transformTo(newJoinNode);
+  }
+
+  private static boolean isExchange(RelNode rel) {
+    RelNode reference = rel;
+    if (reference instanceof HepRelVertex) {
+      reference = ((HepRelVertex) reference).getCurrentRel();
+    }
+    return reference instanceof Exchange;
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotQueryRuleSets.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotQueryRuleSets.java
new file mode 100644
index 0000000000..1b4e0850ac
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/rules/PinotQueryRuleSets.java
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.rules;
+
+import java.util.Arrays;
+import java.util.Collection;
+import org.apache.calcite.adapter.enumerable.EnumerableRules;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.rel.rules.CoreRules;
+import org.apache.calcite.rel.rules.PruneEmptyRules;
+
+
+/**
+ * Default rule sets for Pinot query
+ */
+public class PinotQueryRuleSets {
+  private PinotQueryRuleSets() {
+    // do not instantiate.
+  }
+
+  public static final Collection<RelOptRule> LOGICAL_OPT_RULES =
+      Arrays.asList(EnumerableRules.ENUMERABLE_FILTER_RULE, EnumerableRules.ENUMERABLE_JOIN_RULE,
+          EnumerableRules.ENUMERABLE_PROJECT_RULE, EnumerableRules.ENUMERABLE_SORT_RULE,
+          EnumerableRules.ENUMERABLE_TABLE_SCAN_RULE,
+
+          // push a filter into a join
+          CoreRules.FILTER_INTO_JOIN,
+          // push filter through an aggregation
+          CoreRules.FILTER_AGGREGATE_TRANSPOSE,
+          // push filter through set operation
+          CoreRules.FILTER_SET_OP_TRANSPOSE,
+          // push project through set operation
+          CoreRules.PROJECT_SET_OP_TRANSPOSE,
+
+          // aggregation and projection rules
+          CoreRules.AGGREGATE_PROJECT_MERGE, CoreRules.AGGREGATE_PROJECT_PULL_UP_CONSTANTS,
+          // push a projection past a filter or vice versa
+          CoreRules.PROJECT_FILTER_TRANSPOSE, CoreRules.FILTER_PROJECT_TRANSPOSE,
+          // push a projection to the children of a join
+          // push all expressions to handle the time indicator correctly
+          CoreRules.JOIN_CONDITION_PUSH,
+          // merge projections
+          CoreRules.PROJECT_MERGE,
+          // remove identity project
+          CoreRules.PROJECT_REMOVE,
+          // reorder sort and projection
+          CoreRules.SORT_PROJECT_TRANSPOSE,
+
+          // join rules
+          CoreRules.JOIN_PUSH_EXPRESSIONS,
+
+          // convert non-all union into all-union + distinct
+          CoreRules.UNION_TO_DISTINCT,
+
+          // remove aggregation if it does not aggregate and input is already distinct
+          CoreRules.AGGREGATE_REMOVE,
+          // push aggregate through join
+          CoreRules.AGGREGATE_JOIN_TRANSPOSE,
+          // aggregate union rule
+          CoreRules.AGGREGATE_UNION_AGGREGATE,
+
+          // reduce aggregate functions like AVG, STDDEV_POP etc.
+          CoreRules.AGGREGATE_REDUCE_FUNCTIONS,
+
+          // remove unnecessary sort rule
+          CoreRules.SORT_REMOVE,
+
+          // prune empty results rules
+          PruneEmptyRules.AGGREGATE_INSTANCE, PruneEmptyRules.FILTER_INSTANCE, PruneEmptyRules.JOIN_LEFT_INSTANCE,
+          PruneEmptyRules.JOIN_RIGHT_INSTANCE, PruneEmptyRules.PROJECT_INSTANCE, PruneEmptyRules.SORT_INSTANCE,
+          PruneEmptyRules.UNION_INSTANCE,
+
+          // Pinot specific rules
+          PinotExchangeNodeInsertRule.INSTANCE);
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/type/TypeFactory.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/type/TypeFactory.java
new file mode 100644
index 0000000000..55797b0e52
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/type/TypeFactory.java
@@ -0,0 +1,84 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.type;
+
+import java.util.Map;
+import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+
+
+/**
+ * Extends Java-base TypeFactory from Calcite.
+ *
+ * <p>{@link JavaTypeFactoryImpl} is used here because we are not overriding much of the TypeFactory methods
+ * required by Calcite. We will start extending {@link SqlTypeFactoryImpl} or even {@link RelDataTypeFactory}
+ * when necessary for Pinot to override such mechanism.
+ *
+ * <p>Noted that {@link JavaTypeFactoryImpl} is subject to change. Please pay extra attention to this class when
+ * upgrading Calcite versions.
+ */
+public class TypeFactory extends JavaTypeFactoryImpl {
+  private final RelDataTypeSystem _typeSystem;
+
+  public TypeFactory(RelDataTypeSystem typeSystem) {
+    _typeSystem = typeSystem;
+  }
+
+  public RelDataType createRelDataTypeFromSchema(Schema schema) {
+    Builder builder = new Builder(this);
+    for (Map.Entry<String, FieldSpec> e : schema.getFieldSpecMap().entrySet()) {
+      builder.add(e.getKey(), toRelDataType(e.getValue()));
+    }
+    return builder.build();
+  }
+
+  private RelDataType toRelDataType(FieldSpec fieldSpec) {
+    switch (fieldSpec.getDataType()) {
+      case INT:
+        return createSqlType(SqlTypeName.INTEGER);
+      case LONG:
+        return createSqlType(SqlTypeName.BIGINT);
+      case FLOAT:
+        return createSqlType(SqlTypeName.FLOAT);
+      case DOUBLE:
+        return createSqlType(SqlTypeName.DOUBLE);
+      case BOOLEAN:
+        return createSqlType(SqlTypeName.BOOLEAN);
+      case TIMESTAMP:
+        return createSqlType(SqlTypeName.TIMESTAMP);
+      case STRING:
+        return createSqlType(SqlTypeName.VARCHAR);
+      case BYTES:
+        return createSqlType(SqlTypeName.VARBINARY);
+      case JSON:
+        // TODO: support JSON, JSON should be supported using a special RelDataType as it is not a simple String,
+        // nor can it be easily parsed as a STRUCT.
+      case LIST:
+        // TODO: support LIST, MV column should go fall into this category.
+      case STRUCT:
+      case MAP:
+      default:
+        throw new UnsupportedOperationException("unsupported!");
+    }
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/type/TypeSystem.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/type/TypeSystem.java
new file mode 100644
index 0000000000..b2606d9296
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/type/TypeSystem.java
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.type;
+
+import org.apache.calcite.rel.type.RelDataTypeSystemImpl;
+
+
+/**
+ * The {@code TypeSystem} overwrites Calcite type system with Pinot specific logics.
+ *
+ * TODO: no overwrite for now.
+ */
+public class TypeSystem extends RelDataTypeSystemImpl {
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/validate/Validator.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/validate/Validator.java
new file mode 100644
index 0000000000..f774976c21
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/validate/Validator.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.validate;
+
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.sql.SqlOperatorTable;
+import org.apache.calcite.sql.validate.SqlValidatorCatalogReader;
+import org.apache.calcite.sql.validate.SqlValidatorImpl;
+
+
+/**
+ * The {@code Validator} overwrites Calcite's Validator with Pinot specific logics.
+ */
+public class Validator extends SqlValidatorImpl {
+
+  public Validator(SqlOperatorTable opTab, SqlValidatorCatalogReader catalogReader, RelDataTypeFactory typeFactory) {
+    // TODO: support BABEL validator. Currently parser conformance is set to use BABEL.
+    super(opTab, catalogReader, typeFactory, Config.DEFAULT);
+  }
+}
diff --git a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
new file mode 100644
index 0000000000..911469644b
--- /dev/null
+++ b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
@@ -0,0 +1,122 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query;
+
+import com.google.common.collect.ImmutableList;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.calcite.jdbc.CalciteSchemaBuilder;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelRoot;
+import org.apache.calcite.rel.RelWriter;
+import org.apache.calcite.rel.externalize.RelXmlWriter;
+import org.apache.calcite.sql.SqlExplainLevel;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.pinot.core.routing.RoutingManager;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.catalog.PinotCatalog;
+import org.apache.pinot.query.context.PlannerContext;
+import org.apache.pinot.query.planner.QueryPlan;
+import org.apache.pinot.query.planner.StageMetadata;
+import org.apache.pinot.query.routing.WorkerManager;
+import org.apache.pinot.query.type.TypeFactory;
+import org.apache.pinot.query.type.TypeSystem;
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+
+public class QueryEnvironmentTest {
+  private QueryEnvironment _queryEnvironment;
+
+  @BeforeClass
+  public void setUp() {
+    // the port doesn't matter as we are not actually making a server call.
+    RoutingManager routingManager = QueryEnvironmentTestUtils.getMockRoutingManager(1, 2);
+    _queryEnvironment = new QueryEnvironment(new TypeFactory(new TypeSystem()),
+        CalciteSchemaBuilder.asRootSchema(new PinotCatalog(QueryEnvironmentTestUtils.mockTableCache())),
+        new WorkerManager("localhost", 3, routingManager));
+  }
+
+  @Test
+  public void testSqlStrings()
+      throws Exception {
+    testQueryParsing("SELECT * FROM a JOIN b ON a.col1 = b.col2 WHERE a.col3 >= 0",
+        "SELECT *\n" + "FROM `a`\n" + "INNER JOIN `b` ON `a`.`col1` = `b`.`col2`\n" + "WHERE `a`.`col3` >= 0");
+  }
+
+  @Test
+  public void testQueryToStages()
+      throws Exception {
+    PlannerContext plannerContext = new PlannerContext();
+    String query = "SELECT * FROM a JOIN b ON a.col1 = b.col2";
+    QueryPlan queryPlan = _queryEnvironment.planQuery(query);
+    Assert.assertEquals(queryPlan.getQueryStageMap().size(), 4);
+    Assert.assertEquals(queryPlan.getStageMetadataMap().size(), 4);
+    for (Map.Entry<String, StageMetadata> e : queryPlan.getStageMetadataMap().entrySet()) {
+      List<String> tables = e.getValue().getScannedTables();
+      if (tables.size() == 1) {
+        // table scan stages; for tableA it should have 2 hosts, for tableB it should have only 1
+        Assert.assertEquals(
+            e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
+            tables.get(0).equals("a") ? ImmutableList.of("Server_localhost_1", "Server_localhost_2")
+                : ImmutableList.of("Server_localhost_1"));
+      } else if (!e.getKey().equals("ROOT")) {
+        // join stage should have both servers used.
+        Assert.assertEquals(
+            e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
+            ImmutableList.of("Server_localhost_1", "Server_localhost_2"));
+      } else {
+        // reduce stage should have the reducer instance.
+        Assert.assertEquals(
+            e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
+            ImmutableList.of("Server_localhost_3"));
+      }
+    }
+  }
+
+  @Test
+  public void testQueryToRel()
+      throws Exception {
+    PlannerContext plannerContext = new PlannerContext();
+    String query = "SELECT * FROM a JOIN b ON a.col1 = b.col2 WHERE a.col3 >= 0";
+    SqlNode parsed = _queryEnvironment.parse(query, plannerContext);
+    SqlNode validated = _queryEnvironment.validate(parsed);
+    RelRoot relRoot = _queryEnvironment.toRelation(validated, plannerContext);
+    RelNode optimized = _queryEnvironment.optimize(relRoot, plannerContext);
+
+    // Assert that relational plan can be written into a ALL-ATTRIBUTE digest.
+    StringWriter sw = new StringWriter();
+    PrintWriter pw = new PrintWriter(sw);
+    RelWriter planWriter = new RelXmlWriter(pw, SqlExplainLevel.ALL_ATTRIBUTES);
+    optimized.explain(planWriter);
+    Assert.assertNotNull(sw.toString());
+  }
+
+  private void testQueryParsing(String query, String digest)
+      throws Exception {
+    PlannerContext plannerContext = new PlannerContext();
+    SqlNode sqlNode = _queryEnvironment.parse(query, plannerContext);
+    _queryEnvironment.validate(sqlNode);
+    Assert.assertEquals(sqlNode.toString(), digest);
+  }
+}
diff --git a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTestUtils.java b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTestUtils.java
new file mode 100644
index 0000000000..cc0db385db
--- /dev/null
+++ b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTestUtils.java
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.calcite.jdbc.CalciteSchemaBuilder;
+import org.apache.pinot.common.config.provider.TableCache;
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.core.routing.RoutingManager;
+import org.apache.pinot.core.routing.RoutingTable;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.query.catalog.PinotCatalog;
+import org.apache.pinot.query.planner.QueryPlan;
+import org.apache.pinot.query.routing.WorkerInstance;
+import org.apache.pinot.query.routing.WorkerManager;
+import org.apache.pinot.query.type.TypeFactory;
+import org.apache.pinot.query.type.TypeSystem;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.utils.builder.TableNameBuilder;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+
+/**
+ * Base query environment test that provides a bunch of mock tables / schemas so that
+ * we can run a simple query planning, produce stages / metadata for other components to test.
+ */
+public class QueryEnvironmentTestUtils {
+  public static final Schema.SchemaBuilder SCHEMA_BUILDER;
+  public static final Map<String, List<String>> SERVER1_SEGMENTS =
+      ImmutableMap.of("a", Lists.newArrayList("a1", "a2"), "b", Lists.newArrayList("b1"), "c",
+          Lists.newArrayList("c1"));
+  public static final Map<String, List<String>> SERVER2_SEGMENTS =
+      ImmutableMap.of("a", Lists.newArrayList("a3"), "c", Lists.newArrayList("c2", "c3"));
+
+  static {
+    SCHEMA_BUILDER = new Schema.SchemaBuilder().addSingleValueDimension("col1", FieldSpec.DataType.STRING, "")
+        .addSingleValueDimension("col2", FieldSpec.DataType.STRING, "")
+        .addDateTime("ts", FieldSpec.DataType.LONG, "1:MILLISECONDS:EPOCH", "1:HOURS")
+        .addMetric("col3", FieldSpec.DataType.INT, 0);
+  }
+
+  private QueryEnvironmentTestUtils() {
+    // do not instantiate.
+  }
+
+  public static TableCache mockTableCache() {
+    TableCache mock = mock(TableCache.class);
+    when(mock.getTableNameMap()).thenReturn(ImmutableMap.of("a", "a", "b", "b", "c", "c"));
+    when(mock.getSchema("a")).thenReturn(SCHEMA_BUILDER.setSchemaName("a").build());
+    when(mock.getSchema("b")).thenReturn(SCHEMA_BUILDER.setSchemaName("b").build());
+    when(mock.getSchema("c")).thenReturn(SCHEMA_BUILDER.setSchemaName("c").build());
+    return mock;
+  }
+
+  public static QueryEnvironment getQueryEnvironment(int reducerPort, int port1, int port2) {
+    RoutingManager routingManager = QueryEnvironmentTestUtils.getMockRoutingManager(port1, port2);
+    return new QueryEnvironment(new TypeFactory(new TypeSystem()),
+        CalciteSchemaBuilder.asRootSchema(new PinotCatalog(QueryEnvironmentTestUtils.mockTableCache())),
+        new WorkerManager("localhost", reducerPort, routingManager));
+  }
+
+  public static RoutingManager getMockRoutingManager(int port1, int port2) {
+    String server1 = String.format("localhost_%d", port1);
+    String server2 = String.format("localhost_%d", port2);
+    // this doesn't test the QueryServer functionality so the server port can be the same as the mailbox port.
+    // this is only use for test identifier purpose.
+    ServerInstance host1 = new WorkerInstance("localhost", port1, port1);
+    ServerInstance host2 = new WorkerInstance("localhost", port2, port2);
+
+    RoutingTable rtA = mock(RoutingTable.class);
+    when(rtA.getServerInstanceToSegmentsMap()).thenReturn(
+        ImmutableMap.of(host1, SERVER1_SEGMENTS.get("a"), host2, SERVER2_SEGMENTS.get("a")));
+    RoutingTable rtB = mock(RoutingTable.class);
+    when(rtB.getServerInstanceToSegmentsMap()).thenReturn(ImmutableMap.of(host1, SERVER1_SEGMENTS.get("b")));
+    RoutingTable rtC = mock(RoutingTable.class);
+    when(rtC.getServerInstanceToSegmentsMap()).thenReturn(
+        ImmutableMap.of(host1, SERVER1_SEGMENTS.get("c"), host2, SERVER2_SEGMENTS.get("c")));
+    Map<String, RoutingTable> mockRoutingTableMap = ImmutableMap.of("a", rtA, "b", rtB, "c", rtC);
+    RoutingManager mock = mock(RoutingManager.class);
+    when(mock.getRoutingTable(any())).thenAnswer(invocation -> {
+      BrokerRequest brokerRequest = invocation.getArgument(0);
+      String tableName = brokerRequest.getPinotQuery().getDataSource().getTableName();
+      return mockRoutingTableMap.get(TableNameBuilder.extractRawTableName(tableName));
+    });
+    when(mock.getEnabledServerInstanceMap()).thenReturn(ImmutableMap.of(server1, host1, server2, host2));
+    return mock;
+  }
+
+  public static String getTestStageByServerCount(QueryPlan queryPlan, int serverCount) {
+    List<String> stageIds = queryPlan.getStageMetadataMap().entrySet().stream()
+        .filter(e -> !e.getKey().equals("ROOT") && e.getValue().getServerInstances().size() == serverCount)
+        .map(Map.Entry::getKey).collect(Collectors.toList());
+    return stageIds.size() > 0 ? stageIds.get(0) : null;
+  }
+
+  public static int getAvailablePort() {
+    try {
+      try (ServerSocket socket = new ServerSocket(0)) {
+        return socket.getLocalPort();
+      }
+    } catch (IOException e) {
+      throw new RuntimeException("Failed to find an available port to use", e);
+    }
+  }
+}
diff --git a/pom.xml b/pom.xml
index 8fa7ab76c1..56e6ade7e9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -57,6 +57,8 @@
     <module>pinot-segment-local</module>
     <module>pinot-compatibility-verifier</module>
     <module>contrib/pinot-fmpp-maven-plugin</module>
+
+    <module>pinot-query-planner</module>
   </modules>
 
   <licenses>
@@ -966,10 +968,6 @@
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-dbcp2</artifactId>
           </exclusion>
-          <exclusion>
-            <groupId>com.esri.geometry</groupId>
-            <artifactId>esri-geometry-api</artifactId>
-          </exclusion>
           <exclusion>
             <groupId>com.fasterxml.jackson.dataformat</groupId>
             <artifactId>jackson-dataformat-yaml</artifactId>


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[pinot] 07/09: [hotfix] fix server discovery (#8664)

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rongr pushed a commit to branch multi_stage_query_engine
in repository https://gitbox.apache.org/repos/asf/pinot.git

commit 4d77924e5943c9b3d3ab5f7413a50eadbff7a169
Author: Rong Rong <ro...@apache.org>
AuthorDate: Mon May 9 20:20:03 2022 -0700

    [hotfix] fix server discovery (#8664)
    
    Co-authored-by: Rong Rong <ro...@startree.ai>
---
 .../main/java/org/apache/pinot/query/routing/WorkerInstance.java  | 2 +-
 .../test/java/org/apache/pinot/query/QueryEnvironmentTest.java    | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerInstance.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerInstance.java
index 7ad6ee0b9c..c7e6bcfbd5 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerInstance.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerInstance.java
@@ -41,7 +41,7 @@ public class WorkerInstance extends ServerInstance {
   }
 
   private static InstanceConfig toInstanceConfig(String hostname, int serverPort, int mailboxPort) {
-    String server = String.format("%s_%d", hostname, serverPort);
+    String server = String.format("%s%s_%d", CommonConstants.Helix.PREFIX_OF_SERVER_INSTANCE, hostname, serverPort);
     InstanceConfig instanceConfig = InstanceConfig.toInstanceConfig(server);
     ZNRecord znRecord = instanceConfig.getRecord();
     Map<String, String> simpleFields = znRecord.getSimpleFields();
diff --git a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
index d63c5c55c7..9f4778743b 100644
--- a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
+++ b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
@@ -68,18 +68,18 @@ public class QueryEnvironmentTest extends QueryEnvironmentTestBase {
         // table scan stages; for tableA it should have 2 hosts, for tableB it should have only 1
         Assert.assertEquals(
             e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
-            tables.get(0).equals("a") ? ImmutableList.of("localhost_1", "localhost_2")
-                : ImmutableList.of("localhost_1"));
+            tables.get(0).equals("a") ? ImmutableList.of("Server_localhost_1", "Server_localhost_2")
+                : ImmutableList.of("Server_localhost_1"));
       } else if (!PlannerUtils.isRootStage(e.getKey())) {
         // join stage should have both servers used.
         Assert.assertEquals(
             e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
-            ImmutableList.of("localhost_1", "localhost_2"));
+            ImmutableList.of("Server_localhost_1", "Server_localhost_2"));
       } else {
         // reduce stage should have the reducer instance.
         Assert.assertEquals(
             e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
-            ImmutableList.of("localhost_3"));
+            ImmutableList.of("Server_localhost_3"));
       }
     }
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[pinot] 01/09: make github action trigger on multi_stage_query_engine branch (#8342)

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rongr pushed a commit to branch multi_stage_query_engine
in repository https://gitbox.apache.org/repos/asf/pinot.git

commit 90e722a6b05e993f770451209d1ee08cae0b2e48
Author: Rong Rong <wa...@gmail.com>
AuthorDate: Mon Mar 14 10:02:56 2022 -0700

    make github action trigger on multi_stage_query_engine branch (#8342)
---
 .github/workflows/pinot_tests.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.github/workflows/pinot_tests.yml b/.github/workflows/pinot_tests.yml
index e27af6f790..cd7faab560 100644
--- a/.github/workflows/pinot_tests.yml
+++ b/.github/workflows/pinot_tests.yml
@@ -34,6 +34,7 @@ on:
   pull_request:
     branches:
       - master
+      - multi_stage_query_engine
     paths-ignore:
       - "contrib/**"
       - "docs/**"


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[pinot] 04/09: Use proto for query plan serialization (#8479)

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rongr pushed a commit to branch multi_stage_query_engine
in repository https://gitbox.apache.org/repos/asf/pinot.git

commit c06bbfbaf3b357edb6b3a9d9e7d7c5284f8706dc
Author: Rong Rong <ro...@apache.org>
AuthorDate: Fri Apr 15 14:24:38 2022 -0700

    Use proto for query plan serialization (#8479)
    
    * serializable format
    
    * fix linter
    
    * bump version since feature branch based bumped
    
    * fix auto-rebase error
    
    * fix test async mock intercept issue
    
    * use reflection for ser/de
    
    * also fix test coverage on all node types
    
    * fix java8
    
    * fix JOINNode member type as well as plan.proto comments
    
    Co-authored-by: Rong Rong <ro...@startree.ai>
---
 .../pinot/common/config/provider/TableCache.java   |  10 -
 .../src/main/proto/{worker.proto => plan.proto}    |  66 ++++---
 pinot-common/src/main/proto/worker.proto           |   4 +-
 pinot-query-planner/pom.xml                        |   2 +-
 .../pinot/query/planner/RelToStageConverter.java   |   5 +-
 .../query/planner/nodes/AbstractStageNode.java     |  15 +-
 .../apache/pinot/query/planner/nodes/CalcNode.java |   7 +-
 .../apache/pinot/query/planner/nodes/JoinNode.java |  21 ++-
 .../query/planner/nodes/MailboxReceiveNode.java    |   8 +-
 .../pinot/query/planner/nodes/MailboxSendNode.java |   8 +-
 .../pinot/query/planner/nodes/SerDeUtils.java      |  65 +++++++
 .../pinot/query/planner/nodes/TableScanNode.java   |   8 +-
 .../ProtoSerializable.java}                        |  22 ++-
 .../nodes/serde/ProtoSerializationUtils.java       | 209 +++++++++++++++++++++
 .../partitioning/FieldSelectionKeySelector.java    |   7 +
 pinot-query-runtime/pom.xml                        |   2 +-
 .../runtime/plan/serde/QueryPlanSerDeUtils.java    |   6 +-
 .../runtime/plan/serde/StageNodeSerDeUtils.java    |  56 ------
 .../pinot/query/service/QueryServerTest.java       |  66 ++++++-
 19 files changed, 458 insertions(+), 129 deletions(-)

diff --git a/pinot-common/src/main/java/org/apache/pinot/common/config/provider/TableCache.java b/pinot-common/src/main/java/org/apache/pinot/common/config/provider/TableCache.java
index 005fd65615..17fbf73cd3 100644
--- a/pinot-common/src/main/java/org/apache/pinot/common/config/provider/TableCache.java
+++ b/pinot-common/src/main/java/org/apache/pinot/common/config/provider/TableCache.java
@@ -227,16 +227,6 @@ public class TableCache implements PinotConfigProvider {
     }
   }
 
-  /**
-   * Return a map between lower-case table name and their canonicalized form. Key-value pair are only different in
-   * case-sensitive environment.
-   *
-   * @return the table name map.
-   */
-  public Map<String, String> getTableNameMap() {
-    return _tableNameMap;
-  }
-
   private void addTableConfigs(List<String> paths) {
     // Subscribe data changes before reading the data to avoid missing changes
     for (String path : paths) {
diff --git a/pinot-common/src/main/proto/worker.proto b/pinot-common/src/main/proto/plan.proto
similarity index 54%
copy from pinot-common/src/main/proto/worker.proto
copy to pinot-common/src/main/proto/plan.proto
index c64798fa63..47018197fc 100644
--- a/pinot-common/src/main/proto/worker.proto
+++ b/pinot-common/src/main/proto/plan.proto
@@ -39,36 +39,56 @@ syntax = "proto3";
 
 package org.apache.pinot.common.proto;
 
-service PinotQueryWorker {
-  // Dispatch a QueryRequest to a PinotQueryWorker
-  rpc Submit(QueryRequest) returns (QueryResponse);
+message StageNode {
+  int32 stageId = 1;
+  string nodeName = 2;
+  repeated StageNode inputs = 3;
+  ObjectField objectField = 4;
 }
 
-// QueryRequest is the dispatched content for a specific query stage on a specific worker.
-message QueryRequest {
-  map<string, string> metadata = 1;
-  StagePlan stagePlan = 2;
+// MemberVariableField defines the serialized format of the member variables of a class object.
+// MemberVariableField can be one of
+//   1. literal
+//   2. list
+//   3. map
+//   4. complex class object
+message MemberVariableField {
+  oneof member_variable_field {
+    LiteralField literalField = 1;
+    ListField listField = 2;
+    MapField mapField = 3;
+    ObjectField objectField = 4;
+  }
 }
 
-// QueryResponse is the dispatched response from worker, it doesn't contain actual data, only dispatch status.
-message QueryResponse {
-  map<string, string> metadata = 1;
-  bytes payload = 2;
+// ObjectField defines the serialized format of a complex class object.
+// it contains:
+//   1. its fully-qualified clazz name;
+//   2. its MemberVariableField map.
+message ObjectField {
+  string objectClassName = 1;
+  map<string, MemberVariableField> memberVariables = 2;
 }
 
-message StagePlan {
-  int32 stageId = 1;
-  string instanceId = 2;
-  bytes serializedStageRoot = 3;
-  map<int32, StageMetadata> stageMetadata = 4;
+// LiteralField defines the serialized format of a literal field.
+message LiteralField {
+  oneof literal_field {
+    bool boolField = 1;
+    int32 intField = 2;
+    int64 longField = 3;
+    double doubleField = 4;
+    string stringField = 5;
+  }
 }
 
-message StageMetadata {
-  repeated string instances = 1;
-  repeated string dataSources = 2;
-  map<string, SegmentList> instanceToSegmentList = 3;
+// ListField defines the serialized format of a list field.
+// The content of the list is a MemberVariableField.
+message ListField {
+  repeated MemberVariableField content = 1;
 }
 
-message SegmentList {
-  repeated string segments = 1;
-}
+// ListField defines the serialized format of a map field.
+// The key of the map is a string and the value of the map is a MemberVariableField.
+message MapField {
+  map<string, MemberVariableField> content = 1;
+}
\ No newline at end of file
diff --git a/pinot-common/src/main/proto/worker.proto b/pinot-common/src/main/proto/worker.proto
index c64798fa63..87aecc8391 100644
--- a/pinot-common/src/main/proto/worker.proto
+++ b/pinot-common/src/main/proto/worker.proto
@@ -39,6 +39,8 @@ syntax = "proto3";
 
 package org.apache.pinot.common.proto;
 
+import "plan.proto";
+
 service PinotQueryWorker {
   // Dispatch a QueryRequest to a PinotQueryWorker
   rpc Submit(QueryRequest) returns (QueryResponse);
@@ -59,7 +61,7 @@ message QueryResponse {
 message StagePlan {
   int32 stageId = 1;
   string instanceId = 2;
-  bytes serializedStageRoot = 3;
+  StageNode stageRoot = 3;
   map<int32, StageMetadata> stageMetadata = 4;
 }
 
diff --git a/pinot-query-planner/pom.xml b/pinot-query-planner/pom.xml
index 8d1af64a19..05b9461cdc 100644
--- a/pinot-query-planner/pom.xml
+++ b/pinot-query-planner/pom.xml
@@ -26,7 +26,7 @@
   <parent>
     <artifactId>pinot</artifactId>
     <groupId>org.apache.pinot</groupId>
-    <version>0.10.0-SNAPSHOT</version>
+    <version>0.11.0-SNAPSHOT</version>
   </parent>
   <artifactId>pinot-query-planner</artifactId>
   <name>Pinot Query Planner</name>
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java
index e558694aab..572302ef92 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/RelToStageConverter.java
@@ -37,7 +37,6 @@ import org.apache.pinot.query.planner.nodes.JoinNode;
 import org.apache.pinot.query.planner.nodes.StageNode;
 import org.apache.pinot.query.planner.nodes.TableScanNode;
 import org.apache.pinot.query.planner.partitioning.FieldSelectionKeySelector;
-import org.apache.pinot.query.planner.partitioning.KeySelector;
 
 
 /**
@@ -93,8 +92,8 @@ public final class RelToStageConverter {
     RelDataType rightRowType = node.getRight().getRowType();
     int leftOperandIndex = ((RexInputRef) joinCondition.getOperands().get(0)).getIndex();
     int rightOperandIndex = ((RexInputRef) joinCondition.getOperands().get(1)).getIndex();
-    KeySelector<Object[], Object> leftFieldSelectionKeySelector = new FieldSelectionKeySelector(leftOperandIndex);
-    KeySelector<Object[], Object> rightFieldSelectionKeySelector =
+    FieldSelectionKeySelector leftFieldSelectionKeySelector = new FieldSelectionKeySelector(leftOperandIndex);
+    FieldSelectionKeySelector rightFieldSelectionKeySelector =
           new FieldSelectionKeySelector(rightOperandIndex - leftRowType.getFieldNames().size());
     return new JoinNode(currentStageId, joinType, Collections.singletonList(new JoinNode.JoinClause(
         leftFieldSelectionKeySelector, rightFieldSelectionKeySelector)));
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
index b99075429a..ed1fc9ba3e 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/AbstractStageNode.java
@@ -20,9 +20,12 @@ package org.apache.pinot.query.planner.nodes;
 
 import java.util.ArrayList;
 import java.util.List;
+import org.apache.pinot.common.proto.Plan;
+import org.apache.pinot.query.planner.nodes.serde.ProtoSerializable;
+import org.apache.pinot.query.planner.nodes.serde.ProtoSerializationUtils;
 
 
-public abstract class AbstractStageNode implements StageNode {
+public abstract class AbstractStageNode implements StageNode, ProtoSerializable {
 
   protected final int _stageId;
   protected final List<StageNode> _inputs;
@@ -46,4 +49,14 @@ public abstract class AbstractStageNode implements StageNode {
   public int getStageId() {
     return _stageId;
   }
+
+  @Override
+  public void setObjectField(Plan.ObjectField objectField) {
+    ProtoSerializationUtils.fromObjectField(this, objectField);
+  }
+
+  @Override
+  public Plan.ObjectField getObjectField() {
+    return ProtoSerializationUtils.toObjectField(this);
+  }
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
index b188b8e2f7..0aa8c94ec8 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
@@ -19,8 +19,13 @@
 package org.apache.pinot.query.planner.nodes;
 
 
+
 public class CalcNode extends AbstractStageNode {
-  private final String _expression;
+  private String _expression;
+
+  public CalcNode(int stageId) {
+    super(stageId);
+  }
 
   public CalcNode(int stageId, String expression) {
     super(stageId);
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java
index 94af122c74..bf380639d8 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/JoinNode.java
@@ -18,15 +18,19 @@
  */
 package org.apache.pinot.query.planner.nodes;
 
-import java.io.Serializable;
 import java.util.List;
 import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.pinot.query.planner.partitioning.FieldSelectionKeySelector;
 import org.apache.pinot.query.planner.partitioning.KeySelector;
 
 
 public class JoinNode extends AbstractStageNode {
-  private final JoinRelType _joinRelType;
-  private final List<JoinClause> _criteria;
+  private JoinRelType _joinRelType;
+  private List<JoinClause> _criteria;
+
+  public JoinNode(int stageId) {
+    super(stageId);
+  }
 
   public JoinNode(int stageId, JoinRelType joinRelType, List<JoinClause> criteria
   ) {
@@ -43,11 +47,14 @@ public class JoinNode extends AbstractStageNode {
     return _criteria;
   }
 
-  public static class JoinClause implements Serializable {
-    private final KeySelector<Object[], Object> _leftJoinKeySelector;
-    private final KeySelector<Object[], Object> _rightJoinKeySelector;
+  public static class JoinClause {
+    private KeySelector<Object[], Object> _leftJoinKeySelector;
+    private KeySelector<Object[], Object> _rightJoinKeySelector;
+
+    public JoinClause() {
+    }
 
-    public JoinClause(KeySelector<Object[], Object> leftKeySelector, KeySelector<Object[], Object> rightKeySelector) {
+    public JoinClause(FieldSelectionKeySelector leftKeySelector, FieldSelectionKeySelector rightKeySelector) {
       _leftJoinKeySelector = leftKeySelector;
       _rightJoinKeySelector = rightKeySelector;
     }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java
index d8269346f3..8f0c619b79 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxReceiveNode.java
@@ -22,8 +22,12 @@ import org.apache.calcite.rel.RelDistribution;
 
 
 public class MailboxReceiveNode extends AbstractStageNode {
-  private final int _senderStageId;
-  private final RelDistribution.Type _exchangeType;
+  private int _senderStageId;
+  private RelDistribution.Type _exchangeType;
+
+  public MailboxReceiveNode(int stageId) {
+    super(stageId);
+  }
 
   public MailboxReceiveNode(int stageId, int senderStageId, RelDistribution.Type exchangeType) {
     super(stageId);
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java
index ea39ad3493..9867a16f61 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/MailboxSendNode.java
@@ -22,8 +22,12 @@ import org.apache.calcite.rel.RelDistribution;
 
 
 public class MailboxSendNode extends AbstractStageNode {
-  private final int _receiverStageId;
-  private final RelDistribution.Type _exchangeType;
+  private int _receiverStageId;
+  private RelDistribution.Type _exchangeType;
+
+  public MailboxSendNode(int stageId) {
+    super(stageId);
+  }
 
   public MailboxSendNode(int stageId, int receiverStageId, RelDistribution.Type exchangeType) {
     super(stageId);
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/SerDeUtils.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/SerDeUtils.java
new file mode 100644
index 0000000000..ad7184cdb1
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/SerDeUtils.java
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.nodes;
+
+import org.apache.pinot.common.proto.Plan;
+
+
+public final class SerDeUtils {
+  private SerDeUtils() {
+    // do not instantiate.
+  }
+
+  public static AbstractStageNode deserializeStageNode(Plan.StageNode protoNode) {
+    AbstractStageNode stageNode = newNodeInstance(protoNode.getNodeName(), protoNode.getStageId());
+    stageNode.setObjectField(protoNode.getObjectField());
+    for (Plan.StageNode protoChild : protoNode.getInputsList()) {
+      stageNode.addInput(deserializeStageNode(protoChild));
+    }
+    return stageNode;
+  }
+
+  public static Plan.StageNode serializeStageNode(AbstractStageNode stageNode) {
+    Plan.StageNode.Builder builder = Plan.StageNode.newBuilder()
+        .setStageId(stageNode.getStageId())
+        .setNodeName(stageNode.getClass().getSimpleName())
+        .setObjectField(stageNode.getObjectField());
+    for (StageNode childNode : stageNode.getInputs()) {
+      builder.addInputs(serializeStageNode((AbstractStageNode) childNode));
+    }
+    return builder.build();
+  }
+
+  private static AbstractStageNode newNodeInstance(String nodeName, int stageId) {
+    switch (nodeName) {
+      case "TableScanNode":
+        return new TableScanNode(stageId);
+      case "JoinNode":
+        return new JoinNode(stageId);
+      case "CalcNode":
+        return new CalcNode(stageId);
+      case "MailboxSendNode":
+        return new MailboxSendNode(stageId);
+      case "MailboxReceiveNode":
+        return new MailboxReceiveNode(stageId);
+      default:
+        throw new IllegalArgumentException("Unknown node name: " + nodeName);
+    }
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
index 8d78ec6d0f..9375a7e986 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/TableScanNode.java
@@ -22,8 +22,12 @@ import java.util.List;
 
 
 public class TableScanNode extends AbstractStageNode {
-  private final String _tableName;
-  private final List<String> _tableScanColumns;
+  private String _tableName;
+  private List<String> _tableScanColumns;
+
+  public TableScanNode(int stageId) {
+    super(stageId);
+  }
 
   public TableScanNode(int stageId, String tableName, List<String> tableScanColumns) {
     super(stageId);
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializable.java
similarity index 69%
copy from pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
copy to pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializable.java
index b188b8e2f7..2b99003e87 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/CalcNode.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializable.java
@@ -16,18 +16,20 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.pinot.query.planner.nodes;
+/**
+ * Autogenerated by Thrift Compiler (0.13.0)
+ *
+ * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
+ *  @generated
+ */
+package org.apache.pinot.query.planner.nodes.serde;
+
+import org.apache.pinot.common.proto.Plan;
 
 
-public class CalcNode extends AbstractStageNode {
-  private final String _expression;
+public interface ProtoSerializable {
 
-  public CalcNode(int stageId, String expression) {
-    super(stageId);
-    _expression = expression;
-  }
+  void setObjectField(Plan.ObjectField objFields);
 
-  public String getExpression() {
-    return _expression;
-  }
+  Plan.ObjectField getObjectField();
 }
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializationUtils.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializationUtils.java
new file mode 100644
index 0000000000..c30295a101
--- /dev/null
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/nodes/serde/ProtoSerializationUtils.java
@@ -0,0 +1,209 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.planner.nodes.serde;
+
+import com.google.common.base.Preconditions;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.pinot.common.proto.Plan;
+
+
+@SuppressWarnings({"rawtypes", "unchecked"})
+public class ProtoSerializationUtils {
+  private static final String ENUM_VALUE_KEY = "ENUM_VALUE_KEY";
+
+  private ProtoSerializationUtils() {
+    // do not instantiate.
+  }
+
+  public static void fromObjectField(Object object, Plan.ObjectField objectField) {
+    Map<String, Plan.MemberVariableField> memberVariablesMap = objectField.getMemberVariablesMap();
+    try {
+      for (Map.Entry<String, Plan.MemberVariableField> e : memberVariablesMap.entrySet()) {
+        Object memberVarObject = constructMemberVariable(e.getValue());
+        if (memberVarObject != null) {
+          Field declaredField = object.getClass().getDeclaredField(e.getKey());
+          declaredField.setAccessible(true);
+          declaredField.set(object, memberVarObject);
+        }
+      }
+    } catch (NoSuchFieldException | IllegalAccessException e) {
+      throw new IllegalStateException("Unable to set Object field for: " + objectField.getObjectClassName(), e);
+    }
+  }
+
+  public static Plan.ObjectField toObjectField(Object object) {
+    Plan.ObjectField.Builder builder = Plan.ObjectField.newBuilder();
+    builder.setObjectClassName(object.getClass().getName());
+    // special handling for enum
+    if (object instanceof Enum) {
+      builder.putMemberVariables(ENUM_VALUE_KEY, serializeMemberVariable(((Enum) object).name()));
+    } else {
+      try {
+        for (Field field : object.getClass().getDeclaredFields()) {
+          field.setAccessible(true);
+          Object fieldObject = field.get(object);
+          builder.putMemberVariables(field.getName(), serializeMemberVariable(fieldObject));
+        }
+      } catch (IllegalAccessException e) {
+        throw new IllegalStateException("Unable to serialize Object: " + object.getClass(), e);
+      }
+    }
+    return builder.build();
+  }
+
+  // --------------------------------------------------------------------------
+  // Serialize Utils
+  // --------------------------------------------------------------------------
+
+  private static Plan.LiteralField boolField(boolean val) {
+    return Plan.LiteralField.newBuilder().setBoolField(val).build();
+  }
+
+  private static Plan.LiteralField intField(int val) {
+    return Plan.LiteralField.newBuilder().setIntField(val).build();
+  }
+
+  private static Plan.LiteralField longField(long val) {
+    return Plan.LiteralField.newBuilder().setLongField(val).build();
+  }
+
+  private static Plan.LiteralField doubleField(double val) {
+    return Plan.LiteralField.newBuilder().setDoubleField(val).build();
+  }
+
+  private static Plan.LiteralField stringField(String val) {
+    return Plan.LiteralField.newBuilder().setStringField(val).build();
+  }
+
+  private static Plan.MemberVariableField serializeMemberVariable(Object fieldObject) {
+    Plan.MemberVariableField.Builder builder = Plan.MemberVariableField.newBuilder();
+    if (fieldObject instanceof Boolean) {
+      builder.setLiteralField(boolField((Boolean) fieldObject));
+    } else if (fieldObject instanceof Integer) {
+      builder.setLiteralField(intField((Integer) fieldObject));
+    } else if (fieldObject instanceof Long) {
+      builder.setLiteralField(longField((Long) fieldObject));
+    } else if (fieldObject instanceof Double) {
+      builder.setLiteralField(doubleField((Double) fieldObject));
+    } else if (fieldObject instanceof String) {
+      builder.setLiteralField(stringField((String) fieldObject));
+    } else if (fieldObject instanceof List) {
+      builder.setListField(serializeListMemberVariable(fieldObject));
+    } else if (fieldObject instanceof Map) {
+      builder.setMapField(serializeMapMemberVariable(fieldObject));
+    } else {
+      builder.setObjectField(toObjectField(fieldObject));
+    }
+    return builder.build();
+  }
+
+  private static Plan.ListField serializeListMemberVariable(Object fieldObject) {
+    Preconditions.checkState(fieldObject instanceof List);
+    Plan.ListField.Builder builder = Plan.ListField.newBuilder();
+    for (Object e : (List) fieldObject) {
+      builder.addContent(serializeMemberVariable(e));
+    }
+    return builder.build();
+  }
+
+  private static Plan.MapField serializeMapMemberVariable(Object fieldObject) {
+    Preconditions.checkState(fieldObject instanceof Map);
+    Plan.MapField.Builder builder = Plan.MapField.newBuilder();
+    Set<Map.Entry<String, Object>> entrySet = ((Map) fieldObject).entrySet();
+    for (Map.Entry<String, Object> e : entrySet) {
+      builder.putContent(e.getKey(), serializeMemberVariable(e.getValue()));
+    }
+    return builder.build();
+  }
+
+  // --------------------------------------------------------------------------
+  // Deserialize Utils
+  // --------------------------------------------------------------------------
+
+  private static Object constructMemberVariable(Plan.MemberVariableField memberVariableField) {
+    switch (memberVariableField.getMemberVariableFieldCase()) {
+      case LITERALFIELD:
+        return constructLiteral(memberVariableField.getLiteralField());
+      case LISTFIELD:
+        return constructList(memberVariableField.getListField());
+      case MAPFIELD:
+        return constructMap(memberVariableField.getMapField());
+      case OBJECTFIELD:
+        return constructObject(memberVariableField.getObjectField());
+      case MEMBERVARIABLEFIELD_NOT_SET:
+      default:
+        return null;
+    }
+  }
+
+  private static Object constructLiteral(Plan.LiteralField literalField) {
+    switch (literalField.getLiteralFieldCase()) {
+      case BOOLFIELD:
+        return literalField.getBoolField();
+      case INTFIELD:
+        return literalField.getIntField();
+      case LONGFIELD:
+        return literalField.getLongField();
+      case DOUBLEFIELD:
+        return literalField.getDoubleField();
+      case STRINGFIELD:
+        return literalField.getStringField();
+      case LITERALFIELD_NOT_SET:
+      default:
+        return null;
+    }
+  }
+
+  private static List constructList(Plan.ListField listField) {
+    List list = new ArrayList();
+    for (Plan.MemberVariableField e : listField.getContentList()) {
+      list.add(constructMemberVariable(e));
+    }
+    return list;
+  }
+
+  private static Object constructMap(Plan.MapField mapField) {
+    Map map = new HashMap();
+    for (Map.Entry<String, Plan.MemberVariableField> e : mapField.getContentMap().entrySet()) {
+      map.put(e.getKey(), constructMemberVariable(e.getValue()));
+    }
+    return map;
+  }
+
+  private static Object constructObject(Plan.ObjectField objectField) {
+    try {
+      Class<?> clazz = Class.forName(objectField.getObjectClassName());
+      if (clazz.isEnum()) {
+        return Enum.valueOf((Class<Enum>) clazz,
+            objectField.getMemberVariablesOrDefault(ENUM_VALUE_KEY, null).getLiteralField().getStringField());
+      } else {
+        Object obj = clazz.newInstance();
+        fromObjectField(obj, objectField);
+        return obj;
+      }
+    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
+      throw new IllegalStateException("Unable to create Object of type: " + objectField.getObjectClassName(), e);
+    }
+  }
+}
diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
index 0b846e555c..95991d558b 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/partitioning/FieldSelectionKeySelector.java
@@ -26,10 +26,17 @@ public class FieldSelectionKeySelector implements KeySelector<Object[], Object>
 
   private int _columnIndex;
 
+  public FieldSelectionKeySelector() {
+  }
+
   public FieldSelectionKeySelector(int columnIndex) {
     _columnIndex = columnIndex;
   }
 
+  public int getColumnIndex() {
+    return _columnIndex;
+  }
+
   @Override
   public Object getKey(Object[] input) {
     return input[_columnIndex];
diff --git a/pinot-query-runtime/pom.xml b/pinot-query-runtime/pom.xml
index 3607e51e31..03fa270ea4 100644
--- a/pinot-query-runtime/pom.xml
+++ b/pinot-query-runtime/pom.xml
@@ -26,7 +26,7 @@
   <parent>
     <artifactId>pinot</artifactId>
     <groupId>org.apache.pinot</groupId>
-    <version>0.10.0-SNAPSHOT</version>
+    <version>0.11.0-SNAPSHOT</version>
   </parent>
   <artifactId>pinot-query-runtime</artifactId>
   <name>Pinot Query Runtime</name>
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java
index 1b4ecbbdc0..358ebb8465 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/QueryPlanSerDeUtils.java
@@ -25,6 +25,8 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.pinot.common.proto.Worker;
 import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.query.planner.StageMetadata;
+import org.apache.pinot.query.planner.nodes.AbstractStageNode;
+import org.apache.pinot.query.planner.nodes.SerDeUtils;
 import org.apache.pinot.query.routing.WorkerInstance;
 import org.apache.pinot.query.runtime.plan.DistributedStagePlan;
 
@@ -41,7 +43,7 @@ public class QueryPlanSerDeUtils {
   public static DistributedStagePlan deserialize(Worker.StagePlan stagePlan) {
     DistributedStagePlan distributedStagePlan = new DistributedStagePlan(stagePlan.getStageId());
     distributedStagePlan.setServerInstance(stringToInstance(stagePlan.getInstanceId()));
-    distributedStagePlan.setStageRoot(StageNodeSerDeUtils.deserializeStageRoot(stagePlan.getSerializedStageRoot()));
+    distributedStagePlan.setStageRoot(SerDeUtils.deserializeStageNode(stagePlan.getStageRoot()));
     Map<Integer, Worker.StageMetadata> metadataMap = stagePlan.getStageMetadataMap();
     distributedStagePlan.getMetadataMap().putAll(protoMapToStageMetadataMap(metadataMap));
     return distributedStagePlan;
@@ -51,7 +53,7 @@ public class QueryPlanSerDeUtils {
     return Worker.StagePlan.newBuilder()
         .setStageId(distributedStagePlan.getStageId())
         .setInstanceId(instanceToString(distributedStagePlan.getServerInstance()))
-        .setSerializedStageRoot(StageNodeSerDeUtils.serializeStageRoot(distributedStagePlan.getStageRoot()))
+        .setStageRoot(SerDeUtils.serializeStageNode((AbstractStageNode) distributedStagePlan.getStageRoot()))
         .putAllStageMetadata(stageMetadataMapToProtoMap(distributedStagePlan.getMetadataMap())).build();
   }
 
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/StageNodeSerDeUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/StageNodeSerDeUtils.java
deleted file mode 100644
index 80370128cf..0000000000
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/serde/StageNodeSerDeUtils.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.pinot.query.runtime.plan.serde;
-
-import com.google.common.base.Preconditions;
-import com.google.protobuf.ByteString;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import org.apache.pinot.query.planner.nodes.StageNode;
-
-
-public class StageNodeSerDeUtils {
-  private StageNodeSerDeUtils() {
-    // do not instantiate.
-  }
-
-  public static StageNode deserializeStageRoot(ByteString serializeStagePlan) {
-    try (ByteArrayInputStream bs = new ByteArrayInputStream(serializeStagePlan.toByteArray());
-        ObjectInputStream is = new ObjectInputStream(bs)) {
-      Object o = is.readObject();
-      Preconditions.checkState(o instanceof StageNode, "invalid worker query request object");
-      return (StageNode) o;
-    } catch (Exception e) {
-      throw new RuntimeException(e);
-    }
-  }
-
-  public static ByteString serializeStageRoot(StageNode stageRoot) {
-    try (ByteArrayOutputStream bs = new ByteArrayOutputStream();
-        ObjectOutputStream os = new ObjectOutputStream(bs)) {
-      os.writeObject(stageRoot);
-      return ByteString.copyFrom(bs.toByteArray());
-    } catch (IOException e) {
-      throw new RuntimeException(e);
-    }
-  }
-}
diff --git a/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java b/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java
index 539d5d9a53..eeed6d20d9 100644
--- a/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java
+++ b/pinot-query-runtime/src/test/java/org/apache/pinot/query/service/QueryServerTest.java
@@ -20,29 +20,37 @@ package org.apache.pinot.query.service;
 
 import com.google.common.collect.Lists;
 import io.grpc.ManagedChannelBuilder;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ExecutorService;
 import org.apache.pinot.common.proto.PinotQueryWorkerGrpc;
 import org.apache.pinot.common.proto.Worker;
 import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.query.QueryEnvironment;
 import org.apache.pinot.query.QueryEnvironmentTestUtils;
 import org.apache.pinot.query.planner.QueryPlan;
+import org.apache.pinot.query.planner.StageMetadata;
+import org.apache.pinot.query.planner.nodes.StageNode;
 import org.apache.pinot.query.routing.WorkerInstance;
 import org.apache.pinot.query.runtime.QueryRunner;
 import org.apache.pinot.query.runtime.plan.serde.QueryPlanSerDeUtils;
+import org.apache.pinot.util.TestUtils;
 import org.mockito.Mockito;
 import org.testng.Assert;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
+import static org.mockito.ArgumentMatchers.any;
+
 
 public class QueryServerTest {
   private static final int QUERY_SERVER_COUNT = 2;
-  private Map<Integer, QueryServer> _queryServerMap = new HashMap<>();
-  private Map<Integer, ServerInstance> _queryServerInstanceMap = new HashMap<>();
+  private final Map<Integer, QueryServer> _queryServerMap = new HashMap<>();
+  private final Map<Integer, ServerInstance> _queryServerInstanceMap = new HashMap<>();
+  private final Map<Integer, QueryRunner> _queryRunnerMap = new HashMap<>();
 
   private QueryEnvironment _queryEnvironment;
 
@@ -52,9 +60,11 @@ public class QueryServerTest {
 
     for (int i = 0; i < QUERY_SERVER_COUNT; i++) {
       int availablePort = QueryEnvironmentTestUtils.getAvailablePort();
-      QueryServer queryServer = new QueryServer(availablePort, Mockito.mock(QueryRunner.class));
+      QueryRunner queryRunner = Mockito.mock(QueryRunner.class);
+      QueryServer queryServer = new QueryServer(availablePort, queryRunner);
       queryServer.start();
       _queryServerMap.put(availablePort, queryServer);
+      _queryRunnerMap.put(availablePort, queryRunner);
       // this only test the QueryServer functionality so the server port can be the same as the mailbox port.
       // this is only use for test identifier purpose.
       _queryServerInstanceMap.put(availablePort, new WorkerInstance("localhost", availablePort, availablePort));
@@ -73,17 +83,59 @@ public class QueryServerTest {
     }
   }
 
+  @SuppressWarnings("unchecked")
   @Test
   public void testWorkerAcceptsWorkerRequestCorrect()
       throws Exception {
     QueryPlan queryPlan = _queryEnvironment.planQuery("SELECT * FROM a JOIN b ON a.col1 = b.col2");
 
-    int singleServerStageId = QueryEnvironmentTestUtils.getTestStageByServerCount(queryPlan, 1);
+    for (int stageId : queryPlan.getStageMetadataMap().keySet()) {
+      if (stageId > 0) { // we do not test reduce stage.
+        Worker.QueryRequest queryRequest = getQueryRequest(queryPlan, stageId);
+
+        // submit the request for testing.
+        submitRequest(queryRequest);
+
+        StageMetadata stageMetadata = queryPlan.getStageMetadataMap().get(stageId);
+
+        // ensure mock query runner received correctly deserialized payload.
+        // since submitRequest is async, we need to wait for the mockRunner to receive the query payload.
+        QueryRunner mockRunner = _queryRunnerMap.get(stageMetadata.getServerInstances().get(0).getPort());
+        TestUtils.waitForCondition(aVoid -> {
+          try {
+            Mockito.verify(mockRunner).processQuery(Mockito.argThat(distributedStagePlan -> {
+              StageNode stageNode = queryPlan.getQueryStageMap().get(stageId);
+              return isStageNodesEqual(stageNode, distributedStagePlan.getStageRoot()) && isMetadataMapsEqual(
+                  stageMetadata, distributedStagePlan.getMetadataMap().get(stageId));
+            }), any(ExecutorService.class), any(Map.class));
+            return true;
+          } catch (Throwable t) {
+            return false;
+          }
+        }, 1000L, "Error verifying mock QueryRunner intercepted query payload!");
+      }
+    }
+  }
 
-    Worker.QueryRequest queryRequest = getQueryRequest(queryPlan, singleServerStageId);
+  private static boolean isMetadataMapsEqual(StageMetadata left, StageMetadata right) {
+    return left.getServerInstances().equals(right.getServerInstances())
+        && left.getServerInstanceToSegmentsMap().equals(right.getServerInstanceToSegmentsMap())
+        && left.getScannedTables().equals(right.getScannedTables());
+  }
 
-    // submit the request for testing.
-    submitRequest(queryRequest);
+  private static boolean isStageNodesEqual(StageNode left, StageNode right) {
+    if (left.getStageId() != right.getStageId() || left.getClass() != right.getClass()
+        || left.getInputs().size() != right.getInputs().size()) {
+      return false;
+    }
+    left.getInputs().sort(Comparator.comparingInt(StageNode::getStageId));
+    right.getInputs().sort(Comparator.comparingInt(StageNode::getStageId));
+    for (int i = 0; i < left.getInputs().size(); i++) {
+      if (!isStageNodesEqual(left.getInputs().get(i), right.getInputs().get(i))) {
+        return false;
+      }
+    }
+    return true;
   }
 
   private void submitRequest(Worker.QueryRequest queryRequest) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[pinot] 06/09: fix rebase issue after PQL deprecation (#8652)

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rongr pushed a commit to branch multi_stage_query_engine
in repository https://gitbox.apache.org/repos/asf/pinot.git

commit 30bc434343aa5e662081a7a51d63d5003e0860b7
Author: Rong Rong <ro...@apache.org>
AuthorDate: Fri May 6 09:33:34 2022 -0700

    fix rebase issue after PQL deprecation (#8652)
    
    Co-authored-by: Rong Rong <ro...@startree.ai>
---
 .../main/java/org/apache/pinot/query/routing/WorkerManager.java   | 3 +--
 .../test/java/org/apache/pinot/query/QueryEnvironmentTest.java    | 8 ++++----
 .../org/apache/pinot/query/runtime/operator/HashJoinOperator.java | 6 ------
 .../pinot/query/runtime/operator/MailboxReceiveOperator.java      | 6 ------
 .../apache/pinot/query/runtime/operator/MailboxSendOperator.java  | 6 ------
 5 files changed, 5 insertions(+), 24 deletions(-)

diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java
index 5b6ba6d804..74e2036e77 100644
--- a/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java
+++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java
@@ -45,7 +45,6 @@ import org.apache.pinot.sql.parsers.CalciteSqlCompiler;
  * the worker manager later when we split out the query-spi layer.
  */
 public class WorkerManager {
-  private static final CalciteSqlCompiler CALCITE_SQL_COMPILER = new CalciteSqlCompiler();
 
   private final String _hostName;
   private final int _port;
@@ -90,7 +89,7 @@ public class WorkerManager {
     String rawTableName = TableNameBuilder.extractRawTableName(tableName);
     // TODO: support both offline and realtime, now we hard code offline table.
     String tableNameWithType = TableNameBuilder.forType(TableType.OFFLINE).tableNameWithType(rawTableName);
-    return _routingManager.getRoutingTable(CALCITE_SQL_COMPILER.compileToBrokerRequest(
+    return _routingManager.getRoutingTable(CalciteSqlCompiler.compileToBrokerRequest(
         "SELECT * FROM " + tableNameWithType));
   }
 }
diff --git a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
index 9f4778743b..d63c5c55c7 100644
--- a/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
+++ b/pinot-query-planner/src/test/java/org/apache/pinot/query/QueryEnvironmentTest.java
@@ -68,18 +68,18 @@ public class QueryEnvironmentTest extends QueryEnvironmentTestBase {
         // table scan stages; for tableA it should have 2 hosts, for tableB it should have only 1
         Assert.assertEquals(
             e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
-            tables.get(0).equals("a") ? ImmutableList.of("Server_localhost_1", "Server_localhost_2")
-                : ImmutableList.of("Server_localhost_1"));
+            tables.get(0).equals("a") ? ImmutableList.of("localhost_1", "localhost_2")
+                : ImmutableList.of("localhost_1"));
       } else if (!PlannerUtils.isRootStage(e.getKey())) {
         // join stage should have both servers used.
         Assert.assertEquals(
             e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
-            ImmutableList.of("Server_localhost_1", "Server_localhost_2"));
+            ImmutableList.of("localhost_1", "localhost_2"));
       } else {
         // reduce stage should have the reducer instance.
         Assert.assertEquals(
             e.getValue().getServerInstances().stream().map(ServerInstance::toString).collect(Collectors.toList()),
-            ImmutableList.of("Server_localhost_3"));
+            ImmutableList.of("localhost_3"));
       }
     }
   }
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java
index 6cf9fddd12..238c1d807b 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/HashJoinOperator.java
@@ -43,7 +43,6 @@ import org.apache.pinot.query.runtime.blocks.DataTableBlockUtils;
  * <p>For each of the data block received from the left table, it will generate a joint data block.
  */
 public class HashJoinOperator extends BaseOperator<DataTableBlock> {
-  private static final String OPERATOR_NAME = "BroadcastJoinOperator";
   private static final String EXPLAIN_NAME = "BROADCAST_JOIN";
 
   private final HashMap<Object, List<Object[]>> _broadcastHashTable;
@@ -68,11 +67,6 @@ public class HashJoinOperator extends BaseOperator<DataTableBlock> {
     _broadcastHashTable = new HashMap<>();
   }
 
-  @Override
-  public String getOperatorName() {
-    return OPERATOR_NAME;
-  }
-
   @Override
   public List<Operator> getChildOperators() {
     // WorkerExecutor doesn't use getChildOperators, returns null here.
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java
index d15a9fc1db..7b396b13cc 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxReceiveOperator.java
@@ -44,7 +44,6 @@ import org.slf4j.LoggerFactory;
  */
 public class MailboxReceiveOperator extends BaseOperator<DataTableBlock> {
   private static final Logger LOGGER = LoggerFactory.getLogger(MailboxReceiveOperator.class);
-  private static final String OPERATOR_NAME = "MailboxReceiveOperator";
   private static final String EXPLAIN_NAME = "MAILBOX_RECEIVE";
   private static final long DEFAULT_TIMEOUT_NANO = 10_000_000_000L;
 
@@ -68,11 +67,6 @@ public class MailboxReceiveOperator extends BaseOperator<DataTableBlock> {
     _stageId = stageId;
   }
 
-  @Override
-  public String getOperatorName() {
-    return OPERATOR_NAME;
-  }
-
   @Override
   public List<Operator> getChildOperators() {
     // WorkerExecutor doesn't use getChildOperators, returns null here.
diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
index 3dd9c5ff77..51f24c97d4 100644
--- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
+++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java
@@ -48,7 +48,6 @@ import org.slf4j.LoggerFactory;
  */
 public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
   private static final Logger LOGGER = LoggerFactory.getLogger(MailboxSendOperator.class);
-  private static final String OPERATOR_NAME = "MailboxSendOperator";
   private static final String EXPLAIN_NAME = "MAILBOX_SEND";
   private static final Set<RelDistribution.Type> SUPPORTED_EXCHANGE_TYPE =
       ImmutableSet.of(RelDistribution.Type.SINGLETON, RelDistribution.Type.RANDOM_DISTRIBUTED,
@@ -101,11 +100,6 @@ public class MailboxSendOperator extends BaseOperator<DataTableBlock> {
     _stageId = stageId;
   }
 
-  @Override
-  public String getOperatorName() {
-    return OPERATOR_NAME;
-  }
-
   @Override
   public List<Operator> getChildOperators() {
     // WorkerExecutor doesn't use getChildOperators, returns null here.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org