123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857 |
- #include "mkql_node_printer.h"
- #include "mkql_node_visitor.h"
- #include <util/stream/str.h>
- #include <yql/essentials/public/udf/udf_types.h>
- #include <yql/essentials/parser/pg_catalog/catalog.h>
- namespace NKikimr {
- namespace NMiniKQL {
- namespace {
- class TPrintVisitor : public INodeVisitor {
- friend class TIndentScope;
- public:
- struct TIndentScope {
- TIndentScope(TPrintVisitor* self)
- : Self(self)
- {
- ++Self->Indent;
- }
- ~TIndentScope() {
- --Self->Indent;
- }
- TPrintVisitor* Self;
- };
- TPrintVisitor(bool singleLine)
- : SingleLine(singleLine)
- , Indent(0)
- {}
- void Visit(TTypeType& node) override {
- Y_UNUSED(node);
- WriteIndentation();
- Out << "Type (Type)";
- WriteNewline();
- }
- void Visit(TVoidType& node) override {
- Y_UNUSED(node);
- WriteIndentation();
- Out << "Type (Void) ";
- WriteNewline();
- }
- void Visit(TNullType& node) override {
- Y_UNUSED(node);
- WriteIndentation();
- Out << "Type (Null) ";
- WriteNewline();
- }
- void Visit(TEmptyListType& node) override {
- Y_UNUSED(node);
- WriteIndentation();
- Out << "Type (EmptyList) ";
- WriteNewline();
- }
- void Visit(TEmptyDictType& node) override {
- Y_UNUSED(node);
- WriteIndentation();
- Out << "Type (EmptyDict) ";
- WriteNewline();
- }
- void Visit(TDataType& node) override {
- WriteIndentation();
- auto slot = NUdf::FindDataSlot(node.GetSchemeType());
- Out << "Type (Data), schemeType: ";
- if (slot) {
- Out << GetDataTypeInfo(*slot).Name;
- if (node.GetSchemeType() == NUdf::TDataType<NUdf::TDecimal>::Id) {
- const auto params = static_cast<TDataDecimalType&>(node).GetParams();
- Out << '(' << int(params.first) << ',' << int(params.second) << ')';
- }
- } else {
- Out << "<" << node.GetSchemeType() << ">";
- }
- Out << ", schemeTypeId: ";
- Out << node.GetSchemeType();
- WriteNewline();
- }
- void Visit(TPgType& node) override {
- WriteIndentation();
- Out << "Type (Pg), name: " << NYql::NPg::LookupType(node.GetTypeId()).Name;
- WriteNewline();
- }
- void Visit(TStructType& node) override {
- WriteIndentation();
- Out << "Type (Struct) with " << node.GetMembersCount() << " members {";
- WriteNewline();
- {
- TIndentScope scope(this);
- for (ui32 index = 0; index < node.GetMembersCount(); ++index) {
- WriteIndentation();
- Out << "Member [" << node.GetMemberName(index) << "] : {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetMemberType(index)->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TListType& node) override {
- WriteIndentation();
- Out << "Type (List) {";
- WriteNewline();
- {
- TIndentScope scope(this);
- WriteIndentation();
- Out << "List item type: {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetItemType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TStreamType& node) override {
- WriteIndentation();
- Out << "Type (Stream) {";
- WriteNewline();
- {
- TIndentScope scope(this);
- WriteIndentation();
- Out << "Stream item type: {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetItemType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TFlowType& node) override {
- WriteIndentation();
- Out << "Type (Flow) {";
- WriteNewline();
- {
- TIndentScope scope(this);
- WriteIndentation();
- Out << "Flow item type: {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetItemType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TBlockType& node) override {
- WriteIndentation();
- Out << "Type (Block) {";
- WriteNewline();
- {
- TIndentScope scope(this);
- WriteIndentation();
- Out << "Block item type: {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetItemType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- WriteIndentation();
- Out << "Block shape: " << (node.GetShape() == TBlockType::EShape::Scalar ? "Scalar" : "Many");
- WriteNewline();
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TOptionalType& node) override {
- WriteIndentation();
- Out << "Type (Optional) {";
- WriteNewline();
- {
- TIndentScope scope(this);
- WriteIndentation();
- Out << "Optional item type: {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetItemType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TDictType& node) override {
- WriteIndentation();
- Out << "Type (Dict) {";
- WriteNewline();
- {
- TIndentScope scope(this);
- WriteIndentation();
- Out << "Key type: {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetKeyType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- WriteIndentation();
- Out << "Payload type: {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetPayloadType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TCallableType& node) override {
- WriteIndentation();
- Out << "Type (Callable), name: [" << node.GetName() << "] with " << node.GetArgumentsCount() << " args {";
- WriteNewline();
- {
- TIndentScope scope(this);
- WriteIndentation();
- Out << "Return type";
- if (node.IsMergeDisabled())
- Out << ", merge disabled";
- if (node.GetOptionalArgumentsCount() != 0)
- Out << ", optional args: " << node.GetOptionalArgumentsCount();
- Out << " : {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetReturnType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- for (ui32 index = 0; index < node.GetArgumentsCount(); ++index) {
- WriteIndentation();
- const auto& type = node.GetArgumentType(index);
- Out << "Argument #" << index << " : {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- type->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- if (node.GetPayload()) {
- WriteIndentation();
- Out << "Payload: {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetPayload()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TAnyType& node) override {
- Y_UNUSED(node);
- WriteIndentation();
- Out << "Type (Any) ";
- WriteNewline();
- }
- template<typename T>
- void VisitTupleLike(T& node, std::string_view name) {
- WriteIndentation();
- Out << "Type (" << name << ") with " << node.GetElementsCount() << " elements {";
- WriteNewline();
- {
- TIndentScope scope(this);
- for (ui32 index = 0; index < node.GetElementsCount(); ++index) {
- WriteIndentation();
- Out << "#" << index << " : {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetElementType(index)->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TTupleType& node) override {
- VisitTupleLike(node, "Tuple");
- }
- void Visit(TMultiType& node) override {
- VisitTupleLike(node, "Multi");
- }
- void Visit(TResourceType& node) override {
- Y_UNUSED(node);
- WriteIndentation();
- Out << "Type (Resource) (" << node.GetTag() << ")";
- WriteNewline();
- }
- void Visit(TVariantType& node) override {
- WriteIndentation();
- Out << "Type (Variant) {";
- WriteNewline();
- {
- TIndentScope scope(this);
- WriteIndentation();
- Out << "Underlying type: {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetUnderlyingType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TVoid& node) override {
- WriteIndentation();
- Out << "Void {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TNull& node) override {
- WriteIndentation();
- Out << "Null {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TEmptyList& node) override {
- WriteIndentation();
- Out << "EmptyList {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TEmptyDict& node) override {
- WriteIndentation();
- Out << "EmptyDict {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TDataLiteral& node) override {
- WriteIndentation();
- Out << "Data {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- WriteIndentation();
- if (node.GetType()->GetSchemeType() == 0) {
- Out << "null";
- WriteNewline();
- } else {
- Out << TString(node.AsValue().AsStringRef()).Quote();
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TStructLiteral& node) override {
- WriteIndentation();
- Out << "Struct {";
- WriteNewline();
- {
- TIndentScope scope(this);
- for (ui32 index = 0; index < node.GetValuesCount(); ++index) {
- WriteIndentation();
- const auto& value = node.GetValue(index);
- Out << "Member [" << node.GetType()->GetMemberName(index) << "], "
- << (value.IsImmediate() ? "immediate" : "not immediate") << " {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- value.GetNode()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TListLiteral& node) override {
- WriteIndentation();
- Out << "List with " << node.GetItemsCount() << " items {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- ui32 index = 0;
- for (ui32 i = 0; i < node.GetItemsCount(); ++i) {
- WriteIndentation();
- const auto& item = node.GetItems()[i];
- Out << "Item #" << index << ", " << (item.IsImmediate() ? "immediate" : "not immediate") << " {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- item.GetNode()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TOptionalLiteral& node) override {
- WriteIndentation();
- Out << "Optional " << (node.HasItem() ? "with data" : "empty") << " {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- if (node.HasItem()) {
- WriteIndentation();
- const auto& item = node.GetItem();
- Out << "Item " << (item.IsImmediate() ? "immediate" : "not immediate") << " {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- item.GetNode()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TDictLiteral& node) override {
- WriteIndentation();
- Out << "Dict with " << node.GetItemsCount() << " items {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- for (ui32 index = 0; index < node.GetItemsCount(); ++index) {
- WriteIndentation();
- const auto& item = node.GetItem(index);
- Out << "Key of item #" << index << ", " << (item.first.IsImmediate() ? "immediate" : "not immediate") << " {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- item.first.GetNode()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- WriteIndentation();
- Out << "Payload of item #" << index << ", " << (item.second.IsImmediate() ? "immediate" : "not immediate") << " {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- item.second.GetNode()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TCallable& node) override {
- WriteIndentation();
- Out << "Callable";
- if (node.GetUniqueId() != 0)
- Out << ", uniqueId: " << node.GetUniqueId();
- Out << " {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- for (ui32 index = 0; index < node.GetInputsCount(); ++index) {
- WriteIndentation();
- const auto& input = node.GetInput(index);
- Out << "Input #" << index << ", " << (input.IsImmediate() ? "immediate" : "not immediate") << " {";
- WriteNewline();
- {
- TIndentScope scope3(this);
- input.GetNode()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- if (node.HasResult()) {
- WriteIndentation();
- Out << "Result, " << (node.GetResult().IsImmediate() ? "immediate" : "not immediate") << " {";
- WriteNewline();
- {
- TIndentScope scope3(this);
- node.GetResult().GetNode()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TAny& node) override {
- WriteIndentation();
- Out << "Any " << (node.HasItem() ? "with data" : "empty") << " {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- if (node.HasItem()) {
- WriteIndentation();
- const auto& item = node.GetItem();
- Out << "Item " << (item.IsImmediate() ? "immediate" : "not immediate") << " {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- item.GetNode()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TTupleLiteral& node) override {
- WriteIndentation();
- Out << "Tuple {";
- WriteNewline();
- {
- TIndentScope scope(this);
- for (ui32 index = 0; index < node.GetValuesCount(); ++index) {
- WriteIndentation();
- const auto& value = node.GetValue(index);
- Out << "#" << index << " " << (value.IsImmediate() ? "immediate" : "not immediate") << " {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- value.GetNode()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TVariantLiteral& node) override {
- WriteIndentation();
- Out << "Variant with alternative " << node.GetIndex() << " {";
- WriteNewline();
- {
- TIndentScope scope(this);
- node.GetType()->Accept(*this);
- WriteIndentation();
- const auto& item = node.GetItem();
- Out << "Item " << (item.IsImmediate() ? "immediate" : "not immediate") << " {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- item.GetNode()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- void Visit(TTaggedType& node) override {
- WriteIndentation();
- Out << "Type (Tagged) (" << node.GetTag() << ") {";
- WriteNewline();
- {
- TIndentScope scope(this);
- WriteIndentation();
- Out << "Tagged base type: {";
- WriteNewline();
- {
- TIndentScope scope2(this);
- node.GetBaseType()->Accept(*this);
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- WriteIndentation();
- Out << "}";
- WriteNewline();
- }
- TString ToString() {
- return Out.Str();
- }
- private:
- void WriteIndentation() {
- if (SingleLine) {
- } else {
- for (ui32 i = 0; i < 2 * Indent; ++i) {
- Out << ' ';
- }
- }
- }
- void WriteNewline() {
- if (SingleLine) {
- Out << ' ';
- } else {
- Out << '\n';
- }
- }
- private:
- const bool SingleLine;
- TStringStream Out;
- ui32 Indent;
- };
- }
- TString PrintNode(const TNode* node, bool singleLine) {
- TPrintVisitor visitor(singleLine);
- const_cast<TNode*>(node)->Accept(visitor);
- return visitor.ToString();
- }
- }
- }
- template <>
- void Out<NKikimr::NMiniKQL::TType>(
- IOutputStream& os,
- TTypeTraits<NKikimr::NMiniKQL::TType>::TFuncParam t
- )
- {
- os << PrintNode(&t, true);
- }
|