 ;;; # ------------------------------------------------------------ PLACEHOLDER TYPES: FIELDPLACEHOLDER + VALUEPLACEHOLDER ------------------------------------------------------------
 
 ;; Replace Field IDs with these during first pass
-(s/defrecord FieldPlaceholder [field-id      :- s/Int
-                               fk-field-id   :- (s/maybe (s/constrained s/Int
+(s/defrecord FieldPlaceholder [field-id      :- IntGreaterThanZero
+                               fk-field-id   :- (s/maybe (s/constrained IntGreaterThanZero
                                                                         (fn [_] (or (assert-driver-supports :foreign-keys) true))
                                                                         "foreign-keys is not supported by this driver."))
                                datetime-unit :- (s/maybe (apply s/enum datetime-field-units))])
 
-(s/defrecord AgFieldRef [index :- s/Int]) ; e.g. 0
+(s/defrecord AgFieldRef [index :- (s/constrained s/Int
+                                                 zero?
+                                                 "Ag field index should be 0 -- MBQL currently only supports a single aggregation")])
 
 (def FieldPlaceholderOrAgRef (s/named (s/cond-pre FieldPlaceholder AgFieldRef) "Valid field (not a field ID or aggregate field reference)"))
 

                      "Valid order-by subclause"))


(def Page (s/named {:page  s/Int
                    :items s/Int}
                   "Valid page clause"))




;; These are just used by the QueryExpander to record information about how joins should occur.

(def IntGreaterThanZero (s/constrained s/Int
                                       (partial < 0)
                                       "Integer greater than zero"))

(s/defrecord JoinTableField [field-id   :- IntGreaterThanZero
                             field-name :- s/Str])

(s/defrecord JoinTable [source-field :- JoinTableField
                        pk-field     :- JoinTableField
                        table-id     :- IntGreaterThanZero
                        table-name   :- s/Str])

;;; # ------------------------------------------------------------ PROTOCOLS ------------------------------------------------------------
