Пример #1
0
    def test_chained_function(self):
        field1 = Field('foo').negate()
        field2 = field1.eq('bar')

        self.assertEqual('NOT "foo"', str(field1))
        self.assertEqual('NOT "foo"=\'bar\'', str(field2))
        self.assertIsNot(field1, field2)
Пример #2
0
    def test_chained_function(self):
        field1 = Field("foo").negate()
        field2 = field1.eq("bar")

        self.assertEqual('NOT "foo"', str(field1))
        self.assertEqual("NOT \"foo\"='bar'", str(field2))
        self.assertIsNot(field1, field2)
Пример #3
0
    def test_chained_function(self):
        field1 = Field('foo').negate()
        field2 = field1.eq('bar')

        self.assertEqual('NOT "foo"', str(field1))
        self.assertEqual('NOT "foo"=\'bar\'', str(field2))
        self.assertIsNot(field1, field2)
def build_dataset_query_for_subject(console_subject,
                                    current_user,
                                    for_count=False):
    dataset = console_subject.dataset
    if dataset is None:
        return None

    topic_space_filter = get_topic_sub_query_with_space_filter(
        console_subject, current_user)

    if dataset.joins and len(dataset.joins) > 0:
        topic_id = dataset.joins[0].topicId
        topic_table = topic_space_filter(topic_id)
        if topic_table:
            q = PrestoQuery.with_(topic_table["query"],
                                  topic_table["alias"]).from_(
                                      topic_table["alias"])
        else:
            table = build_table_by_topic_id(topic_id)
            q = PrestoQuery.from_(table)
    else:

        topic_id = dataset.columns[0].parameter.topicId
        topic_table = topic_space_filter(topic_id)
        if topic_table:
            table = AliasedQuery(topic_table["alias"])
            q = PrestoQuery.with_(topic_table["query"],
                                  topic_table["alias"]).from_(table)
        else:
            table = build_table_by_topic_id(topic_id)
            q = PrestoQuery.from_(table)

    for join in dataset.joins:
        right_topic_id = join.secondaryTopicId
        right_topic = get_topic_by_id(right_topic_id)
        right_topic_table = topic_space_filter(right_topic_id)
        if right_topic_table:
            q = q.with_(right_topic_table["query"], right_topic_table["alias"])
            right_table = AliasedQuery(right_topic_table["alias"])
        else:
            right_table = build_table_by_topic_id(right_topic_id)

        left_topic_id = join.topicId
        left_topic = get_topic_by_id(left_topic_id)
        left_topic_table = topic_space_filter(left_topic_id)
        if left_topic_table:
            left_table = AliasedQuery(left_topic_table["alias"])
        else:
            left_table = build_table_by_topic_id(left_topic_id)

        left_factor = get_factor(join.factorId, left_topic)
        left_field = Field(left_factor.name, None, left_table)

        right_factor = get_factor(join.secondaryFactorId, right_topic)
        right_field = Field(right_factor.name, None, right_table)

        if join.type == "inner" or join.type == "":
            join_type = JoinType.inner
        elif join.type == "left":
            join_type = JoinType.left
        elif join.type == "right":
            join_type = JoinType.right
        else:
            join_type = JoinType.inner

        q = q.join(right_table, join_type).on(left_field.eq(right_field))

    q = q.where(build_dataset_where(dataset.filters, topic_space_filter))
    if for_count:
        return q.select(fn.Count("*"))
    else:
        return q.select(
            *build_dataset_select_fields(dataset.columns, topic_space_filter))