Returns whether a node is a literal. Examples: For CAST(literal AS type ) , returns true if allowCast is true, false otherwise. For CAST(CAST(literal AS type ) AS type )) , returns false. @param node The node, never nul
(SqlNode node, boolean allowCast)
| 243 | * @return Whether the node is a literal |
| 244 | */ |
| 245 | public static boolean isLiteral(SqlNode node, boolean allowCast) { |
| 246 | requireNonNull(node, "node"); |
| 247 | if (node instanceof SqlLiteral) { |
| 248 | return true; |
| 249 | } |
| 250 | if (!allowCast) { |
| 251 | return false; |
| 252 | } |
| 253 | switch (node.getKind()) { |
| 254 | case CAST: |
| 255 | // "CAST(e AS type)" is literal if "e" is literal |
| 256 | return isLiteral(((SqlCall) node).operand(0), true); |
| 257 | case MAP_VALUE_CONSTRUCTOR: |
| 258 | case ARRAY_VALUE_CONSTRUCTOR: |
| 259 | return ((SqlCall) node).getOperandList().stream() |
| 260 | .allMatch(o -> isLiteral(o, true)); |
| 261 | case DEFAULT: |
| 262 | return true; // DEFAULT is always NULL |
| 263 | default: |
| 264 | return false; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Returns whether a node is a literal. |