pub fn eval(&self, state: &mut State) -> Result<Value, S<Error>> {
        let span = self.0.span;
        Ok(match &self.0.inner {
            C::RowNum => state.row_num.into(),
            C::SubRowNum => state.sub_row_num.into(),
            C::Constant(v) => v.clone(),
            C::RawFunction { function, args } => {
                let mut eval_args = Arguments::with_capacity(args.len());
                for c in &**args {
                    eval_args.push(c.eval(state)?.span(c.0.span));
                }
                (*function)
                    .compile(&state.compile_context, span, eval_args)?
                    .span(span)
                    .eval(state)?
            }
            C::GetVariable(index) => state.compile_context.variables[*index].clone(),
            C::SetVariable(index, c) => {
                let value = c.eval(state)?;
                state.compile_context.variables[*index] = value.clone();
                value
            }

            C::CaseValueWhen {
                value: Some(value),
                conditions,
                otherwise,
            } => {
                let value = value.eval(state)?;
                for (p, r) in &**conditions {
                    let p_span = p.0.span;
                    let p = p.eval(state)?;
                    if value.sql_cmp(&p).span_err(p_span)? == Some(Ordering::Equal) {
                        return r.eval(state);
                    }
                }
                otherwise.eval(state)?
            }

            C::CaseValueWhen {
                value: None,
                conditions,
                otherwise,
            } => {
                for (p, r) in &**conditions {
                    if p.eval(state)?.is_sql_true().span_err(p.0.span)? {
                        return r.eval(state);
                    }
                }
                otherwise.eval(state)?
            }

            C::RandRegex(generator) => state.rng.sample::<EncodedString, _>(generator).into(),
            C::RandUniformU64(uniform) => state.rng.sample(uniform).into(),
            C::RandUniformI64(uniform) => state.rng.sample(uniform).into(),
            C::RandUniformF64(uniform) => Value::from_finite_f64(state.rng.sample(uniform)),
            C::RandZipf(zipf) => (state.rng.sample(zipf) as u64).into(),
            C::RandLogNormal(log_normal) => Value::from_finite_f64(state.rng.sample(log_normal)),
            C::RandBool(bern) => state.rng.sample(bern).into(),
            C::RandFiniteF32(uniform) => {
                Value::from_finite_f64(f32::from_bits(state.rng.sample(uniform).rotate_right(1)).into())
            }
            C::RandFiniteF64(uniform) => {
                Value::from_finite_f64(f64::from_bits(state.rng.sample(uniform).rotate_right(1)))
            }

            C::RandU31Timestamp(uniform) => {
                let seconds = state.rng.sample(uniform);
                let timestamp = NaiveDateTime::from_timestamp(seconds, 0);
                Value::new_timestamp(timestamp, state.compile_context.time_zone.clone())
            }

            C::RandShuffle(array) => {
                let mut shuffled_array = Arc::<[Value]>::from(&**array);
                Arc::get_mut(&mut shuffled_array).unwrap().shuffle(&mut state.rng);
                Value::Array(shuffled_array)
            }

            C::RandUuid => {
                // we will loss 6 bits but that's still uniform.
                let g = state.rng.gen::<[u16; 8]>();
                format!(
                    "{:04x}{:04x}-{:04x}-4{:03x}-{:04x}-{:04x}{:04x}{:04x}",
                    g[0],
                    g[1],
                    g[2],
                    g[3] & 0xfff,
                    (g[4] & 0x3fff) | 0x8000,
                    g[5],
                    g[6],
                    g[7],
                )
                .into()
            }
        })
    }
/// Evaluates a compiled expression and updates the state. Returns the evaluated value.
Evaluates a compiled expression and updates the state. Returns the evaluated value.
Evaluates a compiled expression and updates the state . Returns the evaluated value .
pub fn eval(&self, state: &mut State) -> Result<Value, S<Error>> {
        let span = self.0.span;
        Ok(match &self.0.inner {
            C::RowNum => state.row_num.into(),
            C::SubRowNum => state.sub_row_num.into(),
            C::Constant(v) => v.clone(),
            C::RawFunction { function, args } => {
                let mut eval_args = Arguments::with_capacity(args.len());
                for c in &**args {
                    eval_args.push(c.eval(state)?.span(c.0.span));
                }
                (*function)
                    .compile(&state.compile_context, span, eval_args)?
                    .span(span)
                    .eval(state)?
            }
            C::GetVariable(index) => state.compile_context.variables[*index].clone(),
            C::SetVariable(index, c) => {
                let value = c.eval(state)?;
                state.compile_context.variables[*index] = value.clone();
                value
            }
            C::CaseValueWhen {
                value: Some(value),
                conditions,
                otherwise,
            } => {
                let value = value.eval(state)?;
                for (p, r) in &**conditions {
                    let p_span = p.0.span;
                    let p = p.eval(state)?;
                    if value.sql_cmp(&p).span_err(p_span)? == Some(Ordering::Equal) {
                        return r.eval(state);
                    }
                }
                otherwise.eval(state)?
            }
            C::CaseValueWhen {
                value: None,
                conditions,
                otherwise,
            } => {
                for (p, r) in &**conditions {
                    if p.eval(state)?.is_sql_true().span_err(p.0.span)? {
                        return r.eval(state);
                    }
                }
                otherwise.eval(state)?
            }
            C::RandRegex(generator) => state.rng.sample::<EncodedString, _>(generator).into(),
            C::RandUniformU64(uniform) => state.rng.sample(uniform).into(),
            C::RandUniformI64(uniform) => state.rng.sample(uniform).into(),
            C::RandUniformF64(uniform) => Value::from_finite_f64(state.rng.sample(uniform)),
            C::RandZipf(zipf) => (state.rng.sample(zipf) as u64).into(),
            C::RandLogNormal(log_normal) => Value::from_finite_f64(state.rng.sample(log_normal)),
            C::RandBool(bern) => state.rng.sample(bern).into(),
            C::RandFiniteF32(uniform) => {
                Value::from_finite_f64(f32::from_bits(state.rng.sample(uniform).rotate_right(1)).into())
            }
            C::RandFiniteF64(uniform) => {
                Value::from_finite_f64(f64::from_bits(state.rng.sample(uniform).rotate_right(1)))
            }
            C::RandU31Timestamp(uniform) => {
                let seconds = state.rng.sample(uniform);
                let timestamp = NaiveDateTime::from_timestamp(seconds, 0);
                Value::new_timestamp(timestamp, state.compile_context.time_zone.clone())
            }
            C::RandShuffle(array) => {
                let mut shuffled_array = Arc::<[Value]>::from(&**array);
                Arc::get_mut(&mut shuffled_array).unwrap().shuffle(&mut state.rng);
                Value::Array(shuffled_array)
            }
            C::RandUuid => {
                // we will loss 6 bits but that's still uniform.
                let g = state.rng.gen::<[u16; 8]>();
                format!(
                    "{:04x}{:04x}-{:04x}-4{:03x}-{:04x}-{:04x}{:04x}{:04x}",
                    g[0],
                    g[1],
                    g[2],
                    g[3] & 0xfff,
                    (g[4] & 0x3fff) | 0x8000,
                    g[5],
                    g[6],
                    g[7],
                )
                .into()
            }
        })
    }
pub fn eval ( & self , state : & mut State ) -> Result < Value , S < Error > > { let span = self . 0 . span ; Ok ( match & self . 0 . inner { C :: RowNum => state . row_num . into ( ) , C :: SubRowNum => state . sub_row_num . into ( ) , C :: Constant ( v ) => v . clone ( ) , C :: RawFunction { function , args } => { let mut eval_args = Arguments :: with_capacity ( args . len ( ) ) ; for c in & * * args { eval_args . push ( c . eval ( state ) ? . span ( c . 0 . span ) ) ; } ( * function ) . compile ( & state . compile_context , span , eval_args ) ? . span ( span ) . eval ( state ) ? } C :: GetVariable ( index ) => state . compile_context . variables [ * index ] . clone ( ) , C :: SetVariable ( index , c ) => { let value = c . eval ( state ) ? ; state . compile_context . variables [ * index ] = value . clone ( ) ; value } C :: CaseValueWhen { value : Some ( value ) , conditions , otherwise , } => { let value = value . eval ( state ) ? ; for ( p , r ) in & * * conditions { let p_span = p . 0 . span ; let p = p . eval ( state ) ? ; if value . sql_cmp ( & p ) . span_err ( p_span ) ? == Some ( Ordering :: Equal ) { return r . eval ( state ) ; } } otherwise . eval ( state ) ? } C :: CaseValueWhen { value : None , conditions , otherwise , } => { for ( p , r ) in & * * conditions { if p . eval ( state ) ? . is_sql_true ( ) . span_err ( p . 0 . span ) ? { return r . eval ( state ) ; } } otherwise . eval ( state ) ? } C :: RandRegex ( generator ) => state . rng . sample :: < EncodedString , _ > ( generator ) . into ( ) , C :: RandUniformU64 ( uniform ) => state . rng . sample ( uniform ) . into ( ) , C :: RandUniformI64 ( uniform ) => state . rng . sample ( uniform ) . into ( ) , C :: RandUniformF64 ( uniform ) => Value :: from_finite_f64 ( state . rng . sample ( uniform ) ) , C :: RandZipf ( zipf ) => ( state . rng . sample ( zipf ) as u64 ) . into ( ) , C :: RandLogNormal ( log_normal ) => Value :: from_finite_f64 ( state . rng . sample ( log_normal ) ) , C :: RandBool ( bern ) => state . rng . sample ( bern ) . into ( ) , C :: RandFiniteF32 ( uniform ) => { Value :: from_finite_f64 ( f32 :: from_bits ( state . rng . sample ( uniform ) . rotate_right ( 1 ) ) . into ( ) ) } C :: RandFiniteF64 ( uniform ) => { Value :: from_finite_f64 ( f64 :: from_bits ( state . rng . sample ( uniform ) . rotate_right ( 1 ) ) ) } C :: RandU31Timestamp ( uniform ) => { let seconds = state . rng . sample ( uniform ) ; let timestamp = NaiveDateTime :: from_timestamp ( seconds , 0 ) ; Value :: new_timestamp ( timestamp , state . compile_context . time_zone . clone ( ) ) } C :: RandShuffle ( array ) => { let mut shuffled_array = Arc :: < [ Value ] > :: from ( & * * array ) ; Arc :: get_mut ( & mut shuffled_array ) . unwrap ( ) . shuffle ( & mut state . rng ) ; Value :: Array ( shuffled_array ) } C :: RandUuid => { // we will loss 6 bits but that's still uniform. let g = state . rng . gen :: < [ u16 ; 8 ] > ( ) ; format ! ( " " g [ 0 ] g [ 1 ] g [ 2 ] g [ 3 ] 0xfff ( g [ 4 ] 0x3fff ) 0x8000 g [ 5 ] g [ 6 ] g [ 7 ] ) . into ( ) } } ) }
Evaluates a compiled expression and updates the state.
Evaluates a compiled expression and updates the state .
&self
state
&mut State
&self
state
&mut State