Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast support to generic #1417

Closed

Conversation

laithsakka
Copy link
Contributor

Summary:

  • This diff adds the support of type access and cast to generics.

  • It introduces 4 functions that can be called on the GenericView:

    • type()
    • kind()
    • castTo: performs an unchecked cast and returns arg_type. A safety debug time type
      check will happen.
    • tryCastTo: return std::optional<returns arg_type>, performs unchecked cast. return
      std::null opt if T does not match the type of the vector.
  • Cost:

    • The first time we do the cast we create the readers corresponding to that type. Then for the
      coming rows, the cost is a couple of instructions; checking reader is created, accessing the reader
      in the variant and returning the element at the row index.
    • TryCastTo is more expensive, since it does a type check as well.
    • In general its not expensive to use it with complex types, but avoid using it with primitives by either
      implementing a function specialized when input is primitive (see == function as example ). Or casting to
      complex types already specialized with primitive types, i.e. Array Array instead of Array
      and then casting Any to int for every element.
  • What can be casted to?
    This diff enabled cast to all basic types plus Array Map<Any, Any>, Row, Row<Any, Any> and
    Row<Any, Any,..etc> up to 5.. This allow to recursively traverse complex types.

  • How to add a new casted to type?

    • its a one line code change, just add the type to readers_variant_t e.g. you can add reader_t<Array>,
      then someone can cast or tryCast to Array directly instead of casting to Array and then casting the
      each inner element.
    • We can allow user to pass additional types through the generic itself instead of changing readers_variant_t.
      Ex: allowing the user to say Generic<T, Array, std::shared_ptr..etc> where those are things that this
      generic can be casted to in addition to the basic types. (this is not done yet but future work).
  • The diff adds example function HasDuplicate, which checks if an array has duplicate items.

Differential Revision: D35616634

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported labels Apr 13, 2022
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D35616634

laithsakka added a commit to laithsakka/velox that referenced this pull request Apr 14, 2022
Summary:
Pull Request resolved: facebookincubator#1417

- This diff adds the support of type access and cast to generics.
- It introduces 4 functions that can be called on the GenericView:
   - type()
   - kind()
   -  castTo<T>: performs an unchecked cast and returns arg_type<T>. A safety debug time type
      check will happen.
   -  tryCastTo<T>: return std::optional<returns arg_type<T>>, performs  unchecked cast. return
      std::null opt if T does not match the type of the vector.

- **Cost**:
   - The first time we do the cast we create the readers corresponding to that type. Then for the
coming rows, the cost is a couple of instructions; checking reader is created,  accessing the reader
in the variant and returning the element at the row index.
  - TryCastTo is more expensive, since it does a type check as well.
  - In general its not expensive to use it with complex types, but avoid using it with primitives by either
implementing a function specialized when input is primitive (see == function as example ).  Or casting to
complex types already specialized with primitive types, i.e. Array<int> Array<double> instead of Array<Any>
 and then casting Any to int for every element.

- **What can be casted to?**
This diff enabled cast to all basic types plus Array<Any> Map<Any, Any>, Row<Any>, Row<Any, Any> and
Row<Any, Any,..etc> up to 5.. This allow to recursively traverse complex types.

- **How to add a new casted to type?**
  - its a one line code change, just add the type to readers_variant_t e.g. you can add reader_t<Array<int>>,
 then someone can cast or tryCast to Array<int> directly instead of casting to Array<Any> and then casting the
 each inner element.
  - We can allow user to pass additional types through the generic itself instead of changing readers_variant_t.
Ex: allowing the user to say Generic<T, Array<int>, std::shared_ptr<X>..etc> where those are things that this
generic can be casted to in addition to the basic types. (this is not done yet but future work).

- The diff adds example function HasDuplicate, which checks if an array has duplicate items.

Differential Revision: D35616634

fbshipit-source-id: d75cebc56a89fb13a1e8b75e5be37b2ebd809ced
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D35616634

laithsakka added a commit to laithsakka/velox that referenced this pull request Apr 16, 2022
Summary:
Pull Request resolved: facebookincubator#1417

- This diff adds the support of type access and cast to generics.
- It introduces 4 functions that can be called on the GenericView:
   - type()
   - kind()
   -  castTo<T>: performs an unchecked cast and returns arg_type<T>. A safety debug time type
      check will happen.
   -  tryCastTo<T>: return std::optional<returns arg_type<T>>, performs  unchecked cast. return
      std::null opt if T does not match the type of the vector.

- **Cost**:
   - The first time we do the cast we create the readers corresponding to that type. Then for the
coming rows, the cost is a couple of instructions; checking reader is created,  accessing the reader
in the variant and returning the element at the row index. In some non-common cases there is additional
check that the type casted to is consistent across rows.
  - TryCastTo is more expensive, since it does a type check as well.
  - In general its not expensive to use it with complex types, but avoid using it with primitives by either
implementing a function specialized when input is primitive (see == function as example ).  Or casting to
complex types already specialized with primitive types, i.e. Array<int> Array<double> instead of Array<Any>
 and then casting Any to int for every element.

- **What can be casted to?**
This diff enabled cast to all basic types plus Array<Any> Map<Any, Any>, Row<Any>, Row<Any, Any> and
Row<Any, Any,..etc> up to 5.. This allow to recursively traverse complex types.

- **How to add a new casted to type?**
  - Any type except Generic<> it self, (Cast to self type) or Variadic<...>.

- The diff adds example function HasDuplicate, which checks if an array has duplicate items.

Differential Revision: D35616634

fbshipit-source-id: 203170e4b680032b0616cc6aee89305dbc57a61f
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D35616634

Summary:
Pull Request resolved: facebookincubator#1417

- This diff adds the support of type access and cast to generics.
- It introduces 4 functions that can be called on the GenericView:
   - type()
   - kind()
   -  castTo<T>: performs an unchecked cast and returns arg_type<T>. A safety debug time type
      check will happen.
   -  tryCastTo<T>: return std::optional<returns arg_type<T>>, performs  unchecked cast. return
      std::null opt if T does not match the type of the vector.

- **Cost**:
   - The first time we do the cast we create the readers corresponding to that type. Then for the
coming rows, the cost is a couple of instructions; checking reader is created,  accessing the reader
in the variant and returning the element at the row index. In some non-common cases there is additional
check that the type casted to is consistent across rows.
  - TryCastTo is more expensive, since it does a type check as well.
  - In general its not expensive to use it with complex types, but avoid using it with primitives by either
implementing a function specialized when input is primitive (see == function as example ).  Or casting to
complex types already specialized with primitive types, i.e. Array<int> Array<double> instead of Array<Any>
 and then casting Any to int for every element.

- **What can be casted to?**
This diff enabled cast to all basic types plus Array<Any> Map<Any, Any>, Row<Any>, Row<Any, Any> and
Row<Any, Any,..etc> up to 5.. This allow to recursively traverse complex types.

- **How to add a new casted to type?**
  - Any type except Generic<> it self, (Cast to self type) or Variadic<...>.

- The diff adds example function HasDuplicate, which checks if an array has duplicate items.

Reviewed By: kevinwilfong

Differential Revision: D35616634

fbshipit-source-id: f33bba0cd5ed81c8352085cbf4a8b196f85c5a8b
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D35616634

Arty-Maly pushed a commit to Arty-Maly/velox that referenced this pull request May 13, 2022
Summary:
Pull Request resolved: facebookincubator#1417

- This diff adds the support of type access and cast to generics.
- It introduces 4 functions that can be called on the GenericView:
   - type()
   - kind()
   -  castTo<T>: performs an unchecked cast and returns arg_type<T>. A safety debug time type
      check will happen.
   -  tryCastTo<T>: return std::optional<returns arg_type<T>>, performs  unchecked cast. return
      std::null opt if T does not match the type of the vector.

- **Cost**:
   - The first time we do the cast we create the readers corresponding to that type. Then for the
coming rows, the cost is a couple of instructions; checking reader is created,  accessing the reader
in the variant and returning the element at the row index. In some non-common cases there is additional
check that the type casted to is consistent across rows.
  - TryCastTo is more expensive, since it does a type check as well.
  - In general its not expensive to use it with complex types, but avoid using it with primitives by either
implementing a function specialized when input is primitive (see == function as example ).  Or casting to
complex types already specialized with primitive types, i.e. Array<int> Array<double> instead of Array<Any>
 and then casting Any to int for every element.

- **What can be casted to?**
This diff enabled cast to all basic types plus Array<Any> Map<Any, Any>, Row<Any>, Row<Any, Any> and
Row<Any, Any,..etc> up to 5.. This allow to recursively traverse complex types.

- **How to add a new casted to type?**
  - Any type except Generic<> it self, (Cast to self type) or Variadic<...>.

- The diff adds example function HasDuplicate, which checks if an array has duplicate items.

Reviewed By: kevinwilfong

Differential Revision: D35616634

fbshipit-source-id: 847af1dfc3af9b49ce32386b05bef585be955a81
shiyu-bytedance pushed a commit to shiyu-bytedance/velox-1 that referenced this pull request Aug 18, 2022
Summary:
Pull Request resolved: facebookincubator#1417

- This diff adds the support of type access and cast to generics.
- It introduces 4 functions that can be called on the GenericView:
   - type()
   - kind()
   -  castTo<T>: performs an unchecked cast and returns arg_type<T>. A safety debug time type
      check will happen.
   -  tryCastTo<T>: return std::optional<returns arg_type<T>>, performs  unchecked cast. return
      std::null opt if T does not match the type of the vector.

- **Cost**:
   - The first time we do the cast we create the readers corresponding to that type. Then for the
coming rows, the cost is a couple of instructions; checking reader is created,  accessing the reader
in the variant and returning the element at the row index. In some non-common cases there is additional
check that the type casted to is consistent across rows.
  - TryCastTo is more expensive, since it does a type check as well.
  - In general its not expensive to use it with complex types, but avoid using it with primitives by either
implementing a function specialized when input is primitive (see == function as example ).  Or casting to
complex types already specialized with primitive types, i.e. Array<int> Array<double> instead of Array<Any>
 and then casting Any to int for every element.

- **What can be casted to?**
This diff enabled cast to all basic types plus Array<Any> Map<Any, Any>, Row<Any>, Row<Any, Any> and
Row<Any, Any,..etc> up to 5.. This allow to recursively traverse complex types.

- **How to add a new casted to type?**
  - Any type except Generic<> it self, (Cast to self type) or Variadic<...>.

- The diff adds example function HasDuplicate, which checks if an array has duplicate items.

Reviewed By: kevinwilfong

Differential Revision: D35616634

fbshipit-source-id: 847af1dfc3af9b49ce32386b05bef585be955a81
marin-ma pushed a commit to marin-ma/velox-oap that referenced this pull request Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants