Class QueryTreeNode

java.lang.Object
org.apache.derby.impl.sql.compile.QueryTreeNode
All Implemented Interfaces:
Visitable
Direct Known Subclasses:
JavaValueNode, MatchingClauseNode, OrderedColumn, Predicate, PrivilegeNode, QueryTreeNodeVector, ResultSetNode, StatementNode, TableElementNode, TableName, TablePrivilegesNode, ValueNode, WindowNode

public abstract class QueryTreeNode extends Object implements Visitable
QueryTreeNode is the root class for all query tree nodes. All query tree nodes inherit from QueryTreeNode except for those that extend QueryTreeNodeVector.
  • Field Details

    • AUTOINCREMENT_START_INDEX

      static final int AUTOINCREMENT_START_INDEX
      See Also:
    • AUTOINCREMENT_INC_INDEX

      static final int AUTOINCREMENT_INC_INDEX
      See Also:
    • AUTOINCREMENT_IS_AUTOINCREMENT_INDEX

      static final int AUTOINCREMENT_IS_AUTOINCREMENT_INDEX
      See Also:
    • AUTOINCREMENT_CREATE_MODIFY

      static final int AUTOINCREMENT_CREATE_MODIFY
      See Also:
    • AUTOINCREMENT_CYCLE

      static final int AUTOINCREMENT_CYCLE
      See Also:
    • beginOffset

      private int beginOffset
    • endOffset

      private int endOffset
    • cm

      private ContextManager cm
    • lcc

    • constantActionFactory

      private GenericConstantActionFactory constantActionFactory
    • visitableTags

      private ArrayList<String> visitableTags
    • isPrivilegeCollectionRequired

      private boolean isPrivilegeCollectionRequired
      In Derby SQL Standard Authorization, views, triggers and constraints execute with definer's privileges. Taking a specific eg of views user1 create table t1 (c11 int); create view v1 as select * from user1.t1; grant select on v1 to user2; user2 select * from user1.v1; Running with definer's privileges mean that since user2 has select privileges on view v1 owned by user1, then that is sufficient for user2 to do a select from view v1. View v1 underneath might access some objects that user2 doesn't have privileges on, but that is not a problem since views execute with definer's privileges. In order to implement this behavior, when doing a select from view v1, we only want to check for select privilege on view v1. While processing the underlying query for view v1, we want to stop collecting the privilege requirements for the query underneath. Following flag, isPrivilegeCollectionRequired is used for this purpose. The flag will be true when we are the top level of view and then it is turned off while we process the query underlying the view v1.
  • Constructor Details

  • Method Details

    • getContextManager

      final ContextManager getContextManager()
      Get the current ContextManager.
      Returns:
      The current ContextManager.
    • getOptimizerFactory

      public final OptimizerFactory getOptimizerFactory()
      Gets the NodeFactory for this database.
      Returns:
      the node factory for this database.
    • getOptimizerTracer

      public OptTrace getOptimizerTracer()
      Convenience method for finding the optimizer tracer
    • optimizerTracingIsOn

      public boolean optimizerTracingIsOn()
      Convenience method for checking whether optimizer tracing is on
    • getGenericConstantActionFactory

      public final GenericConstantActionFactory getGenericConstantActionFactory()
      Gets the constant action factory for this database.
      Returns:
      the constant action factory.
    • getExecutionFactory

      public final ExecutionFactory getExecutionFactory()
    • getClassFactory

      protected final ClassFactory getClassFactory()
      Get the ClassFactory to use with this database.
    • getLanguageConnectionContext

      protected final LanguageConnectionContext getLanguageConnectionContext()
      Gets the LanguageConnectionContext for this connection.
      Returns:
      the lcc for this connection
    • getBeginOffset

      public int getBeginOffset()
      Gets the beginning offset of the SQL substring which this query node represents.
      Returns:
      The beginning offset of the SQL substring. -1 means unknown.
    • setBeginOffset

      public void setBeginOffset(int beginOffset)
      Sets the beginning offset of the SQL substring which this query node represents.
      Parameters:
      beginOffset - The beginning offset of the SQL substring.
    • getEndOffset

      public int getEndOffset()
      Gets the ending offset of the SQL substring which this query node represents.
      Returns:
      The ending offset of the SQL substring. -1 means unknown.
    • setEndOffset

      public void setEndOffset(int endOffset)
      Sets the ending offset of the SQL substring which this query node represents.
      Parameters:
      endOffset - The ending offset of the SQL substring.
    • nodeHeader

      protected String nodeHeader()
      Return header information for debug printing of this query tree node.
      Returns:
      Header information for debug printing of this query tree node.
    • formatNodeString

      static String formatNodeString(String nodeString, int depth)
      Format a node that has been converted to a String for printing as part of a tree. This method indents the String to the given depth by inserting tabs at the beginning of the string, and also after every newline.
      Parameters:
      nodeString - The node formatted as a String
      depth - The depth to indent the given node
      Returns:
      The node String reformatted with tab indentation
    • treePrint

      public void treePrint()
      Print this tree for debugging purposes. This recurses through all the sub-nodes and prints them indented by their depth in the tree.
    • stackPrint

      void stackPrint()
      Print call stack for debug purposes
    • treePrint

      void treePrint(int depth)
      Print this tree for debugging purposes. This recurses through all the sub-nodes and prints them indented by their depth in the tree, starting with the given indentation.
      Parameters:
      depth - The depth of this node in the tree, thus, the amount to indent it when printing it.
    • containsInfo

      private static boolean containsInfo(String str)
    • debugPrint

      static void debugPrint(String outputString)
      Print a String for debugging
      Parameters:
      outputString - The String to print
    • debugFlush

      protected static void debugFlush()
      Flush the debug stream out
    • printSubNodes

      void printSubNodes(int depth)
      Print the sub-nodes of this node. Each sub-class of QueryTreeNode is expected to provide its own printSubNodes() method. In each case, it calls super.printSubNodes(), passing along its depth, to get the sub-nodes of the super-class. Then it prints its own sub-nodes by calling treePrint() on each of its members that is a type of QueryTreeNode. In each case where it calls treePrint(), it should pass "depth + 1" to indicate that the sub-node should be indented one more level when printing. Also, it should call printLabel() to print the name of each sub-node before calling treePrint() on the sub-node, so that the reader of the printed tree can tell what the sub-node is. This printSubNodes() exists in here merely to act as a backstop. In other words, the calls to printSubNodes() move up the type hierarchy, and in this node the calls stop. I would have liked to put the call to super.printSubNodes() in this super-class, but Java resolves "super" statically, so it wouldn't get to the right super-class.
      Parameters:
      depth - The depth to indent the sub-nodes
    • toString

      public String toString()
      Format this node as a string Each sub-class of QueryTreeNode should implement its own toString() method. In each case, toString() should format the class members that are not sub-types of QueryTreeNode (printSubNodes() takes care of following the references to sub-nodes, and toString() takes care of all members that are not sub-nodes). Newlines should be used liberally - one good way to do this is to have a newline at the end of each formatted member. It's also a good idea to put the name of each member in front of the formatted value. For example, the code might look like: "memberName: " + memberName + "\n" + ... Vector members containing subclasses of QueryTreeNode should subclass QueryTreeNodeVector. Such subclasses form a special case: These classes should not implement printSubNodes, since there is generic handling in QueryTreeNodeVector. They should only implement toString if they contain additional members.
      Overrides:
      toString in class Object
      Returns:
      This node formatted as a String
    • printLabel

      void printLabel(int depth, String label)
      Print the given label at the given indentation depth.
      Parameters:
      depth - The depth of indentation to use when printing the label
      label - The String to print
    • referencesSessionSchema

      public boolean referencesSessionSchema() throws StandardException
      Return true if the node references SESSION schema tables (temporary or permanent)
      Returns:
      true if references SESSION schema tables, else false
      Throws:
      StandardException - Thrown on error
    • isSessionSchema

      final boolean isSessionSchema(SchemaDescriptor sd)
      Checks if the passed schema descriptor is for SESSION schema
      Returns:
      true if the passed schema descriptor is for SESSION schema
      Throws:
      StandardException - Thrown on error
    • isSessionSchema

      static boolean isSessionSchema(String schemaName)
      Checks if the passed schema name is for SESSION schema
      Returns:
      true if the passed schema name is for SESSION schema
      Throws:
      StandardException - Thrown on error
    • disablePrivilegeCollection

      final void disablePrivilegeCollection()
      Triggers, constraints and views get executed with their definers' privileges and they can exist in the system only if their definers still have all the privileges to create them. Based on this, any time a trigger/view/constraint is executing, we do not need to waste time in checking if the definer still has the right set of privileges. At compile time, we will make sure that we do not collect the privilege requirement for objects accessed with definer privileges by calling the following method.
    • isPrivilegeCollectionRequired

      boolean isPrivilegeCollectionRequired() throws StandardException
      Return true from this method means that we need to collect privilege requirement for this node. For following cases, this method will return true. 1)execute view - collect privilege to access view but do not collect privilege requirements for objects accessed by actual view uqery 2)execute select - collect privilege requirements for objects accessed by select statement 3)create view - collect privileges for select statement : the select statement for create view falls under 2) category above.
      Returns:
      true if need to collect privilege requirement for this node
      Throws:
      StandardException
    • generate

      void generate(ActivationClassBuilder acb, MethodBuilder mb) throws StandardException
      Do the code generation for this node. This is a place-holder method - it should be over-ridden in the sub-classes.
      Parameters:
      acb - The ActivationClassBuilder for the class being built
      mb - The method for the generated code to go into
      Throws:
      StandardException - Thrown on error
    • getParameterTypes

      public DataTypeDescriptor[] getParameterTypes() throws StandardException
      Parameter info is stored in the compiler context. Hide this from the callers.
      Returns:
      null
      Throws:
      StandardException - on error
    • makeConstantAction

      public ConstantAction makeConstantAction() throws StandardException
      This creates a class that will do the work that's constant across all Executions of a PreparedStatement. It's up to our subclasses to override this method if they need to compile constant actions into PreparedStatements.
      Throws:
      StandardException - Thrown on failure
    • getDataDictionary

      public final DataDictionary getDataDictionary()
      Get the DataDictionary
      Returns:
      The DataDictionary
    • getDependencyManager

      final DependencyManager getDependencyManager()
    • getCompilerContext

      protected final CompilerContext getCompilerContext()
      Get the CompilerContext
      Returns:
      The CompilerContext
    • getTypeCompiler

      protected final TypeCompiler getTypeCompiler(TypeId typeId)
      Get the TypeCompiler associated with the given TypeId
      Parameters:
      typeId - The TypeId to get a TypeCompiler for
      Returns:
      The corresponding TypeCompiler
    • accept

      public final Visitable accept(Visitor v) throws StandardException
      Accept a visitor, and call v.visit() on child nodes as necessary. Sub-classes should not override this method, but instead override the acceptChildren(Visitor) method.
      Specified by:
      accept in interface Visitable
      Parameters:
      v - the visitor
      Throws:
      StandardException - on error
    • acceptChildren

      void acceptChildren(Visitor v) throws StandardException
      Accept a visitor on all child nodes. All sub-classes that add fields that should be visited, should override this method and call accept(v) on all visitable fields, as well as super.acceptChildren(v) to make sure all visitable fields defined by the super-class are accepted too.
      Parameters:
      v - the visitor
      Throws:
      StandardException - on errors raised by the visitor
    • addTag

      public void addTag(String tag)
      Description copied from interface: Visitable
      Add a tag to this Visitable.
      Specified by:
      addTag in interface Visitable
    • taggedWith

      public boolean taggedWith(String tag)
      Description copied from interface: Visitable
      Return true if this Visitable is tagged with the indicated tag.
      Specified by:
      taggedWith in interface Visitable
    • copyTagsFrom

      protected void copyTagsFrom(QueryTreeNode that)
      Copy the tags from another QueryTreeNode
    • getIntProperty

      protected int getIntProperty(String value, String key) throws StandardException
      Get the int value of a Property
      Parameters:
      value - Property value as a String
      key - Key value of property
      Returns:
      The int value of the property
      Throws:
      StandardException - Thrown on failure
    • getLongProperty

      protected long getLongProperty(String value, String key) throws StandardException
      Get the long value of a Property
      Parameters:
      value - Property value as a String
      key - Key value of property
      Returns:
      The long value of the property
      Throws:
      StandardException - Thrown on failure
    • parseStatement

      StatementNode parseStatement(String sql, boolean internalSQL) throws StandardException
      Parse the a SQL statement from the body of another SQL statement. Pushes and pops a separate CompilerContext to perform the compilation.
      Throws:
      StandardException
    • parseSearchCondition

      ValueNode parseSearchCondition(String sql, boolean internalSQL) throws StandardException
      Parse an SQL fragment that represents a <search condition>.
      Parameters:
      sql - a fragment of an SQL statement
      internalSQL - true if the SQL fragment is allowed to contain internal syntax, false otherwise
      Returns:
      a ValueNode representing the parse tree of the SQL fragment
      Throws:
      StandardException - if an error happens while parsing
    • parseStatementOrSearchCondition

      private Visitable parseStatementOrSearchCondition(String sql, boolean internalSQL, boolean isStatement) throws StandardException
      Parse a full SQL statement or a fragment representing a <search condition>. This is a worker method that contains common logic for parseStatement(java.lang.String, boolean) and parseSearchCondition(java.lang.String, boolean).
      Parameters:
      sql - the SQL statement or fragment to parse
      internalSQL - true if it is allowed to contain internal syntax, false otherwise
      isStatement - true if sql is a full SQL statement, false if it is a fragment
      Returns:
      a parse tree
      Throws:
      StandardException - if an error happens while parsing
    • getStatementType

      protected int getStatementType()
      Return the type of statement, something from StatementType.
      Returns:
      the type of statement
    • getNullNode

      Get a ConstantNode to represent a typed null value.
      Parameters:
      type - Type of the null node.
      Returns:
      A ConstantNode with the specified type, and a value of null
      Throws:
      StandardException - Thrown on error
    • convertDefaultNode

      DataValueDescriptor convertDefaultNode(DataTypeDescriptor typeDescriptor) throws StandardException
      Translate a Default node into a default value, given a type descriptor.
      Parameters:
      typeDescriptor - A description of the required data type.
      Throws:
      StandardException - Thrown on error
    • makeTableName

      public TableName makeTableName(String schemaName, String flatName) throws StandardException
      Throws:
      StandardException
    • makeTableName

      public static TableName makeTableName(ContextManager contextManager, String schemaName, String flatName) throws StandardException
      Throws:
      StandardException
    • isAtomic

      public boolean isAtomic() throws StandardException
      Throws:
      StandardException
    • getTableDescriptor

      protected final TableDescriptor getTableDescriptor(String tableName, SchemaDescriptor schema) throws StandardException
      Get the descriptor for the named table within the given schema. If the schema parameter is NULL, it looks for the table in the current (default) schema. Table descriptors include object ids, object types (table, view, etc.) If the schema is SESSION, then before looking into the data dictionary for persistent tables, it first looks into LCC for temporary tables. If no temporary table tableName found for the SESSION schema, then it goes and looks through the data dictionary for persistent table We added getTableDescriptor here so that we can look for non data dictionary tables(ie temp tables) here. Any calls to getTableDescriptor in data dictionary should be only for persistent tables
      Parameters:
      tableName - The name of the table to get the descriptor for
      schema - The descriptor for the schema the table lives in. If null, use the current (default) schema.
      Returns:
      The descriptor for the table, null if table does not exist.
      Throws:
      StandardException - Thrown on failure
    • getSchemaDescriptor

      final SchemaDescriptor getSchemaDescriptor(String schemaName) throws StandardException
      Get the descriptor for the named schema. If the schemaName parameter is NULL, it gets the descriptor for the current compilation schema. QueryTreeNodes must obtain schemas using this method or the two argument version of it. This is to ensure that the correct default compliation schema is returned and to allow determination of if the statement being compiled depends on the current schema. Schema descriptors include authorization ids and schema ids. SQL92 allows a schema to specify a default character set - we will not support this. Will check default schema for a match before scanning a system table.
      Parameters:
      schemaName - The name of the schema we're interested in. If the name is NULL, get the descriptor for the current compilation schema.
      Returns:
      The descriptor for the schema.
      Throws:
      StandardException - Thrown on error
    • getSchemaDescriptor

      final SchemaDescriptor getSchemaDescriptor(String schemaName, boolean raiseError) throws StandardException
      Get the descriptor for the named schema. If the schemaName parameter is NULL, it gets the descriptor for the current compilation schema. QueryTreeNodes must obtain schemas using this method or the single argument version of it. This is to ensure that the correct default compliation schema is returned and to allow determination of if the statement being compiled depends on the current schema.
      Parameters:
      schemaName - The name of the schema we're interested in. If the name is NULL, get the descriptor for the current compilation schema.
      raiseError - True to raise an error if the schema does not exist, false to return null if the schema does not exist.
      Returns:
      Valid SchemaDescriptor or null if raiseError is false and the schema does not exist.
      Throws:
      StandardException - Schema does not exist and raiseError is true.
    • resolveTableToSynonym

      TableName resolveTableToSynonym(TableName tabName) throws StandardException
      Resolve table/view reference to a synonym. May have to follow a synonym chain.
      Parameters:
      tabName - to match for a synonym
      Returns:
      Synonym TableName if a match is found, NULL otherwise.
      Throws:
      StandardException - Thrown on error
    • verifyClassExist

      void verifyClassExist(String javaClassName) throws StandardException
      Verify that a java class exists, is accessible (public) and not a class representing a primitive type.
      Parameters:
      javaClassName - The name of the java class to resolve.
      Throws:
      StandardException - Thrown on error
    • setRefActionInfo

      void setRefActionInfo(long fkIndexConglomId, int[] fkColArray, String parentResultSetId, boolean dependentScan)
      set the Information gathered from the parent table that is required to perform a referential action on dependent table.
    • generateAuthorizeCheck

      void generateAuthorizeCheck(ActivationClassBuilder acb, MethodBuilder mb, int sqlOperation)
      Add an authorization check into the passed in method.
    • checkReliability

      public void checkReliability(String fragmentType, int fragmentBitMask) throws StandardException
      Bind time logic. Raises an error if this ValueNode, once compiled, returns unstable results AND if we're in a context where unstable results are forbidden. Called by children who may NOT appear in the WHERE subclauses of ADD TABLE clauses.
      Parameters:
      fragmentType - Type of fragment as a String, for inclusion in error messages.
      fragmentBitMask - Type of fragment as a bitmask of possible fragment types
      Throws:
      StandardException - Thrown on error
    • checkReliability

      public void checkReliability(int fragmentBitMask, String fragmentType) throws StandardException
      Bind time logic. Raises an error if this ValueNode, once compiled, returns unstable results AND if we're in a context where unstable results are forbidden. Called by children who may NOT appear in the WHERE subclauses of ADD TABLE clauses.
      Parameters:
      fragmentBitMask - Type of fragment as a bitmask of possible fragment types
      fragmentType - Type of fragment as a String, to be fetch for the error message.
      Throws:
      StandardException - Thrown on error
    • bindUserType

      public DataTypeDescriptor bindUserType(DataTypeDescriptor originalDTD) throws StandardException
      Bind a UDT. This involves looking it up in the DataDictionary and filling in its class name.
      Parameters:
      originalDTD - A datatype: might be an unbound UDT and might not be
      Returns:
      The bound UDT if originalDTD was an unbound UDT; otherwise returns originalDTD.
      Throws:
      StandardException
    • bindUserCatalogType

      public TypeDescriptor bindUserCatalogType(TypeDescriptor td) throws StandardException
      Bind user defined types as necessary
      Throws:
      StandardException
    • getUDTDesc

      public AliasDescriptor getUDTDesc(DataTypeDescriptor dtd) throws StandardException
      Get the AliasDescriptor of a UDT
      Throws:
      StandardException
    • addUDTUsagePriv

      void addUDTUsagePriv(List<ValueNode> valueNodes) throws StandardException
      Add USAGE privilege for all UDTs mentioned in the indicated ValueNodes.
      Throws:
      StandardException
    • addUDTUsagePriv

      void addUDTUsagePriv(ValueNode val) throws StandardException
      Add USAGE privilege for a single UDT.
      Throws:
      StandardException
    • bindRowMultiSet

      public DataTypeDescriptor bindRowMultiSet(DataTypeDescriptor originalDTD) throws StandardException
      Bind the UDTs in a table type.
      Parameters:
      originalDTD - A datatype: might be an unbound UDT and might not be
      Returns:
      The bound table type if originalDTD was an unbound table type; otherwise returns originalDTD.
      Throws:
      StandardException
    • createTypeDependency

      public void createTypeDependency(DataTypeDescriptor dtd) throws StandardException
      Declare a dependency on a type and check that you have privilege to use it. This is only used if the type is an ANSI UDT.
      Parameters:
      dtd - Type which may have a dependency declared on it.
      Throws:
      StandardException
    • createTypeDependency

      private void createTypeDependency(AliasDescriptor ad) throws StandardException
      Declare a dependency on an ANSI UDT, identified by its AliasDescriptor, and check that you have privilege to use it.
      Throws:
      StandardException
    • throwReliabilityException

      private void throwReliabilityException(String fragmentType, int fragmentBitMask) throws StandardException
      Common code for the 2 checkReliability functions. Always throws StandardException.
      Parameters:
      fragmentType - Type of fragment as a string, for inclusion in error messages.
      fragmentBitMask - Describes the kinds of expressions we ar suspicious of
      Throws:
      StandardException - Throws an error, always.
    • orReliability

      public int orReliability(int newBits)
      OR in more reliability bits and return the old reliability value.
    • bindOffsetFetch

      public static void bindOffsetFetch(ValueNode offset, ValueNode fetchFirst) throws StandardException
      Bind the parameters of OFFSET n ROWS and FETCH FIRST n ROWS ONLY, if any.
      Parameters:
      offset - the OFFSET parameter, if any
      fetchFirst - the FETCH parameter, if any
      Throws:
      StandardException - Thrown on error
    • getOffsetOrderedNodes

      public <N extends QueryTreeNode> SortedSet<N> getOffsetOrderedNodes(Class<N> type) throws StandardException
      Get all child nodes of a specific type, and return them in the order in which they appear in the SQL text.
      Type Parameters:
      N - the type of node to look for
      Parameters:
      type - the type of node to look for
      Returns:
      all nodes of the specified type
      Throws:
      StandardException - if an error occurs
    • getContext

      static Context getContext(String contextID)
      Privileged lookup of a Context. Must be package protected so that user code can't call this entry point.