cube-js / cube

📊 Cube — Universal semantic layer platform for AI, BI, spreadsheets, and embedded analytics
https://cube.dev
Other
18.01k stars 1.78k forks source link

Is there a way to pass the parameters to `segments` ? #7915

Open fyfirman opened 8 months ago

fyfirman commented 8 months ago

Problem

I need a complex filter which based on the documentation I need to use segments. The filter that I want to achieve is gonna be look like these:

segments: {
    notMemberExcept: {
      sql: `(
        ${CUBE}.user_id NOT IN (
        SELECT user_id FROM members
      ) OR ${CUBE.userId} IN ($ValueFromUser) )`,
    },
  },

This where clause is important to be like that because previously I used alternative SQL that complied with CubeJS implementation but turns out it's gonna be a performance issue.

The problem is how I get the value from API in cube schema?

Related Cube.js schema

cube(`Users`, {
  sql: `SELECT * FROM users`,

  joins: {
    Members: {
      relationship: `hasMany`,
      sql: `
        ${CUBE.orgId} = ${Members.orgId} AND
        ${CUBE.userId} = ${Members.userId}
      `,
    }
  }

  dimensions: {
    orgId: {
      sql: `org_id`,
      type: `string`,
      primaryKey: true,
      shown: true,
    },

    issueId: {
      sql: `user_id`,
      type: `string`,
      primaryKey: true,
      shown: true,
    },

    name: {
      sql: `name`,
      type: `string`,
    },
  },
igorlukanin commented 8 months ago

Hi @fyfirman 👋

Segments are not a good fit for this use case. Please decompose this segment using boolean logic operators in your query.

igorlukanin commented 6 months ago

@fyfirman Did my advice help?