Write a function to merge two dataframes.
Implement merge_dataframes(left, right, key) using an inner join on key. Each dataframe is an array of records, where each record is a map from strings to values. Assume the key exists in every record, is unique within each dataframe, and non-key field names do not overlap. Return an array containing records whose key appears in both inputs, combining fields from both records and preserving the order of matching records from left.
Example: left = [{"id": 1, "name": "A"}], right = [{"id": 1, "team": "X"}] produces [{"id": 1, "name": "A", "team": "X"}].
Constraints: each dataframe contains at most 10^4 records; an input may be empty.
def merge_dataframes(left, right, key):