Class DruidQuery

    • Field Detail

      • DEFAULT_OPERATORS_LIST

        public static final java.util.List<DruidSqlOperatorConverter> DEFAULT_OPERATORS_LIST
        Provides a standard list of supported Calcite operators that can be converted to Druid Expressions. This can be used as is or re-adapted based on underline engine operator syntax.
      • intervals

        final com.google.common.collect.ImmutableList<org.joda.time.Interval> intervals
      • rels

        final com.google.common.collect.ImmutableList<RelNode> rels
      • converterOperatorMap

        final java.util.Map<SqlOperator,​DruidSqlOperatorConverter> converterOperatorMap
        This operator map provides DruidSqlOperatorConverter instance to convert a Calcite RexNode to Druid Expression when possible.
      • VALID_SIG

        private static final java.util.regex.Pattern VALID_SIG
      • EXTRACT_COLUMN_NAME_PREFIX

        private static final java.lang.String EXTRACT_COLUMN_NAME_PREFIX
        See Also:
        Constant Field Values
      • FLOOR_COLUMN_NAME_PREFIX

        private static final java.lang.String FLOOR_COLUMN_NAME_PREFIX
        See Also:
        Constant Field Values
      • DRUID_QUERY_FETCH

        protected static final java.lang.String DRUID_QUERY_FETCH
        See Also:
        Constant Field Values
    • Constructor Detail

      • DruidQuery

        protected DruidQuery​(RelOptCluster cluster,
                             RelTraitSet traitSet,
                             RelOptTable table,
                             DruidTable druidTable,
                             java.util.List<org.joda.time.Interval> intervals,
                             java.util.List<RelNode> rels,
                             java.util.Map<SqlOperator,​DruidSqlOperatorConverter> converterOperatorMap)
        Creates a DruidQuery.
        Parameters:
        cluster - Cluster
        traitSet - Traits
        table - Table
        druidTable - Druid table
        intervals - Intervals for the query
        rels - Internal relational expressions
        converterOperatorMap - mapping of Calcite Sql Operator to Druid Expression API.
    • Method Detail

      • isValidSignature

        static boolean isValidSignature​(java.lang.String signature)
        Returns whether a signature represents an sequence of relational operators that can be translated into a valid Druid query.
      • extendQuery

        public static DruidQuery extendQuery​(DruidQuery query,
                                             java.util.List<org.joda.time.Interval> intervals)
        Extends a DruidQuery.
      • toDruidColumn

        protected static Pair<java.lang.String,​ExtractionFunction> toDruidColumn​(RexNode rexNode,
                                                                                       RelDataType rowType,
                                                                                       DruidQuery druidQuery)
        Parameters:
        rexNode - leaf Input Ref to Druid Column
        rowType - row type
        druidQuery - druid query
        Returns:
        Pair of Column name and Extraction Function on the top of the input ref or of(null, null) when can not translate to valid Druid column
      • isValidLeafCast

        private static boolean isValidLeafCast​(RexNode rexNode)
        Parameters:
        rexNode - rexNode
        Returns:
        true if the operand is an inputRef and it is a valid Druid Cast operation
      • extractColumnName

        @Nullable
        protected static java.lang.String extractColumnName​(RexNode rexNode,
                                                            RelDataType rowType,
                                                            DruidQuery query)
        Parameters:
        rexNode - Druid input ref node
        rowType - rowType
        query - Druid Query
        Returns:
        Druid column name or null when not possible to translate.
      • format

        public static java.lang.String format​(java.lang.String message,
                                              java.lang.Object... formatArgs)
        Equivalent of String.format(Locale.ENGLISH, message, formatArgs).
      • isValid

        public boolean isValid​(Litmus litmus,
                               RelNode.Context context)
        Description copied from interface: RelNode
        Returns whether this relational expression is valid.

        If assertions are enabled, this method is typically called with litmus = THROW, as follows:

        assert rel.isValid(Litmus.THROW)

        This signals that the method can throw an AssertionError if it is not valid.

        Specified by:
        isValid in interface RelNode
        Overrides:
        isValid in class AbstractRelNode
        Parameters:
        litmus - What to do if invalid
        context - Context for validity checking
        Returns:
        Whether relational expression is valid
      • copy

        public RelNode copy​(RelTraitSet traitSet,
                            java.util.List<RelNode> inputs)
        Description copied from interface: RelNode
        Creates a copy of this relational expression, perhaps changing traits and inputs.

        Sub-classes with other important attributes are encouraged to create variants of this method with more parameters.

        Specified by:
        copy in interface RelNode
        Overrides:
        copy in class AbstractRelNode
        Parameters:
        traitSet - Trait set
        inputs - Inputs
        Returns:
        Copy of this relational expression, substituting traits and inputs
      • getTableScan

        public TableScan getTableScan()
      • getTopNode

        public RelNode getTopNode()
      • getTable

        public RelOptTable getTable()
        Description copied from interface: RelNode
        If this relational expression represents an access to a table, returns that table, otherwise returns null.
        Specified by:
        getTable in interface RelNode
        Overrides:
        getTable in class AbstractRelNode
        Returns:
        If this relational expression represents an access to a table, returns that table, otherwise returns null
      • getDruidTable

        public DruidTable getDruidTable()
      • getIntervalCostMultiplier

        private double getIntervalCostMultiplier()
      • getQueryTypeCostMultiplier

        private double getQueryTypeCostMultiplier()
      • register

        public void register​(RelOptPlanner planner)
        Description copied from interface: RelNode
        Registers any special rules specific to this kind of relational expression.

        The planner calls this method this first time that it sees a relational expression of this class. The derived class should call RelOptPlanner.addRule(org.apache.calcite.plan.RelOptRule) for each rule, and then call super.register.

        Specified by:
        register in interface RelNode
        Overrides:
        register in class AbstractRelNode
        Parameters:
        planner - Planner to be used to register additional relational expressions
      • getElementType

        public java.lang.Class<java.lang.Object[]> getElementType()
        Description copied from interface: Typed
        Gets the type of the element(s) that are returned in this collection.
        Specified by:
        getElementType in interface ArrayBindable
        Specified by:
        getElementType in interface Typed
      • bind

        public Enumerable<java.lang.Object[]> bind​(DataContext dataContext)
        Description copied from interface: Bindable
        Executes this statement and returns an enumerable which will yield rows. The environment parameter provides the values in the root of the environment (usually schemas).
        Specified by:
        bind in interface Bindable<java.lang.Object[]>
        Parameters:
        dataContext - Environment that provides tables
        Returns:
        Enumerable over rows
      • getQueryType

        public QueryType getQueryType()
      • getQueryString

        public java.lang.String getQueryString()
      • computeFilter

        @Nullable
        private static DruidJsonFilter computeFilter​(@Nullable
                                                     Filter filterRel,
                                                     DruidQuery druidQuery)
        Translates Filter rel to Druid Filter Json object if possible. Currently Filter rel input has to be Druid Table scan
        Parameters:
        filterRel - input filter rel
        druidQuery - Druid query
        Returns:
        DruidJson Filter or null if can not translate one of filters
      • computeProjectAsScan

        @Nullable
        protected static Pair<java.util.List<java.lang.String>,​java.util.List<VirtualColumn>> computeProjectAsScan​(@Nullable
                                                                                                                         Project projectRel,
                                                                                                                         RelDataType inputRowType,
                                                                                                                         DruidQuery druidQuery)
        Translates list of projects to Druid Column names and Virtual Columns if any We can not use Pair.zip(Object[], Object[]), since size can be different
        Parameters:
        projectRel - Project Rel
        druidQuery - Druid query
        Returns:
        Pair of list of Druid Columns and Expression Virtual Columns or null when can not translate one of the projects.
      • computeProjectGroupSet

        @Nullable
        protected static Pair<java.util.List<DimensionSpec>,​java.util.List<VirtualColumn>> computeProjectGroupSet​(@Nullable
                                                                                                                        Project projectNode,
                                                                                                                        ImmutableBitSet groupSet,
                                                                                                                        RelDataType inputRowType,
                                                                                                                        DruidQuery druidQuery)
        Computes the project group set.
        Parameters:
        projectNode - Project under the Aggregates if any
        groupSet - Ids of grouping keys as they are listed in projects list
        inputRowType - Input row type under the project
        druidQuery - Druid Query
        Returns:
        A list of DimensionSpec containing the group by dimensions, and a list of VirtualColumn containing Druid virtual column projections; or null, if translation is not possible. Note that the size of lists can be different.
      • computeDruidJsonAgg

        @Nullable
        protected static java.util.List<DruidQuery.JsonAggregation> computeDruidJsonAgg​(java.util.List<AggregateCall> aggCalls,
                                                                                        java.util.List<java.lang.String> aggNames,
                                                                                        @Nullable
                                                                                        Project project,
                                                                                        DruidQuery druidQuery)
        Translates aggregate calls to Druid DruidQuery.JsonAggregations when possible.
        Parameters:
        aggCalls - List of AggregateCalls to translate
        aggNames - List of aggregate names
        project - Input project under the aggregate calls, or null if we have TableScan immediately under the Aggregate
        druidQuery - Druid Query Rel
        Returns:
        List of valid Druid DruidQuery.JsonAggregations, or null if any of the aggregates is not supported
      • computeSort

        private DruidQuery.JsonLimit computeSort​(@Nullable
                                                 java.lang.Integer fetch,
                                                 java.util.List<java.lang.Integer> collationIndexes,
                                                 java.util.List<RelFieldCollation.Direction> collationDirections,
                                                 ImmutableBitSet numericCollationIndexes,
                                                 java.util.List<java.lang.String> queryOutputFieldNames)
        Parameters:
        fetch - limit to fetch
        collationIndexes - index of fields as listed in query row output
        collationDirections - direction of sort
        numericCollationIndexes - flag of to determine sort comparator
        queryOutputFieldNames - query output fields
        Returns:
        always an non null Json Limit object
      • writeField

        protected static void writeField​(com.fasterxml.jackson.core.JsonGenerator generator,
                                         java.lang.String fieldName,
                                         java.lang.Object o)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • writeFieldIf

        protected static void writeFieldIf​(com.fasterxml.jackson.core.JsonGenerator generator,
                                           java.lang.String fieldName,
                                           java.lang.Object o)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • writeArray

        protected static void writeArray​(com.fasterxml.jackson.core.JsonGenerator generator,
                                         java.util.List<?> elements)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • writeObject

        protected static void writeObject​(com.fasterxml.jackson.core.JsonGenerator generator,
                                          java.lang.Object o)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • metadataQuery

        static java.lang.String metadataQuery​(java.lang.String dataSourceName,
                                              java.util.List<org.joda.time.Interval> intervals)
        Generates a JSON string to query metadata about a data source.
      • getTimestampFieldIndex

        protected int getTimestampFieldIndex()
        Returns:
        index of the timestamp ref or -1 if not present