ContextSelect#

relationalai
#class ContextSelect

ContextSelect objects are returned by the Context.__enter__() method. They are used to select results in query contexts. ContextSelect objects are also used in Model.match() contexts as a variable to which values are conditionally assigned.

Methods#

NameDescriptionReturns
.__call__()Selects query results. Must be called in a query context.Context
.distinct()Selects only distinct rows in the query results. Must be called in a query context.Context
.__getattr__()Accesses properties of values assigned to the ContestSelect. Only used in .match() contexts.Instance or InstanceProperty
.add()Assigns values to the ContextSelect. Only used in .match() contexts.None

Example#

Both Model.query() and Model.match() return Context objects. A Context is a context manager and must be used in a with statement.

ContextSelect objects are returned by the Context.__enter__() method, which is called automatically when the with statement executes. The ContextSelect object it returns can be named in the with statement’s as clause.

In queries, the ContextSelect object is used to select the data to be returned. By convention, we name the ContextSelect object select. You call select() at the end of the query and pass to it the objects, properties, and variables you want returned in the results:

#import relationalai as rai


# =====
# SETUP
# =====

model = rai.Model("MyModel")
Item = model.Type("Item")


with model.rule():
    Item.add(id=1).set(name="A", price=10.00)
    Item.add(id=2).set(name="B", price=10.00)
    Item.add(id=3).set(name="C", price=20.00)


# =======
# EXAMPLE
# =======

with model.query() as select:  # select is a ContextSelect object
    item = Item()
    # Calling select returns the Context object created by model.query().
    response = select(item.id, item.name, item.price)

# The query results are assigned to the response.results attribute.
print(response.results)
#    id name  price
# 0   1    A   10.0
# 1   2    B   10.0
# 2   3    C   20.0


# Rows for each item filtered by the query are returned in the results. This
# means duplicate rows may be returned:

with model.query() as select:
    item = Item()
    response = select(item.price)

# There are two rows with 10.00 in the results because there are two two items
# with a price of 10.00.
print(response.results)
#    price
# 0   10.0
# 1   10.0
# 2   20.0


# Use select.distinct() to return only unique rows in the results:

with model.query() as select:
    item = Item()
    response = select.distinct(item.price)

print(response.results)
#    price
# 0   10.0
# 1   20.0

In a Model.match() context, the ContextSelect object represents a variable to which values are conditionally assigned. Give the ContextSelect object a descriptive name and use the ContextSelect.add() method to assign values to it:

#from relationalai.std import alias

# Compute an item's discount based on its price.
with model.query() as select:
    item = Item()
    with model.match() as discount: # discount is a ContextSelect object
        # Items with a price greater than 15 get a 25% discount.
        with item.price > 15:
            discount.add(0.25)  #  Assign 0.25 to discount.
        # All other items get a 10% discount.
        with model.case():
            discount.add(0.1)

    # Compute the discounted price.
    discounted_price = item.price * (1 - discount)

    response = select(
        item.id,
        item.name,
        item.price,
        alias(discount, "discount"),
        alias(discounted_price, "discounted_price"),
    )

print(response.results)
#    id name  price  discount  discounted_price
# 0   1    A   10.0      0.10               9.0
# 1   2    B   10.0      0.10               9.0
# 2   3    C   20.0      0.25              15.0

See Model.match() for more information on .match() contexts.

See Also#